HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / wmem / wmem_user_cb.c
blob7c5844a7d744bff6f27046b7b01533866e96ecdf
1 /* wmem_user_cb.c
2 * Wireshark Memory Manager User Callbacks
3 * Copyright 2012, Evan Huus <eapache@gmail.com>
5 * $Id$
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "wmem_core.h"
27 #include "wmem_allocator.h"
29 #include "wmem_user_cb.h"
30 #include "wmem_user_cb_int.h"
32 typedef struct _wmem_user_cb_container_t {
33 wmem_user_cb_t cb;
34 void *user_data;
35 struct _wmem_user_cb_container_t *next;
36 guint id;
37 } wmem_user_cb_container_t;
39 void
40 wmem_call_callbacks(wmem_allocator_t *allocator, wmem_cb_event_t event)
42 wmem_user_cb_container_t **prev, *cur;
43 gboolean again;
45 prev = &(allocator->callbacks);
46 cur = allocator->callbacks;
48 while (cur) {
50 /* call it */
51 again = cur->cb(allocator, event, cur->user_data);
53 /* if the callback requested deregistration, or this is being triggered
54 * by the final destruction of the allocator, remove the callback */
55 if (! again || event == WMEM_CB_DESTROY_EVENT) {
56 *prev = cur->next;
57 wmem_free(NULL, cur);
58 cur = *prev;
60 else {
61 prev = &(cur->next);
62 cur = cur->next;
67 guint
68 wmem_register_callback(wmem_allocator_t *allocator,
69 wmem_user_cb_t callback, void *user_data)
71 wmem_user_cb_container_t *container;
72 static guint next_id = 0;
74 container = wmem_new(NULL, wmem_user_cb_container_t);
76 container->cb = callback;
77 container->user_data = user_data;
78 container->next = allocator->callbacks;
79 container->id = next_id++;
81 allocator->callbacks = container;
83 return container->id;
86 void
87 wmem_unregister_callback(wmem_allocator_t *allocator, guint id)
89 wmem_user_cb_container_t **prev, *cur;
91 prev = &(allocator->callbacks);
92 cur = allocator->callbacks;
94 while (cur) {
96 if (cur->id == id) {
97 *prev = cur->next;
98 wmem_free(NULL, cur);
99 return;
102 prev = &(cur->next);
103 cur = cur->next;
108 * Editor modelines - http://www.wireshark.org/tools/modelines.html
110 * Local variables:
111 * c-basic-offset: 4
112 * tab-width: 8
113 * indent-tabs-mode: nil
114 * End:
116 * vi: set shiftwidth=4 tabstop=8 expandtab:
117 * :indentSize=4:tabSize=8:noTabs=true: