/* remove worker from job assignment list */ static void remove_worker(struct wproc_worker *worker) { unsigned int i, j = 0; struct wproc_list *wpl = worker->wp_list; for (i = 0; i < wpl->len; i++) { if (wpl->wps[i] == worker) continue; wpl->wps[j++] = wpl->wps[i]; } wpl->len = j; if (!specialized_workers || wpl->len) return; to_remove = wpl; dkhash_walk_data(specialized_workers, remove_specialized); }
/* * This gets called from both parent and worker process, so * we must take care not to blindly shut down everything here */ void free_worker_memory(int flags) { if (workers.wps) { unsigned int i; for (i = 0; i < workers.len; i++) { if (!workers.wps[i]) continue; wproc_destroy(workers.wps[i], flags); workers.wps[i] = NULL; } free(workers.wps); } to_remove = NULL; dkhash_walk_data(specialized_workers, remove_specialized); dkhash_destroy(specialized_workers); workers.wps = NULL; workers.len = 0; workers.idx = 0; }
/* * This gets called from both parent and worker process, so * we must take care not to blindly shut down everything here */ void free_worker_memory(int flags) { if (workers.wps) { unsigned int i; for (i = 0; i < workers.len; i++) { if (!workers.wps[i]) continue; wproc_destroy(workers.wps[i], flags); workers.wps[i] = NULL; } my_free(workers.wps); } workers.len = 0; workers.idx = 0; to_remove = NULL; dkhash_walk_data(specialized_workers, remove_specialized); dkhash_destroy(specialized_workers); specialized_workers = NULL; /* Don't leave pointers to freed memory. */ }
int main(int argc, char **argv) { dkhash_table *tx, *t; unsigned int x; int ret, r2; struct test_data s; char *p1, *p2; char *strs[10]; char tmp[32]; t_set_colors(0); t_start("dkhash basic test"); t = dkhash_create(512); p1 = strdup("a not-so secret value"); dkhash_insert(t, "nisse", NULL, p1); ok_int(dkhash_num_entries_max(t), 1, "Added one entry, so that's max"); ok_int(dkhash_num_entries_added(t), 1, "Added one entry, so one added"); ok_int(dkhash_table_size(t), 512, "Table must be sized properly"); ok_int(dkhash_collisions(t), 0, "One entry, so zero collisions"); p2 = dkhash_get(t, "nisse", NULL); test(p1 == p2, "get should get what insert set"); dkhash_insert(t, "kalle", "bananas", p1); p2 = dkhash_get(t, "kalle", "bezinga"); test(p1 != p2, "we should never get the wrong key"); ok_int(2, dkhash_num_entries(t), "should be 2 entries after 2 inserts"); p2 = dkhash_remove(t, "kalle", "bezinga"); ok_int(2, dkhash_num_entries(t), "should be 2 entries after 2 inserts and 1 failed remove"); ok_int(0, dkhash_num_entries_removed(t), "should be 0 removed entries after failed remove"); p2 = dkhash_remove(t, "kalle", "bananas"); test(p1 == p2, "dkhash_remove() should return removed data"); ok_int(dkhash_num_entries(t), 1, "should be 1 entries after 2 inserts and 1 successful remove"); p2 = dkhash_remove(t, "nisse", NULL); test(p1 == p2, "dkhash_remove() should return removed data"); ret = t_end(); t_reset(); /* lots of tests below, so we shut up while they're running */ t_verbose = 0; t_start("dkhash_walk_data() test"); memset(&s, 0, sizeof(s)); /* first we set up the dkhash-tables */ tx = dkhash_create(16); for (x = 0; x < ARRAY_SIZE(keys); x++) { dkhash_insert(tx, keys[x].k1, NULL, ddup(x, 0, 0)); dkhash_insert(tx, keys[x].k2, NULL, ddup(x, 0, 0)); dkhash_insert(tx, keys[x].k1, keys[x].k2, ddup(x, 0, 0)); s.x += 3; ok_int(s.x, dkhash_num_entries(tx), "x table adding"); } ok_int(s.x, dkhash_num_entries(tx), "x table done adding"); for (x = 0; x < ARRAY_SIZE(keys); x++) { del.x = x; del.i = del.j = 0; ok_int(s.x, dkhash_num_entries(tx), "x table pre-delete"); s.x -= 3; dkhash_walk_data(tx, del_matching); ok_int(s.x, dkhash_num_entries(tx), "x table post-delete"); } test(0 == dkhash_num_entries(tx), "x table post all ops"); test(0 == dkhash_check_table(tx), "x table consistency post all ops"); dkhash_debug_table(tx, 0); r2 = t_end(); ret = r2 ? r2 : ret; t_reset(); for (x = 0; x < 10; x++) { sprintf(tmp, "string %d", x); strs[x] = strdup(tmp); } t_start("dkhash single bucket add remove forward"); t = dkhash_create(1); for (x = 0; x < 10; x++) { dkhash_insert(t, strs[x], NULL, strs[x]); } for (x = 0; x < 10; x++) { p1 = strs[x]; p2 = dkhash_remove(t, p1, NULL); test(p1 == p2, "remove should return a value"); } r2 = t_end(); ret = r2 ? r2 : ret; t_reset(); t_start("dkhash single bucket add remove backward"); t = dkhash_create(1); for (x = 0; x < 10; x++) { dkhash_insert(t, strs[x], NULL, strs[x]); } for (x = 9; x; x--) { p1 = strs[x]; p2 = dkhash_remove(t, p1, NULL); test(p1 == p2, "remove should return a value"); } dkhash_destroy(t); r2 = t_end(); return r2 ? r2 : ret; }