5 * Chris Toshok (toshok@novell.com)
7 * (C) 2006 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #define INITIAL_CAPACITY 16
35 #define element_offset(p,i) ((p)->array.data + (i) * (p)->element_size)
36 #define element_length(p,i) ((i) * (p)->element_size)
41 gboolean element_size;
42 gboolean zero_terminated;
47 ensure_capacity (GArrayPriv *priv,
50 int new_capacity = MAX (priv->capacity, INITIAL_CAPACITY);
52 if (capacity < priv->capacity)
55 while (new_capacity < capacity) {
58 capacity = new_capacity;
59 priv->array.data = (gchar*)g_realloc (priv->array.data, element_length (priv, capacity));
62 memset (element_offset (priv, priv->capacity),
64 element_length (priv, capacity - priv->capacity));
66 priv->capacity = capacity;
70 g_array_new (gboolean zero_terminated,
74 GArrayPriv *rv = g_new0 (GArrayPriv, 1);
75 rv->zero_terminated = zero_terminated;
77 rv->element_size = element_size;
79 ensure_capacity (rv, INITIAL_CAPACITY);
85 g_array_free (GArray *array,
86 gboolean free_segment)
100 g_array_append_vals (GArray *array,
104 GArrayPriv *priv = (GArrayPriv*)array;
106 ensure_capacity (priv, priv->array.len + len + (priv->zero_terminated ? 1 : 0));
108 memmove (element_offset (priv, priv->array.len),
110 element_length (priv, len));
112 priv->array.len += len;
114 if (priv->zero_terminated) {
115 memset (element_offset (priv, priv->array.len),
124 g_array_insert_vals (GArray *array,
129 GArrayPriv *priv = (GArrayPriv*)array;
131 ensure_capacity (priv, array->len + len + (priv->zero_terminated ? 1 : 0));
133 /* first move the existing elements out of the way */
134 memmove (element_offset (priv, index_ + len),
135 element_offset (priv, index_),
136 element_length (priv, len - index_));
138 /* then copy the new elements into the array */
139 memmove (element_offset (priv, array->len),
141 element_length (priv, index_));
145 if (priv->zero_terminated) {
146 memset (element_offset (priv, priv->array.len),
155 g_array_remove_index (GArray *array,
158 GArrayPriv *priv = (GArrayPriv*)array;
160 memmove (element_offset (priv, index_),
161 element_offset (priv, index_ + 1),
162 element_length (priv, array->len - index_));
166 if (priv->zero_terminated) {
167 memset (element_offset (priv, priv->array.len),