2 * Wireshark Memory Manager Hash Multimap
3 * Copyright 2021, John Thacker <johnthacker@gmail.com>
4 * Copyright 2014, Evan Huus <eapache@gmail.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
16 #include "wmem_core.h"
17 #include "wmem_list.h"
19 #include "wmem_multimap.h"
20 #include "wmem_tree.h"
21 #include "wmem_user_cb.h"
23 struct _wmem_multimap_t
{
27 unsigned metadata_scope_cb_id
;
28 unsigned data_scope_cb_id
;
30 wmem_allocator_t
*metadata_allocator
;
31 wmem_allocator_t
*data_allocator
;
35 wmem_multimap_new(wmem_allocator_t
*allocator
,
36 GHashFunc hash_func
, GEqualFunc eql_func
)
38 wmem_multimap_t
*multimap
;
40 multimap
= wmem_new(allocator
, wmem_multimap_t
);
42 multimap
->map
= wmem_map_new(allocator
, hash_func
, eql_func
);
43 multimap
->metadata_allocator
= allocator
;
44 multimap
->data_allocator
= allocator
;
50 wmem_multimap_reset_cb(wmem_allocator_t
*allocator _U_
, wmem_cb_event_t event
,
53 wmem_multimap_t
*multimap
= (wmem_multimap_t
*)user_data
;
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
);
64 wmem_multimap_destroy_cb(wmem_allocator_t
*allocator _U_
, wmem_cb_event_t event _U_
,
67 wmem_multimap_t
*multimap
= (wmem_multimap_t
*)user_data
;
69 wmem_unregister_callback(multimap
->data_allocator
, multimap
->data_scope_cb_id
);
75 wmem_multimap_new_autoreset(wmem_allocator_t
*metadata_scope
, wmem_allocator_t
*data_scope
,
76 GHashFunc hash_func
, GEqualFunc eql_func
)
78 wmem_multimap_t
*multimap
;
80 multimap
= wmem_new(metadata_scope
, wmem_multimap_t
);
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
;
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
);
93 wmem_multimap_get_keys(wmem_allocator_t
*list_allocator
, wmem_multimap_t
*map
)
95 return wmem_map_get_keys(list_allocator
, map
->map
);
99 count_nodes(void * key _U_
, void * value
, void * user_data
)
101 unsigned* count
= (unsigned*)user_data
;
102 (*count
) += wmem_tree_count(value
);
106 wmem_multimap_size(wmem_multimap_t
*map
)
110 wmem_map_foreach(map
->map
, count_nodes
, &count
);
115 wmem_multimap_count(wmem_multimap_t
*map
, const void *key
)
119 if ((tree
= wmem_map_lookup(map
->map
, key
)) == NULL
) {
122 return wmem_tree_count(tree
);
126 wmem_multimap_insert32(wmem_multimap_t
*map
, const void *key
, uint32_t frame_num
, void *value
)
131 if ((tree
= wmem_map_lookup(map
->map
, key
)) == NULL
) {
132 tree
= wmem_tree_new(map
->data_allocator
);
133 wmem_map_insert(map
->map
, key
, tree
);
136 wmem_tree_insert32(tree
, frame_num
, value
);
142 wmem_multimap_lookup32(wmem_multimap_t
*map
, const void *key
, uint32_t frame_num
)
146 if ((tree
= wmem_map_lookup(map
->map
, key
)) == NULL
) {
149 return wmem_tree_lookup32(tree
, frame_num
);
153 wmem_multimap_lookup32_le(wmem_multimap_t
*map
, const void *key
, uint32_t frame_num
)
157 if ((tree
= wmem_map_lookup(map
->map
, key
)) == NULL
) {
160 return wmem_tree_lookup32_le(tree
, frame_num
);
164 wmem_multimap_remove32(wmem_multimap_t
*map
, const void *key
, const uint32_t frame_num
)
168 if ((tree
= wmem_map_lookup(map
->map
, key
)) == NULL
) {
171 return wmem_tree_remove32(tree
, frame_num
);
175 * Editor modelines - https://www.wireshark.org/tools/modelines.html
180 * indent-tabs-mode: nil
183 * vi: set shiftwidth=4 tabstop=8 expandtab:
184 * :indentSize=4:tabSize=8:noTabs=true: