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.
23 #define MIN_ARRAY_SIZE 16
26 typedef struct _GRealArray GRealArray;
33 guint zero_terminated;
37 static gint g_nearest_pow (gint num);
38 static void g_array_maybe_expand (GRealArray *array,
42 static GMemChunk *array_mem_chunk = NULL;
46 g_array_new (gint zero_terminated)
51 array_mem_chunk = g_mem_chunk_new ("array mem chunk",
53 1024, G_ALLOC_AND_FREE);
55 array = g_chunk_new (GRealArray, array_mem_chunk);
60 array->zero_terminated = (zero_terminated ? 1 : 0);
62 return (GArray*) array;
66 g_array_free (GArray *array,
72 g_mem_chunk_free (array_mem_chunk, array);
76 g_rarray_append (GArray *array,
80 g_array_maybe_expand ((GRealArray*) array, size);
82 memcpy (array->data + array->len, data, size);
90 g_rarray_prepend (GArray *array,
94 g_array_maybe_expand ((GRealArray*) array, size);
96 g_memmove (array->data + size, array->data, array->len);
97 memcpy (array->data, data, size);
105 g_rarray_truncate (GArray *array,
110 memset (array->data + length * size, 0, size);
111 array->len = length * size;
117 g_nearest_pow (gint num)
128 g_array_maybe_expand (GRealArray *array,
133 if ((array->len + len) > array->alloc)
135 old_alloc = array->alloc;
137 array->alloc = g_nearest_pow (array->len + array->zero_terminated + len);
138 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
139 array->data = g_realloc (array->data, array->alloc);
141 memset (array->data + old_alloc, 0, array->alloc - old_alloc);