Generalize the allocator test function and use it to test the simple and

the strict allocators as well.

svn path=/trunk/; revision=48586
This commit is contained in:
Evan Huus 2013-03-27 12:43:29 +00:00
parent 5a877ea9fe
commit 0a8b96a41b
1 changed files with 66 additions and 14 deletions

View File

@ -27,24 +27,56 @@
#include "wmem.h"
#include "wmem_allocator.h"
#include "wmem_allocator_block.h"
#include "wmem_allocator_simple.h"
#include "wmem_allocator_strict.h"
#include "config.h"
#define MAX_SIMULTANEOUS_ALLOCS 1024
#define MAX_ALLOC_SIZE (1024*64)
typedef void (*wmem_verify_func)(wmem_allocator_t *allocator);
/* A local copy of wmem_allocator_new that ignores the
* WIRESHARK_DEBUG_WMEM_OVERRIDE variable so that test functions are
* guaranteed to actually get the allocator type they asked for */
static wmem_allocator_t *
wmem_allocator_force_new(const wmem_allocator_type_t type)
{
wmem_allocator_t *allocator;
switch (type) {
case WMEM_ALLOCATOR_SIMPLE:
allocator = wmem_simple_allocator_new();
break;
case WMEM_ALLOCATOR_BLOCK:
allocator = wmem_block_allocator_new();
break;
case WMEM_ALLOCATOR_STRICT:
allocator = wmem_strict_allocator_new();
break;
default:
g_assert_not_reached();
/* This is necessary to squelch MSVC errors; is there
any way to tell it that g_assert_not_reached()
never returns? */
return NULL;
};
allocator->type = type;
return allocator;
}
static void
wmem_test_block_allocator(void)
wmem_test_allocator(wmem_allocator_type_t type, wmem_verify_func verify)
{
int i;
char *ptrs[MAX_SIMULTANEOUS_ALLOCS];
wmem_allocator_t *allocator;
/* we set up our allocator directly to ensure our type doesn't get
* overridden */
allocator = wmem_block_allocator_new();
allocator->type = WMEM_ALLOCATOR_BLOCK;
allocator = wmem_allocator_force_new(type);
wmem_block_verify(allocator);
if (verify) (*verify)(allocator);
/* start with some fairly simple deterministic tests */
@ -59,10 +91,10 @@ wmem_test_block_allocator(void)
wmem_free(allocator, ptrs[i]);
}
wmem_block_verify(allocator);
if (verify) (*verify)(allocator);
wmem_free_all(allocator);
wmem_gc(allocator);
wmem_block_verify(allocator);
if (verify) (*verify)(allocator);
for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) {
ptrs[i] = (char *)wmem_alloc0(allocator, 64);
@ -71,10 +103,10 @@ wmem_test_block_allocator(void)
wmem_free(allocator, ptrs[i]);
}
wmem_block_verify(allocator);
if (verify) (*verify)(allocator);
wmem_free_all(allocator);
wmem_gc(allocator);
wmem_block_verify(allocator);
if (verify) (*verify)(allocator);
for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) {
ptrs[i] = (char *)wmem_alloc0(allocator, 512);
@ -88,10 +120,10 @@ wmem_test_block_allocator(void)
wmem_free(allocator, ptrs[i]);
}
wmem_block_verify(allocator);
if (verify) (*verify)(allocator);
wmem_free_all(allocator);
wmem_gc(allocator);
wmem_block_verify(allocator);
if (verify) (*verify)(allocator);
/* now do some random fuzz-like tests */
@ -135,18 +167,38 @@ wmem_test_block_allocator(void)
wmem_free(allocator, ptrs[ptrs_index]);
ptrs[ptrs_index] = NULL;
}
wmem_block_verify(allocator);
if (verify) (*verify)(allocator);
}
wmem_destroy_allocator(allocator);
}
static void
wmem_test_allocator_block(void)
{
wmem_test_allocator(WMEM_ALLOCATOR_BLOCK, &wmem_block_verify);
}
static void
wmem_test_allocator_simple(void)
{
wmem_test_allocator(WMEM_ALLOCATOR_SIMPLE, NULL);
}
static void
wmem_test_allocator_strict(void)
{
wmem_test_allocator(WMEM_ALLOCATOR_STRICT, &wmem_strict_check_canaries);
}
int
main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/wmem/block-allocator", wmem_test_block_allocator);
g_test_add_func("/wmem/allocator/block", wmem_test_allocator_block);
g_test_add_func("/wmem/allocator/simple", wmem_test_allocator_simple);
g_test_add_func("/wmem/allocator/strict", wmem_test_allocator_strict);
return g_test_run();
}