Rework wmem scoping limits so that users can still get a handle to a pool when

it is out of scope, they just can't *allocate* in the pool. This is necessary
because file-scope trees (migrating from emem) are set up on program
initialization when there is no file in scope - they need to initialize with the
handle, they just won't use it until a file is actually in scope.

svn path=/trunk/; revision=50046
This commit is contained in:
Evan Huus 2013-06-19 18:28:13 +00:00
parent 7670683e3b
commit e13d73e038
5 changed files with 35 additions and 31 deletions

View File

@ -846,9 +846,9 @@ emem_alloc(size_t size, emem_pool_t *mem)
#if 0
/* For testing wmem, effectively redirects most emem memory to wmem.
* You will also have to comment out several assertions in
* wmem_packet_scope() and wmem_file_scope() since they are much
* stricter about when they are permitted to be called. */
* You will also have to comment out several assertions in wmem_core.c,
* specifically anything g_assert(allocator->in_scope), since it is much
* stricter about when it is permitted to be called. */
if (mem == &ep_packet_mem) {
return wmem_alloc(wmem_packet_scope(), size);
}

View File

@ -55,6 +55,7 @@ struct _wmem_allocator_t {
/* Implementation details */
void *private_data;
enum _wmem_allocator_type_t type;
gboolean in_scope;
};
#ifdef __cplusplus

View File

@ -42,6 +42,8 @@ wmem_alloc(wmem_allocator_t *allocator, const size_t size)
return g_malloc(size);
}
g_assert(allocator->in_scope);
if (size == 0) {
return NULL;
}
@ -54,13 +56,11 @@ wmem_alloc0(wmem_allocator_t *allocator, const size_t size)
{
void *buf;
if (size == 0) {
return NULL;
}
buf = wmem_alloc(allocator, size);
memset(buf, 0, size);
if (buf) {
memset(buf, 0, size);
}
return buf;
}
@ -73,6 +73,8 @@ wmem_free(wmem_allocator_t *allocator, void *ptr)
return;
}
g_assert(allocator->in_scope);
if (ptr == NULL) {
return;
}
@ -96,6 +98,8 @@ wmem_realloc(wmem_allocator_t *allocator, void *ptr, const size_t size)
return NULL;
}
g_assert(allocator->in_scope);
return allocator->realloc(allocator->private_data, ptr, size);
}
@ -159,8 +163,9 @@ wmem_allocator_new(const wmem_allocator_type_t type)
}
allocator = g_slice_new(wmem_allocator_t);
allocator->type = real_type;
allocator->type = real_type;
allocator->callbacks = NULL;
allocator->in_scope = TRUE;
switch (real_type) {
case WMEM_ALLOCATOR_SIMPLE:

View File

@ -27,6 +27,7 @@
#include "wmem_core.h"
#include "wmem_scopes.h"
#include "wmem_allocator.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
@ -53,16 +54,12 @@ static wmem_allocator_t *packet_scope = NULL;
static wmem_allocator_t *file_scope = NULL;
static wmem_allocator_t *epan_scope = NULL;
static gboolean in_packet_scope = FALSE;
static gboolean in_file_scope = FALSE;
/* Packet Scope */
wmem_allocator_t *
wmem_packet_scope(void)
{
g_assert(packet_scope);
g_assert(in_packet_scope);
return packet_scope;
}
@ -71,20 +68,20 @@ void
wmem_enter_packet_scope(void)
{
g_assert(packet_scope);
g_assert(in_file_scope);
g_assert(!in_packet_scope);
g_assert(file_scope->in_scope);
g_assert(!packet_scope->in_scope);
in_packet_scope = TRUE;
packet_scope->in_scope = TRUE;
}
void
wmem_leave_packet_scope(void)
{
g_assert(packet_scope);
g_assert(in_packet_scope);
g_assert(packet_scope->in_scope);
wmem_free_all(packet_scope);
in_packet_scope = FALSE;
packet_scope->in_scope = FALSE;
}
/* File Scope */
@ -93,7 +90,6 @@ wmem_allocator_t *
wmem_file_scope(void)
{
g_assert(file_scope);
g_assert(in_file_scope);
return file_scope;
}
@ -102,9 +98,9 @@ void
wmem_enter_file_scope(void)
{
g_assert(file_scope);
g_assert(!in_file_scope);
g_assert(!file_scope->in_scope);
in_file_scope = TRUE;
file_scope->in_scope = TRUE;
}
void
@ -112,20 +108,20 @@ wmem_leave_file_scope(void)
{
/* XXX: cleanup_dissection (the current caller of this function) is
* itself sometimes called when no file exists to be cleaned up. It's not
* a huge problem really, but it means that we can't assert in_file_scope
* here because it's not always true.
* a huge problem really, but it means that we can't assert
* file_scope->in_scope here because it's not always true.
*
* At some point the code should be fixed so that cleanup_dissection is
* only ever called when it's really needed.
*
* g_assert(in_file_scope);
* g_assert(file_scope->in_scope);
*/
g_assert(file_scope);
g_assert(!in_packet_scope);
g_assert(!packet_scope->in_scope);
wmem_free_all(file_scope);
in_file_scope = FALSE;
file_scope->in_scope = FALSE;
/* this seems like a good time to do garbage collection */
wmem_gc(file_scope);
@ -151,12 +147,13 @@ wmem_init_scopes(void)
g_assert(file_scope == NULL);
g_assert(epan_scope == NULL);
g_assert(in_packet_scope == FALSE);
g_assert(in_file_scope == FALSE);
packet_scope = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK);
file_scope = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK);
epan_scope = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE);
/* Scopes are initialized to TRUE by default on creation */
packet_scope->in_scope = FALSE;
file_scope->in_scope = FALSE;
}
void
@ -166,8 +163,8 @@ wmem_cleanup_scopes(void)
g_assert(file_scope);
g_assert(epan_scope);
g_assert(in_packet_scope == FALSE);
g_assert(in_file_scope == FALSE);
g_assert(packet_scope->in_scope == FALSE);
g_assert(file_scope->in_scope == FALSE);
wmem_destroy_allocator(packet_scope);
wmem_destroy_allocator(file_scope);

View File

@ -49,6 +49,7 @@ wmem_allocator_force_new(const wmem_allocator_type_t type)
allocator = g_slice_new(wmem_allocator_t);
allocator->type = type;
allocator->callbacks = NULL;
allocator->in_scope = TRUE;
switch (type) {
case WMEM_ALLOCATOR_SIMPLE: