As suggested separately by both Jakub and Anders: only read the

WIRESHARK_DEBUG_WMEM_OVERRIDE environment variable once in wmem_init, not every
time wmem_allocator_new is called. We currently create a new pinfo pool for
every packet we dissect, so this is a small performance win, especially when
getenv is slow (which may happen if a large number of environment variables are
set, such as when fuzz-testing).

svn path=/trunk/; revision=52634
This commit is contained in:
Evan Huus 2013-10-15 23:16:35 +00:00
parent c4dcd0ca24
commit 7b3dfaa525
2 changed files with 39 additions and 55 deletions

View File

@ -35,6 +35,11 @@
#include "wmem_allocator_block.h"
#include "wmem_allocator_strict.h"
/* Set according to the WIRESHARK_DEBUG_WMEM_OVERRIDE environment variable in
* wmem_init. Should not be set again. */
static gboolean do_override = FALSE;
static wmem_allocator_type_t override_type;
void *
wmem_alloc(wmem_allocator_t *allocator, const size_t size)
{
@ -135,30 +140,13 @@ wmem_destroy_allocator(wmem_allocator_t *allocator)
wmem_allocator_t *
wmem_allocator_new(const wmem_allocator_type_t type)
{
const char *override;
wmem_allocator_t *allocator;
wmem_allocator_type_t real_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. */
override = getenv("WIRESHARK_DEBUG_WMEM_OVERRIDE");
if (override == NULL) {
real_type = type;
}
else if (strncmp(override, "simple", strlen("simple")) == 0) {
real_type = WMEM_ALLOCATOR_SIMPLE;
}
else if (strncmp(override, "block", strlen("block")) == 0) {
real_type = WMEM_ALLOCATOR_BLOCK;
}
else if (strncmp(override, "strict", strlen("strict")) == 0) {
real_type = WMEM_ALLOCATOR_STRICT;
if (do_override) {
real_type = override_type;
}
else {
g_warning("Unrecognized wmem override");
real_type = type;
}
@ -191,6 +179,34 @@ wmem_allocator_new(const wmem_allocator_type_t type)
void
wmem_init(void)
{
const char *override_env;
/* 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. */
override_env = getenv("WIRESHARK_DEBUG_WMEM_OVERRIDE");
if (override_env == NULL) {
do_override = FALSE;
}
else {
do_override = TRUE;
if (strncmp(override_env, "simple", strlen("simple")) == 0) {
override_type = WMEM_ALLOCATOR_SIMPLE;
}
else if (strncmp(override_env, "block", strlen("block")) == 0) {
override_type = WMEM_ALLOCATOR_BLOCK;
}
else if (strncmp(override_env, "strict", strlen("strict")) == 0) {
override_type = WMEM_ALLOCATOR_STRICT;
}
else {
g_warning("Unrecognized wmem override");
do_override = FALSE;
}
}
wmem_init_scopes();
}

View File

@ -40,40 +40,6 @@
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;
allocator = wmem_new(NULL, wmem_allocator_t);
allocator->type = type;
allocator->callbacks = NULL;
allocator->in_scope = TRUE;
switch (type) {
case WMEM_ALLOCATOR_SIMPLE:
wmem_simple_allocator_init(allocator);
break;
case WMEM_ALLOCATOR_BLOCK:
wmem_block_allocator_init(allocator);
break;
case WMEM_ALLOCATOR_STRICT:
wmem_strict_allocator_init(allocator);
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;
};
return allocator;
}
/* A helper for generating pseudo-random strings. Just uses glib's random number
* functions to generate 'numbers' in the printable character range. */
static gchar *
@ -236,7 +202,9 @@ wmem_test_allocator(wmem_allocator_type_t type, wmem_verify_func verify)
char *ptrs[MAX_SIMULTANEOUS_ALLOCS];
wmem_allocator_t *allocator;
allocator = wmem_allocator_force_new(type);
/* We never run wmem_init so we don't have to worry about overrides giving
* us the wrong type. */
allocator = wmem_allocator_new(type);
if (verify) (*verify)(allocator);
@ -337,7 +305,7 @@ wmem_time_allocator(wmem_allocator_type_t type)
int i, j;
wmem_allocator_t *allocator;
allocator = wmem_allocator_force_new(type);
allocator = wmem_allocator_new(type);
for (j=0; j<128; j++) {
for (i=0; i<MAX_SIMULTANEOUS_ALLOCS; i++) {