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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define MIN_ARRAY_SIZE 16
25 typedef struct _GRealArray GRealArray;
32 guint zero_terminated;
36 static gint g_nearest_pow (gint num);
37 static void g_array_maybe_expand (GRealArray *array,
41 static GMemChunk *array_mem_chunk = NULL;
45 g_array_new (zero_terminated)
50 array_mem_chunk = g_mem_chunk_new ("array mem chunk",
52 1024, G_ALLOC_AND_FREE);
54 array = g_chunk_new (GRealArray, array_mem_chunk);
59 array->zero_terminated = (zero_terminated ? 1 : 0);
61 return (GArray*) array;
65 g_array_free (GArray *array,
71 g_mem_chunk_free (array_mem_chunk, array);
75 g_rarray_append (GArray *array,
79 g_array_maybe_expand ((GRealArray*) array, size);
81 memcpy (array->data + array->len, data, size);
89 g_rarray_prepend (GArray *array,
93 g_array_maybe_expand ((GRealArray*) array, size);
95 memmove (array->data + size, array->data, array->len);
96 memcpy (array->data, data, size);
104 g_rarray_truncate (GArray *array,
109 memset (array->data + length * size, 0, size);
116 g_nearest_pow (gint num)
127 g_array_maybe_expand (GRealArray *array,
132 if ((array->len + len) > array->alloc)
134 old_alloc = array->alloc;
136 array->alloc = g_nearest_pow (array->len + array->zero_terminated + len);
137 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
138 array->data = g_realloc (array->data, array->alloc);
140 memset (array->data + old_alloc, 0, array->alloc - old_alloc);