Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
wmem_core.h
Go to the documentation of this file.
1
12#ifndef __WMEM_CORE_H__
13#define __WMEM_CORE_H__
14
15#include <stdint.h>
16#include <stdbool.h>
17#include <string.h>
18#include <glib.h>
19#include <ws_symbol_export.h>
20#include <ws_attributes.h>
21#include <ws_posix_compat.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif /* __cplusplus */
26
45
66
74WS_DLL_PUBLIC
75void *
76wmem_alloc(wmem_allocator_t *allocator, const size_t size)
77G_GNUC_MALLOC
78G_GNUC_ALLOC_SIZE(2);
79
87#define wmem_new(allocator, type) \
88 ((type*)wmem_alloc((allocator), sizeof(type)))
89
90/*
91 * Overflow-safe multiplication of the size of a type by a number of
92 * items of that type, returning 0 if the result would overflow (or
93 * if the number of elements is negative), and the product otherwise.
94 */
95#define wmem_safe_mult_type_size(type, num) \
96 ((((num) <= 0) || ((size_t)sizeof(type) > (G_MAXSSIZE / (size_t)(num)))) ? 0 : (sizeof(type) * (num)))
97
106#define wmem_alloc_array(allocator, type, num) \
107 ((type*)wmem_alloc((allocator), wmem_safe_mult_type_size(type, (num))))
108
117WS_DLL_PUBLIC
118void *
119wmem_alloc0(wmem_allocator_t *allocator, const size_t size)
120G_GNUC_MALLOC
121G_GNUC_ALLOC_SIZE(2);
122
131#define wmem_new0(allocator, type) \
132 ((type*)wmem_alloc0((allocator), sizeof(type)))
133
143#define wmem_alloc0_array(allocator, type, num) \
144 ((type*)wmem_alloc0((allocator), wmem_safe_mult_type_size(type, (num))))
145
159WS_DLL_PUBLIC
160void
161wmem_free(wmem_allocator_t *allocator, void *ptr);
162
173WS_DLL_PUBLIC
174void *
175wmem_realloc(wmem_allocator_t *allocator, void *ptr, const size_t size)
176G_GNUC_ALLOC_SIZE(3);
177
188WS_DLL_PUBLIC
189void
191
201WS_DLL_PUBLIC
202void
203wmem_gc(wmem_allocator_t *allocator);
204
213WS_DLL_PUBLIC
214void
216
226WS_DLL_PUBLIC
229
236WS_DLL_PUBLIC
237void
238wmem_init(void);
239
247WS_DLL_PUBLIC
248void
249wmem_cleanup(void);
250
259WS_DLL_PUBLIC
260void
262
271WS_DLL_PUBLIC
272void
274
283WS_DLL_PUBLIC
284bool
286
289#ifdef __cplusplus
290}
291#endif /* __cplusplus */
292
293#endif /* __WMEM_CORE_H__ */
294
295/*
296 * Editor modelines - https://www.wireshark.org/tools/modelines.html
297 *
298 * Local variables:
299 * c-basic-offset: 4
300 * tab-width: 8
301 * indent-tabs-mode: nil
302 * End:
303 *
304 * vi: set shiftwidth=4 tabstop=8 expandtab:
305 * :indentSize=4:tabSize=8:noTabs=true:
306 */
WS_DLL_PUBLIC void wmem_init(void)
Initialize the wmem subsystem.
Definition wmem_core.c:168
WS_DLL_PUBLIC void wmem_free_all(wmem_allocator_t *allocator)
Frees all the memory allocated in a pool.
Definition wmem_core.c:108
WS_DLL_PUBLIC wmem_allocator_t * wmem_allocator_new(const wmem_allocator_type_t type)
Create a new allocator of the given type.
Definition wmem_core.c:129
WS_DLL_PUBLIC bool wmem_in_scope(wmem_allocator_t *allocator)
Check whether an allocator is currently in a scoped region.
Definition wmem_core.c:223
WS_DLL_PUBLIC void wmem_free(wmem_allocator_t *allocator, void *ptr)
Returns the allocated memory to the allocator.
Definition wmem_core.c:62
WS_DLL_PUBLIC void wmem_leave_scope(wmem_allocator_t *allocator)
Exit a scoped memory region and free all scoped allocations.
Definition wmem_core.c:216
_wmem_allocator_type_t
Definition wmem_core.h:47
WS_DLL_PUBLIC void wmem_cleanup(void)
Teardown the wmem subsystem.
Definition wmem_core.c:205
WS_DLL_PUBLIC void wmem_gc(wmem_allocator_t *allocator)
Triggers a garbage-collection in the allocator.
Definition wmem_core.c:114
WS_DLL_PUBLIC void * wmem_realloc(wmem_allocator_t *allocator, void *ptr, const size_t size) G_GNUC_ALLOC_SIZE(3)
Resizes a block of memory, potentially moving it if resizing it in place is not possible.
Definition wmem_core.c:79
WS_DLL_PUBLIC void * wmem_alloc(wmem_allocator_t *allocator, const size_t size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(2)
Allocate the requested amount of memory in the given pool.
Definition wmem_core.c:32
WS_DLL_PUBLIC void wmem_destroy_allocator(wmem_allocator_t *allocator)
Destroy the given allocator, freeing all memory allocated in it.
Definition wmem_core.c:120
enum _wmem_allocator_type_t wmem_allocator_type_t
WS_DLL_PUBLIC void wmem_enter_scope(wmem_allocator_t *allocator)
Mark an allocator as entering a scoped memory region.
Definition wmem_core.c:210
WS_DLL_PUBLIC void * wmem_alloc0(wmem_allocator_t *allocator, const size_t size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(2)
Allocate the requested amount of memory in the given pool. Initializes the allocated memory with zero...
Definition wmem_core.c:48
@ WMEM_ALLOCATOR_BLOCK
Definition wmem_core.h:52
@ WMEM_ALLOCATOR_STRICT
Definition wmem_core.h:56
@ WMEM_ALLOCATOR_SIMPLE
Definition wmem_core.h:48
@ WMEM_ALLOCATOR_BLOCK_FAST
Definition wmem_core.h:60
Definition wmem_allocator.h:27