diff options
author | Evan Huus <eapache@gmail.com> | 2012-12-19 00:43:36 +0000 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2012-12-19 00:43:36 +0000 |
commit | 5cf858ec52d1f3a05fcaa43ac0571d321244a87e (patch) | |
tree | d2a49636b7fd471647f1f2a0635be5ca45903c17 /epan/wmem | |
parent | 561525cbb538b030d28aa4951cf77c9f3f286954 (diff) |
Dispatch all allocator creations through a single function using an enum to
determine the desired type. This has two advantages over the old way:
- just one environment variable for valgrind to override in order to guarantee
that ALL allocators use memory it can track, and just one place to check that
variable
- allocator owners no longer have to include headers specific to their
allocator, allowing them to change allocators without adjusting all their
#includes
svn path=/trunk/; revision=46604
Diffstat (limited to 'epan/wmem')
-rw-r--r-- | epan/wmem/wmem_core.c | 25 | ||||
-rw-r--r-- | epan/wmem/wmem_core.h | 10 | ||||
-rw-r--r-- | epan/wmem/wmem_scopes.c | 17 |
3 files changed, 38 insertions, 14 deletions
diff --git a/epan/wmem/wmem_core.c b/epan/wmem/wmem_core.c index 82bde49ca1..9946c70a53 100644 --- a/epan/wmem/wmem_core.c +++ b/epan/wmem/wmem_core.c @@ -23,11 +23,15 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include <stdlib.h> #include <string.h> +#include <glib.h> #include "wmem_core.h" #include "wmem_scopes.h" #include "wmem_allocator.h" +#include "wmem_allocator_simple.h" +#include "wmem_allocator_block.h" void * wmem_alloc(wmem_allocator_t *allocator, const size_t size) @@ -58,6 +62,27 @@ wmem_destroy_allocator(wmem_allocator_t *allocator) allocator->destroy(allocator); } +wmem_allocator_t * +wmem_allocator_new(const wmem_allocator_type_t type) +{ + /* Our valgrind script uses this environment variable to override the + * usual allocator choice so that everything goes through system-level + * allocations that it understands and can track. Otherwise it will get + * confused by the block allocator etc. */ + if (getenv("WIRESHARK_DEBUG_WMEM_SIMPLE")) { + return wmem_simple_allocator_new(); + } + + switch (type) { + case WMEM_ALLOCATOR_SIMPLE: + return wmem_simple_allocator_new(); + case WMEM_ALLOCATOR_BLOCK: + return wmem_block_allocator_new(); + default: + g_assert_not_reached(); + }; +} + void wmem_init(void) { diff --git a/epan/wmem/wmem_core.h b/epan/wmem/wmem_core.h index 2b6e1ff3f1..a405b269ad 100644 --- a/epan/wmem/wmem_core.h +++ b/epan/wmem/wmem_core.h @@ -32,9 +32,14 @@ extern "C" { #endif /* __cplusplus */ +typedef enum _wmem_allocator_type_t { + WMEM_ALLOCATOR_SIMPLE, + WMEM_ALLOCATOR_BLOCK +} wmem_allocator_type_t; + struct _wmem_allocator_t; -typedef struct _wmem_allocator_t wmem_allocator_t; +typedef struct _wmem_allocator_t wmem_allocator_t; void * wmem_alloc(wmem_allocator_t *allocator, const size_t size); @@ -48,6 +53,9 @@ wmem_free_all(wmem_allocator_t *allocator); void wmem_destroy_allocator(wmem_allocator_t *allocator); +wmem_allocator_t * +wmem_allocator_new(const wmem_allocator_type_t type); + void wmem_init(void); diff --git a/epan/wmem/wmem_scopes.c b/epan/wmem/wmem_scopes.c index 9242659fa3..e7fcdff9a2 100644 --- a/epan/wmem/wmem_scopes.c +++ b/epan/wmem/wmem_scopes.c @@ -23,13 +23,10 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include <stdlib.h> - #include <glib.h> +#include "wmem_core.h" #include "wmem_scopes.h" -#include "wmem_allocator_block.h" -#include "wmem_allocator_simple.h" /* One of the supposed benefits of wmem over the old emem was going to be that * the scoping of the various memory pools would be obvious, since they would @@ -153,15 +150,9 @@ wmem_init_scopes(void) g_assert(in_packet_scope == FALSE); g_assert(in_file_scope == FALSE); - if (getenv("WIRESHARK_DEBUG_WMEM_PACKET_NO_CHUNKS")) { - packet_scope = wmem_simple_allocator_new(); - } - else { - packet_scope = wmem_block_allocator_new(); - } - - file_scope = wmem_simple_allocator_new(); - epan_scope = wmem_simple_allocator_new(); + packet_scope = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK); + file_scope = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE); + epan_scope = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE); } void |