regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / wsutil / wmem / wmem_user_cb.c
blob232b16929adaa310c0309ebe89097da4aa9b0172
1 /* wmem_user_cb.c
2 * Wireshark Memory Manager User Callbacks
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 "wmem_core.h"
13 #include "wmem_allocator.h"
15 #include "wmem_user_cb.h"
16 #include "wmem_user_cb_int.h"
18 typedef struct _wmem_user_cb_container_t {
19 wmem_user_cb_t cb;
20 void *user_data;
21 struct _wmem_user_cb_container_t *next;
22 unsigned id;
23 } wmem_user_cb_container_t;
25 void
26 wmem_call_callbacks(wmem_allocator_t *allocator, wmem_cb_event_t event)
28 wmem_user_cb_container_t **prev, *cur;
29 bool again;
31 prev = &(allocator->callbacks);
32 cur = allocator->callbacks;
34 while (cur) {
36 /* call it */
37 again = cur->cb(allocator, event, cur->user_data);
39 /* if the callback requested deregistration, or this is being triggered
40 * by the final destruction of the allocator, remove the callback */
41 if (! again || event == WMEM_CB_DESTROY_EVENT) {
42 *prev = cur->next;
43 wmem_free(NULL, cur);
44 cur = *prev;
46 else {
47 prev = &(cur->next);
48 cur = cur->next;
53 unsigned
54 wmem_register_callback(wmem_allocator_t *allocator,
55 wmem_user_cb_t callback, void *user_data)
57 wmem_user_cb_container_t *container;
58 static unsigned next_id = 1;
60 container = wmem_new(NULL, wmem_user_cb_container_t);
62 container->cb = callback;
63 container->user_data = user_data;
64 container->next = allocator->callbacks;
65 container->id = next_id++;
67 allocator->callbacks = container;
69 return container->id;
72 void
73 wmem_unregister_callback(wmem_allocator_t *allocator, unsigned id)
75 wmem_user_cb_container_t **prev, *cur;
77 prev = &(allocator->callbacks);
78 cur = allocator->callbacks;
80 while (cur) {
82 if (cur->id == id) {
83 *prev = cur->next;
84 wmem_free(NULL, cur);
85 return;
88 prev = &(cur->next);
89 cur = cur->next;
94 * Editor modelines - https://www.wireshark.org/tools/modelines.html
96 * Local variables:
97 * c-basic-offset: 4
98 * tab-width: 8
99 * indent-tabs-mode: nil
100 * End:
102 * vi: set shiftwidth=4 tabstop=8 expandtab:
103 * :indentSize=4:tabSize=8:noTabs=true: