1 /* wmem_allocator_simple.c
2 * Wireshark Memory Manager Simple Allocator
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
18 #include "wmem_core.h"
19 #include "wmem_allocator.h"
20 #include "wmem_allocator_simple.h"
22 #define DEFAULT_ALLOCS 8192
24 typedef struct _wmem_simple_allocator_t
{
28 } wmem_simple_allocator_t
;
31 wmem_simple_alloc(void *private_data
, const size_t size
)
33 wmem_simple_allocator_t
*allocator
;
35 allocator
= (wmem_simple_allocator_t
*) private_data
;
37 if (G_UNLIKELY(allocator
->count
== allocator
->size
)) {
39 allocator
->ptrs
= (void**)wmem_realloc(NULL
, allocator
->ptrs
,
40 sizeof(void*) * allocator
->size
);
43 return allocator
->ptrs
[allocator
->count
++] = wmem_alloc(NULL
, size
);
47 wmem_simple_free(void *private_data
, void *ptr
)
50 wmem_simple_allocator_t
*allocator
;
52 allocator
= (wmem_simple_allocator_t
*) private_data
;
57 for (i
=allocator
->count
; i
>=0; i
--) {
58 if (ptr
== allocator
->ptrs
[i
]) {
59 if (i
< allocator
->count
) {
60 allocator
->ptrs
[i
] = allocator
->ptrs
[allocator
->count
];
66 g_assert_not_reached();
70 wmem_simple_realloc(void *private_data
, void *ptr
, const size_t size
)
73 wmem_simple_allocator_t
*allocator
;
75 allocator
= (wmem_simple_allocator_t
*) private_data
;
77 for (i
=allocator
->count
-1; i
>=0; i
--) {
78 if (ptr
== allocator
->ptrs
[i
]) {
79 return allocator
->ptrs
[i
] = wmem_realloc(NULL
, allocator
->ptrs
[i
], size
);
83 g_assert_not_reached();
89 wmem_simple_free_all(void *private_data
)
91 wmem_simple_allocator_t
*allocator
;
94 allocator
= (wmem_simple_allocator_t
*) private_data
;
96 for (i
= 0; i
<allocator
->count
; i
++) {
97 wmem_free(NULL
, allocator
->ptrs
[i
]);
103 wmem_simple_gc(void *private_data _U_
)
105 /* In this simple allocator, there is nothing to garbage-collect */
109 wmem_simple_allocator_cleanup(void *private_data
)
111 wmem_simple_allocator_t
*allocator
;
113 allocator
= (wmem_simple_allocator_t
*) private_data
;
115 wmem_free(NULL
, allocator
->ptrs
);
116 wmem_free(NULL
, allocator
);
120 wmem_simple_allocator_init(wmem_allocator_t
*allocator
)
122 wmem_simple_allocator_t
*simple_allocator
;
124 simple_allocator
= wmem_new(NULL
, wmem_simple_allocator_t
);
126 allocator
->walloc
= &wmem_simple_alloc
;
127 allocator
->wrealloc
= &wmem_simple_realloc
;
128 allocator
->wfree
= &wmem_simple_free
;
130 allocator
->free_all
= &wmem_simple_free_all
;
131 allocator
->gc
= &wmem_simple_gc
;
132 allocator
->cleanup
= &wmem_simple_allocator_cleanup
;
134 allocator
->private_data
= (void*) simple_allocator
;
136 simple_allocator
->count
= 0;
137 simple_allocator
->size
= DEFAULT_ALLOCS
;
138 simple_allocator
->ptrs
= wmem_alloc_array(NULL
, void*, DEFAULT_ALLOCS
);
142 * Editor modelines - https://www.wireshark.org/tools/modelines.html
147 * indent-tabs-mode: nil
150 * vi: set shiftwidth=4 tabstop=8 expandtab:
151 * :indentSize=4:tabSize=8:noTabs=true: