]> Pileus Git - ~andy/gtk/blob - glib/garray.c
4d814a44a95c0f869601a569dfee4d3fd82d5b26
[~andy/gtk] / glib / garray.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19 #include <string.h>
20 #include "glib.h"
21
22
23 #define MIN_ARRAY_SIZE  16
24
25
26 typedef struct _GRealArray  GRealArray;
27
28 struct _GRealArray
29 {
30   guint8 *data;
31   guint   len;
32   guint   alloc;
33   guint   zero_terminated;
34 };
35
36
37 static gint g_nearest_pow        (gint        num);
38 static void g_array_maybe_expand (GRealArray *array,
39                                   gint        len);
40
41
42 static GMemChunk *array_mem_chunk = NULL;
43
44
45 GArray*
46 g_array_new (gint zero_terminated)
47 {
48   GRealArray *array;
49
50   if (!array_mem_chunk)
51     array_mem_chunk = g_mem_chunk_new ("array mem chunk",
52                                        sizeof (GRealArray),
53                                        1024, G_ALLOC_AND_FREE);
54
55   array = g_chunk_new (GRealArray, array_mem_chunk);
56
57   array->data = NULL;
58   array->len = 0;
59   array->alloc = 0;
60   array->zero_terminated = (zero_terminated ? 1 : 0);
61
62   return (GArray*) array;
63 }
64
65 void
66 g_array_free (GArray *array,
67               gint    free_segment)
68 {
69   if (free_segment)
70     g_free (array->data);
71
72   g_mem_chunk_free (array_mem_chunk, array);
73 }
74
75 GArray*
76 g_rarray_append (GArray   *array,
77                  gpointer  data,
78                  gint      size)
79 {
80   g_array_maybe_expand ((GRealArray*) array, size);
81
82   memcpy (array->data + array->len, data, size);
83
84   array->len += size;
85
86   return array;
87 }
88
89 GArray*
90 g_rarray_prepend (GArray   *array,
91                   gpointer  data,
92                   gint      size)
93 {
94   g_array_maybe_expand ((GRealArray*) array, size);
95
96   g_memmove (array->data + size, array->data, array->len);
97   memcpy (array->data, data, size);
98
99   array->len += size;
100
101   return array;
102 }
103
104 GArray*
105 g_rarray_truncate (GArray *array,
106                    gint    length,
107                    gint    size)
108 {
109   if (array->data)
110     memset (array->data + length * size, 0, size);
111   array->len = length * size;
112   return array;
113 }
114
115
116 static gint
117 g_nearest_pow (gint num)
118 {
119   gint n = 1;
120
121   while (n < num)
122     n <<= 1;
123
124   return n;
125 }
126
127 static void
128 g_array_maybe_expand (GRealArray *array,
129                       gint        len)
130 {
131   guint old_alloc;
132
133   if ((array->len + len) > array->alloc)
134     {
135       old_alloc = array->alloc;
136
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);
140
141       memset (array->data + old_alloc, 0, array->alloc - old_alloc);
142     }
143 }