#define BITS_PER_LONG 64 #include #include #include #include #include struct my_data { int x; struct hlist_node node; }; static DECLARE_HASHTABLE(test, 10); static struct my_data *values[1000000]; struct my_data *get_data(int v) { struct my_data *p; struct hlist_node *n; hash_for_each_possible(test, p, n, node, v) if (p->x == v) return p; return NULL; } bool verify(int i) { if (values[i] == NULL && get_data(i)) printf("No data, but found at %p (%d)\n", get_data(i), i); if (values[i]) return get_data(i) ? true : false; else return get_data(i) ? false : true; } int main(void) { int i; hash_init(test); printf("Empty? %d\n", hash_empty(test)); while (1) { for (i = 0; i < ARRAY_SIZE(values); i++) { int r = rand() % 3; if (i % 10000 == 0) { printf("\r%d ...", i); fflush(stdout); } switch (r) { case 0: assert(verify(i)); if (values[i]) break; values[i] = malloc(sizeof(values[i])); values[i]->x = i; hash_add(test, &values[i]->node, i); assert(verify(i)); break; case 1: assert(verify(i)); break; case 2: assert(verify(i)); if (values[i]) { hash_del(&values[i]->node); free(values[i]); values[i] = NULL; } assert(verify(i)); break; } } } return 0; }