2 * Wireshark Memory Manager Doubly-Linked List
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
15 #include "wmem_core.h"
16 #include "wmem_list.h"
18 struct _wmem_list_frame_t
{
19 struct _wmem_list_frame_t
*next
, *prev
;
25 wmem_list_frame_t
*head
, *tail
;
26 wmem_allocator_t
*allocator
;
30 wmem_list_count(const wmem_list_t
*list
)
36 wmem_list_head(const wmem_list_t
*list
)
42 wmem_list_tail(const wmem_list_t
*list
)
48 wmem_list_frame_next(const wmem_list_frame_t
*frame
)
54 wmem_list_frame_prev(const wmem_list_frame_t
*frame
)
60 wmem_list_frame_data(const wmem_list_frame_t
*frame
)
66 wmem_list_remove(wmem_list_t
*list
, void *data
)
68 wmem_list_frame_t
*frame
;
72 while (frame
&& frame
->data
!= data
) {
80 wmem_list_remove_frame(list
, frame
);
84 wmem_list_remove_frame(wmem_list_t
*list
, wmem_list_frame_t
*frame
)
87 frame
->prev
->next
= frame
->next
;
90 list
->head
= frame
->next
;
94 frame
->next
->prev
= frame
->prev
;
97 list
->tail
= frame
->prev
;
101 wmem_free(list
->allocator
, frame
);
105 wmem_list_find(wmem_list_t
*list
, const void *data
)
107 wmem_list_frame_t
*cur
;
109 for (cur
= list
->head
; cur
; cur
= cur
->next
) {
110 if(cur
->data
== data
)
118 wmem_list_find_custom(wmem_list_t
*list
, const void *data
, GCompareFunc compare_func
)
120 wmem_list_frame_t
*cur
;
122 for (cur
= list
->head
; cur
!= NULL
; cur
= cur
->next
) {
123 if (compare_func(cur
->data
, data
) == 0) {
132 wmem_list_prepend(wmem_list_t
*list
, void *data
)
134 wmem_list_frame_t
*new_frame
;
136 new_frame
= wmem_new(list
->allocator
, wmem_list_frame_t
);
138 new_frame
->data
= data
;
139 new_frame
->next
= list
->head
;
140 new_frame
->prev
= NULL
;
143 list
->head
->prev
= new_frame
;
146 list
->tail
= new_frame
;
149 list
->head
= new_frame
;
154 wmem_list_append(wmem_list_t
*list
, void *data
)
156 wmem_list_frame_t
*new_frame
;
158 new_frame
= wmem_new(list
->allocator
, wmem_list_frame_t
);
159 new_frame
->data
= data
;
160 new_frame
->next
= NULL
;
161 new_frame
->prev
= list
->tail
;
164 list
->tail
->next
= new_frame
;
167 list
->head
= new_frame
;
170 list
->tail
= new_frame
;
175 wmem_list_insert_sorted(wmem_list_t
*list
, void* data
, GCompareFunc func
)
177 wmem_list_frame_t
*new_frame
;
178 wmem_list_frame_t
*cur
;
179 wmem_list_frame_t
*prev
;
181 new_frame
= wmem_new(list
->allocator
, wmem_list_frame_t
);
182 new_frame
->data
= data
;
183 new_frame
->next
= NULL
;
184 new_frame
->prev
= NULL
;
189 list
->head
= new_frame
;
190 list
->tail
= new_frame
;
196 if (func(cur
->data
, data
) >= 0) {
197 cur
->prev
= new_frame
;
198 new_frame
->next
= cur
;
199 list
->head
= new_frame
;
206 } while (cur
&& func(cur
->data
, data
) <= 0);
209 prev
->next
= new_frame
;
210 new_frame
->prev
= prev
;
211 list
->tail
= new_frame
;
215 new_frame
->prev
= prev
;
216 new_frame
->next
= cur
;
217 new_frame
->prev
->next
= new_frame
;
218 new_frame
->next
->prev
= new_frame
;
222 wmem_list_append_sorted(wmem_list_t
*list
, void* data
, GCompareFunc func
)
224 wmem_list_frame_t
*new_frame
;
225 wmem_list_frame_t
*cur
;
226 wmem_list_frame_t
*next
;
228 new_frame
= wmem_new(list
->allocator
, wmem_list_frame_t
);
229 new_frame
->data
= data
;
230 new_frame
->next
= NULL
;
231 new_frame
->prev
= NULL
;
236 list
->head
= new_frame
;
237 list
->tail
= new_frame
;
243 /* best case scenario: append */
244 if (func(cur
->data
, data
) <= 0) {
245 cur
->next
= new_frame
;
246 new_frame
->prev
= cur
;
247 list
->tail
= new_frame
;
254 } while (cur
&& func(cur
->data
, data
) >= 0);
256 /* worst case scenario: prepend */
258 next
->prev
= new_frame
;
259 new_frame
->next
= next
;
260 list
->head
= new_frame
;
264 /* ordinary case: insert */
265 new_frame
->next
= next
;
266 new_frame
->prev
= cur
;
267 new_frame
->prev
->next
= new_frame
;
268 new_frame
->next
->prev
= new_frame
;
272 wmem_list_new(wmem_allocator_t
*allocator
)
276 list
= wmem_new(allocator
, wmem_list_t
);
281 list
->allocator
= allocator
;
287 wmem_destroy_list(wmem_list_t
*list
)
289 wmem_list_frame_t
*cur
, *next
;
299 wmem_free(list
->allocator
, cur
);
303 wmem_free(list
->allocator
, list
);
307 wmem_list_foreach(wmem_list_t
*list
, GFunc foreach_func
, void * user_data
)
309 wmem_list_frame_t
*cur
;
314 foreach_func(cur
->data
, user_data
);
320 * Editor modelines - https://www.wireshark.org/tools/modelines.html
325 * indent-tabs-mode: nil
328 * vi: set shiftwidth=4 tabstop=8 expandtab:
329 * :indentSize=4:tabSize=8:noTabs=true: