regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / wsutil / wmem / wmem_allocator_simple.c
blob0c7d9b0be4e4ce76ad3506e3274c95a02bb7a5c4
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
12 #include "config.h"
14 #include <string.h>
16 #include <glib.h>
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 {
25 int size;
26 int count;
27 void **ptrs;
28 } wmem_simple_allocator_t;
30 static void *
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)) {
38 allocator->size *= 2;
39 allocator->ptrs = (void**)wmem_realloc(NULL, allocator->ptrs,
40 sizeof(void*) * allocator->size);
43 return allocator->ptrs[allocator->count++] = wmem_alloc(NULL, size);
46 static void
47 wmem_simple_free(void *private_data, void *ptr)
49 int i;
50 wmem_simple_allocator_t *allocator;
52 allocator = (wmem_simple_allocator_t*) private_data;
54 wmem_free(NULL, ptr);
55 allocator->count--;
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];
62 return;
66 g_assert_not_reached();
69 static void *
70 wmem_simple_realloc(void *private_data, void *ptr, const size_t size)
72 int i;
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();
84 /* not reached */
85 return NULL;
88 static void
89 wmem_simple_free_all(void *private_data)
91 wmem_simple_allocator_t *allocator;
92 int i;
94 allocator = (wmem_simple_allocator_t*) private_data;
96 for (i = 0; i<allocator->count; i++) {
97 wmem_free(NULL, allocator->ptrs[i]);
99 allocator->count = 0;
102 static void
103 wmem_simple_gc(void *private_data _U_)
105 /* In this simple allocator, there is nothing to garbage-collect */
108 static void
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);
119 void
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
144 * Local variables:
145 * c-basic-offset: 4
146 * tab-width: 8
147 * indent-tabs-mode: nil
148 * End:
150 * vi: set shiftwidth=4 tabstop=8 expandtab:
151 * :indentSize=4:tabSize=8:noTabs=true: