aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvan Huus <eapache@gmail.com>2013-05-22 15:42:12 +0000
committerEvan Huus <eapache@gmail.com>2013-05-22 15:42:12 +0000
commit5426ba4e027326d7b5bea08fd1bb8cfab0847c76 (patch)
tree16147df109ecbdf895d72ea44cf10462f2e38933
parenta2f6822feab6985a3b097dec62de3691ae6d57d6 (diff)
Minor refactor: make the framework responsible for allocating and freeing the
actual wmem_allocator_t structure. This simplifies the internal API and deduplicates a few alloc/free calls in the individual allocator implementations. I'd originally made the allocators responsible for this on purpose with the idea that they'd be able to optimize something clever based on the type of allocator, but that's clearly more work and complexity than it's worth given the small number of allocators we create/destroy. svn path=/trunk/; revision=49512
-rw-r--r--doc/README.wmem35
-rw-r--r--epan/wmem/wmem_allocator.h2
-rw-r--r--epan/wmem/wmem_allocator_block.c25
-rw-r--r--epan/wmem/wmem_allocator_block.h4
-rw-r--r--epan/wmem/wmem_allocator_simple.c25
-rw-r--r--epan/wmem/wmem_allocator_simple.h4
-rw-r--r--epan/wmem/wmem_allocator_strict.c21
-rw-r--r--epan/wmem/wmem_allocator_strict.h4
-rw-r--r--epan/wmem/wmem_core.c16
-rw-r--r--epan/wmem/wmem_test.c13
10 files changed, 67 insertions, 82 deletions
diff --git a/doc/README.wmem b/doc/README.wmem
index 31ebf1bf11..9bdf4e4f7b 100644
--- a/doc/README.wmem
+++ b/doc/README.wmem
@@ -284,23 +284,24 @@ and realloc) are non-0.
- free_all()
- gc()
- - destroy()
-
-The free_all() function takes the private_data pointer and should free all the
-memory currently allocated in the pool. Note that this is not necessarilly
-exactly the same as calling free() on all the allocated blocks - free_all() is
-allowed to do additional cleanup or to make use of optimizations not available
-when freeing one block at a time.
-
-The gc() function takes the private_data pointer and should do whatever it can
-to reduce excess memory usage in the dissector by returning unused blocks to
-the OS, optimizing internal data structures, etc.
-
-The destroy() function does NOT take the private_data pointer - it instead takes
-a pointer to the allocator structure as a whole, since that structure may also
-need freeing. This function can assume that free_all() has been called
-immediately before it (though it can make no assumptions about whether or not
-gc() has ever been called).
+ - cleanup()
+
+All of these functions take only one parameter, which is the allocator's
+private_data pointer.
+
+The free_all() function should free all the memory currently allocated in the
+pool. Note that this is not necessarily exactly the same as calling free()
+on all the allocated blocks - free_all() is allowed to do additional cleanup
+or to make use of optimizations not available when freeing one block at a time.
+
+The gc() function should do whatever it can to reduce excess memory usage in
+the dissector by returning unused blocks to the OS, optimizing internal data
+structures, etc.
+
+The cleanup() function should do any final cleanup and free any and all memory.
+It is basically the equivalent of a destructor function. For simplicity, wmem
+is guaranteed to call free_all() immediately before this function. There is no
+such guarantee that gc() has (ever) been called.
4.2 Pool-Agnostic API
diff --git a/epan/wmem/wmem_allocator.h b/epan/wmem/wmem_allocator.h
index f62f71f183..d76c8770a3 100644
--- a/epan/wmem/wmem_allocator.h
+++ b/epan/wmem/wmem_allocator.h
@@ -47,7 +47,7 @@ struct _wmem_allocator_t {
/* Producer/Manager functions */
void (*free_all)(void *private_data);
void (*gc)(void *private_data);
- void (*destroy)(struct _wmem_allocator_t *allocator);
+ void (*cleanup)(void *private_data);
/* Callback List */
struct _wmem_user_cb_container_t *callbacks;
diff --git a/epan/wmem/wmem_allocator_block.c b/epan/wmem/wmem_allocator_block.c
index f31a898328..da64eee7ea 100644
--- a/epan/wmem/wmem_allocator_block.c
+++ b/epan/wmem/wmem_allocator_block.c
@@ -853,45 +853,36 @@ wmem_block_gc(void *private_data)
}
static void
-wmem_block_allocator_destroy(wmem_allocator_t *allocator)
+wmem_block_allocator_cleanup(void *private_data)
{
- wmem_block_allocator_t *real_allocator;
-
- real_allocator = (wmem_block_allocator_t*) allocator->private_data;
-
/* wmem guarantees that free_all() is called directly before this, so
* calling gc will return all our blocks to the OS automatically */
- wmem_block_gc(real_allocator);
+ wmem_block_gc(private_data);
/* then just free the allocator structs */
- g_slice_free(wmem_block_allocator_t, real_allocator);
- g_slice_free(wmem_allocator_t, allocator);
+ g_slice_free(wmem_block_allocator_t, private_data);
}
-wmem_allocator_t *
-wmem_block_allocator_new(void)
+void
+wmem_block_allocator_init(wmem_allocator_t *allocator)
{
- wmem_allocator_t *allocator;
wmem_block_allocator_t *block_allocator;
- allocator = g_slice_new(wmem_allocator_t);
block_allocator = g_slice_new(wmem_block_allocator_t);
- allocator->private_data = (void*) block_allocator;
-
allocator->alloc = &wmem_block_alloc;
allocator->realloc = &wmem_block_realloc;
allocator->free = &wmem_block_free;
allocator->free_all = &wmem_block_free_all;
allocator->gc = &wmem_block_gc;
- allocator->destroy = &wmem_block_allocator_destroy;
+ allocator->cleanup = &wmem_block_allocator_cleanup;
+
+ allocator->private_data = (void*) block_allocator;
block_allocator->block_list = NULL;
block_allocator->free_list_head = NULL;
block_allocator->free_insert_point = NULL;
-
- return allocator;
}
/*
diff --git a/epan/wmem/wmem_allocator_block.h b/epan/wmem/wmem_allocator_block.h
index 52648ef728..4434ef8d4b 100644
--- a/epan/wmem/wmem_allocator_block.h
+++ b/epan/wmem/wmem_allocator_block.h
@@ -32,8 +32,8 @@
extern "C" {
#endif /* __cplusplus */
-wmem_allocator_t *
-wmem_block_allocator_new(void);
+void
+wmem_block_allocator_init(wmem_allocator_t *allocator);
/* Exposed only for testing purposes */
void
diff --git a/epan/wmem/wmem_allocator_simple.c b/epan/wmem/wmem_allocator_simple.c
index db43310b47..c08abb0dee 100644
--- a/epan/wmem/wmem_allocator_simple.c
+++ b/epan/wmem/wmem_allocator_simple.c
@@ -111,40 +111,35 @@ wmem_simple_gc(void *private_data _U_)
}
static void
-wmem_simple_allocator_destroy(wmem_allocator_t *allocator)
+wmem_simple_allocator_cleanup(void *private_data)
{
- wmem_simple_allocator_t *private_allocator;
+ wmem_simple_allocator_t *allocator;
- private_allocator = (wmem_simple_allocator_t*) allocator->private_data;
+ allocator = (wmem_simple_allocator_t*) private_data;
- g_hash_table_destroy(private_allocator->block_table);
- g_slice_free(wmem_simple_allocator_t, private_allocator);
- g_slice_free(wmem_allocator_t, allocator);
+ g_hash_table_destroy(allocator->block_table);
+ g_slice_free(wmem_simple_allocator_t, allocator);
}
-wmem_allocator_t *
-wmem_simple_allocator_new(void)
+void
+wmem_simple_allocator_init(wmem_allocator_t *allocator)
{
- wmem_allocator_t *allocator;
wmem_simple_allocator_t *simple_allocator;
- allocator = g_slice_new(wmem_allocator_t);
simple_allocator = g_slice_new(wmem_simple_allocator_t);
- allocator->private_data = (void*) simple_allocator;
-
allocator->alloc = &wmem_simple_alloc;
allocator->realloc = &wmem_simple_realloc;
allocator->free = &wmem_simple_free;
allocator->free_all = &wmem_simple_free_all;
allocator->gc = &wmem_simple_gc;
- allocator->destroy = &wmem_simple_allocator_destroy;
+ allocator->cleanup = &wmem_simple_allocator_cleanup;
+
+ allocator->private_data = (void*) simple_allocator;
simple_allocator->block_table = g_hash_table_new_full(
&g_direct_hash, &g_direct_equal, NULL, &g_free);
-
- return allocator;
}
/*
diff --git a/epan/wmem/wmem_allocator_simple.h b/epan/wmem/wmem_allocator_simple.h
index 1493084704..1cc048810d 100644
--- a/epan/wmem/wmem_allocator_simple.h
+++ b/epan/wmem/wmem_allocator_simple.h
@@ -32,8 +32,8 @@
extern "C" {
#endif /* __cplusplus */
-wmem_allocator_t *
-wmem_simple_allocator_new(void);
+void
+wmem_simple_allocator_init(wmem_allocator_t *allocator);
#ifdef __cplusplus
}
diff --git a/epan/wmem/wmem_allocator_strict.c b/epan/wmem/wmem_allocator_strict.c
index 45ba002de4..c83c2f6b40 100644
--- a/epan/wmem/wmem_allocator_strict.c
+++ b/epan/wmem/wmem_allocator_strict.c
@@ -222,24 +222,21 @@ wmem_strict_gc(void *private_data _U_)
}
static void
-wmem_strict_allocator_destroy(wmem_allocator_t *allocator)
+wmem_strict_allocator_cleanup(void *private_data)
{
- wmem_strict_allocator_t *private_allocator;
+ wmem_strict_allocator_t *allocator;
- private_allocator = (wmem_strict_allocator_t*) allocator->private_data;
+ allocator = (wmem_strict_allocator_t*) private_data;
- g_hash_table_destroy(private_allocator->block_table);
- g_slice_free(wmem_strict_allocator_t, private_allocator);
- g_slice_free(wmem_allocator_t, allocator);
+ g_hash_table_destroy(allocator->block_table);
+ g_slice_free(wmem_strict_allocator_t, allocator);
}
-wmem_allocator_t *
-wmem_strict_allocator_new(void)
+void
+wmem_strict_allocator_init(wmem_allocator_t *allocator)
{
- wmem_allocator_t *allocator;
wmem_strict_allocator_t *strict_allocator;
- allocator = g_slice_new(wmem_allocator_t);
strict_allocator = g_slice_new(wmem_strict_allocator_t);
allocator->alloc = &wmem_strict_alloc;
@@ -248,15 +245,13 @@ wmem_strict_allocator_new(void)
allocator->free_all = &wmem_strict_free_all;
allocator->gc = &wmem_strict_gc;
- allocator->destroy = &wmem_strict_allocator_destroy;
+ allocator->cleanup = &wmem_strict_allocator_cleanup;
allocator->private_data = (void*) strict_allocator;
strict_allocator->block_table = g_hash_table_new_full(
&g_direct_hash, &g_direct_equal,
NULL, &wmem_strict_ghash_block_free);
-
- return allocator;
}
/*
diff --git a/epan/wmem/wmem_allocator_strict.h b/epan/wmem/wmem_allocator_strict.h
index c4eb1e4269..08d185da18 100644
--- a/epan/wmem/wmem_allocator_strict.h
+++ b/epan/wmem/wmem_allocator_strict.h
@@ -32,8 +32,8 @@
extern "C" {
#endif /* __cplusplus */
-wmem_allocator_t *
-wmem_strict_allocator_new(void);
+void
+wmem_strict_allocator_init(wmem_allocator_t *allocator);
void
wmem_strict_check_canaries(wmem_allocator_t *allocator);
diff --git a/epan/wmem/wmem_core.c b/epan/wmem/wmem_core.c
index f8b24557e4..7ffe0fedad 100644
--- a/epan/wmem/wmem_core.c
+++ b/epan/wmem/wmem_core.c
@@ -123,7 +123,8 @@ wmem_destroy_allocator(wmem_allocator_t *allocator)
{
wmem_free_all_real(allocator, TRUE);
- allocator->destroy(allocator);
+ allocator->cleanup(allocator->private_data);
+ g_slice_free(wmem_allocator_t, allocator);
}
wmem_allocator_t *
@@ -156,15 +157,19 @@ wmem_allocator_new(const wmem_allocator_type_t type)
real_type = type;
}
+ allocator = g_slice_new(wmem_allocator_t);
+ allocator->type = real_type;
+ allocator->callbacks = NULL;
+
switch (real_type) {
case WMEM_ALLOCATOR_SIMPLE:
- allocator = wmem_simple_allocator_new();
+ wmem_simple_allocator_init(allocator);
break;
case WMEM_ALLOCATOR_BLOCK:
- allocator = wmem_block_allocator_new();
+ wmem_block_allocator_init(allocator);
break;
case WMEM_ALLOCATOR_STRICT:
- allocator = wmem_strict_allocator_new();
+ wmem_strict_allocator_init(allocator);
break;
default:
g_assert_not_reached();
@@ -174,9 +179,6 @@ wmem_allocator_new(const wmem_allocator_type_t type)
return NULL;
};
- allocator->type = real_type;
- allocator->callbacks = NULL;
-
return allocator;
}
diff --git a/epan/wmem/wmem_test.c b/epan/wmem/wmem_test.c
index 6c703b8f03..64c4470146 100644
--- a/epan/wmem/wmem_test.c
+++ b/epan/wmem/wmem_test.c
@@ -46,15 +46,19 @@ wmem_allocator_force_new(const wmem_allocator_type_t type)
{
wmem_allocator_t *allocator;
+ allocator = g_slice_new(wmem_allocator_t);
+ allocator->type = type;
+ allocator->callbacks = NULL;
+
switch (type) {
case WMEM_ALLOCATOR_SIMPLE:
- allocator = wmem_simple_allocator_new();
+ wmem_simple_allocator_init(allocator);
break;
case WMEM_ALLOCATOR_BLOCK:
- allocator = wmem_block_allocator_new();
+ wmem_block_allocator_init(allocator);
break;
case WMEM_ALLOCATOR_STRICT:
- allocator = wmem_strict_allocator_new();
+ wmem_strict_allocator_init(allocator);
break;
default:
g_assert_not_reached();
@@ -64,9 +68,6 @@ wmem_allocator_force_new(const wmem_allocator_type_t type)
return NULL;
};
- allocator->type = type;
- allocator->callbacks = NULL;
-
return allocator;
}