static void wmem_time_allocator(wmem_allocator_type_t type) { int i, j; wmem_allocator_t *allocator; allocator = wmem_allocator_force_new(type); for (j=0; j<2048; j++) { for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) { wmem_alloc(allocator, 8); } wmem_free_all(allocator); for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) { wmem_alloc(allocator, 32); } wmem_free_all(allocator); for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) { wmem_alloc(allocator, 256); } wmem_free_all(allocator); for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) { wmem_alloc(allocator, 1024); } wmem_free_all(allocator); } wmem_destroy_allocator(allocator); }
void epan_dissect_cleanup(epan_dissect_t* edt) { g_assert(edt); g_slist_free(edt->pi.proto_data); g_slist_free(edt->pi.dependent_frames); /* Free the data sources list. */ free_data_sources(&edt->pi); if (edt->tvb) { /* Free all tvb's chained from this tvb */ tvb_free_chain(edt->tvb); } if (edt->tree) { proto_tree_free(edt->tree); } if (pinfo_pool_cache == NULL) { wmem_free_all(edt->pi.pool); pinfo_pool_cache = edt->pi.pool; } else { wmem_destroy_allocator(edt->pi.pool); } }
void epan_dissect_reset(epan_dissect_t *edt) { /* We have to preserve the pool pointer across the memzeroing */ wmem_allocator_t *tmp; g_assert(edt); g_slist_free(edt->pi.proto_data); g_slist_free(edt->pi.dependent_frames); /* Free the data sources list. */ free_data_sources(&edt->pi); if (edt->tvb) { /* Free all tvb's chained from this tvb */ tvb_free_chain(edt->tvb); edt->tvb = NULL; } if (edt->tree) proto_tree_reset(edt->tree); tmp = edt->pi.pool; wmem_free_all(tmp); memset(&edt->pi, 0, sizeof(edt->pi)); edt->pi.pool = tmp; }
static void wmem_test_allocator_jumbo(wmem_allocator_type_t type, wmem_verify_func verify) { wmem_allocator_t *allocator; char *ptr, *ptr1; allocator = wmem_allocator_force_new(type); ptr = (char*)wmem_alloc0(allocator, 4*1024*1024); wmem_free(allocator, ptr); wmem_gc(allocator); ptr = (char*)wmem_alloc0(allocator, 4*1024*1024); if (verify) (*verify)(allocator); wmem_free(allocator, ptr); wmem_gc(allocator); if (verify) (*verify)(allocator); ptr = (char *)wmem_alloc0(allocator, 10*1024*1024); ptr1 = (char *)wmem_alloc0(allocator, 13*1024*1024); ptr1 = (char *)wmem_realloc(allocator, ptr1, 10*1024*1024); memset(ptr1, 0, 10*1024*1024); ptr = (char *)wmem_realloc(allocator, ptr, 13*1024*1024); memset(ptr, 0, 13*1024*1024); if (verify) (*verify)(allocator); wmem_gc(allocator); if (verify) (*verify)(allocator); wmem_free(allocator, ptr1); if (verify) (*verify)(allocator); wmem_free_all(allocator); wmem_gc(allocator); if (verify) (*verify)(allocator); wmem_destroy_allocator(allocator); }
static void wmem_test_allocator_det(wmem_allocator_t *allocator, wmem_verify_func verify, guint len) { int i; char *ptrs[MAX_SIMULTANEOUS_ALLOCS]; /* we use wmem_alloc0 in part because it tests slightly more code, but * primarily so that if the allocator doesn't give us enough memory or * gives us memory that includes its own metadata, we write to it and * things go wrong, causing the tests to fail */ for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) { ptrs[i] = (char *)wmem_alloc0(allocator, len); } for (i=MAX_SIMULTANEOUS_ALLOCS-1; i>=0; i--) { /* no wmem_realloc0 so just use memset manually */ ptrs[i] = (char *)wmem_realloc(allocator, ptrs[i], 4*len); memset(ptrs[i], 0, 4*len); } for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) { wmem_free(allocator, ptrs[i]); } if (verify) (*verify)(allocator); wmem_free_all(allocator); wmem_gc(allocator); if (verify) (*verify)(allocator); }
static void wmem_test_allocator_callbacks(void) { wmem_allocator_t *allocator; gboolean t = TRUE; gboolean f = FALSE; guint cb_id; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); expected_allocator = allocator; wmem_register_callback(expected_allocator, &wmem_test_cb, &f); wmem_register_callback(expected_allocator, &wmem_test_cb, &f); cb_id = wmem_register_callback(expected_allocator, &wmem_test_cb, &t); wmem_register_callback(expected_allocator, &wmem_test_cb, &t); wmem_register_callback(expected_allocator, &wmem_test_cb, &f); expected_event = WMEM_CB_FREE_EVENT; cb_called_count = 0; wmem_free_all(allocator); g_assert(cb_called_count == 5); cb_called_count = 0; wmem_free_all(allocator); g_assert(cb_called_count == 2); cb_called_count = 0; wmem_free_all(allocator); g_assert(cb_called_count == 2); wmem_unregister_callback(allocator, cb_id); cb_called_count = 0; wmem_free_all(allocator); g_assert(cb_called_count == 1); cb_id = wmem_register_callback(expected_allocator, &wmem_test_cb, &f); wmem_register_callback(expected_allocator, &wmem_test_cb, &t); cb_called_count = 0; wmem_free_all(allocator); g_assert(cb_called_count == 3); wmem_unregister_callback(allocator, cb_id); cb_called_count = 0; wmem_free_all(allocator); g_assert(cb_called_count == 2); wmem_register_callback(expected_allocator, &wmem_test_cb, &t); expected_event = WMEM_CB_DESTROY_EVENT; cb_called_count = 0; wmem_destroy_allocator(allocator); g_assert(cb_called_count == 3); }
static void wmem_test_map(void) { wmem_allocator_t *allocator; wmem_map_t *map; gchar *str_key; unsigned int i; void *ret; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); /* insertion, lookup and removal of simple integer keys */ map = wmem_map_new(allocator, g_direct_hash, g_direct_equal); g_assert(map); for (i=0; i<CONTAINER_ITERS; i++) { ret = wmem_map_insert(map, GINT_TO_POINTER(i), GINT_TO_POINTER(777777)); g_assert(ret == NULL); ret = wmem_map_insert(map, GINT_TO_POINTER(i), GINT_TO_POINTER(i)); g_assert(ret == GINT_TO_POINTER(777777)); ret = wmem_map_insert(map, GINT_TO_POINTER(i), GINT_TO_POINTER(i)); g_assert(ret == GINT_TO_POINTER(i)); } for (i=0; i<CONTAINER_ITERS; i++) { ret = wmem_map_lookup(map, GINT_TO_POINTER(i)); g_assert(ret == GINT_TO_POINTER(i)); ret = wmem_map_remove(map, GINT_TO_POINTER(i)); g_assert(ret == GINT_TO_POINTER(i)); ret = wmem_map_lookup(map, GINT_TO_POINTER(i)); g_assert(ret == NULL); ret = wmem_map_remove(map, GINT_TO_POINTER(i)); g_assert(ret == NULL); } wmem_free_all(allocator); map = wmem_map_new(allocator, wmem_str_hash, g_str_equal); g_assert(map); /* string keys and for-each */ for (i=0; i<CONTAINER_ITERS; i++) { str_key = wmem_test_rand_string(allocator, 1, 64); wmem_map_insert(map, str_key, GINT_TO_POINTER(i)); ret = wmem_map_lookup(map, str_key); g_assert(ret == GINT_TO_POINTER(i)); } wmem_destroy_allocator(allocator); }
static void wmem_test_tree(void) { wmem_allocator_t *allocator, *extra_allocator; wmem_tree_t *tree; guint32 i; int seen_values = 0; int j; gchar *str_key; #define WMEM_TREE_MAX_KEY_COUNT 8 #define WMEM_TREE_MAX_KEY_LEN 4 int key_count; wmem_tree_key_t keys[WMEM_TREE_MAX_KEY_COUNT]; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); extra_allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); tree = wmem_tree_new(allocator); g_assert(tree); g_assert(wmem_tree_is_empty(tree)); /* test basic 32-bit key operations */ for (i=0; i<CONTAINER_ITERS; i++) { g_assert(wmem_tree_lookup32(tree, i) == NULL); if (i > 0) { g_assert(wmem_tree_lookup32_le(tree, i) == GINT_TO_POINTER(i-1)); } wmem_tree_insert32(tree, i, GINT_TO_POINTER(i)); g_assert(wmem_tree_lookup32(tree, i) == GINT_TO_POINTER(i)); g_assert(!wmem_tree_is_empty(tree)); } wmem_free_all(allocator); tree = wmem_tree_new(allocator); for (i=0; i<CONTAINER_ITERS; i++) { guint32 rand_int = g_test_rand_int(); wmem_tree_insert32(tree, rand_int, GINT_TO_POINTER(i)); g_assert(wmem_tree_lookup32(tree, rand_int) == GINT_TO_POINTER(i)); } wmem_free_all(allocator); /* test auto-reset functionality */ tree = wmem_tree_new_autoreset(allocator, extra_allocator); for (i=0; i<CONTAINER_ITERS; i++) { g_assert(wmem_tree_lookup32(tree, i) == NULL); wmem_tree_insert32(tree, i, GINT_TO_POINTER(i)); g_assert(wmem_tree_lookup32(tree, i) == GINT_TO_POINTER(i)); } wmem_free_all(extra_allocator); for (i=0; i<CONTAINER_ITERS; i++) { g_assert(wmem_tree_lookup32(tree, i) == NULL); g_assert(wmem_tree_lookup32_le(tree, i) == NULL); } wmem_free_all(allocator); /* test array key functionality */ tree = wmem_tree_new(allocator); key_count = g_random_int_range(1, WMEM_TREE_MAX_KEY_COUNT); for (j=0; j<key_count; j++) { keys[j].length = g_random_int_range(1, WMEM_TREE_MAX_KEY_LEN); } keys[key_count].length = 0; for (i=0; i<CONTAINER_ITERS; i++) { for (j=0; j<key_count; j++) { keys[j].key = (guint32*)wmem_test_rand_string(allocator, (keys[j].length*4), (keys[j].length*4)+1); } wmem_tree_insert32_array(tree, keys, GINT_TO_POINTER(i)); g_assert(wmem_tree_lookup32_array(tree, keys) == GINT_TO_POINTER(i)); } wmem_free_all(allocator); tree = wmem_tree_new(allocator); keys[0].length = 1; keys[0].key = wmem_new(allocator, guint32); *(keys[0].key) = 0; keys[1].length = 0; for (i=0; i<CONTAINER_ITERS; i++) { wmem_tree_insert32_array(tree, keys, GINT_TO_POINTER(i)); *(keys[0].key) += 4; } *(keys[0].key) = 0; for (i=0; i<CONTAINER_ITERS; i++) { g_assert(wmem_tree_lookup32_array(tree, keys) == GINT_TO_POINTER(i)); for (j=0; j<3; j++) { (*(keys[0].key)) += 1; g_assert(wmem_tree_lookup32_array_le(tree, keys) == GINT_TO_POINTER(i)); } *(keys[0].key) += 1; } wmem_free_all(allocator); /* test string key functionality */ tree = wmem_tree_new(allocator); for (i=0; i<CONTAINER_ITERS; i++) { str_key = wmem_test_rand_string(allocator, 1, 64); wmem_tree_insert_string(tree, str_key, GINT_TO_POINTER(i), 0); g_assert(wmem_tree_lookup_string(tree, str_key, 0) == GINT_TO_POINTER(i)); } wmem_free_all(allocator); tree = wmem_tree_new(allocator); for (i=0; i<CONTAINER_ITERS; i++) { str_key = wmem_test_rand_string(allocator, 1, 64); wmem_tree_insert_string(tree, str_key, GINT_TO_POINTER(i), WMEM_TREE_STRING_NOCASE); g_assert(wmem_tree_lookup_string(tree, str_key, WMEM_TREE_STRING_NOCASE) == GINT_TO_POINTER(i)); } wmem_free_all(allocator); /* test for-each functionality */ tree = wmem_tree_new(allocator); expected_user_data = GINT_TO_POINTER(g_test_rand_int()); for (i=0; i<CONTAINER_ITERS; i++) { gint tmp; do { tmp = g_test_rand_int(); } while (wmem_tree_lookup32(tree, tmp)); value_seen[i] = FALSE; wmem_tree_insert32(tree, tmp, GINT_TO_POINTER(i)); } cb_called_count = 0; cb_continue_count = CONTAINER_ITERS; wmem_tree_foreach(tree, wmem_test_foreach_cb, expected_user_data); g_assert(cb_called_count == CONTAINER_ITERS); g_assert(cb_continue_count == 0); for (i=0; i<CONTAINER_ITERS; i++) { g_assert(value_seen[i]); value_seen[i] = FALSE; } cb_called_count = 0; cb_continue_count = 10; wmem_tree_foreach(tree, wmem_test_foreach_cb, expected_user_data); g_assert(cb_called_count == 10); g_assert(cb_continue_count == 0); for (i=0; i<CONTAINER_ITERS; i++) { if (value_seen[i]) { seen_values++; } } g_assert(seen_values == 10); wmem_destroy_allocator(extra_allocator); wmem_destroy_allocator(allocator); }
static void wmem_test_strbuf(void) { wmem_allocator_t *allocator; wmem_strbuf_t *strbuf; int i; char *str; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); strbuf = wmem_strbuf_new(allocator, "TEST"); g_assert(strbuf); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TEST"); g_assert(wmem_strbuf_get_len(strbuf) == 4); wmem_strbuf_append(strbuf, "FUZZ"); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TESTFUZZ"); g_assert(wmem_strbuf_get_len(strbuf) == 8); wmem_strbuf_append_printf(strbuf, "%d%s", 3, "a"); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TESTFUZZ3a"); g_assert(wmem_strbuf_get_len(strbuf) == 10); wmem_strbuf_append_c(strbuf, 'q'); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TESTFUZZ3aq"); g_assert(wmem_strbuf_get_len(strbuf) == 11); wmem_strbuf_append_unichar(strbuf, g_utf8_get_char("\xC2\xA9")); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TESTFUZZ3aq\xC2\xA9"); g_assert(wmem_strbuf_get_len(strbuf) == 13); wmem_strbuf_truncate(strbuf, 32); wmem_strbuf_truncate(strbuf, 24); wmem_strbuf_truncate(strbuf, 16); wmem_strbuf_truncate(strbuf, 13); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TESTFUZZ3aq\xC2\xA9"); g_assert(wmem_strbuf_get_len(strbuf) == 13); wmem_strbuf_truncate(strbuf, 3); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "TES"); g_assert(wmem_strbuf_get_len(strbuf) == 3); strbuf = wmem_strbuf_sized_new(allocator, 10, 10); g_assert(strbuf); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, ""); g_assert(wmem_strbuf_get_len(strbuf) == 0); wmem_strbuf_append(strbuf, "FUZZ"); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ"); g_assert(wmem_strbuf_get_len(strbuf) == 4); wmem_strbuf_append_printf(strbuf, "%d%s", 3, "abcdefghijklmnop"); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ3abcd"); g_assert(wmem_strbuf_get_len(strbuf) == 9); wmem_strbuf_append(strbuf, "abcdefghijklmnopqrstuvwxyz"); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ3abcd"); g_assert(wmem_strbuf_get_len(strbuf) == 9); wmem_strbuf_append_c(strbuf, 'q'); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ3abcd"); g_assert(wmem_strbuf_get_len(strbuf) == 9); wmem_strbuf_append_unichar(strbuf, g_utf8_get_char("\xC2\xA9")); g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ3abcd"); g_assert(wmem_strbuf_get_len(strbuf) == 9); str = wmem_strbuf_finalize(strbuf); g_assert_cmpstr(str, ==, "FUZZ3abcd"); g_assert(strlen(str) == 9); wmem_free_all(allocator); strbuf = wmem_strbuf_new(allocator, "TEST"); for (i=0; i<1024; i++) { if (g_test_rand_bit()) { wmem_strbuf_append(strbuf, "ABC"); } else { wmem_strbuf_append_printf(strbuf, "%d%d", 3, 777); } wmem_strict_check_canaries(allocator); } g_assert(strlen(wmem_strbuf_get_str(strbuf)) == wmem_strbuf_get_len(strbuf)); wmem_destroy_allocator(allocator); }
static void wmem_test_allocator(wmem_allocator_type_t type, wmem_verify_func verify, int iterations) { int i; char *ptrs[MAX_SIMULTANEOUS_ALLOCS]; wmem_allocator_t *allocator; allocator = wmem_allocator_force_new(type); if (verify) (*verify)(allocator); /* start with some fairly simple deterministic tests */ wmem_test_allocator_det(allocator, verify, 8); wmem_test_allocator_det(allocator, verify, 64); wmem_test_allocator_det(allocator, verify, 512); for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) { ptrs[i] = wmem_alloc0_array(allocator, char, 32); } if (verify) (*verify)(allocator); wmem_free_all(allocator); wmem_gc(allocator); if (verify) (*verify)(allocator); /* now do some random fuzz-like tests */ /* reset our ptr array */ for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) { ptrs[i] = NULL; } /* Run enough iterations to fill the array 32 times */ for (i=0; i<iterations; i++) { gint ptrs_index; gint new_size; /* returns value 0 <= x < MAX_SIMULTANEOUS_ALLOCS which is a valid * index into ptrs */ ptrs_index = g_test_rand_int_range(0, MAX_SIMULTANEOUS_ALLOCS); if (ptrs[ptrs_index] == NULL) { /* if that index is unused, allocate some random amount of memory * between 0 and MAX_ALLOC_SIZE */ new_size = g_test_rand_int_range(0, MAX_ALLOC_SIZE); ptrs[ptrs_index] = (char *) wmem_alloc0(allocator, new_size); } else if (g_test_rand_bit()) { /* the index is used, and our random bit has determined we will be * reallocating instead of freeing. Do so to some random size * between 0 and MAX_ALLOC_SIZE, then manually zero the * new memory */ new_size = g_test_rand_int_range(0, MAX_ALLOC_SIZE); ptrs[ptrs_index] = (char *) wmem_realloc(allocator, ptrs[ptrs_index], new_size); memset(ptrs[ptrs_index], 0, new_size); } else { /* the index is used, and our random bit has determined we will be * freeing instead of reallocating. Do so and NULL the pointer for * the next iteration. */ wmem_free(allocator, ptrs[ptrs_index]); ptrs[ptrs_index] = NULL; } if (verify) (*verify)(allocator); } wmem_destroy_allocator(allocator); }
void wmem_destroy_allocator(wmem_allocator_t *allocator) { wmem_free_all(allocator); allocator->destroy(allocator); }