2 * Wireshark Memory Manager Core
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
17 #include "wmem_core.h"
18 #include "wmem_map_int.h"
19 #include "wmem_user_cb_int.h"
20 #include "wmem_allocator.h"
21 #include "wmem_allocator_simple.h"
22 #include "wmem_allocator_block.h"
23 #include "wmem_allocator_block_fast.h"
24 #include "wmem_allocator_strict.h"
26 /* Set according to the WIRESHARK_DEBUG_WMEM_OVERRIDE environment variable in
27 * wmem_init. Should not be set again. */
28 static bool do_override
;
29 static wmem_allocator_type_t override_type
;
32 wmem_alloc(wmem_allocator_t
*allocator
, const size_t size
)
34 if (allocator
== NULL
) {
35 return g_malloc(size
);
38 ws_assert(allocator
->in_scope
);
44 return allocator
->walloc(allocator
->private_data
, size
);
48 wmem_alloc0(wmem_allocator_t
*allocator
, const size_t size
)
52 buf
= wmem_alloc(allocator
, size
);
62 wmem_free(wmem_allocator_t
*allocator
, void *ptr
)
64 if (allocator
== NULL
) {
69 ws_assert(allocator
->in_scope
);
75 allocator
->wfree(allocator
->private_data
, ptr
);
79 wmem_realloc(wmem_allocator_t
*allocator
, void *ptr
, const size_t size
)
81 if (allocator
== NULL
) {
82 return g_realloc(ptr
, size
);
86 return wmem_alloc(allocator
, size
);
90 wmem_free(allocator
, ptr
);
94 ws_assert(allocator
->in_scope
);
96 return allocator
->wrealloc(allocator
->private_data
, ptr
, size
);
100 wmem_free_all_real(wmem_allocator_t
*allocator
, bool final
)
102 wmem_call_callbacks(allocator
,
103 final
? WMEM_CB_DESTROY_EVENT
: WMEM_CB_FREE_EVENT
);
104 allocator
->free_all(allocator
->private_data
);
108 wmem_free_all(wmem_allocator_t
*allocator
)
110 wmem_free_all_real(allocator
, false);
114 wmem_gc(wmem_allocator_t
*allocator
)
116 allocator
->gc(allocator
->private_data
);
120 wmem_destroy_allocator(wmem_allocator_t
*allocator
)
123 wmem_free_all_real(allocator
, true);
124 allocator
->cleanup(allocator
->private_data
);
125 wmem_free(NULL
, allocator
);
129 wmem_allocator_new(const wmem_allocator_type_t type
)
131 wmem_allocator_t
*allocator
;
132 wmem_allocator_type_t real_type
;
135 real_type
= override_type
;
141 allocator
= wmem_new(NULL
, wmem_allocator_t
);
142 allocator
->type
= real_type
;
143 allocator
->callbacks
= NULL
;
144 allocator
->in_scope
= true;
147 case WMEM_ALLOCATOR_SIMPLE
:
148 wmem_simple_allocator_init(allocator
);
150 case WMEM_ALLOCATOR_BLOCK
:
151 wmem_block_allocator_init(allocator
);
153 case WMEM_ALLOCATOR_BLOCK_FAST
:
154 wmem_block_fast_allocator_init(allocator
);
156 case WMEM_ALLOCATOR_STRICT
:
157 wmem_strict_allocator_init(allocator
);
160 g_assert_not_reached();
170 const char *override_env
;
172 /* Our valgrind script uses this environment variable to override the
173 * usual allocator choice so that everything goes through system-level
174 * allocations that it understands and can track. Otherwise it will get
175 * confused by the block allocator etc. */
176 override_env
= getenv("WIRESHARK_DEBUG_WMEM_OVERRIDE");
178 if (override_env
== NULL
) {
183 if (strncmp(override_env
, "simple", strlen("simple")) == 0) {
184 override_type
= WMEM_ALLOCATOR_SIMPLE
;
186 else if (strncmp(override_env
, "block", strlen("block")) == 0) {
187 override_type
= WMEM_ALLOCATOR_BLOCK
;
189 else if (strncmp(override_env
, "strict", strlen("strict")) == 0) {
190 override_type
= WMEM_ALLOCATOR_STRICT
;
192 else if (strncmp(override_env
, "block_fast", strlen("block_fast")) == 0) {
193 override_type
= WMEM_ALLOCATOR_BLOCK_FAST
;
196 g_warning("Unrecognized wmem override");
210 wmem_enter_scope(wmem_allocator_t
*allocator
)
212 allocator
->in_scope
= true;
216 wmem_leave_scope(wmem_allocator_t
*allocator
)
218 wmem_free_all(allocator
);
219 allocator
->in_scope
= false;
223 wmem_in_scope(wmem_allocator_t
*allocator
)
225 return allocator
->in_scope
;
230 * Editor modelines - https://www.wireshark.org/tools/modelines.html
235 * indent-tabs-mode: nil
238 * vi: set shiftwidth=4 tabstop=8 expandtab:
239 * :indentSize=4:tabSize=8:noTabs=true: