summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--epan/emem.c6
-rw-r--r--epan/wmem/wmem_allocator.h1
-rw-r--r--epan/wmem/wmem_core.c17
-rw-r--r--epan/wmem/wmem_scopes.c41
-rw-r--r--epan/wmem/wmem_test.c1
5 files changed, 35 insertions, 31 deletions
diff --git a/epan/emem.c b/epan/emem.c
index 1882b26a76..e188372964 100644
--- a/epan/emem.c
+++ b/epan/emem.c
@@ -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);
}
diff --git a/epan/wmem/wmem_allocator.h b/epan/wmem/wmem_allocator.h
index d76c8770a3..68ad9c67f2 100644
--- a/epan/wmem/wmem_allocator.h
+++ b/epan/wmem/wmem_allocator.h
@@ -55,6 +55,7 @@ struct _wmem_allocator_t {
/* Implementation details */
void *private_data;
enum _wmem_allocator_type_t type;
+ gboolean in_scope;
};
#ifdef __cplusplus
diff --git a/epan/wmem/wmem_core.c b/epan/wmem/wmem_core.c
index baaee5530e..ed5f73c4cc 100644
--- a/epan/wmem/wmem_core.c
+++ b/epan/wmem/wmem_core.c
@@ -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:
diff --git a/epan/wmem/wmem_scopes.c b/epan/wmem/wmem_scopes.c
index 26a360ea16..95f9a3b1b5 100644
--- a/epan/wmem/wmem_scopes.c
+++ b/epan/wmem/wmem_scopes.c
@@ -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);
diff --git a/epan/wmem/wmem_test.c b/epan/wmem/wmem_test.c
index 04d547e260..ff91ee577f 100644
--- a/epan/wmem/wmem_test.c
+++ b/epan/wmem/wmem_test.c
@@ -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: