2 * Wireshark Memory Manager Scopes
3 * Copyright 2012, Evan Huus <eapache@gmail.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "wmem_core.h"
27 #include "wmem_scopes.h"
28 #include "wmem_allocator.h"
30 /* One of the supposed benefits of wmem over the old emem was going to be that
31 * the scoping of the various memory pools would be obvious, since they would
32 * no longer be global. Instead, the pools would be managed as variables scoped
33 * by the compiler, so functions outside of that scope wouldn't have access to
34 * the pools and wouldn't be able to allocate memory in a scope to which they
37 * That idea fell apart rather quickly :P
39 * The principle still stands, and most pools should be managed in that way.
40 * The three in this file are *exceptions*. They are the three scopes that emem
41 * provided as globals. Converting all of the code that used them to pass an
42 * extra parameter (or three) around would have been a nightmare of epic
43 * proportions, so we provide these three as globals still.
45 * We do, however, use some extra booleans and a mountain of assertions to try
46 * and catch anybody accessing the pools out of the correct scope. It's not
47 * perfect, but it should stop most of the bad behaviour that emem permitted.
50 /* TODO: Make these thread-local */
51 static wmem_allocator_t *packet_scope = NULL;
52 static wmem_allocator_t *file_scope = NULL;
53 static wmem_allocator_t *epan_scope = NULL;
58 wmem_packet_scope(void)
60 g_assert(packet_scope);
66 wmem_enter_packet_scope(void)
68 g_assert(packet_scope);
69 g_assert(file_scope->in_scope);
70 g_assert(!packet_scope->in_scope);
72 packet_scope->in_scope = TRUE;
76 wmem_leave_packet_scope(void)
78 g_assert(packet_scope);
79 g_assert(packet_scope->in_scope);
81 wmem_free_all(packet_scope);
82 packet_scope->in_scope = FALSE;
96 wmem_enter_file_scope(void)
99 g_assert(!file_scope->in_scope);
101 file_scope->in_scope = TRUE;
105 wmem_leave_file_scope(void)
107 g_assert(file_scope);
108 g_assert(file_scope->in_scope);
109 g_assert(!packet_scope->in_scope);
111 wmem_free_all(file_scope);
112 file_scope->in_scope = FALSE;
114 /* this seems like a good time to do garbage collection */
116 wmem_gc(packet_scope);
122 wmem_epan_scope(void)
124 g_assert(epan_scope);
129 /* Scope Management */
132 wmem_init_scopes(void)
134 g_assert(packet_scope == NULL);
135 g_assert(file_scope == NULL);
136 g_assert(epan_scope == NULL);
138 packet_scope = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK_FAST);
139 file_scope = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK);
140 epan_scope = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE);
142 /* Scopes are initialized to TRUE by default on creation */
143 packet_scope->in_scope = FALSE;
144 file_scope->in_scope = FALSE;
148 wmem_cleanup_scopes(void)
150 g_assert(packet_scope);
151 g_assert(file_scope);
152 g_assert(epan_scope);
154 g_assert(packet_scope->in_scope == FALSE);
155 g_assert(file_scope->in_scope == FALSE);
157 wmem_destroy_allocator(packet_scope);
158 wmem_destroy_allocator(file_scope);
159 wmem_destroy_allocator(epan_scope);
167 * Editor modelines - http://www.wireshark.org/tools/modelines.html
172 * indent-tabs-mode: nil
175 * vi: set shiftwidth=4 tabstop=8 expandtab:
176 * :indentSize=4:tabSize=8:noTabs=true: