| File: | builds/wireshark/wireshark/wsutil/wmem/wmem_multimap.c |
| Warning: | line 112, column 26 Access to field 'map' results in a dereference of a null pointer (loaded from variable 'map') |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* wmem_multimap.c | |||
| 2 | * Wireshark Memory Manager Hash Multimap | |||
| 3 | * Copyright 2021, John Thacker <johnthacker@gmail.com> | |||
| 4 | * Copyright 2014, Evan Huus <eapache@gmail.com> | |||
| 5 | * | |||
| 6 | * Wireshark - Network traffic analyzer | |||
| 7 | * By Gerald Combs <gerald@wireshark.org> | |||
| 8 | * Copyright 1998 Gerald Combs | |||
| 9 | * | |||
| 10 | * SPDX-License-Identifier: GPL-2.0-or-later | |||
| 11 | */ | |||
| 12 | #include "config.h" | |||
| 13 | ||||
| 14 | #include <glib.h> | |||
| 15 | ||||
| 16 | #include "wmem_core.h" | |||
| 17 | #include "wmem_list.h" | |||
| 18 | #include "wmem_map.h" | |||
| 19 | #include "wmem_multimap.h" | |||
| 20 | #include "wmem_tree.h" | |||
| 21 | #include "wmem_user_cb.h" | |||
| 22 | ||||
| 23 | struct _wmem_multimap_t { | |||
| 24 | ||||
| 25 | wmem_map_t *map; | |||
| 26 | ||||
| 27 | unsigned metadata_scope_cb_id; | |||
| 28 | unsigned data_scope_cb_id; | |||
| 29 | ||||
| 30 | wmem_allocator_t *metadata_allocator; | |||
| 31 | wmem_allocator_t *data_allocator; | |||
| 32 | }; | |||
| 33 | ||||
| 34 | wmem_multimap_t * | |||
| 35 | wmem_multimap_new(wmem_allocator_t *allocator, | |||
| 36 | GHashFunc hash_func, GEqualFunc eql_func) | |||
| 37 | { | |||
| 38 | wmem_multimap_t *multimap; | |||
| 39 | ||||
| 40 | multimap = wmem_new(allocator, wmem_multimap_t)((wmem_multimap_t*)wmem_alloc((allocator), sizeof(wmem_multimap_t ))); | |||
| 41 | ||||
| 42 | multimap->map = wmem_map_new(allocator, hash_func, eql_func); | |||
| 43 | multimap->metadata_allocator = allocator; | |||
| 44 | multimap->data_allocator = allocator; | |||
| 45 | ||||
| 46 | return multimap; | |||
| 47 | } | |||
| 48 | ||||
| 49 | static bool_Bool | |||
| 50 | wmem_multimap_reset_cb(wmem_allocator_t *allocator _U___attribute__((unused)), wmem_cb_event_t event, | |||
| 51 | void *user_data) | |||
| 52 | { | |||
| 53 | wmem_multimap_t *multimap = (wmem_multimap_t*)user_data; | |||
| 54 | ||||
| 55 | if (event == WMEM_CB_DESTROY_EVENT) { | |||
| 56 | wmem_unregister_callback(multimap->metadata_allocator, multimap->metadata_scope_cb_id); | |||
| 57 | wmem_free(multimap->metadata_allocator, multimap); | |||
| 58 | } | |||
| 59 | ||||
| 60 | return true1; | |||
| 61 | } | |||
| 62 | ||||
| 63 | static bool_Bool | |||
| 64 | wmem_multimap_destroy_cb(wmem_allocator_t *allocator _U___attribute__((unused)), wmem_cb_event_t event _U___attribute__((unused)), | |||
| 65 | void *user_data) | |||
| 66 | { | |||
| 67 | wmem_multimap_t *multimap = (wmem_multimap_t*)user_data; | |||
| 68 | ||||
| 69 | wmem_unregister_callback(multimap->data_allocator, multimap->data_scope_cb_id); | |||
| 70 | ||||
| 71 | return false0; | |||
| 72 | } | |||
| 73 | ||||
| 74 | wmem_multimap_t * | |||
| 75 | wmem_multimap_new_autoreset(wmem_allocator_t *metadata_scope, wmem_allocator_t *data_scope, | |||
| 76 | GHashFunc hash_func, GEqualFunc eql_func) | |||
| 77 | { | |||
| 78 | wmem_multimap_t *multimap; | |||
| 79 | ||||
| 80 | multimap = wmem_new(metadata_scope, wmem_multimap_t)((wmem_multimap_t*)wmem_alloc((metadata_scope), sizeof(wmem_multimap_t ))); | |||
| 81 | ||||
| 82 | multimap->map = wmem_map_new_autoreset(metadata_scope, data_scope, hash_func, eql_func); | |||
| 83 | multimap->metadata_allocator = metadata_scope; | |||
| 84 | multimap->data_allocator = data_scope; | |||
| 85 | ||||
| 86 | multimap->metadata_scope_cb_id = wmem_register_callback(metadata_scope, wmem_multimap_destroy_cb, multimap); | |||
| 87 | multimap->data_scope_cb_id = wmem_register_callback(data_scope, wmem_multimap_reset_cb, multimap); | |||
| 88 | ||||
| 89 | return multimap; | |||
| 90 | } | |||
| 91 | ||||
| 92 | wmem_list_t* | |||
| 93 | wmem_multimap_get_keys(wmem_allocator_t *list_allocator, const wmem_multimap_t *map) | |||
| 94 | { | |||
| 95 | return wmem_map_get_keys(list_allocator, map->map); | |||
| 96 | } | |||
| 97 | ||||
| 98 | static void | |||
| 99 | count_nodes(void * key _U___attribute__((unused)), void * value, void * user_data) | |||
| 100 | { | |||
| 101 | unsigned* count = (unsigned*)user_data; | |||
| 102 | (*count) += wmem_tree_count(value); | |||
| 103 | } | |||
| 104 | ||||
| 105 | unsigned | |||
| 106 | wmem_multimap_size(const wmem_multimap_t *map) | |||
| 107 | { | |||
| 108 | unsigned count = 0; | |||
| 109 | ||||
| 110 | /* Make sure we have map and a multimap */ | |||
| 111 | if (map == NULL((void*)0) || map->map == NULL((void*)0)) { | |||
| ||||
| 112 | wmem_map_foreach(map->map, count_nodes, &count); | |||
| ||||
| 113 | } | |||
| 114 | return count; | |||
| 115 | } | |||
| 116 | ||||
| 117 | unsigned | |||
| 118 | wmem_multimap_count(const wmem_multimap_t *map, const void *key) | |||
| 119 | { | |||
| 120 | wmem_tree_t *tree; | |||
| 121 | ||||
| 122 | /* Make sure we have map and a multimap */ | |||
| 123 | if (map == NULL((void*)0) || map->map == NULL((void*)0)) { | |||
| 124 | return 0; | |||
| 125 | } | |||
| 126 | ||||
| 127 | if ((tree = wmem_map_lookup(map->map, key)) == NULL((void*)0)) { | |||
| 128 | return 0; | |||
| 129 | } | |||
| 130 | return wmem_tree_count(tree); | |||
| 131 | } | |||
| 132 | ||||
| 133 | bool_Bool | |||
| 134 | wmem_multimap_insert32(wmem_multimap_t *map, const void *key, uint32_t frame_num, void *value) | |||
| 135 | { | |||
| 136 | wmem_tree_t *tree; | |||
| 137 | bool_Bool ret = true1; | |||
| 138 | ||||
| 139 | if ((tree = wmem_map_lookup(map->map, key)) == NULL((void*)0)) { | |||
| 140 | tree = wmem_tree_new(map->data_allocator); | |||
| 141 | wmem_map_insert(map->map, key, tree); | |||
| 142 | ret = false0; | |||
| 143 | } | |||
| 144 | wmem_tree_insert32(tree, frame_num, value); | |||
| 145 | ||||
| 146 | return ret; | |||
| 147 | } | |||
| 148 | ||||
| 149 | void * | |||
| 150 | wmem_multimap_lookup32(const wmem_multimap_t *map, const void *key, uint32_t frame_num) | |||
| 151 | { | |||
| 152 | wmem_tree_t *tree; | |||
| 153 | ||||
| 154 | /* Make sure we have map and a multimap */ | |||
| 155 | if (map == NULL((void*)0) || map->map == NULL((void*)0)) { | |||
| 156 | return NULL((void*)0); | |||
| 157 | } | |||
| 158 | ||||
| 159 | if ((tree = wmem_map_lookup(map->map, key)) == NULL((void*)0)) { | |||
| 160 | return NULL((void*)0); | |||
| 161 | } | |||
| 162 | return wmem_tree_lookup32(tree, frame_num); | |||
| 163 | } | |||
| 164 | ||||
| 165 | void * | |||
| 166 | wmem_multimap_lookup32_le(const wmem_multimap_t *map, const void *key, uint32_t frame_num) | |||
| 167 | { | |||
| 168 | wmem_tree_t *tree; | |||
| 169 | ||||
| 170 | /* Make sure we have map and a multimap */ | |||
| 171 | if (map == NULL((void*)0) || map->map == NULL((void*)0)) { | |||
| 172 | return NULL((void*)0); | |||
| 173 | } | |||
| 174 | ||||
| 175 | if ((tree = wmem_map_lookup(map->map, key)) == NULL((void*)0)) { | |||
| 176 | return NULL((void*)0); | |||
| 177 | } | |||
| 178 | return wmem_tree_lookup32_le(tree, frame_num); | |||
| 179 | } | |||
| 180 | ||||
| 181 | void * | |||
| 182 | wmem_multimap_lookup32_le_full(const wmem_multimap_t *map, const void *key, uint32_t frame_num, uint32_t *orig_frame_num) | |||
| 183 | { | |||
| 184 | wmem_tree_t *tree; | |||
| 185 | ||||
| 186 | /* Make sure we have map and a multimap */ | |||
| 187 | if (map == NULL((void*)0) || map->map == NULL((void*)0)) { | |||
| 188 | return NULL((void*)0); | |||
| 189 | } | |||
| 190 | ||||
| 191 | if ((tree = wmem_map_lookup(map->map, key)) == NULL((void*)0)) { | |||
| 192 | return NULL((void*)0); | |||
| 193 | } | |||
| 194 | return wmem_tree_lookup32_le_full(tree, frame_num, orig_frame_num); | |||
| 195 | } | |||
| 196 | ||||
| 197 | void * | |||
| 198 | wmem_multimap_remove32(wmem_multimap_t *map, const void *key, const uint32_t frame_num) | |||
| 199 | { | |||
| 200 | wmem_tree_t *tree; | |||
| 201 | ||||
| 202 | /* Make sure we have map and a multimap */ | |||
| 203 | if (map == NULL((void*)0) || map->map == NULL((void*)0)) { | |||
| 204 | return NULL((void*)0); | |||
| 205 | } | |||
| 206 | ||||
| 207 | if ((tree = wmem_map_lookup(map->map, key)) == NULL((void*)0)) { | |||
| 208 | return NULL((void*)0); | |||
| 209 | } | |||
| 210 | return wmem_tree_remove32(tree, frame_num); | |||
| 211 | } | |||
| 212 | ||||
| 213 | /* | |||
| 214 | * Editor modelines - https://www.wireshark.org/tools/modelines.html | |||
| 215 | * | |||
| 216 | * Local variables: | |||
| 217 | * c-basic-offset: 4 | |||
| 218 | * tab-width: 8 | |||
| 219 | * indent-tabs-mode: nil | |||
| 220 | * End: | |||
| 221 | * | |||
| 222 | * vi: set shiftwidth=4 tabstop=8 expandtab: | |||
| 223 | * :indentSize=4:tabSize=8:noTabs=true: | |||
| 224 | */ |