decrypt drsuapi attributes
[wireshark-sm.git] / epan / wmem_scopes.c
blob184d21657c938d42869d240d92c3e0af6360ac44
1 /* wmem_scopes.c
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
12 #include <glib.h>
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
23 * didn't belong.
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;
43 /* Packet Scope */
45 wmem_allocator_t *
46 wmem_packet_scope(void)
48 ws_assert(packet_scope);
50 return packet_scope;
53 void
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);
63 void
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);
72 /* File Scope */
74 wmem_allocator_t *
75 wmem_file_scope(void)
77 ws_assert(file_scope);
79 return file_scope;
82 void
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);
91 void
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 */
101 wmem_gc(file_scope);
102 wmem_gc(packet_scope);
105 /* Epan Scope */
107 wmem_allocator_t *
108 wmem_epan_scope(void)
110 ws_assert(epan_scope);
112 return epan_scope;
115 /* Scope Management */
117 void
118 wmem_init_scopes(void)
120 ws_assert(packet_scope == NULL);
121 ws_assert(file_scope == NULL);
122 ws_assert(epan_scope == NULL);
124 wmem_init();
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);
135 void
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);
149 wmem_cleanup();
151 packet_scope = NULL;
152 file_scope = NULL;
153 epan_scope = NULL;
157 * Editor modelines - https://www.wireshark.org/tools/modelines.html
159 * Local variables:
160 * c-basic-offset: 4
161 * tab-width: 8
162 * indent-tabs-mode: nil
163 * End:
165 * vi: set shiftwidth=4 tabstop=8 expandtab:
166 * :indentSize=4:tabSize=8:noTabs=true: