0
static struct hashtable *quark_string_hash = NULL; /* const char * -> int */
0
static ptr_array quark_string_index = NULL;
0
+static CRITICAL_SECTION quark_static_cs;
0
+static bool cs_initialized = false;
0
+static void mutex_lock()
0
+ if (!cs_initialized) {
0
+ InitializeCriticalSection(&quark_static_cs);
0
+ cs_initialized = true;
0
+ EnterCriticalSection(&quark_static_cs);
0
+static void mutex_unlock()
0
+ LeaveCriticalSection(&quark_static_cs);
0
static pthread_mutex_t quark_static_lock = PTHREAD_MUTEX_INITIALIZER;
0
+static void mutex_lock()
0
+ pthread_mutex_lock(&quark_static_lock);
0
+static void mutex_unlock()
0
+ pthread_mutex_unlock(&quark_static_lock);
0
enum {QUARK_NOT_FOUND = ~0L};
0
+/* djb2 hash function */
0
static unsigned int string_hash(const void *val)
0
- unsigned int
retval = 0;
0
+ unsigned int
hash = 5381;
0
const char *str = (const char *)val;
0
+ hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
0
static int string_eq(const void* a1, const void* a2)
0
return strcmp((const char*) a1, (const char*) a2) == 0;
0
/* assumes static lock is held */
0
static inline bool quark_init_if_needed()
0
@@ -46,49 +84,49 @@ static inline bool quark_init_if_needed()
0
quark quark_from_string(const char *string)
0
if (string == NULL) return 0;
0
-
pthread_mutex_lock(&quark_static_lock);
0
if (!quark_init_if_needed())
0
uintptr_t value = (uintptr_t)hashtable_search(quark_string_hash, string);
0
-
pthread_mutex_unlock(&quark_static_lock);
0
string = strdup(string);
0
if (!ptr_array_append(quark_string_index, string))
0
-
pthread_mutex_unlock(&quark_static_lock);
0
return QUARK_NOT_FOUND;
0
size_t index = ptr_array_length(quark_string_index) - 1;
0
hashtable_insert(quark_string_hash, (char*) string, (void*) index);
0
-
pthread_mutex_unlock(&quark_static_lock);
0
quark quark_from_static_string(const char *string)
0
if (string == NULL) return 0;
0
-
pthread_mutex_lock(&quark_static_lock);
0
if (!quark_init_if_needed())
0
uintptr_t value = (uintptr_t)hashtable_search(quark_string_hash, string);
0
-
pthread_mutex_unlock(&quark_static_lock);
0
if (!ptr_array_append(quark_string_index, string))
0
-
pthread_mutex_unlock(&quark_static_lock);
0
return QUARK_NOT_FOUND;
0
size_t index = ptr_array_length(quark_string_index) - 1;
0
hashtable_insert(quark_string_hash, (char *)string, (void*) index);
0
-
pthread_mutex_unlock(&quark_static_lock);
0
@@ -96,13 +134,13 @@ const char * quark_to_string(quark q)
0
if (q == 0) return NULL;
0
-
pthread_mutex_lock(&quark_static_lock);
0
if (quark_init_if_needed() || q >= ptr_array_length(quark_string_index))
0
-
pthread_mutex_unlock(&quark_static_lock);
0
retval = ptr_array_get_index(quark_string_index, q);
0
-
pthread_mutex_unlock(&quark_static_lock);
Comments
No one has commented yet.