Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / glib / garray.c
blobd018ec7c119c40b710804402573eb04bf773fbca
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 /*
28 * MT safe
31 #include <string.h>
32 #include "glib.h"
35 #define MIN_ARRAY_SIZE 16
38 typedef struct _GRealArray GRealArray;
40 struct _GRealArray
42 guint8 *data;
43 guint len;
44 guint alloc;
45 guint elt_size;
46 guint zero_terminated : 1;
47 guint clear : 1;
51 static gint g_nearest_pow (gint num);
52 static void g_array_maybe_expand (GRealArray *array,
53 gint len);
55 static GMemChunk *array_mem_chunk = NULL;
56 G_LOCK_DEFINE_STATIC (array_mem_chunk);
58 GArray*
59 g_array_new (gboolean zero_terminated,
60 gboolean clear,
61 guint elt_size)
63 GRealArray *array;
65 G_LOCK (array_mem_chunk);
66 if (!array_mem_chunk)
67 array_mem_chunk = g_mem_chunk_new ("array mem chunk",
68 sizeof (GRealArray),
69 1024, G_ALLOC_AND_FREE);
71 array = g_chunk_new (GRealArray, array_mem_chunk);
72 G_UNLOCK (array_mem_chunk);
74 array->data = NULL;
75 array->len = 0;
76 array->alloc = 0;
77 array->zero_terminated = (zero_terminated ? 1 : 0);
78 array->clear = (clear ? 1 : 0);
79 array->elt_size = elt_size;
81 return (GArray*) array;
84 void
85 g_array_free (GArray *array,
86 gboolean free_segment)
88 if (free_segment)
89 g_free (array->data);
91 G_LOCK (array_mem_chunk);
92 g_mem_chunk_free (array_mem_chunk, array);
93 G_UNLOCK (array_mem_chunk);
96 GArray*
97 g_array_append_vals (GArray *farray,
98 gconstpointer data,
99 guint len)
101 GRealArray *array = (GRealArray*) farray;
103 g_array_maybe_expand (array, len);
105 memcpy (array->data + array->elt_size * array->len, data, array->elt_size * len);
107 array->len += len;
109 return farray;
112 GArray*
113 g_array_prepend_vals (GArray *farray,
114 gconstpointer data,
115 guint len)
117 GRealArray *array = (GRealArray*) farray;
119 g_array_maybe_expand (array, len);
121 g_memmove (array->data + array->elt_size * len, array->data, array->elt_size * array->len);
123 memcpy (array->data, data, len * array->elt_size);
125 array->len += len;
127 return farray;
130 GArray*
131 g_array_insert_vals (GArray *farray,
132 guint index,
133 gconstpointer data,
134 guint len)
136 GRealArray *array = (GRealArray*) farray;
138 g_array_maybe_expand (array, len);
140 g_memmove (array->data + array->elt_size * (len + index),
141 array->data + array->elt_size * index,
142 array->elt_size * (array->len - index));
144 memcpy (array->data + array->elt_size * index, data, len * array->elt_size);
146 array->len += len;
148 return farray;
151 GArray*
152 g_array_set_size (GArray *farray,
153 guint length)
155 GRealArray *array = (GRealArray*) farray;
157 if (array->len < length)
158 g_array_maybe_expand (array, length - array->len);
160 array->len = length;
162 return farray;
165 GArray*
166 g_array_remove_index (GArray* farray,
167 guint index)
169 GRealArray* array = (GRealArray*) farray;
171 g_return_val_if_fail (array, NULL);
173 g_return_val_if_fail (index < array->len, NULL);
175 if (index != array->len - 1)
176 g_memmove (array->data + array->elt_size * index,
177 array->data + array->elt_size * (index + 1),
178 array->elt_size * (array->len - index - 1));
180 if (array->zero_terminated)
181 memset (array->data + array->elt_size * (array->len - 1), 0,
182 array->elt_size);
184 array->len -= 1;
186 return farray;
189 GArray*
190 g_array_remove_index_fast (GArray* farray,
191 guint index)
193 GRealArray* array = (GRealArray*) farray;
195 g_return_val_if_fail (array, NULL);
197 g_return_val_if_fail (index < array->len, NULL);
199 if (index != array->len - 1)
200 g_memmove (array->data + array->elt_size * index,
201 array->data + array->elt_size * (array->len - 1),
202 array->elt_size);
204 if (array->zero_terminated)
205 memset (array->data + array->elt_size * (array->len - 1), 0,
206 array->elt_size);
208 array->len -= 1;
210 return farray;
213 static gint
214 g_nearest_pow (gint num)
216 gint n = 1;
218 while (n < num)
219 n <<= 1;
221 return n;
224 static void
225 g_array_maybe_expand (GRealArray *array,
226 gint len)
228 guint want_alloc = (array->len + len + array->zero_terminated) * array->elt_size;
230 if (want_alloc > array->alloc)
232 guint old_alloc = array->alloc;
234 array->alloc = g_nearest_pow (want_alloc);
235 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
237 array->data = g_realloc (array->data, array->alloc);
239 if (array->clear || array->zero_terminated)
240 memset (array->data + old_alloc, 0, array->alloc - old_alloc);
244 /* Pointer Array
247 typedef struct _GRealPtrArray GRealPtrArray;
249 struct _GRealPtrArray
251 gpointer *pdata;
252 guint len;
253 guint alloc;
256 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
257 gint len);
259 static GMemChunk *ptr_array_mem_chunk = NULL;
260 G_LOCK_DEFINE_STATIC (ptr_array_mem_chunk);
263 GPtrArray*
264 g_ptr_array_new (void)
266 GRealPtrArray *array;
268 G_LOCK (ptr_array_mem_chunk);
269 if (!ptr_array_mem_chunk)
270 ptr_array_mem_chunk = g_mem_chunk_new ("array mem chunk",
271 sizeof (GRealPtrArray),
272 1024, G_ALLOC_AND_FREE);
274 array = g_chunk_new (GRealPtrArray, ptr_array_mem_chunk);
275 G_UNLOCK (ptr_array_mem_chunk);
277 array->pdata = NULL;
278 array->len = 0;
279 array->alloc = 0;
281 return (GPtrArray*) array;
284 void
285 g_ptr_array_free (GPtrArray *array,
286 gboolean free_segment)
288 g_return_if_fail (array);
290 if (free_segment)
291 g_free (array->pdata);
293 G_LOCK (ptr_array_mem_chunk);
294 g_mem_chunk_free (ptr_array_mem_chunk, array);
295 G_UNLOCK (ptr_array_mem_chunk);
298 static void
299 g_ptr_array_maybe_expand (GRealPtrArray *array,
300 gint len)
302 guint old_alloc;
304 if ((array->len + len) > array->alloc)
306 old_alloc = array->alloc;
308 array->alloc = g_nearest_pow (array->len + len);
309 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
310 if (array->pdata)
311 array->pdata = g_realloc (array->pdata, sizeof(gpointer) * array->alloc);
312 else
313 array->pdata = g_new0 (gpointer, array->alloc);
315 memset (array->pdata + old_alloc, 0,
316 sizeof (gpointer) * (array->alloc - old_alloc));
320 void
321 g_ptr_array_set_size (GPtrArray *farray,
322 gint length)
324 GRealPtrArray* array = (GRealPtrArray*) farray;
326 g_return_if_fail (array);
328 if (length > array->len)
329 g_ptr_array_maybe_expand (array, (length - array->len));
331 array->len = length;
334 gpointer
335 g_ptr_array_remove_index (GPtrArray* farray,
336 guint index)
338 GRealPtrArray* array = (GRealPtrArray*) farray;
339 gpointer result;
341 g_return_val_if_fail (array, NULL);
343 g_return_val_if_fail (index < array->len, NULL);
345 result = array->pdata[index];
347 if (index != array->len - 1)
348 g_memmove (array->pdata + index, array->pdata + index + 1,
349 sizeof (gpointer) * (array->len - index - 1));
351 array->pdata[array->len - 1] = NULL;
353 array->len -= 1;
355 return result;
358 gpointer
359 g_ptr_array_remove_index_fast (GPtrArray* farray,
360 guint index)
362 GRealPtrArray* array = (GRealPtrArray*) farray;
363 gpointer result;
365 g_return_val_if_fail (array, NULL);
367 g_return_val_if_fail (index < array->len, NULL);
369 result = array->pdata[index];
371 if (index != array->len - 1)
372 array->pdata[index] = array->pdata[array->len - 1];
374 array->pdata[array->len - 1] = NULL;
376 array->len -= 1;
378 return result;
381 gboolean
382 g_ptr_array_remove (GPtrArray* farray,
383 gpointer data)
385 GRealPtrArray* array = (GRealPtrArray*) farray;
386 int i;
388 g_return_val_if_fail (array, FALSE);
390 for (i = 0; i < array->len; i += 1)
392 if (array->pdata[i] == data)
394 g_ptr_array_remove_index (farray, i);
395 return TRUE;
399 return FALSE;
402 gboolean
403 g_ptr_array_remove_fast (GPtrArray* farray,
404 gpointer data)
406 GRealPtrArray* array = (GRealPtrArray*) farray;
407 int i;
409 g_return_val_if_fail (array, FALSE);
411 for (i = 0; i < array->len; i += 1)
413 if (array->pdata[i] == data)
415 g_ptr_array_remove_index_fast (farray, i);
416 return TRUE;
420 return FALSE;
423 void
424 g_ptr_array_add (GPtrArray* farray,
425 gpointer data)
427 GRealPtrArray* array = (GRealPtrArray*) farray;
429 g_return_if_fail (array);
431 g_ptr_array_maybe_expand (array, 1);
433 array->pdata[array->len++] = data;
436 /* Byte arrays
439 GByteArray* g_byte_array_new (void)
441 return (GByteArray*) g_array_new (FALSE, FALSE, 1);
444 void g_byte_array_free (GByteArray *array,
445 gboolean free_segment)
447 g_array_free ((GArray*) array, free_segment);
450 GByteArray* g_byte_array_append (GByteArray *array,
451 const guint8 *data,
452 guint len)
454 g_array_append_vals ((GArray*) array, (guint8*)data, len);
456 return array;
459 GByteArray* g_byte_array_prepend (GByteArray *array,
460 const guint8 *data,
461 guint len)
463 g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
465 return array;
468 GByteArray* g_byte_array_set_size (GByteArray *array,
469 guint length)
471 g_array_set_size ((GArray*) array, length);
473 return array;
476 GByteArray* g_byte_array_remove_index (GByteArray *array,
477 guint index)
479 g_array_remove_index((GArray*) array, index);
481 return array;
484 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
485 guint index)
487 g_array_remove_index_fast((GArray*) array, index);
489 return array;