diff --git a/src/CoreServices/MacLocales.cpp b/src/CoreServices/MacLocales.cpp index a3796c9c1..5e4ad3be4 100644 --- a/src/CoreServices/MacLocales.cpp +++ b/src/CoreServices/MacLocales.cpp @@ -9,9 +9,16 @@ #include #include #include +#include +#include +#include "../util/mutex.h" static iconv_t g_icUtf32ToUtf16 = nullptr; +static std::map g_mapLocaleString; +static std::map g_mapLocaleStringRev; +static Darling::Mutex g_mapLocaleStringMutex; + __attribute__((constructor)) void initConversions() { g_icUtf32ToUtf16 = iconv_open("UTF-16", "UTF-32"); @@ -23,11 +30,38 @@ __attribute__((destructor)) void exitConversions() iconv_close(g_icUtf32ToUtf16); } +static int getLocaleUID(const std::string& str) +{ + auto it = g_mapLocaleString.find(str); + if (it != g_mapLocaleString.end()) + return it->second; + else + { + g_mapLocaleStringMutex.lock(); + size_t id = g_mapLocaleString.size()+1; + + g_mapLocaleString[str] = id; + g_mapLocaleStringRev[id] = str; + g_mapLocaleStringMutex.unlock(); + + return id; + } +} + +static const char* getLocaleString(int uid) +{ + auto it = g_mapLocaleStringRev.find(uid); + if (it != g_mapLocaleStringRev.end()) + return it->second.c_str(); + else + return "INVALID"; +} + OSStatus LocaleRefFromLangOrRegionCode(LangCode langCode, RegionCode regionCode, LocaleRef* refOut) { if (langCode == kTextLanguageDontCare || regionCode == kTextRegionDontCare) { - *refOut = Locale::getDefault().getName(); + *refOut = getLocaleUID(Locale::getDefault().getName()); return 0; } @@ -47,7 +81,7 @@ OSStatus LocaleRefFromLangOrRegionCode(LangCode langCode, RegionCode regionCode, } else { - *refOut = loc.getName(); + *refOut = getLocaleUID(loc.getName()); return 0; } } @@ -62,14 +96,14 @@ OSStatus LocaleRefFromLocaleString(const char* str, LocaleRef* refOut) } else { - *refOut = loc.getName(); + *refOut = getLocaleUID(loc.getBaseName()); return 0; } } OSStatus LocaleRefGetPartString(LocaleRef ref, uint32_t partMask, unsigned long maxStringLen, char* stringOut) { - Locale loc(ref); + Locale loc(getLocaleString(ref)); char buffer[50] = ""; if (loc.isBogus()) @@ -139,7 +173,7 @@ OSStatus LocaleOperationGetLocales(LocaleOperationClass cls, unsigned long max, for (int i = 0; i < cc && i < max; i++) { - out[i].ref = locales[i].getName(); + out[i].ref = getLocaleUID(locales[i].getName()); out[i].variant = 0; } *countOut = std::min(cc, max); @@ -150,8 +184,8 @@ OSStatus LocaleOperationGetLocales(LocaleOperationClass cls, unsigned long max, OSStatus LocaleGetName(LocaleRef ref, LocaleOperationVariant variant, uint32_t nameMask, LocaleRef refDisplay, unsigned long maxLen, unsigned long* lenOut, Utf16Char* displayName) { - Locale loc(ref); - Locale locDisplay(refDisplay); + Locale loc(getLocaleString(ref)); + Locale locDisplay(getLocaleString(refDisplay)); // variant and nameMask currently ignored diff --git a/src/CoreServices/MacLocales.h b/src/CoreServices/MacLocales.h index a92921114..66d7a3b49 100644 --- a/src/CoreServices/MacLocales.h +++ b/src/CoreServices/MacLocales.h @@ -7,7 +7,7 @@ typedef int16_t RegionCode; typedef int16_t LangCode; typedef int16_t Utf16Char; -typedef const char* LocaleRef; +typedef int LocaleRef; typedef uint32_t LocaleOperationClass; typedef uint32_t LocaleOperationVariant; diff --git a/tests/src/mach_semaphore.c b/tests/src/mach_semaphore.c index 16fda1024..9287a72e4 100644 --- a/tests/src/mach_semaphore.c +++ b/tests/src/mach_semaphore.c @@ -20,11 +20,11 @@ void * start_routine(void *id) { semaphore_signal(g_sem[1]); - printf("thread: %lx about to decrement semaphore count\n", id); + fprintf(stderr, "thread: %lx about to decrement semaphore count\n", id); semaphore_wait(g_sem[0]); - printf("thread: %lx succeeded in decrementing semaphore count\n", id); + fprintf(stderr, "thread: %lx succeeded in decrementing semaphore count\n", id); semaphore_signal(g_sem[1]); - return (void *)0; + return NULL; } int