Use g_slice. (#404430, Chris Wilson)
[platform/upstream/glib.git] / gobject / gvaluearray.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2001 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public 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
20 /*
21  * MT safe
22  */
23
24 #include <config.h>
25
26 #include        "gvaluearray.h"
27 #include        "gobjectalias.h"
28 #include        <string.h>
29 #include        <stdlib.h>      /* qsort() */
30
31 #ifdef  DISABLE_MEM_POOLS
32 #  define       GROUP_N_VALUES  (1)     /* power of 2 !! */
33 #else
34 #  define       GROUP_N_VALUES  (8)     /* power of 2 !! */
35 #endif
36
37
38 /* --- functions --- */
39 GValue*
40 g_value_array_get_nth (GValueArray *value_array,
41                        guint        index)
42 {
43   g_return_val_if_fail (value_array != NULL, NULL);
44   g_return_val_if_fail (index < value_array->n_values, NULL);
45
46   return value_array->values + index;
47 }
48
49 static inline void
50 value_array_grow (GValueArray *value_array,
51                   guint        n_values,
52                   gboolean     zero_init)
53 {
54   g_return_if_fail (n_values >= value_array->n_values);
55
56   value_array->n_values = n_values;
57   if (value_array->n_values > value_array->n_prealloced)
58     {
59       guint i = value_array->n_prealloced;
60
61       value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
62       value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
63       if (!zero_init)
64         i = value_array->n_values;
65       memset (value_array->values + i, 0,
66               (value_array->n_prealloced - i) * sizeof (value_array->values[0]));
67     }
68 }
69
70 static inline void
71 value_array_shrink (GValueArray *value_array)
72 {
73 #ifdef  DISABLE_MEM_POOLS
74   if (value_array->n_prealloced >= value_array->n_values + GROUP_N_VALUES)
75     {
76       value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
77       value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
78     }
79 #endif
80 }
81
82 GValueArray*
83 g_value_array_new (guint n_prealloced)
84 {
85   GValueArray *value_array = g_slice_new (GValueArray);
86
87   value_array->n_values = 0;
88   value_array->n_prealloced = 0;
89   value_array->values = NULL;
90   value_array_grow (value_array, n_prealloced, TRUE);
91   value_array->n_values = 0;
92
93   return value_array;
94 }
95
96 void
97 g_value_array_free (GValueArray *value_array)
98 {
99   guint i;
100
101   g_return_if_fail (value_array != NULL);
102
103   for (i = 0; i < value_array->n_values; i++)
104     {
105       GValue *value = value_array->values + i;
106
107       if (G_VALUE_TYPE (value) != 0) /* we allow unset values in the array */
108         g_value_unset (value);
109     }
110   g_free (value_array->values);
111   g_slice_free (GValueArray, value_array);
112 }
113
114 GValueArray*
115 g_value_array_copy (const GValueArray *value_array)
116 {
117   GValueArray *new_array;
118   guint i;
119
120   g_return_val_if_fail (value_array != NULL, NULL);
121
122   new_array = g_slice_new (GValueArray);
123   new_array->n_values = 0;
124   new_array->values = NULL;
125   new_array->n_prealloced = 0;
126   value_array_grow (new_array, value_array->n_values, TRUE);
127   for (i = 0; i < new_array->n_values; i++)
128     if (G_VALUE_TYPE (value_array->values + i) != 0)
129       {
130         GValue *value = new_array->values + i;
131         
132         g_value_init (value, G_VALUE_TYPE (value_array->values + i));
133         g_value_copy (value_array->values + i, value);
134       }
135   return new_array;
136 }
137
138 GValueArray*
139 g_value_array_prepend (GValueArray  *value_array,
140                        const GValue *value)
141 {
142   g_return_val_if_fail (value_array != NULL, NULL);
143
144   return g_value_array_insert (value_array, 0, value);
145 }
146
147 GValueArray*
148 g_value_array_append (GValueArray  *value_array,
149                       const GValue *value)
150 {
151   g_return_val_if_fail (value_array != NULL, NULL);
152
153   return g_value_array_insert (value_array, value_array->n_values, value);
154 }
155
156 GValueArray*
157 g_value_array_insert (GValueArray  *value_array,
158                       guint         index,
159                       const GValue *value)
160 {
161   guint i;
162
163   g_return_val_if_fail (value_array != NULL, NULL);
164   g_return_val_if_fail (index <= value_array->n_values, value_array);
165
166   /* we support NULL for "value" as a shortcut for an unset value */
167
168   i = value_array->n_values;
169   value_array_grow (value_array, value_array->n_values + 1, FALSE);
170   if (index + 1 < value_array->n_values)
171     g_memmove (value_array->values + index + 1, value_array->values + index,
172                (i - index) * sizeof (value_array->values[0]));
173   memset (value_array->values + index, 0, sizeof (value_array->values[0]));
174   if (value)
175     {
176       g_value_init (value_array->values + index, G_VALUE_TYPE (value));
177       g_value_copy (value, value_array->values + index);
178     }
179   return value_array;
180 }
181
182 GValueArray*
183 g_value_array_remove (GValueArray *value_array,
184                       guint        index)
185 {
186   g_return_val_if_fail (value_array != NULL, NULL);
187   g_return_val_if_fail (index < value_array->n_values, value_array);
188
189   if (G_VALUE_TYPE (value_array->values + index) != 0)
190     g_value_unset (value_array->values + index);
191   value_array->n_values--;
192   if (index < value_array->n_values)
193     g_memmove (value_array->values + index, value_array->values + index + 1,
194                (value_array->n_values - index) * sizeof (value_array->values[0]));
195   value_array_shrink (value_array);
196   if (value_array->n_prealloced > value_array->n_values)
197     memset (value_array->values + value_array->n_values, 0, sizeof (value_array->values[0]));
198
199   return value_array;
200 }
201
202 GValueArray*
203 g_value_array_sort (GValueArray *value_array,
204                     GCompareFunc compare_func)
205 {
206   g_return_val_if_fail (compare_func != NULL, NULL);
207
208   if (value_array->n_values)
209     qsort (value_array->values,
210            value_array->n_values,
211            sizeof (value_array->values[0]),
212            compare_func);
213   return value_array;
214 }
215
216 GValueArray*
217 g_value_array_sort_with_data (GValueArray     *value_array,
218                               GCompareDataFunc compare_func,
219                               gpointer         user_data)
220 {
221   g_return_val_if_fail (value_array != NULL, NULL);
222   g_return_val_if_fail (compare_func != NULL, NULL);
223
224   if (value_array->n_values)
225     g_qsort_with_data (value_array->values,
226                        value_array->n_values,
227                        sizeof (value_array->values[0]),
228                        compare_func, user_data);
229   return value_array;
230 }
231
232 #define __G_VALUE_ARRAY_C__
233 #include "gobjectaliasdef.c"