Wireshark  4.3.0
The Wireshark network protocol analyzer
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
24 extern "C" {
25 #endif /* __cplusplus */
26 
42 struct _wmem_allocator_t;
44 typedef struct _wmem_allocator_t wmem_allocator_t;
45 
47 typedef enum _wmem_allocator_type_t {
66 
73 WS_DLL_PUBLIC
74 void *
75 wmem_alloc(wmem_allocator_t *allocator, const size_t size)
76 G_GNUC_MALLOC;
77 
84 #define wmem_new(allocator, type) \
85  ((type*)wmem_alloc((allocator), sizeof(type)))
86 
87 /*
88  * Overflow-safe multiplication of the size of a type by a number of
89  * items of that type, returning 0 if the result would overflow (or
90  * if the number of elements is negative), and the product otherwise.
91  */
92 #define wmem_safe_mult_type_size(type, num) \
93  ((((num) <= 0) || ((size_t)sizeof(type) > (G_MAXSSIZE / (size_t)(num)))) ? 0 : (sizeof(type) * (num)))
94 
102 #define wmem_alloc_array(allocator, type, num) \
103  ((type*)wmem_alloc((allocator), wmem_safe_mult_type_size(type, (num))))
104 
112 WS_DLL_PUBLIC
113 void *
114 wmem_alloc0(wmem_allocator_t *allocator, const size_t size)
115 G_GNUC_MALLOC;
116 
124 #define wmem_new0(allocator, type) \
125  ((type*)wmem_alloc0((allocator), sizeof(type)))
126 
135 #define wmem_alloc0_array(allocator, type, num) \
136  ((type*)wmem_alloc0((allocator), wmem_safe_mult_type_size(type, (num))))
137 
148 WS_DLL_PUBLIC
149 void
150 wmem_free(wmem_allocator_t *allocator, void *ptr);
151 
161 WS_DLL_PUBLIC
162 void *
163 wmem_realloc(wmem_allocator_t *allocator, void *ptr, const size_t size)
164 G_GNUC_MALLOC;
165 
173 WS_DLL_PUBLIC
174 void
175 wmem_free_all(wmem_allocator_t *allocator);
176 
183 WS_DLL_PUBLIC
184 void
185 wmem_gc(wmem_allocator_t *allocator);
186 
192 WS_DLL_PUBLIC
193 void
195 
202 WS_DLL_PUBLIC
205 
209 WS_DLL_PUBLIC
210 void
211 wmem_init(void);
212 
217 WS_DLL_PUBLIC
218 void
219 wmem_cleanup(void);
220 
221 WS_DLL_PUBLIC
222 void
223 wmem_enter_scope(wmem_allocator_t *allocator);
224 
225 WS_DLL_PUBLIC
226 void
227 wmem_leave_scope(wmem_allocator_t *allocator);
228 
229 WS_DLL_PUBLIC
230 bool
231 wmem_in_scope(wmem_allocator_t *allocator);
232 
235 #ifdef __cplusplus
236 }
237 #endif /* __cplusplus */
238 
239 #endif /* __WMEM_CORE_H__ */
240 
241 /*
242  * Editor modelines - https://www.wireshark.org/tools/modelines.html
243  *
244  * Local variables:
245  * c-basic-offset: 4
246  * tab-width: 8
247  * indent-tabs-mode: nil
248  * End:
249  *
250  * vi: set shiftwidth=4 tabstop=8 expandtab:
251  * :indentSize=4:tabSize=8:noTabs=true:
252  */
WS_DLL_PUBLIC void wmem_init(void)
Definition: wmem_core.c:168
WS_DLL_PUBLIC void * wmem_alloc(wmem_allocator_t *allocator, const size_t size) G_GNUC_MALLOC
Definition: wmem_core.c:32
WS_DLL_PUBLIC void wmem_free_all(wmem_allocator_t *allocator)
Definition: wmem_core.c:108
WS_DLL_PUBLIC void wmem_free(wmem_allocator_t *allocator, void *ptr)
Definition: wmem_core.c:62
_wmem_allocator_type_t
Definition: wmem_core.h:47
WS_DLL_PUBLIC void wmem_cleanup(void)
Definition: wmem_core.c:205
WS_DLL_PUBLIC void wmem_gc(wmem_allocator_t *allocator)
Definition: wmem_core.c:114
WS_DLL_PUBLIC wmem_allocator_t * wmem_allocator_new(const wmem_allocator_type_t type)
Definition: wmem_core.c:129
WS_DLL_PUBLIC void * wmem_realloc(wmem_allocator_t *allocator, void *ptr, const size_t size) G_GNUC_MALLOC
Definition: wmem_core.c:79
WS_DLL_PUBLIC void * wmem_alloc0(wmem_allocator_t *allocator, const size_t size) G_GNUC_MALLOC
Definition: wmem_core.c:48
WS_DLL_PUBLIC void wmem_destroy_allocator(wmem_allocator_t *allocator)
Definition: wmem_core.c:120
enum _wmem_allocator_type_t wmem_allocator_type_t
@ 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