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 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include "wmem_scopes.h"
16 #include <wsutil/ws_assert.h>
18 /* One of the supposed benefits of wmem over the old emem was going to be that
19 * the scoping of the various memory pools would be obvious, since they would
20 * no longer be global. Instead, the pools would be managed as variables scoped
21 * by the compiler, so functions outside of that scope wouldn't have access to
22 * the pools and wouldn't be able to allocate memory in a scope to which they
25 * That idea fell apart rather quickly :P
27 * The principle still stands, and most pools should be managed in that way.
28 * The three in this file are *exceptions*. They are the three scopes that emem
29 * provided as globals. Converting all of the code that used them to pass an
30 * extra parameter (or three) around would have been a nightmare of epic
31 * proportions, so we provide these three as globals still.
33 * We do, however, use some extra booleans and a mountain of assertions to try
34 * and catch anybody accessing the pools out of the correct scope. It's not
35 * perfect, but it should stop most of the bad behaviour that emem permitted.
38 /* TODO: Make these thread-local */
39 static wmem_allocator_t
*packet_scope
;
40 static wmem_allocator_t
*file_scope
;
41 static wmem_allocator_t
*epan_scope
;
46 wmem_packet_scope(void)
48 ws_assert(packet_scope
);
54 wmem_enter_packet_scope(void)
56 ws_assert(packet_scope
);
57 ws_assert(wmem_in_scope(file_scope
));
58 ws_assert(!wmem_in_scope(packet_scope
));
60 wmem_enter_scope(packet_scope
);
64 wmem_leave_packet_scope(void)
66 ws_assert(packet_scope
);
67 ws_assert(wmem_in_scope(packet_scope
));
69 wmem_leave_scope(packet_scope
);
77 ws_assert(file_scope
);
83 wmem_enter_file_scope(void)
85 ws_assert(file_scope
);
86 ws_assert(!wmem_in_scope(file_scope
));
88 wmem_enter_scope(file_scope
);
92 wmem_leave_file_scope(void)
94 ws_assert(file_scope
);
95 ws_assert(wmem_in_scope(file_scope
));
96 ws_assert(!wmem_in_scope(packet_scope
));
98 wmem_leave_scope(file_scope
);
100 /* this seems like a good time to do garbage collection */
102 wmem_gc(packet_scope
);
108 wmem_epan_scope(void)
110 ws_assert(epan_scope
);
115 /* Scope Management */
118 wmem_init_scopes(void)
120 ws_assert(packet_scope
== NULL
);
121 ws_assert(file_scope
== NULL
);
122 ws_assert(epan_scope
== NULL
);
126 packet_scope
= wmem_allocator_new(WMEM_ALLOCATOR_BLOCK_FAST
);
127 file_scope
= wmem_allocator_new(WMEM_ALLOCATOR_BLOCK
);
128 epan_scope
= wmem_allocator_new(WMEM_ALLOCATOR_BLOCK
);
130 /* Scopes are initialized to true by default on creation */
131 wmem_leave_scope(packet_scope
);
132 wmem_leave_scope(file_scope
);
136 wmem_cleanup_scopes(void)
138 ws_assert(packet_scope
);
139 ws_assert(file_scope
);
140 ws_assert(epan_scope
);
142 ws_assert(!wmem_in_scope(packet_scope
));
143 ws_assert(!wmem_in_scope(file_scope
));
145 wmem_destroy_allocator(packet_scope
);
146 wmem_destroy_allocator(file_scope
);
147 wmem_destroy_allocator(epan_scope
);
157 * Editor modelines - https://www.wireshark.org/tools/modelines.html
162 * indent-tabs-mode: nil
165 * vi: set shiftwidth=4 tabstop=8 expandtab:
166 * :indentSize=4:tabSize=8:noTabs=true: