int main() { MHASH *table; MERR *err; char str[101], *pstr[NODE_NUM]; mtc_init("load.log", MTC_DEBUG); for (int i = 0; i < NODE_NUM; i++) { //mstr_rand_string(str, 100); //mstr_rand_string_fixlen(str, 100); snprintf(str, sizeof(str), "%d", i); pstr[i] = strdup(str); } err = mhash_init(&table, mhash_str_hash, mhash_str_comp, mhash_str_free); TRACE_NOK(err); mtimer_start(); for (int i = 0; i < NODE_NUM; i++) { mhash_insert(table, pstr[i], NULL); } mtimer_stop("hash insert"); mtimer_start(); for (int i = 0; i < NODE_NUM; i++) { mhash_lookup(table, pstr[i]); } mtimer_stop("hash lookup"); mhash_destroy(&table); return 0; }
void test_iterate() { MHASH *table; char s[KEY_LEN + 1], *ps[100]; MERR *err; for (int i = 0; i < 100; i++) { //mstr_rand_string(s, KEY_LEN); snprintf(s, sizeof(s), "str %d", i); ps[i] = strdup(s); /* TODO memory leak */ } err = mhash_init(&table, mhash_str_hash, mhash_str_comp, mhash_str_free); MTEST_ASSERT(err == MERR_OK); for (int i = 0; i < 100; i++) { err = mhash_insert(table, ps[i], strdup(ps[i])); MTEST_ASSERT(err == MERR_OK); } int x = 0; char *key, *val; MHASH_ITERATE(table, key, val) { //printf("key %s %s\n", key, ps[x]); MTEST_ASSERT_STR_EQ(key, val); x++; }
void * _typecheck_realloc(void * ptr, size_t size) { void * newmem; size_t oldsiz; if(ptr == 0) /* behaves like malloc */ return _typecheck_malloc(size); if(size == 0){ /* behaves like free */ _typecheck_free(ptr); return 0; } oldsiz = mhash_remove(ptr); newmem = realloc(ptr, size + TC_MALLOC_PADDING); if(newmem){ mhash_insert(newmem, size); if(newmem == ptr){ /* memory block extended */ if(size < oldsiz){ /* shrink */ _tcptr_clearTags((char *)ptr+size, oldsiz-size); } else { /* extend */ _tcptr_setTags((char *)ptr+oldsiz, size-oldsiz); } } else { /* new memory block created */ /*NOTE: newmem and old block may overlap: must clear before set!! */ _tcptr_clearTags(ptr, oldsiz); _tcptr_setTags(newmem, size); } } return newmem; }
MERR* mhash_insertf(MHASH *table, void *value, const char *fmt, ...) { char key[1024]; va_list ap; va_start(ap, fmt); vsnprintf(key, sizeof(key), fmt, ap); va_end(ap); /* TODO memory leak */ return mhash_insert(table, strdup(key), value); }
void * _typecheck_memalign(size_t alignment, size_t size) { void * newmem; newmem = memalign(alignment, size + TC_MALLOC_PADDING); if(newmem){ mhash_insert(newmem, size); _tcptr_setTags(newmem, size); } return newmem; }
void * _typecheck_valloc(size_t size) { void * newmem; newmem = valloc(size + TC_MALLOC_PADDING); if(newmem){ mhash_insert(newmem, size); _tcptr_setTags(newmem, size); } return newmem; }
void * _typecheck_calloc(size_t nelem, size_t elsize) { void * newmem; newmem = calloc(nelem + TC_MALLOC_PADDING/elsize, elsize); if(newmem){ size_t size = nelem * elsize; mhash_insert(newmem, size); _tcptr_setTags(newmem, size); } return newmem; }
void test_basic() { MHASH *table; char s[KEY_LEN + 1], *ps[NODE_NUM], *data; MERR *err; for (int i = 0; i < NODE_NUM; i++) { //mstr_rand_string(s, KEY_LEN); snprintf(s, sizeof(s), "str %d", i); ps[i] = strdup(s); } err = mhash_init(&table, mhash_str_hash, mhash_str_comp, NULL); MTEST_ASSERT(err == MERR_OK); for (int i = 0; i < NODE_NUM; i++) { err = mhash_insert(table, ps[i], ps[i]); MTEST_ASSERT(err == MERR_OK); } MTEST_ASSERT(mhash_length(table) == NODE_NUM); for (int i = 0; i < NODE_NUM; i++) { MTEST_ASSERT(mhash_has_key(table, ps[i])); } for (int i = 0; i < NODE_NUM; i++) { data = mhash_lookup(table, ps[i]); MTEST_ASSERT(data == ps[i]); } for (int i = 0; i < NODE_NUM; i++) { MTEST_ASSERT(mhash_remove(table, ps[i])); } MTEST_ASSERT(mhash_length(table) == 0); mhash_destroy(&table); }