/src/wireshark/wsutil/wmem/wmem_user_cb.c
Line | Count | Source |
1 | | /* wmem_user_cb.c |
2 | | * Wireshark Memory Manager User Callbacks |
3 | | * Copyright 2012, Evan Huus <eapache@gmail.com> |
4 | | * |
5 | | * Wireshark - Network traffic analyzer |
6 | | * By Gerald Combs <gerald@wireshark.org> |
7 | | * Copyright 1998 Gerald Combs |
8 | | * |
9 | | * SPDX-License-Identifier: GPL-2.0-or-later |
10 | | */ |
11 | | |
12 | | #include "wmem_core.h" |
13 | | #include "wmem_allocator.h" |
14 | | |
15 | | #include "wmem_user_cb.h" |
16 | | #include "wmem_user_cb_int.h" |
17 | | |
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; |
24 | | |
25 | | void |
26 | | wmem_call_callbacks(wmem_allocator_t *allocator, wmem_cb_event_t event) |
27 | 196k | { |
28 | 196k | wmem_user_cb_container_t **prev, *cur; |
29 | 196k | bool again; |
30 | | |
31 | 196k | prev = &(allocator->callbacks); |
32 | 196k | cur = allocator->callbacks; |
33 | | |
34 | 196k | while (cur) { |
35 | | |
36 | | /* call it */ |
37 | 0 | again = cur->cb(allocator, event, cur->user_data); |
38 | | |
39 | | /* if the callback requested deregistration, or this is being triggered |
40 | | * by the final destruction of the allocator, remove the callback */ |
41 | 0 | if (! again || event == WMEM_CB_DESTROY_EVENT) { |
42 | 0 | *prev = cur->next; |
43 | 0 | wmem_free(NULL, cur); |
44 | 0 | cur = *prev; |
45 | 0 | } |
46 | 0 | else { |
47 | 0 | prev = &(cur->next); |
48 | 0 | cur = cur->next; |
49 | 0 | } |
50 | 0 | } |
51 | 196k | } |
52 | | |
53 | | unsigned |
54 | | wmem_register_callback(wmem_allocator_t *allocator, |
55 | | wmem_user_cb_t callback, void *user_data) |
56 | 10.9k | { |
57 | 10.9k | wmem_user_cb_container_t *container; |
58 | 10.9k | static unsigned next_id = 1; |
59 | | |
60 | 10.9k | container = wmem_new(NULL, wmem_user_cb_container_t); |
61 | | |
62 | 10.9k | container->cb = callback; |
63 | 10.9k | container->user_data = user_data; |
64 | 10.9k | container->next = allocator->callbacks; |
65 | 10.9k | container->id = next_id++; |
66 | | |
67 | 10.9k | allocator->callbacks = container; |
68 | | |
69 | 10.9k | return container->id; |
70 | 10.9k | } |
71 | | |
72 | | void |
73 | | wmem_unregister_callback(wmem_allocator_t *allocator, unsigned id) |
74 | 0 | { |
75 | 0 | wmem_user_cb_container_t **prev, *cur; |
76 | |
|
77 | 0 | prev = &(allocator->callbacks); |
78 | 0 | cur = allocator->callbacks; |
79 | |
|
80 | 0 | while (cur) { |
81 | |
|
82 | 0 | if (cur->id == id) { |
83 | 0 | *prev = cur->next; |
84 | 0 | wmem_free(NULL, cur); |
85 | 0 | return; |
86 | 0 | } |
87 | | |
88 | 0 | prev = &(cur->next); |
89 | 0 | cur = cur->next; |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | /* |
94 | | * Editor modelines - https://www.wireshark.org/tools/modelines.html |
95 | | * |
96 | | * Local variables: |
97 | | * c-basic-offset: 4 |
98 | | * tab-width: 8 |
99 | | * indent-tabs-mode: nil |
100 | | * End: |
101 | | * |
102 | | * vi: set shiftwidth=4 tabstop=8 expandtab: |
103 | | * :indentSize=4:tabSize=8:noTabs=true: |
104 | | */ |