Repository: lucy-clownfish Updated Branches: refs/heads/master 752d20610 -> 9fea2c92c
String-only keys for LockFreeRegistry Fixes CLOWNFISH-34. Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/c9af487f Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/c9af487f Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/c9af487f Branch: refs/heads/master Commit: c9af487f4b9b59367159c1bc1bb6f6a37c8a228a Parents: 752d206 Author: Nick Wellnhofer <[email protected]> Authored: Fri Apr 17 20:52:00 2015 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Fri Apr 17 20:52:00 2015 +0200 ---------------------------------------------------------------------- runtime/core/Clownfish/Class.c | 16 +++++++--------- runtime/core/Clownfish/LockFreeRegistry.c | 17 +++++++++-------- runtime/core/Clownfish/LockFreeRegistry.cfh | 4 ++-- runtime/core/Clownfish/Test/TestLockFreeRegistry.c | 12 ++++++------ 4 files changed, 24 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c9af487f/runtime/core/Clownfish/Class.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Class.c b/runtime/core/Clownfish/Class.c index 284e57d..1b480b3 100644 --- a/runtime/core/Clownfish/Class.c +++ b/runtime/core/Clownfish/Class.c @@ -250,7 +250,7 @@ Class_singleton(String *class_name, Class *parent) { Class_init_registry(); } - Class *singleton = (Class*)LFReg_Fetch(Class_registry, (Obj*)class_name); + Class *singleton = (Class*)LFReg_Fetch(Class_registry, class_name); if (singleton == NULL) { VArray *fresh_host_methods; uint32_t num_fresh; @@ -309,7 +309,7 @@ Class_singleton(String *class_name, Class *parent) { } else { DECREF(singleton); - singleton = (Class*)LFReg_Fetch(Class_registry, (Obj*)class_name); + singleton = (Class*)LFReg_Fetch(Class_registry, class_name); if (!singleton) { THROW(ERR, "Failed to either insert or fetch Class for '%o'", class_name); @@ -325,13 +325,12 @@ Class_add_to_registry(Class *klass) { if (Class_registry == NULL) { Class_init_registry(); } - if (LFReg_Fetch(Class_registry, (Obj*)klass->name)) { + if (LFReg_Fetch(Class_registry, klass->name)) { return false; } else { String *class_name = Str_Clone(klass->name); - bool retval - = LFReg_Register(Class_registry, (Obj*)class_name, (Obj*)klass); + bool retval = LFReg_Register(Class_registry, class_name, (Obj*)klass); DECREF(class_name); return retval; } @@ -344,13 +343,12 @@ Class_add_alias_to_registry(Class *klass, const char *alias_ptr, Class_init_registry(); } StackString *alias = SSTR_WRAP_UTF8(alias_ptr, alias_len); - if (LFReg_Fetch(Class_registry, (Obj*)alias)) { + if (LFReg_Fetch(Class_registry, (String*)alias)) { return false; } else { String *class_name = SStr_Clone(alias); - bool retval - = LFReg_Register(Class_registry, (Obj*)class_name, (Obj*)klass); + bool retval = LFReg_Register(Class_registry, class_name, (Obj*)klass); DECREF(class_name); return retval; } @@ -360,7 +358,7 @@ Class* Class_fetch_class(String *class_name) { Class *klass = NULL; if (Class_registry != NULL) { - klass = (Class*)LFReg_Fetch(Class_registry, (Obj*)class_name); + klass = (Class*)LFReg_Fetch(Class_registry, class_name); } return klass; } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c9af487f/runtime/core/Clownfish/LockFreeRegistry.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/LockFreeRegistry.c b/runtime/core/Clownfish/LockFreeRegistry.c index c95a791..59e4608 100644 --- a/runtime/core/Clownfish/LockFreeRegistry.c +++ b/runtime/core/Clownfish/LockFreeRegistry.c @@ -20,11 +20,12 @@ #include "Clownfish/LockFreeRegistry.h" #include "Clownfish/Err.h" #include "Clownfish/Class.h" +#include "Clownfish/String.h" #include "Clownfish/Util/Atomic.h" #include "Clownfish/Util/Memory.h" typedef struct cfish_LFRegEntry { - Obj *key; + String *key; Obj *value; int32_t hash_sum; struct cfish_LFRegEntry *volatile next; @@ -46,9 +47,9 @@ LFReg_init(LockFreeRegistry *self, size_t capacity) { } bool -LFReg_Register_IMP(LockFreeRegistry *self, Obj *key, Obj *value) { +LFReg_Register_IMP(LockFreeRegistry *self, String *key, Obj *value) { LFRegEntry *new_entry = NULL; - int32_t hash_sum = Obj_Hash_Sum(key); + int32_t hash_sum = Str_Hash_Sum(key); size_t bucket = (uint32_t)hash_sum % self->capacity; LFRegEntry *volatile *entries = (LFRegEntry*volatile*)self->entries; LFRegEntry *volatile *slot = &(entries[bucket]); @@ -59,7 +60,7 @@ FIND_END_OF_LINKED_LIST: while (*slot) { LFRegEntry *entry = *slot; if (entry->hash_sum == hash_sum) { - if (Obj_Equals(key, entry->key)) { + if (Str_Equals(key, (Obj*)entry->key)) { return false; } } @@ -70,7 +71,7 @@ FIND_END_OF_LINKED_LIST: if (!new_entry) { new_entry = (LFRegEntry*)MALLOCATE(sizeof(LFRegEntry)); new_entry->hash_sum = hash_sum; - new_entry->key = INCREF(key); + new_entry->key = (String*)INCREF(key); new_entry->value = INCREF(value); new_entry->next = NULL; } @@ -88,15 +89,15 @@ FIND_END_OF_LINKED_LIST: } Obj* -LFReg_Fetch_IMP(LockFreeRegistry *self, Obj *key) { - int32_t hash_sum = Obj_Hash_Sum(key); +LFReg_Fetch_IMP(LockFreeRegistry *self, String *key) { + int32_t hash_sum = Str_Hash_Sum(key); size_t bucket = (uint32_t)hash_sum % self->capacity; LFRegEntry **entries = (LFRegEntry**)self->entries; LFRegEntry *entry = entries[bucket]; while (entry) { if (entry->hash_sum == hash_sum) { - if (Obj_Equals(key, entry->key)) { + if (Str_Equals(key, (Obj*)entry->key)) { return entry->value; } } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c9af487f/runtime/core/Clownfish/LockFreeRegistry.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/LockFreeRegistry.cfh b/runtime/core/Clownfish/LockFreeRegistry.cfh index 330507b..cb6ddeb 100644 --- a/runtime/core/Clownfish/LockFreeRegistry.cfh +++ b/runtime/core/Clownfish/LockFreeRegistry.cfh @@ -33,10 +33,10 @@ class Clownfish::LockFreeRegistry nickname LFReg inherits Clownfish::Obj { Destroy(LockFreeRegistry *self); bool - Register(LockFreeRegistry *self, Obj *key, Obj *value); + Register(LockFreeRegistry *self, String *key, Obj *value); nullable Obj* - Fetch(LockFreeRegistry *self, Obj *key); + Fetch(LockFreeRegistry *self, String *key); } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c9af487f/runtime/core/Clownfish/Test/TestLockFreeRegistry.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Test/TestLockFreeRegistry.c b/runtime/core/Clownfish/Test/TestLockFreeRegistry.c index 46d725e..6e7f852 100644 --- a/runtime/core/Clownfish/Test/TestLockFreeRegistry.c +++ b/runtime/core/Clownfish/Test/TestLockFreeRegistry.c @@ -50,20 +50,20 @@ test_all(TestBatchRunner *runner) { StupidHashCharBuf *baz = StupidHashCharBuf_new("baz"); StupidHashCharBuf *foo_dupe = StupidHashCharBuf_new("foo"); - TEST_TRUE(runner, LFReg_Register(registry, (Obj*)foo, (Obj*)foo), + TEST_TRUE(runner, LFReg_Register(registry, (String*)foo, (Obj*)foo), "Register() returns true on success"); TEST_FALSE(runner, - LFReg_Register(registry, (Obj*)foo_dupe, (Obj*)foo_dupe), + LFReg_Register(registry, (String*)foo_dupe, (Obj*)foo_dupe), "Can't Register() keys that test equal"); - TEST_TRUE(runner, LFReg_Register(registry, (Obj*)bar, (Obj*)bar), + TEST_TRUE(runner, LFReg_Register(registry, (String*)bar, (Obj*)bar), "Register() key with the same Hash_Sum but that isn't Equal"); - TEST_TRUE(runner, LFReg_Fetch(registry, (Obj*)foo_dupe) == (Obj*)foo, + TEST_TRUE(runner, LFReg_Fetch(registry, (String*)foo_dupe) == (Obj*)foo, "Fetch()"); - TEST_TRUE(runner, LFReg_Fetch(registry, (Obj*)bar) == (Obj*)bar, + TEST_TRUE(runner, LFReg_Fetch(registry, (String*)bar) == (Obj*)bar, "Fetch() again"); - TEST_TRUE(runner, LFReg_Fetch(registry, (Obj*)baz) == NULL, + TEST_TRUE(runner, LFReg_Fetch(registry, (String*)baz) == NULL, "Fetch() non-existent key returns NULL"); DECREF(foo_dupe);
