2 * Wireshark Memory Manager Array
3 * Copyright 2013, Evan Huus <eapache@gmail.com>
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.
32 #include "wmem_core.h"
33 #include "wmem_array.h"
35 /* Holds a wmem-allocated array.
36 * elem_len is the size of each element
37 * elem_count is the number of used elements
38 * alloc_count is the length (in elems) of the raw buffer pointed to by buf,
39 * regardless of how many elems are used (the contents)
41 struct _wmem_array_t
{
42 wmem_allocator_t
*allocator
;
53 wmem_array_sized_new(wmem_allocator_t
*allocator
, gsize elem_size
,
58 array
= wmem_new(allocator
, wmem_array_t
);
60 array
->allocator
= allocator
;
61 array
->elem_size
= elem_size
;
62 array
->elem_count
= 0;
63 array
->alloc_count
= alloc_count
? alloc_count
: 1;
65 array
->buf
= (guint8
*)wmem_alloc(array
->allocator
,
66 array
->elem_size
* array
->alloc_count
);
72 wmem_array_new(wmem_allocator_t
*allocator
, const gsize elem_size
)
76 array
= wmem_array_sized_new(allocator
, elem_size
, 1);
82 wmem_array_grow(wmem_array_t
*array
, const guint to_add
)
84 guint new_alloc_count
, new_count
;
86 new_alloc_count
= array
->alloc_count
;
87 new_count
= array
->elem_count
+ to_add
;
89 while (new_alloc_count
< new_count
) {
93 if (new_alloc_count
== array
->alloc_count
) {
97 array
->buf
= (guint8
*)wmem_realloc(array
->allocator
, array
->buf
,
98 new_alloc_count
* array
->elem_size
);
100 array
->alloc_count
= new_alloc_count
;
104 wmem_array_append(wmem_array_t
*array
, const void *in
, guint count
)
106 wmem_array_grow(array
, count
);
108 memcpy(&array
->buf
[array
->elem_count
* array
->elem_size
], in
,
109 count
* array
->elem_size
);
111 array
->elem_count
+= count
;
115 wmem_array_index(wmem_array_t
*array
, guint array_index
)
117 g_assert(array_index
< array
->elem_count
);
118 return &array
->buf
[array_index
* array
->elem_size
];
122 wmem_array_sort(wmem_array_t
*array
, int (*compar
)(const void*,const void*))
124 qsort(array
->buf
, array
->elem_count
, array
->elem_size
, compar
);
128 wmem_array_get_raw(wmem_array_t
*array
)
134 wmem_array_get_count(wmem_array_t
*array
)
136 return array
->elem_count
;
140 * Editor modelines - http://www.wireshark.org/tools/modelines.html
145 * indent-tabs-mode: nil
148 * vi: set shiftwidth=4 tabstop=8 expandtab:
149 * :indentSize=4:tabSize=8:noTabs=true: