void *dictionary_get(DICTIONARY *dict, const char *name) { debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name); dictionary_read_lock(dict); NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0); dictionary_unlock(dict); if(unlikely(!nv)) { debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name); return NULL; } debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name); return nv->value; }
void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) { debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name); uint32_t hash = simple_hash(name); dictionary_write_lock(dict); NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash); if(unlikely(!nv)) { debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name); nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash); if(unlikely(!nv)) fatal("Cannot create name_value."); } else { debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name); if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE) { debug(D_REGISTRY, "Dictionary: linking value to '%s'", name); nv->value = value; } else { debug(D_REGISTRY, "Dictionary: cloning value to '%s'", name); // copy the new value without breaking // any other thread accessing the same entry void *new = malloc(value_len), *old = nv->value; if(unlikely(!new)) fatal("Cannot allocate value of size %zu", value_len); memcpy(new, value, value_len); nv->value = new; debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name); free(old); } } dictionary_unlock(dict); return nv->value; }
int dictionary_del(DICTIONARY *dict, const char *name) { int ret; debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name); dictionary_write_lock(dict); NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0); if(unlikely(!nv)) { debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name); ret = -1; } else { debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name); dictionary_name_value_destroy_nolock(dict, nv); ret = 0; } dictionary_unlock(dict); return ret; }