/** * Destroys the metrics * @return 0 on success. */ int destroy_metrics(metrics *m) { // Clear the copied quantiles array free(m->quantiles); // Nuke all the k/v pairs key_val *current = m->kv_vals; key_val *prev = NULL; while (current) { free(current->name); prev = current; current = current->next; free(prev); } // Nuke the counters hashmap_iter(m->counters, counter_delete_cb, NULL); hashmap_destroy(m->counters); // Nuke the timers hashmap_iter(m->timers, timer_delete_cb, NULL); hashmap_destroy(m->timers); // Nuke the timers hashmap_iter(m->sets, set_delete_cb, NULL); hashmap_destroy(m->sets); // Nuke the gauges hashmap_iter(m->gauges, gauge_delete_cb, NULL); hashmap_destroy(m->gauges); return 0; }
void destroy() { hashmap_foreach(b.plugins, __unload_plugin); hashmap_destroy(b.plugins); hashmap_destroy(b.handlers); hashmap_destroy(b.commands); for (int i = 0; i < b.channels->index; ++i) { free(b.channels->list[i]); } VEC_DESTROY(b.channels); cJSON_Delete(b.config); }
static char* test_hashmap_get () { // Test replacement. hashmap_t hashmap; hashmap_init(&hashmap, sizeof(int), 64); mu_assert("invalid missing key return", hashmap_get(&hashmap, "doesn't exist") == NULL); int value = 42; hashmap_put(&hashmap, "a", &value); mu_assert("hashmap['a'] != 42", hashmap_value(&hashmap, "a", int)); value = 13001; hashmap_put(&hashmap, "a", &value); mu_assert("hashmap value not replaced", hashmap_value(&hashmap, "a", int)); hashmap_destroy(&hashmap); // Test collisions. hashmap_init(&hashmap, sizeof(int), 1); value = 52; hashmap_put(&hashmap, "a", &value); mu_assert("hashmap['a'] != 52", hashmap_value(&hashmap, "a", int)); value = 64; hashmap_put(&hashmap, "b", &value); mu_assert("hashmap['b'] != 64", hashmap_value(&hashmap, "b", int)); value = 92; hashmap_put(&hashmap, "b", &value); mu_assert("hashmap[b] != 92", hashmap_value(&hashmap, "b", int)); hashmap_destroy(&hashmap); // Test string storage. hashmap_init(&hashmap, sizeof(char*), 64); char* str = calloc(9, sizeof(char)); strcpy(str, "test str"); hashmap_put(&hashmap, "a", &str); mu_assert("hashmap['a'] != 'test str'", strcmp(str, hashmap_value(&hashmap, "a", char*)) == 0); char* str2 = calloc(10, sizeof(char)); strcpy(str2, "test str2"); hashmap_put(&hashmap, "b", &str2); mu_assert("hashmap['b'] != 'test str2'", strcmp(str2, hashmap_value(&hashmap, "b", char*)) == 0); hashmap_destroy_deep(&hashmap); return 0; }
void free_sequential_query(sequential_query_t* q) { switch( q->type ) { case SEQ_BOOLEAN: { sequential_boolean_query_t* b = (sequential_boolean_query_t*) q; free_sequential_query(b->left); free_sequential_query(b->right); free(b); } break; case SEQ_REGEXP: { sequential_regexp_query_t* b = (sequential_regexp_query_t*) q; results_writer_destroy( & b->results_writer ); //free_nfa_description( & b->nfa ); seq_free_regexp(b->matcher); if( b->matches.num > 0 ) { // pull out all of the matches and free the keys and values. for( hash_t i = 0; i < b->matches.size; i++ ) { free_seq_search_entry( & b->matches.map[i] ); } } hashmap_destroy( & b->matches ); free(b); } break; } }
END_TEST START_TEST(test_map_put_clear_get) { bloom_hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); char buf[100]; void *out; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); out = (void*)(intptr_t)i; fail_unless(hashmap_put(map, (char*)buf, out) == 1); } fail_unless(hashmap_size(map) == 100); fail_unless(hashmap_clear(map) == 0); fail_unless(hashmap_size(map) == 0); for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_get(map, (char*)buf, &out) == -1); } res = hashmap_destroy(map); fail_unless(res == 0); }
END_TEST START_TEST(test_map_put_get) { hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); char buf[100]; void *out; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); out = 0 & i; fail_unless(hashmap_put(map, (char*)buf, out) == 1); } for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_get(map, (char*)buf, &out) == 0); fail_unless(out == (0 & i)); } res = hashmap_destroy(map); fail_unless(res == 0); }
static int free_data(void* data, void *arg) { userdata *dat = (userdata *) data; /* 删除整个子 map */ hashmap_destroy(dat->map, free_elem, 0); free(dat); return 0; }
END_TEST START_TEST(test_map_put_delete) { hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); char buf[100]; void *out; int j; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); j = 0 & i; out = (void *)&j; fail_unless(hashmap_put(map, (char*)buf, out) == 1); } fail_unless(hashmap_size(map) == 100); for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_delete(map, (char*)buf) == 0); fail_unless(hashmap_size(map) == (100-i-1)); } fail_unless(hashmap_size(map) == 0); res = hashmap_destroy(map); fail_unless(res == 0); }
END_TEST START_TEST(test_hashmap_contains) { HashMap *map = hashmap_create(5); hashmap_put(map, "foo", "bar"); ck_assert_int_eq(hashmap_contains(map, "foo"), 1); ck_assert_int_eq(hashmap_contains(map, "baz"), 0); hashmap_destroy(map); }
static char* test_hashmap_init () { hashmap_t hashmap; hashmap_init(&hashmap, sizeof(int), 64); mu_assert("hashmap element size incorrect", hashmap.element_size == sizeof(int)); mu_assert("hashmap capacity incorrect", hashmap.capacity == 64); mu_assert("hashmap data not allocated", hashmap.data != NULL); hashmap_destroy(&hashmap); return 0; }
static char* test_hashmap_func_ptr () { hashmap_t hashmap; hashmap_init(&hashmap, sizeof(thm__func), 64); thm__func value = thm__dummy; hashmap_put(&hashmap, "func", &value); mu_assert("function pointers not equal", thm__dummy == hashmap_value(&hashmap, "func", thm__func)); hashmap_destroy(&hashmap); return 0; }
void ini_destroy(ini_context_t *context) { if (context == NULL) { return; } if (context->global.items != NULL) { free(context->global.items); memset(&context->global, 0, sizeof(ini_section_t)); } hashmap_destroy(context->sections, ini_free_hash_data_func, NULL); }
END_TEST START_TEST(test_map_clear_no_keys) { hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); fail_unless(hashmap_clear(map) == 0); res = hashmap_destroy(map); fail_unless(res == 0); }
END_TEST START_TEST(test_hashmap_clear_index) { HashMap *map = hashmap_create(5); hashmap_put(map, "a", "test"); hashmap_clear_index(map, 0); ck_assert_int_eq(map->size, 0); ck_assert_int_eq(hashmap_contains(map, "a"), 0); hashmap_destroy(map); }
END_TEST START_TEST(test_hashmap_index) { HashMap *map = hashmap_create(5); ck_assert_int_eq(hashmap_index(map, "a"), 0); ck_assert_int_eq(hashmap_index(map, "b"), 1); ck_assert_int_eq(hashmap_index(map, "c"), 2); ck_assert_int_eq(hashmap_index(map, "d"), 3); ck_assert_int_eq(hashmap_index(map, "e"), 4); ck_assert_int_eq(hashmap_index(map, "f"), 0); hashmap_destroy(map); }
END_TEST START_TEST(test_hashmap_locate_entry) { HashMap *map = hashmap_create(5); const char *key = "foo"; const char *value = "bar"; hashmap_put(map, key, value); Entry *entry = hashmap_locate_entry(map, key); ck_assert_str_eq(entry->key, key); ck_assert_str_eq(entry->value, value); hashmap_destroy(map); }
static char* test_hashmap_keys () { hashmap_t hashmap; hashmap_init(&hashmap, sizeof(int), 64); int val1 = 99; int val2 = 100; hashmap_put(&hashmap, "key1", &val1); hashmap_put(&hashmap, "key2", &val2); mu_assert("key1 not stored", strcmp("key1", vector_get(hashmap.keys, 0, char*)) == 0); mu_assert("key2 not stored", strcmp("key2", vector_get(hashmap.keys, 1, char*)) == 0); hashmap_destroy(&hashmap); return 0; }
END_TEST START_TEST(test_hashmap_get) { HashMap *map = hashmap_create(5); const char *key = "foo"; const char *value = "bar"; unsigned int retrieved; const char *result; hashmap_put(map, key, value); retrieved = hashmap_get(map, key, &result); ck_assert_str_eq(result, value); ck_assert_int_eq(retrieved, 1); hashmap_destroy(map); }
Hashmap *hashmap_create(Hashmap_compare compare, Hashmap_hash hash) { Hashmap *map = calloc(1, sizeof(Hashmap)); check_mem(map); map->compare = compare == NULL ? default_compare : compare; map->hash = (hash == NULL) ? default_hash : hash; map->buckets = darray_create(sizeof(DArray *), DEFAULT_NUMBER_OF_BUCKETS); check_mem(map->buckets); map->buckets->end = map->buckets->max; // fake out expanding it return map; error: if (map) { hashmap_destroy(map); } return NULL; }
int main() { HashMap* hashmap = hashmap_create(); hashmap_add_node(hashmap, "trykey", "tryval", NULL, NULL); hashmap_add_node(hashmap, "trykey2", "tryval2", NULL, NULL); HashMapNode* node = hashmap_get(hashmap, "trykey"); assert(!strcmp("trykey", node->key)); assert(!strcmp("tryval", node->value)); node = hashmap_get(hashmap, "trykey2"); assert(!strcmp("trykey2", node->key)); assert(!strcmp("tryval2", node->value)); assert(hashmap->count == 2); hashmap_destroy(hashmap); assert(hashmap->count == 0); return 0; }
END_TEST START_TEST(test_map_delete_no_keys) { hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); fail_unless(hashmap_size(map) == 0); char buf[100]; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_delete(map, (char*)buf) == -1); } res = hashmap_destroy(map); fail_unless(res == 0); }
END_TEST START_TEST(test_hashmap_entry_for_key) { HashMap *map = hashmap_create(5); Entry *entry; const char *key = "foo"; hashmap_put(map, key, "bar"); // Existing entry entry = hashmap_entry_for_key(map, key); ck_assert_str_eq(entry->key, key); // Expecting a new entry entry = hashmap_entry_for_key(map, "baz"); ck_assert(entry->key == NULL); hashmap_destroy(map); }
END_TEST START_TEST(test_map_put) { bloom_hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); fail_unless(hashmap_size(map) == 0); char buf[100]; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_put(map, (char*)buf, NULL) == 1); } fail_unless(hashmap_size(map) == 100); res = hashmap_destroy(map); fail_unless(res == 0); }
END_TEST START_TEST(test_hashmap_remove) { HashMap *map = hashmap_create(5); const char *key = "foo"; unsigned int result; result = hashmap_remove(map, key); ck_assert_int_eq(result, 0); hashmap_put(map, key, "bar"); ck_assert_int_eq(hashmap_contains(map, key), 1); ck_assert_int_eq(map->size, 1); result = hashmap_remove(map, key); ck_assert_int_eq(result, 1); ck_assert_int_eq(hashmap_contains(map, key), 0); ck_assert_int_eq(map->size, 0); hashmap_destroy(map); }
END_TEST START_TEST(test_map_put_iter) { bloom_hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); char buf[100]; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_put(map, (char*)buf, NULL) == 1); } int val = 0; fail_unless(hashmap_iter(map, iter_test, (void*)&val) == 0); fail_unless(val == 100); res = hashmap_destroy(map); fail_unless(res == 0); }
/* clean up routine for flow monitoring */ void flowmon_cleanup(void) { int i; net_disable_timestamp(); /* destroy each table file we created */ for (i = pna_tables - 1; i >= 0; i--) { if (flowtab_info[i].table_name[0] != '\0') { remove_proc_entry(flowtab_info[i].table_name, proc_parent); } if (flowtab_info[i].map != NULL) { hashmap_destroy(flowtab_info[i].map); } } /* free up table meta-information struct */ vfree(flowtab_info); /* destroy /proc directory */ remove_proc_entry(PNA_PROCDIR, NULL); }
END_TEST START_TEST(test_hashmap_put) { HashMap *map = hashmap_create(5); const char *key = "foo"; const char *value = "bar"; const char *value_replacement = "baz"; const char *result; ck_assert_int_eq(hashmap_put(map, key, value), 1); ck_assert_int_eq(hashmap_get(map, key, &result), 1); ck_assert_str_eq(result, value); ck_assert_int_eq(map->size, 1); ck_assert_int_eq(hashmap_put(map, key, value_replacement), 1); ck_assert_int_eq(hashmap_get(map, key, &result), 1); ck_assert_str_eq(result, value_replacement); ck_assert_int_eq(map->size, 1); hashmap_destroy(map); }
END_TEST START_TEST(test_map_put_grow) { hashmap *map; int res = hashmap_init(32, &map); // Only 32 slots fail_unless(res == 0); char buf[100]; void *out; for (int i=0; i<1000;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_put(map, (char*)buf, NULL) == 1); } int val = 0; fail_unless(hashmap_iter(map, iter_test, (void*)&val) == 0); fail_unless(val == 1000); fail_unless(hashmap_size(map) == 1000); res = hashmap_destroy(map); fail_unless(res == 0); }
static void arbitrary( void ) { int res, val; hashmap_t *hashmap; hashmap = hashmap_create(); assert( hashmap ); res = hashmap_insert( hashmap, "test", 100 ); assert( 0 == res ); res = hashmap_insert( hashmap, "test", 101 ); assert( 0 == res ); res = hashmap_insert( hashmap, "test2", 3 ); assert( 0 == res ); res = hashmap_find( hashmap, "test2", NULL ); assert( 1 == res ); res = hashmap_find( hashmap, "test2", &val ); assert( 1 == res ); assert( 3 == val ); res = hashmap_find( hashmap, "test", &val ); assert( 1 == res ); assert( 101 == val ); hashmap_erase( hashmap, "test" ); res = hashmap_find( hashmap, "test", NULL ); assert( 0 == res ); hashmap_erase( hashmap, "test" ); res = hashmap_find( hashmap, "test", NULL ); assert( 0 == res ); hashmap_destroy( hashmap ); }
void cache_destroy(cache_t* c) { int i; cache_list_entry_t* le; unsigned char* data; int data_number; // OK to destroy a zero-initialized cache. if( c->data == NULL ) return; for( i = 0; i < c->cache_size; i++ ) { le = & c->list[i]; data_number = i; data = ((unsigned char*) c->data) + data_number * c->data_size; if( le->id != NULL && c->evict ) c->evict(le->id, c->data_size, data, c->context); if( le->id ) c->free_id(le->id); } free(c->list); free(c->data); hashmap_destroy(&c->mapping); c->list = NULL; c->data = NULL; }