Coverage Report

Created: 2025-07-23 08:13

/src/pango/subprojects/glib/gobject/gvaluearray.c
Line
Count
Source (jump to first uncovered line)
1
/* GObject - GLib Type, Object, Parameter and Signal Library
2
 * Copyright (C) 2001 Red Hat, Inc.
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General
17
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
/*
21
 * MT safe
22
 */
23
24
#include "config.h"
25
26
#include <string.h>
27
#include <stdlib.h>  /* qsort() */
28
29
#include "gvaluearray.h"
30
31
32
/**
33
 * GValueArray:
34
 * @n_values: number of values contained in the array
35
 * @values: array of values
36
 *
37
 * A `GValueArray` is a container structure to hold an array of generic values.
38
 *
39
 * The prime purpose of a `GValueArray` is for it to be used as an
40
 * object property that holds an array of values. A `GValueArray` wraps
41
 * an array of `GValue` elements in order for it to be used as a boxed
42
 * type through `G_TYPE_VALUE_ARRAY`.
43
 *
44
 * `GValueArray` is deprecated in favour of `GArray` since GLib 2.32.
45
 * It is possible to create a `GArray` that behaves like a `GValueArray`
46
 * by using the size of `GValue` as the element size, and by setting
47
 * [method@GObject.Value.unset] as the clear function using
48
 * [func@GLib.Array.set_clear_func], for instance, the following code:
49
 *
50
 * ```c
51
 *   GValueArray *array = g_value_array_new (10);
52
 * ```
53
 *
54
 * can be replaced by:
55
 *
56
 * ```c
57
 *   GArray *array = g_array_sized_new (FALSE, TRUE, sizeof (GValue), 10);
58
 *   g_array_set_clear_func (array, (GDestroyNotify) g_value_unset);
59
 * ```
60
 *
61
 * Deprecated: 2.32: Use `GArray` instead, if possible for the given use case,
62
 *    as described above.
63
 */
64
65
0
#define GROUP_N_VALUES  (8u)  /* power of 2 !! */
66
67
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
73
 *
74
 * Return a pointer to the value at @index_ contained in @value_array.
75
 *
76
 * Returns: (transfer none): pointer to a value at @index_ in @value_array
77
 *
78
 * Deprecated: 2.32: Use g_array_index() instead.
79
 */
80
GValue*
81
g_value_array_get_nth (GValueArray *value_array,
82
           guint        index)
83
0
{
84
0
  g_return_val_if_fail (value_array != NULL, NULL);
85
0
  g_return_val_if_fail (index < value_array->n_values, NULL);
86
87
0
  return value_array->values + index;
88
0
}
89
90
static inline void
91
value_array_grow (GValueArray *value_array,
92
      guint        n_values,
93
      gboolean     zero_init)
94
0
{
95
0
  g_return_if_fail (n_values >= value_array->n_values);
96
97
0
  value_array->n_values = n_values;
98
0
  if (value_array->n_values > value_array->n_prealloced)
99
0
    {
100
0
      guint i = value_array->n_prealloced;
101
102
      /* round up to the next multiple of GROUP_N_VALUES */
103
0
      value_array->n_prealloced = (value_array->n_values + GROUP_N_VALUES - 1) & ~(GROUP_N_VALUES - 1);
104
0
      value_array->values = g_renew (GValue, value_array->values, value_array->n_prealloced);
105
0
      if (!zero_init)
106
0
  i = value_array->n_values;
107
0
      memset (value_array->values + i, 0,
108
0
        (value_array->n_prealloced - i) * sizeof (value_array->values[0]));
109
0
    }
110
0
}
111
112
/**
113
 * g_value_array_new:
114
 * @n_prealloced: number of values to preallocate space for
115
 *
116
 * Allocate and initialize a new #GValueArray, optionally preserve space
117
 * for @n_prealloced elements. New arrays always contain 0 elements,
118
 * regardless of the value of @n_prealloced.
119
 *
120
 * Returns: a newly allocated #GValueArray with 0 values
121
 *
122
 * Deprecated: 2.32: Use #GArray and g_array_sized_new() instead.
123
 */
124
GValueArray*
125
g_value_array_new (guint n_prealloced)
126
0
{
127
0
  GValueArray *value_array = g_slice_new (GValueArray);
128
129
0
  value_array->n_values = 0;
130
0
  value_array->n_prealloced = 0;
131
0
  value_array->values = NULL;
132
0
  value_array_grow (value_array, n_prealloced, TRUE);
133
0
  value_array->n_values = 0;
134
135
0
  return value_array;
136
0
}
137
138
/**
139
 * g_value_array_free: (skip)
140
 * @value_array: #GValueArray to free
141
 *
142
 * Free a #GValueArray including its contents.
143
 *
144
 * Deprecated: 2.32: Use #GArray and g_array_unref() instead.
145
 */
146
void
147
g_value_array_free (GValueArray *value_array)
148
0
{
149
0
  guint i;
150
151
0
  g_return_if_fail (value_array != NULL);
152
153
0
  for (i = 0; i < value_array->n_values; i++)
154
0
    {
155
0
      GValue *value = value_array->values + i;
156
157
0
      if (G_VALUE_TYPE (value) != 0) /* we allow unset values in the array */
158
0
  g_value_unset (value);
159
0
    }
160
0
  g_free (value_array->values);
161
0
  g_slice_free (GValueArray, value_array);
162
0
}
163
164
/**
165
 * g_value_array_copy:
166
 * @value_array: #GValueArray to copy
167
 *
168
 * Construct an exact copy of a #GValueArray by duplicating all its
169
 * contents.
170
 *
171
 * Returns: (transfer full): Newly allocated copy of #GValueArray
172
 *
173
 * Deprecated: 2.32: Use #GArray and g_array_ref() instead.
174
 */
175
GValueArray*
176
g_value_array_copy (const GValueArray *value_array)
177
0
{
178
0
  GValueArray *new_array;
179
0
  guint i;
180
181
0
  g_return_val_if_fail (value_array != NULL, NULL);
182
183
0
  new_array = g_slice_new (GValueArray);
184
0
  new_array->n_values = 0;
185
0
  new_array->values = NULL;
186
0
  new_array->n_prealloced = 0;
187
0
  value_array_grow (new_array, value_array->n_values, TRUE);
188
0
  for (i = 0; i < new_array->n_values; i++)
189
0
    if (G_VALUE_TYPE (value_array->values + i) != 0)
190
0
      {
191
0
  GValue *value = new_array->values + i;
192
  
193
0
  g_value_init (value, G_VALUE_TYPE (value_array->values + i));
194
0
  g_value_copy (value_array->values + i, value);
195
0
      }
196
0
  return new_array;
197
0
}
198
199
/**
200
 * g_value_array_prepend:
201
 * @value_array: #GValueArray to add an element to
202
 * @value: (nullable): #GValue to copy into #GValueArray, or %NULL
203
 *
204
 * Insert a copy of @value as first element of @value_array. If @value is
205
 * %NULL, an uninitialized value is prepended.
206
 *
207
 *
208
 * Returns: (transfer none): the #GValueArray passed in as @value_array
209
 *
210
 * Deprecated: 2.32: Use #GArray and g_array_prepend_val() instead.
211
 */
212
GValueArray*
213
g_value_array_prepend (GValueArray  *value_array,
214
           const GValue *value)
215
0
{
216
0
  g_return_val_if_fail (value_array != NULL, NULL);
217
218
0
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
219
0
  return g_value_array_insert (value_array, 0, value);
220
0
  G_GNUC_END_IGNORE_DEPRECATIONS
221
0
}
222
223
/**
224
 * g_value_array_append:
225
 * @value_array: #GValueArray to add an element to
226
 * @value: (nullable): #GValue to copy into #GValueArray, or %NULL
227
 *
228
 * Insert a copy of @value as last element of @value_array. If @value is
229
 * %NULL, an uninitialized value is appended.
230
 *
231
 * Returns: (transfer none): the #GValueArray passed in as @value_array
232
 *
233
 * Deprecated: 2.32: Use #GArray and g_array_append_val() instead.
234
 */
235
GValueArray*
236
g_value_array_append (GValueArray  *value_array,
237
          const GValue *value)
238
0
{
239
0
  g_return_val_if_fail (value_array != NULL, NULL);
240
241
0
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
242
0
  return g_value_array_insert (value_array, value_array->n_values, value);
243
0
  G_GNUC_END_IGNORE_DEPRECATIONS
244
0
}
245
246
/**
247
 * g_value_array_insert:
248
 * @value_array: #GValueArray to add an element to
249
 * @index_: insertion position, must be <= value_array->;n_values
250
 * @value: (nullable): #GValue to copy into #GValueArray, or %NULL
251
 *
252
 * Insert a copy of @value at specified position into @value_array. If @value
253
 * is %NULL, an uninitialized value is inserted.
254
 *
255
 * Returns: (transfer none): the #GValueArray passed in as @value_array
256
 *
257
 * Deprecated: 2.32: Use #GArray and g_array_insert_val() instead.
258
 */
259
GValueArray*
260
g_value_array_insert (GValueArray  *value_array,
261
          guint         index,
262
          const GValue *value)
263
0
{
264
0
  guint i;
265
266
0
  g_return_val_if_fail (value_array != NULL, NULL);
267
0
  g_return_val_if_fail (index <= value_array->n_values, value_array);
268
269
0
  i = value_array->n_values;
270
0
  value_array_grow (value_array, value_array->n_values + 1, FALSE);
271
0
  if (index + 1 < value_array->n_values)
272
0
    memmove (value_array->values + index + 1, value_array->values + index,
273
0
             (i - index) * sizeof (value_array->values[0]));
274
0
  memset (value_array->values + index, 0, sizeof (value_array->values[0]));
275
0
  if (value)
276
0
    {
277
0
      g_value_init (value_array->values + index, G_VALUE_TYPE (value));
278
0
      g_value_copy (value, value_array->values + index);
279
0
    }
280
0
  return value_array;
281
0
}
282
283
/**
284
 * g_value_array_remove:
285
 * @value_array: #GValueArray to remove an element from
286
 * @index_: position of value to remove, which must be less than
287
 *     @value_array->n_values
288
 *
289
 * Remove the value at position @index_ from @value_array.
290
 *
291
 * Returns: (transfer none): the #GValueArray passed in as @value_array
292
 *
293
 * Deprecated: 2.32: Use #GArray and g_array_remove_index() instead.
294
 */
295
GValueArray*
296
g_value_array_remove (GValueArray *value_array,
297
          guint        index)
298
0
{
299
0
  g_return_val_if_fail (value_array != NULL, NULL);
300
0
  g_return_val_if_fail (index < value_array->n_values, value_array);
301
302
0
  if (G_VALUE_TYPE (value_array->values + index) != 0)
303
0
    g_value_unset (value_array->values + index);
304
0
  value_array->n_values--;
305
0
  if (index < value_array->n_values)
306
0
    memmove (value_array->values + index, value_array->values + index + 1,
307
0
             (value_array->n_values - index) * sizeof (value_array->values[0]));
308
0
  if (value_array->n_prealloced > value_array->n_values)
309
0
    memset (value_array->values + value_array->n_values, 0, sizeof (value_array->values[0]));
310
311
0
  return value_array;
312
0
}
313
314
/**
315
 * g_value_array_sort:
316
 * @value_array: #GValueArray to sort
317
 * @compare_func: (scope call): function to compare elements
318
 *
319
 * Sort @value_array using @compare_func to compare the elements according to
320
 * the semantics of #GCompareFunc.
321
 *
322
 * The current implementation uses the same sorting algorithm as standard
323
 * C qsort() function.
324
 *
325
 * Returns: (transfer none): the #GValueArray passed in as @value_array
326
 *
327
 * Deprecated: 2.32: Use #GArray and g_array_sort().
328
 */
329
GValueArray*
330
g_value_array_sort (GValueArray *value_array,
331
        GCompareFunc compare_func)
332
0
{
333
0
  g_return_val_if_fail (compare_func != NULL, NULL);
334
335
0
  if (value_array->n_values)
336
0
    qsort (value_array->values,
337
0
     value_array->n_values,
338
0
     sizeof (value_array->values[0]),
339
0
     compare_func);
340
0
  return value_array;
341
0
}
342
343
/**
344
 * g_value_array_sort_with_data: (rename-to g_value_array_sort)
345
 * @value_array: #GValueArray to sort
346
 * @compare_func: (scope call): function to compare elements
347
 * @user_data: (closure): extra data argument provided for @compare_func
348
 *
349
 * Sort @value_array using @compare_func to compare the elements according
350
 * to the semantics of #GCompareDataFunc.
351
 *
352
 * The current implementation uses the same sorting algorithm as standard
353
 * C qsort() function.
354
 *
355
 * Returns: (transfer none): the #GValueArray passed in as @value_array
356
 *
357
 * Deprecated: 2.32: Use #GArray and g_array_sort_with_data().
358
 */
359
GValueArray*
360
g_value_array_sort_with_data (GValueArray     *value_array,
361
            GCompareDataFunc compare_func,
362
            gpointer         user_data)
363
0
{
364
0
  g_return_val_if_fail (value_array != NULL, NULL);
365
0
  g_return_val_if_fail (compare_func != NULL, NULL);
366
367
0
  if (value_array->n_values)
368
0
    g_sort_array (value_array->values,
369
0
      value_array->n_values,
370
0
      sizeof (value_array->values[0]),
371
0
      compare_func, user_data);
372
0
  return value_array;
373
0
}