diff options
author | Evan Huus <eapache@gmail.com> | 2012-11-03 15:49:15 +0000 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2012-11-03 15:49:15 +0000 |
commit | 2737b7b7d315b1bef98159fec17808d82bdcd5cd (patch) | |
tree | b95cd71f03c5133af81f9adee692540f3d769ee7 /epan | |
parent | b1173839618743eff4028ef6d4c1a634a2b54335 (diff) | |
download | wireshark-2737b7b7d315b1bef98159fec17808d82bdcd5cd.tar.gz |
Add wmem scopes for packet and file lifetimes. The file lifetime scope isn't
yet initialized because I can't figure out where the enter() and leave() calls
should go - the obvious place in packet.c causes a lot of assertion errors.
svn path=/trunk/; revision=45879
Diffstat (limited to 'epan')
-rw-r--r-- | epan/CMakeLists.txt | 1 | ||||
-rw-r--r-- | epan/epan.c | 4 | ||||
-rw-r--r-- | epan/wmem/Makefile.common | 2 | ||||
-rw-r--r-- | epan/wmem/wmem.h | 1 | ||||
-rw-r--r-- | epan/wmem/wmem_core.c | 13 | ||||
-rw-r--r-- | epan/wmem/wmem_core.h | 3 | ||||
-rw-r--r-- | epan/wmem/wmem_scopes.c | 176 | ||||
-rw-r--r-- | epan/wmem/wmem_scopes.h | 87 |
8 files changed, 274 insertions, 13 deletions
diff --git a/epan/CMakeLists.txt b/epan/CMakeLists.txt index 197923ebd7..19d7ab64f2 100644 --- a/epan/CMakeLists.txt +++ b/epan/CMakeLists.txt @@ -1345,6 +1345,7 @@ set(FTYPE_FILES set(WMEM_FILES wmem/wmem_core.c wmem/wmem_allocator_glib.c + wmem/wmem_scopes.c wmem/wmem_slab.c wmem/wmem_strutl.c ) diff --git a/epan/epan.c b/epan/epan.c index 6e64eece2b..bf1a455b27 100644 --- a/epan/epan.c +++ b/epan/epan.c @@ -195,22 +195,26 @@ void epan_dissect_run(epan_dissect_t *edt, struct wtap_pkthdr *phdr, const guint8* data, frame_data *fd, column_info *cinfo) { + wmem_enter_packet_scope(); dissect_packet(edt, phdr, data, fd, cinfo); /* free all memory allocated */ ep_free_all(); + wmem_leave_packet_scope(); } void epan_dissect_run_with_taps(epan_dissect_t *edt, struct wtap_pkthdr *phdr, const guint8* data, frame_data *fd, column_info *cinfo) { + wmem_enter_packet_scope(); tap_queue_init(edt); dissect_packet(edt, phdr, data, fd, cinfo); tap_push_tapped_queue(edt); /* free all memory allocated */ ep_free_all(); + wmem_leave_packet_scope(); } void diff --git a/epan/wmem/Makefile.common b/epan/wmem/Makefile.common index c9f7790dbb..f312dec4c8 100644 --- a/epan/wmem/Makefile.common +++ b/epan/wmem/Makefile.common @@ -26,6 +26,7 @@ LIBWMEM_SRC = \ wmem_core.c \ wmem_allocator_glib.c \ + wmem_scopes.c \ wmem_slab.c \ wmem_strutl.c @@ -34,6 +35,7 @@ LIBWMEM_INCLUDES = \ wmem_core.h \ wmem_allocator.h \ wmem_allocator_glib.h \ + wmem_scopes.h \ wmem_slab.h \ wmem_strutl.h diff --git a/epan/wmem/wmem.h b/epan/wmem/wmem.h index c6c906e850..54e6374d2e 100644 --- a/epan/wmem/wmem.h +++ b/epan/wmem/wmem.h @@ -27,6 +27,7 @@ #define __WMEM_H__ #include "wmem_core.h" +#include "wmem_scopes.h" #include "wmem_strutl.h" #endif /* __WMEM_H__ */ diff --git a/epan/wmem/wmem_core.c b/epan/wmem/wmem_core.c index 4e796a6697..f7ad1aed86 100644 --- a/epan/wmem/wmem_core.c +++ b/epan/wmem/wmem_core.c @@ -26,11 +26,10 @@ #include <string.h> #include "wmem_core.h" +#include "wmem_scopes.h" #include "wmem_allocator.h" #include "wmem_allocator_glib.h" -static wmem_allocator_t *epan_scope; - void * wmem_alloc(wmem_allocator_t *allocator, size_t size) { @@ -60,22 +59,16 @@ wmem_destroy_allocator(wmem_allocator_t *allocator) allocator->destroy(allocator); } -wmem_allocator_t * -wmem_epan_scope(void) -{ - return epan_scope; -} - void wmem_init(void) { - epan_scope = wmem_create_glib_allocator(); + wmem_init_scopes(); } void wmem_cleanup(void) { - wmem_destroy_allocator(epan_scope); + wmem_cleanup_scopes(); } /* diff --git a/epan/wmem/wmem_core.h b/epan/wmem/wmem_core.h index fdb2d82fce..7937a8bda3 100644 --- a/epan/wmem/wmem_core.h +++ b/epan/wmem/wmem_core.h @@ -48,9 +48,6 @@ wmem_free_all(wmem_allocator_t *allocator); void wmem_destroy_allocator(wmem_allocator_t *allocator); -wmem_allocator_t * -wmem_epan_scope(void); - void wmem_init(void); diff --git a/epan/wmem/wmem_scopes.c b/epan/wmem/wmem_scopes.c new file mode 100644 index 0000000000..8d0e1a2268 --- /dev/null +++ b/epan/wmem/wmem_scopes.c @@ -0,0 +1,176 @@ +/* wmem_scopes.c + * Wireshark Memory Manager Scopes + * Copyright 2012, Evan Huus <eapache@gmail.com> + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <glib.h> + +#include "wmem_scopes.h" +#include "wmem_allocator_glib.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 + * no longer be global. Instead, the pools would be managed as variables scoped + * by the compiler, so functions outside of that scope wouldn't have access to + * the pools and wouldn't be able to allocate memory in a scope to which they + * didn't belong. + * + * That idea fell apart rather quickly :P + * + * The principle still stands, and most pools should be managed in that way. + * The three in this file are *exceptions*. They are the three scopes that emem + * provided as globals. Converting all of the code that used them to pass an + * extra parameter (or three) around would have been a nightmare of epic + * proportions, so we provide these three as globals still. + * + * We do, however, use some extra booleans and a mountain of assertions to try + * and catch anybody accessing the pools out of the correct scope. It's not + * perfect, but it should stop most of the bad behaviour that emem permitted. + */ + +/* TODO: Make these thread-local */ +static wmem_allocator_t *epan_scope = NULL; +static wmem_allocator_t *packet_scope = NULL; +static wmem_allocator_t *file_scope = NULL; + +static gboolean in_packet_scope = FALSE; +static gboolean in_file_scope = FALSE; + +/* Epan Scope */ + +wmem_allocator_t * +wmem_epan_scope(void) +{ + g_assert(epan_scope); + + return epan_scope; +} + +/* Packet Scope */ + +wmem_allocator_t * +wmem_packet_scope(void) +{ + g_assert(packet_scope); + g_assert(in_packet_scope); + + return epan_scope; +} + +void +wmem_enter_packet_scope(void) +{ + g_assert(packet_scope); + g_assert(!in_packet_scope); + + in_packet_scope = TRUE; +} + +void +wmem_leave_packet_scope(void) +{ + g_assert(packet_scope); + g_assert(in_packet_scope); + + wmem_free_all(packet_scope); + in_packet_scope = FALSE; +} + +/* File Scope */ + +wmem_allocator_t * +wmem_file_scope(void) +{ + g_assert(file_scope); + g_assert(in_file_scope); + + return epan_scope; +} + +void +wmem_enter_file_scope(void) +{ + g_assert(file_scope); + g_assert(!in_file_scope); + + in_file_scope = TRUE; +} + +void +wmem_leave_file_scope(void) +{ + g_assert(file_scope); + g_assert(in_file_scope); + + wmem_free_all(file_scope); + in_file_scope = FALSE; +} + +/* Scope Management */ + +void +wmem_init_scopes(void) +{ + g_assert(epan_scope == NULL); + g_assert(packet_scope == NULL); + g_assert(file_scope == NULL); + + g_assert(in_packet_scope == FALSE); + g_assert(in_file_scope == FALSE); + + epan_scope = wmem_create_glib_allocator(); + packet_scope = wmem_create_glib_allocator(); + file_scope = wmem_create_glib_allocator(); +} + +void +wmem_cleanup_scopes(void) +{ + g_assert(epan_scope); + g_assert(packet_scope); + g_assert(file_scope); + + g_assert(in_packet_scope == FALSE); + g_assert(in_file_scope == FALSE); + + wmem_destroy_allocator(epan_scope); + wmem_destroy_allocator(packet_scope); + wmem_destroy_allocator(file_scope); + + epan_scope = NULL; + packet_scope = NULL; + file_scope = NULL; +} + +/* + * Editor modelines - http://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * vi: set shiftwidth=4 tabstop=8 expandtab: + * :indentSize=4:tabSize=8:noTabs=true: + */ diff --git a/epan/wmem/wmem_scopes.h b/epan/wmem/wmem_scopes.h new file mode 100644 index 0000000000..06b6560c74 --- /dev/null +++ b/epan/wmem/wmem_scopes.h @@ -0,0 +1,87 @@ +/* wmem_scopes.h + * Definitions for the Wireshark Memory Manager Scopes + * Copyright 2012, Evan Huus <eapache@gmail.com> + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __WMEM_SCOPES_H__ +#define __WMEM_SCOPES_H__ + +#include "wmem_core.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* Epan Scope */ + +wmem_allocator_t * +wmem_epan_scope(void); + +/* Packet Scope */ + +wmem_allocator_t * +wmem_packet_scope(void); + +void +wmem_enter_packet_scope(void); + +void +wmem_leave_packet_scope(void); + +/* File Scope */ + +wmem_allocator_t * +wmem_file_scope(void); + +void +wmem_enter_file_scope(void); + +void +wmem_leave_file_scope(void); + +/* Scope Management */ + +void +wmem_init_scopes(void); + +void +wmem_cleanup_scopes(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __WMEM_SCOPES_H__ */ + +/* + * Editor modelines - http://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * vi: set shiftwidth=4 tabstop=8 expandtab: + * :indentSize=4:tabSize=8:noTabs=true: + */ |