#include "hb-private.hh"
+#include "hb-version.h"
+
+#include "hb-mutex-private.hh"
+#include "hb-object-private.hh"
+
+#include <locale.h>
+
HB_BEGIN_DECLS
#endif
-hb_language_t
-hb_language_from_string (const char *str)
-{
- static unsigned int num_langs;
- static unsigned int num_alloced;
- static hb_language_t *langs;
- unsigned int i;
- unsigned char *p;
+struct hb_language_item_t {
- /* TODO Use a hash table or something */
+ hb_language_t lang;
- if (!str || !*str)
- return NULL;
-
- for (i = 0; i < num_langs; i++)
- if (lang_equal (str, langs[i]->s))
- return langs[i];
-
- if (unlikely (num_langs == num_alloced)) {
- unsigned int new_alloced = 2 * (8 + num_alloced);
- hb_language_t *new_langs = (hb_language_t *) realloc (langs, new_alloced * sizeof (langs[0]));
- if (!new_langs)
- return NULL;
- num_alloced = new_alloced;
- langs = new_langs;
+ inline bool operator == (const char *s) const {
+ return lang_equal (lang, s);
}
- langs[i] = (hb_language_t) strdup (str);
- for (p = (unsigned char *) langs[i]->s; *p; p++)
- *p = canon_map[*p];
+ inline hb_language_item_t & operator = (const char *s) {
+ lang = (hb_language_t) strdup (s);
+ for (unsigned char *p = (unsigned char *) lang; *p; p++)
+ *p = canon_map[*p];
- num_langs++;
+ return *this;
+ }
+
+ void finish (void) { free (lang); }
+};
- return langs[i];
+static hb_static_mutex_t langs_lock;
+static hb_lockable_set_t<hb_language_item_t, hb_static_mutex_t> langs;
+
+hb_language_t
+hb_language_from_string (const char *str)
+{
+ if (!str || !*str)
+ return HB_LANGUAGE_INVALID;
+
+ hb_language_item_t *item = langs.find_or_insert (str, langs_lock);
+
+ return likely (item) ? item->lang : HB_LANGUAGE_INVALID;
}
const char *
hb_language_to_string (hb_language_t language)
{
+ /* This is actually NULL-safe! */
return language->s;
}
+hb_language_t
+hb_language_get_default (void)
+{
+ static hb_language_t default_language;
+
+ if (!default_language) {
+ /* This block is not quite threadsafe, but is not as bad as
+ * it looks since it's idempotent. As long as pointer ops
+ * are atomic, we are safe. */
+
+ /* I hear that setlocale() doesn't honor env vars on Windows,
+ * but for now we ignore that. */
+
+ default_language = hb_language_from_string (setlocale (LC_CTYPE, NULL));
+ }
+
+ return default_language;
+}
+
/* hb_script_t */
}
+/* hb_user_data_array_t */
-/* System stuff */
+/* NOTE: Currently we use a global lock for user_data access
+ * threadsafety. If one day we add a mutex to any object, we
+ * should switch to using that insted for these too.
+ */
-#ifdef _MSC_VER
-
-#include <Windows.h>
+static hb_static_mutex_t user_data_lock;
-hb_mutex_t
-_hb_win32_mutex_create ()
+bool
+hb_user_data_array_t::set (hb_user_data_key_t *key,
+ void * data,
+ hb_destroy_func_t destroy)
{
- hb_mutex_t m;
- _hb_win32_mutex_init (&m);
- return m;
+ if (!key)
+ return false;
+
+ if (!data && !destroy) {
+ items.remove (key, user_data_lock);
+ return true;
+ }
+ hb_user_data_item_t item = {key, data, destroy};
+ bool ret = !!items.replace_or_insert (item, user_data_lock);
+
+ return ret;
}
-void
-_hb_win32_mutex_init (hb_mutex_t *m)
+void *
+hb_user_data_array_t::get (hb_user_data_key_t *key)
{
- LPCRITICAL_SECTION lpcs = (LPCRITICAL_SECTION) calloc(1, sizeof(CRITICAL_SECTION));
- InitializeCriticalSection (lpcs);
- *m = (void*) lpcs;
+ hb_user_data_item_t item = {NULL };
+
+ return items.find (key, &item, user_data_lock) ? item.data : NULL;
}
void
-_hb_win32_mutex_lock (hb_mutex_t m)
+hb_user_data_array_t::finish (void)
{
- EnterCriticalSection ((LPCRITICAL_SECTION) m);
+ items.finish (user_data_lock);
}
-int
-_hb_win32_mutex_trylock (hb_mutex_t m)
-{
- return TryEnterCriticalSection ((LPCRITICAL_SECTION) m);
-}
+
+/* hb_version */
void
-_hb_win32_mutex_unlock (hb_mutex_t m)
+hb_version (unsigned int *major,
+ unsigned int *minor,
+ unsigned int *micro)
{
- LeaveCriticalSection ((LPCRITICAL_SECTION) m);
+ *major = HB_VERSION_MAJOR;
+ *minor = HB_VERSION_MINOR;
+ *micro = HB_VERSION_MICRO;
}
-void
-_hb_win32_mutex_free (hb_mutex_t *m)
+const char *
+hb_version_string (void)
{
- LPCRITICAL_SECTION lpcs = (LPCRITICAL_SECTION) *m;
- DeleteCriticalSection (lpcs);
- free(lpcs);
- *m = 0;
+ return HB_VERSION_STRING;
}
-#endif
+hb_bool_t
+hb_version_check (unsigned int major,
+ unsigned int minor,
+ unsigned int micro)
+{
+ return HB_VERSION_CHECK (major, minor, micro);
+}
HB_END_DECLS