match_strval > try_val_to_str
[wireshark-wip.git] / epan / wmem / wmem_array.c
blob4307a1c11c13a70d60786e5340bcf4c8968fce96
1 /* wmem_array.c
2 * Wireshark Memory Manager Array
3 * Copyright 2013, Evan Huus <eapache@gmail.com>
5 * $Id$
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.
26 #include "config.h"
28 #include <string.h>
29 #include <stdlib.h>
30 #include <glib.h>
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;
44 guint8 *buf;
46 gsize elem_size;
48 guint elem_count;
49 guint alloc_count;
52 wmem_array_t *
53 wmem_array_sized_new(wmem_allocator_t *allocator, gsize elem_size,
54 guint alloc_count)
56 wmem_array_t *array;
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);
68 return array;
71 wmem_array_t *
72 wmem_array_new(wmem_allocator_t *allocator, const gsize elem_size)
74 wmem_array_t *array;
76 array = wmem_array_sized_new(allocator, elem_size, 1);
78 return array;
81 static void
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) {
90 new_alloc_count *= 2;
93 if (new_alloc_count == array->alloc_count) {
94 return;
97 array->buf = (guint8 *)wmem_realloc(array->allocator, array->buf,
98 new_alloc_count * array->elem_size);
100 array->alloc_count = new_alloc_count;
103 void
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;
114 void *
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];
121 void
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);
127 void *
128 wmem_array_get_raw(wmem_array_t *array)
130 return array->buf;
133 guint
134 wmem_array_get_count(wmem_array_t *array)
136 return array->elem_count;
140 * Editor modelines - http://www.wireshark.org/tools/modelines.html
142 * Local variables:
143 * c-basic-offset: 4
144 * tab-width: 8
145 * indent-tabs-mode: nil
146 * End:
148 * vi: set shiftwidth=4 tabstop=8 expandtab:
149 * :indentSize=4:tabSize=8:noTabs=true: