Meson: Add -Wl,-z,nodelete and -Wl,-Bsymbolic-functions where supported
[glib.git] / gobject / gvaluearray.c
blob6648c7318c199f849aab0fee2f2a424a780b7442
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001 Red Hat, Inc.
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.1 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * MT safe
22 #include "config.h"
24 #include <string.h>
25 #include <stdlib.h> /* qsort() */
27 #include "gvaluearray.h"
30 /**
31 * SECTION:value_arrays
32 * @short_description: A container structure to maintain an array of
33 * generic values
34 * @see_also: #GValue, #GParamSpecValueArray, g_param_spec_value_array()
35 * @title: Value arrays
37 * The prime purpose of a #GValueArray is for it to be used as an
38 * object property that holds an array of values. A #GValueArray wraps
39 * an array of #GValue elements in order for it to be used as a boxed
40 * type through %G_TYPE_VALUE_ARRAY.
42 * #GValueArray is deprecated in favour of #GArray since GLib 2.32. It
43 * is possible to create a #GArray that behaves like a #GValueArray by
44 * using the size of #GValue as the element size, and by setting
45 * g_value_unset() as the clear function using g_array_set_clear_func(),
46 * for instance, the following code:
48 * |[<!-- language="C" -->
49 * GValueArray *array = g_value_array_new (10);
50 * ]|
52 * can be replaced by:
54 * |[<!-- language="C" -->
55 * GArray *array = g_array_sized_new (FALSE, TRUE, sizeof (GValue), 10);
56 * g_array_set_clear_func (array, (GDestroyNotify) g_value_unset);
57 * ]|
61 #ifdef DISABLE_MEM_POOLS
62 # define GROUP_N_VALUES (1) /* power of 2 !! */
63 #else
64 # define GROUP_N_VALUES (8) /* power of 2 !! */
65 #endif
68 /* --- functions --- */
69 /**
70 * g_value_array_get_nth:
71 * @value_array: #GValueArray to get a value from
72 * @index_: index of the value of interest
74 * Return a pointer to the value at @index_ containd in @value_array.
76 * Returns: (transfer none): pointer to a value at @index_ in @value_array
78 * Deprecated: 2.32: Use g_array_index() instead.
80 GValue*
81 g_value_array_get_nth (GValueArray *value_array,
82 guint index)
84 g_return_val_if_fail (value_array != NULL, NULL);
85 g_return_val_if_fail (index < value_array->n_values, NULL);
87 return value_array->values + index;
90 static inline void
91 value_array_grow (GValueArray *value_array,
92 guint n_values,
93 gboolean zero_init)
95 g_return_if_fail (n_values >= value_array->n_values);
97 value_array->n_values = n_values;
98 if (value_array->n_values > value_array->n_prealloced)
100 guint i = value_array->n_prealloced;
102 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
103 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
104 if (!zero_init)
105 i = value_array->n_values;
106 memset (value_array->values + i, 0,
107 (value_array->n_prealloced - i) * sizeof (value_array->values[0]));
111 static inline void
112 value_array_shrink (GValueArray *value_array)
114 #ifdef DISABLE_MEM_POOLS
115 if (value_array->n_prealloced >= value_array->n_values + GROUP_N_VALUES)
117 value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
118 value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
120 #endif
124 * g_value_array_new:
125 * @n_prealloced: number of values to preallocate space for
127 * Allocate and initialize a new #GValueArray, optionally preserve space
128 * for @n_prealloced elements. New arrays always contain 0 elements,
129 * regardless of the value of @n_prealloced.
131 * Returns: a newly allocated #GValueArray with 0 values
133 * Deprecated: 2.32: Use #GArray and g_array_sized_new() instead.
135 GValueArray*
136 g_value_array_new (guint n_prealloced)
138 GValueArray *value_array = g_slice_new (GValueArray);
140 value_array->n_values = 0;
141 value_array->n_prealloced = 0;
142 value_array->values = NULL;
143 value_array_grow (value_array, n_prealloced, TRUE);
144 value_array->n_values = 0;
146 return value_array;
150 * g_value_array_free: (skip)
151 * @value_array: #GValueArray to free
153 * Free a #GValueArray including its contents.
155 * Deprecated: 2.32: Use #GArray and g_array_unref() instead.
157 void
158 g_value_array_free (GValueArray *value_array)
160 guint i;
162 g_return_if_fail (value_array != NULL);
164 for (i = 0; i < value_array->n_values; i++)
166 GValue *value = value_array->values + i;
168 if (G_VALUE_TYPE (value) != 0) /* we allow unset values in the array */
169 g_value_unset (value);
171 g_free (value_array->values);
172 g_slice_free (GValueArray, value_array);
176 * g_value_array_copy:
177 * @value_array: #GValueArray to copy
179 * Construct an exact copy of a #GValueArray by duplicating all its
180 * contents.
182 * Returns: (transfer full): Newly allocated copy of #GValueArray
184 * Deprecated: 2.32: Use #GArray and g_array_ref() instead.
186 GValueArray*
187 g_value_array_copy (const GValueArray *value_array)
189 GValueArray *new_array;
190 guint i;
192 g_return_val_if_fail (value_array != NULL, NULL);
194 new_array = g_slice_new (GValueArray);
195 new_array->n_values = 0;
196 new_array->values = NULL;
197 new_array->n_prealloced = 0;
198 value_array_grow (new_array, value_array->n_values, TRUE);
199 for (i = 0; i < new_array->n_values; i++)
200 if (G_VALUE_TYPE (value_array->values + i) != 0)
202 GValue *value = new_array->values + i;
204 g_value_init (value, G_VALUE_TYPE (value_array->values + i));
205 g_value_copy (value_array->values + i, value);
207 return new_array;
211 * g_value_array_prepend:
212 * @value_array: #GValueArray to add an element to
213 * @value: (nullable): #GValue to copy into #GValueArray, or %NULL
215 * Insert a copy of @value as first element of @value_array. If @value is
216 * %NULL, an uninitialized value is prepended.
219 * Returns: (transfer none): the #GValueArray passed in as @value_array
221 * Deprecated: 2.32: Use #GArray and g_array_prepend_val() instead.
223 GValueArray*
224 g_value_array_prepend (GValueArray *value_array,
225 const GValue *value)
227 g_return_val_if_fail (value_array != NULL, NULL);
229 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
230 return g_value_array_insert (value_array, 0, value);
231 G_GNUC_END_IGNORE_DEPRECATIONS
235 * g_value_array_append:
236 * @value_array: #GValueArray to add an element to
237 * @value: (nullable): #GValue to copy into #GValueArray, or %NULL
239 * Insert a copy of @value as last element of @value_array. If @value is
240 * %NULL, an uninitialized value is appended.
242 * Returns: (transfer none): the #GValueArray passed in as @value_array
244 * Deprecated: 2.32: Use #GArray and g_array_append_val() instead.
246 GValueArray*
247 g_value_array_append (GValueArray *value_array,
248 const GValue *value)
250 g_return_val_if_fail (value_array != NULL, NULL);
252 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
253 return g_value_array_insert (value_array, value_array->n_values, value);
254 G_GNUC_END_IGNORE_DEPRECATIONS
258 * g_value_array_insert:
259 * @value_array: #GValueArray to add an element to
260 * @index_: insertion position, must be <= value_array->;n_values
261 * @value: (nullable): #GValue to copy into #GValueArray, or %NULL
263 * Insert a copy of @value at specified position into @value_array. If @value
264 * is %NULL, an uninitialized value is inserted.
266 * Returns: (transfer none): the #GValueArray passed in as @value_array
268 * Deprecated: 2.32: Use #GArray and g_array_insert_val() instead.
270 GValueArray*
271 g_value_array_insert (GValueArray *value_array,
272 guint index,
273 const GValue *value)
275 guint i;
277 g_return_val_if_fail (value_array != NULL, NULL);
278 g_return_val_if_fail (index <= value_array->n_values, value_array);
280 i = value_array->n_values;
281 value_array_grow (value_array, value_array->n_values + 1, FALSE);
282 if (index + 1 < value_array->n_values)
283 memmove (value_array->values + index + 1, value_array->values + index,
284 (i - index) * sizeof (value_array->values[0]));
285 memset (value_array->values + index, 0, sizeof (value_array->values[0]));
286 if (value)
288 g_value_init (value_array->values + index, G_VALUE_TYPE (value));
289 g_value_copy (value, value_array->values + index);
291 return value_array;
295 * g_value_array_remove:
296 * @value_array: #GValueArray to remove an element from
297 * @index_: position of value to remove, which must be less than
298 * @value_array->n_values
300 * Remove the value at position @index_ from @value_array.
302 * Returns: (transfer none): the #GValueArray passed in as @value_array
304 * Deprecated: 2.32: Use #GArray and g_array_remove_index() instead.
306 GValueArray*
307 g_value_array_remove (GValueArray *value_array,
308 guint index)
310 g_return_val_if_fail (value_array != NULL, NULL);
311 g_return_val_if_fail (index < value_array->n_values, value_array);
313 if (G_VALUE_TYPE (value_array->values + index) != 0)
314 g_value_unset (value_array->values + index);
315 value_array->n_values--;
316 if (index < value_array->n_values)
317 memmove (value_array->values + index, value_array->values + index + 1,
318 (value_array->n_values - index) * sizeof (value_array->values[0]));
319 value_array_shrink (value_array);
320 if (value_array->n_prealloced > value_array->n_values)
321 memset (value_array->values + value_array->n_values, 0, sizeof (value_array->values[0]));
323 return value_array;
327 * g_value_array_sort:
328 * @value_array: #GValueArray to sort
329 * @compare_func: (scope call): function to compare elements
331 * Sort @value_array using @compare_func to compare the elements according to
332 * the semantics of #GCompareFunc.
334 * The current implementation uses the same sorting algorithm as standard
335 * C qsort() function.
337 * Returns: (transfer none): the #GValueArray passed in as @value_array
339 * Deprecated: 2.32: Use #GArray and g_array_sort().
341 GValueArray*
342 g_value_array_sort (GValueArray *value_array,
343 GCompareFunc compare_func)
345 g_return_val_if_fail (compare_func != NULL, NULL);
347 if (value_array->n_values)
348 qsort (value_array->values,
349 value_array->n_values,
350 sizeof (value_array->values[0]),
351 compare_func);
352 return value_array;
356 * g_value_array_sort_with_data: (rename-to g_value_array_sort)
357 * @value_array: #GValueArray to sort
358 * @compare_func: (scope call): function to compare elements
359 * @user_data: (closure): extra data argument provided for @compare_func
361 * Sort @value_array using @compare_func to compare the elements according
362 * to the semantics of #GCompareDataFunc.
364 * The current implementation uses the same sorting algorithm as standard
365 * C qsort() function.
367 * Returns: (transfer none): the #GValueArray passed in as @value_array
369 * Deprecated: 2.32: Use #GArray and g_array_sort_with_data().
371 GValueArray*
372 g_value_array_sort_with_data (GValueArray *value_array,
373 GCompareDataFunc compare_func,
374 gpointer user_data)
376 g_return_val_if_fail (value_array != NULL, NULL);
377 g_return_val_if_fail (compare_func != NULL, NULL);
379 if (value_array->n_values)
380 g_qsort_with_data (value_array->values,
381 value_array->n_values,
382 sizeof (value_array->values[0]),
383 compare_func, user_data);
384 return value_array;