Coverage Report

Created: 2018-09-25 13:52

/src/glib/gobject/gboxed.c
Line
Count
Source (jump to first uncovered line)
1
/* GObject - GLib Type, Object, Parameter and Signal Library
2
 * Copyright (C) 2000-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.1 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, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include "config.h"
19
20
#include <string.h>
21
22
/* for GValueArray */
23
#define GLIB_DISABLE_DEPRECATION_WARNINGS
24
25
#include "gboxed.h"
26
#include "gclosure.h"
27
#include "gtype-private.h"
28
#include "gvalue.h"
29
#include "gvaluearray.h"
30
#include "gvaluecollector.h"
31
32
33
/**
34
 * SECTION:gboxed
35
 * @short_description: A mechanism to wrap opaque C structures registered
36
 *     by the type system
37
 * @see_also: #GParamSpecBoxed, g_param_spec_boxed()
38
 * @title: Boxed Types
39
 *
40
 * GBoxed is a generic wrapper mechanism for arbitrary C structures. The only
41
 * thing the type system needs to know about the structures is how to copy and
42
 * free them, beyond that they are treated as opaque chunks of memory.
43
 *
44
 * Boxed types are useful for simple value-holder structures like rectangles or
45
 * points. They can also be used for wrapping structures defined in non-GObject
46
 * based libraries.
47
 */
48
49
static inline void              /* keep this function in sync with gvalue.c */
50
value_meminit (GValue *value,
51
         GType   value_type)
52
0
{
53
0
  value->g_type = value_type;
54
0
  memset (value->data, 0, sizeof (value->data));
55
0
}
56
57
static GValue *
58
value_copy (GValue *src_value)
59
0
{
60
0
  GValue *dest_value = g_new0 (GValue, 1);
61
0
62
0
  if (G_VALUE_TYPE (src_value))
63
0
    {
64
0
      g_value_init (dest_value, G_VALUE_TYPE (src_value));
65
0
      g_value_copy (src_value, dest_value);
66
0
    }
67
0
  return dest_value;
68
0
}
69
70
static void
71
value_free (GValue *value)
72
0
{
73
0
  if (G_VALUE_TYPE (value))
74
0
    g_value_unset (value);
75
0
  g_free (value);
76
0
}
77
78
static GPollFD *
79
pollfd_copy (GPollFD *src)
80
0
{
81
0
  GPollFD *dest = g_new0 (GPollFD, 1);
82
0
  /* just a couple of integers */
83
0
  memcpy (dest, src, sizeof (GPollFD));
84
0
  return dest;
85
0
}
86
87
void
88
_g_boxed_type_init (void)
89
1
{
90
1
  const GTypeInfo info = {
91
1
    0,                          /* class_size */
92
1
    NULL,                       /* base_init */
93
1
    NULL,                       /* base_destroy */
94
1
    NULL,                       /* class_init */
95
1
    NULL,                       /* class_destroy */
96
1
    NULL,                       /* class_data */
97
1
    0,                          /* instance_size */
98
1
    0,                          /* n_preallocs */
99
1
    NULL,                       /* instance_init */
100
1
    NULL,                       /* value_table */
101
1
  };
102
1
  const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
103
1
  GType type;
104
1
105
1
  /* G_TYPE_BOXED
106
1
   */
107
1
  type = g_type_register_fundamental (G_TYPE_BOXED, g_intern_static_string ("GBoxed"), &info, &finfo,
108
1
              G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
109
1
  g_assert (type == G_TYPE_BOXED);
110
1
}
111
112
static GString *
113
gstring_copy (GString *src_gstring)
114
0
{
115
0
  return g_string_new_len (src_gstring->str, src_gstring->len);
116
0
}
117
118
static void
119
gstring_free (GString *gstring)
120
0
{
121
0
  g_string_free (gstring, TRUE);
122
0
}
123
124
G_DEFINE_BOXED_TYPE (GClosure, g_closure, g_closure_ref, g_closure_unref)
125
G_DEFINE_BOXED_TYPE (GValue, g_value, value_copy, value_free)
126
G_DEFINE_BOXED_TYPE (GValueArray, g_value_array, g_value_array_copy, g_value_array_free)
127
G_DEFINE_BOXED_TYPE (GDate, g_date, g_date_copy, g_date_free)
128
/* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
129
G_DEFINE_BOXED_TYPE (GString, g_gstring, gstring_copy, gstring_free)
130
G_DEFINE_BOXED_TYPE (GHashTable, g_hash_table, g_hash_table_ref, g_hash_table_unref)
131
G_DEFINE_BOXED_TYPE (GArray, g_array, g_array_ref, g_array_unref)
132
G_DEFINE_BOXED_TYPE (GPtrArray, g_ptr_array,g_ptr_array_ref, g_ptr_array_unref)
133
G_DEFINE_BOXED_TYPE (GByteArray, g_byte_array, g_byte_array_ref, g_byte_array_unref)
134
G_DEFINE_BOXED_TYPE (GBytes, g_bytes, g_bytes_ref, g_bytes_unref)
135
136
G_DEFINE_BOXED_TYPE (GRegex, g_regex, g_regex_ref, g_regex_unref)
137
G_DEFINE_BOXED_TYPE (GMatchInfo, g_match_info, g_match_info_ref, g_match_info_unref)
138
139
#define g_variant_type_get_type g_variant_type_get_gtype
140
G_DEFINE_BOXED_TYPE (GVariantType, g_variant_type, g_variant_type_copy, g_variant_type_free)
141
#undef g_variant_type_get_type
142
143
G_DEFINE_BOXED_TYPE (GVariantBuilder, g_variant_builder, g_variant_builder_ref, g_variant_builder_unref)
144
G_DEFINE_BOXED_TYPE (GVariantDict, g_variant_dict, g_variant_dict_ref, g_variant_dict_unref)
145
146
G_DEFINE_BOXED_TYPE (GError, g_error, g_error_copy, g_error_free)
147
148
G_DEFINE_BOXED_TYPE (GDateTime, g_date_time, g_date_time_ref, g_date_time_unref)
149
G_DEFINE_BOXED_TYPE (GTimeZone, g_time_zone, g_time_zone_ref, g_time_zone_unref)
150
G_DEFINE_BOXED_TYPE (GKeyFile, g_key_file, g_key_file_ref, g_key_file_unref)
151
G_DEFINE_BOXED_TYPE (GMappedFile, g_mapped_file, g_mapped_file_ref, g_mapped_file_unref)
152
153
G_DEFINE_BOXED_TYPE (GMainLoop, g_main_loop, g_main_loop_ref, g_main_loop_unref)
154
G_DEFINE_BOXED_TYPE (GMainContext, g_main_context, g_main_context_ref, g_main_context_unref)
155
G_DEFINE_BOXED_TYPE (GSource, g_source, g_source_ref, g_source_unref)
156
G_DEFINE_BOXED_TYPE (GPollFD, g_pollfd, pollfd_copy, g_free)
157
G_DEFINE_BOXED_TYPE (GMarkupParseContext, g_markup_parse_context, g_markup_parse_context_ref, g_markup_parse_context_unref)
158
159
G_DEFINE_BOXED_TYPE (GThread, g_thread, g_thread_ref, g_thread_unref)
160
G_DEFINE_BOXED_TYPE (GChecksum, g_checksum, g_checksum_copy, g_checksum_free)
161
162
G_DEFINE_BOXED_TYPE (GOptionGroup, g_option_group, g_option_group_ref, g_option_group_unref)
163
164
/* This one can't use G_DEFINE_BOXED_TYPE (GStrv, g_strv, g_strdupv, g_strfreev) */
165
GType
166
g_strv_get_type (void)
167
0
{
168
0
  static volatile gsize g_define_type_id__volatile = 0;
169
0
170
0
  if (g_once_init_enter (&g_define_type_id__volatile))
171
0
    {
172
0
      GType g_define_type_id =
173
0
        g_boxed_type_register_static (g_intern_static_string ("GStrv"),
174
0
                                      (GBoxedCopyFunc) g_strdupv,
175
0
                                      (GBoxedFreeFunc) g_strfreev);
176
0
177
0
      g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
178
0
    }
179
0
180
0
  return g_define_type_id__volatile;
181
0
}
182
183
GType
184
g_variant_get_gtype (void)
185
0
{
186
0
  return G_TYPE_VARIANT;
187
0
}
188
189
static void
190
boxed_proxy_value_init (GValue *value)
191
0
{
192
0
  value->data[0].v_pointer = NULL;
193
0
}
194
195
static void
196
boxed_proxy_value_free (GValue *value)
197
0
{
198
0
  if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
199
0
    _g_type_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
200
0
}
201
202
static void
203
boxed_proxy_value_copy (const GValue *src_value,
204
      GValue       *dest_value)
205
0
{
206
0
  if (src_value->data[0].v_pointer)
207
0
    dest_value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (src_value), src_value->data[0].v_pointer);
208
0
  else
209
0
    dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
210
0
}
211
212
static gpointer
213
boxed_proxy_value_peek_pointer (const GValue *value)
214
0
{
215
0
  return value->data[0].v_pointer;
216
0
}
217
218
static gchar*
219
boxed_proxy_collect_value (GValue      *value,
220
         guint        n_collect_values,
221
         GTypeCValue *collect_values,
222
         guint        collect_flags)
223
0
{
224
0
  if (!collect_values[0].v_pointer)
225
0
    value->data[0].v_pointer = NULL;
226
0
  else
227
0
    {
228
0
      if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
229
0
  {
230
0
    value->data[0].v_pointer = collect_values[0].v_pointer;
231
0
    value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
232
0
  }
233
0
      else
234
0
  value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (value), collect_values[0].v_pointer);
235
0
    }
236
0
237
0
  return NULL;
238
0
}
239
240
static gchar*
241
boxed_proxy_lcopy_value (const GValue *value,
242
       guint         n_collect_values,
243
       GTypeCValue  *collect_values,
244
       guint         collect_flags)
245
0
{
246
0
  gpointer *boxed_p = collect_values[0].v_pointer;
247
0
248
0
  if (!boxed_p)
249
0
    return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
250
0
251
0
  if (!value->data[0].v_pointer)
252
0
    *boxed_p = NULL;
253
0
  else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
254
0
    *boxed_p = value->data[0].v_pointer;
255
0
  else
256
0
    *boxed_p = _g_type_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer);
257
0
258
0
  return NULL;
259
0
}
260
261
/**
262
 * g_boxed_type_register_static:
263
 * @name: Name of the new boxed type.
264
 * @boxed_copy: Boxed structure copy function.
265
 * @boxed_free: Boxed structure free function.
266
 *
267
 * This function creates a new %G_TYPE_BOXED derived type id for a new
268
 * boxed type with name @name. Boxed type handling functions have to be
269
 * provided to copy and free opaque boxed structures of this type.
270
 *
271
 * Returns: New %G_TYPE_BOXED derived type id for @name.
272
 */
273
GType
274
g_boxed_type_register_static (const gchar   *name,
275
            GBoxedCopyFunc boxed_copy,
276
            GBoxedFreeFunc boxed_free)
277
1
{
278
1
  static const GTypeValueTable vtable = {
279
1
    boxed_proxy_value_init,
280
1
    boxed_proxy_value_free,
281
1
    boxed_proxy_value_copy,
282
1
    boxed_proxy_value_peek_pointer,
283
1
    "p",
284
1
    boxed_proxy_collect_value,
285
1
    "p",
286
1
    boxed_proxy_lcopy_value,
287
1
  };
288
1
  GTypeInfo type_info = {
289
1
    0,      /* class_size */
290
1
    NULL,   /* base_init */
291
1
    NULL,   /* base_finalize */
292
1
    NULL,   /* class_init */
293
1
    NULL,   /* class_finalize */
294
1
    NULL,   /* class_data */
295
1
    0,      /* instance_size */
296
1
    0,      /* n_preallocs */
297
1
    NULL,   /* instance_init */
298
1
    &vtable,    /* value_table */
299
1
  };
300
1
  GType type;
301
1
302
1
  g_return_val_if_fail (name != NULL, 0);
303
1
  g_return_val_if_fail (boxed_copy != NULL, 0);
304
1
  g_return_val_if_fail (boxed_free != NULL, 0);
305
1
  g_return_val_if_fail (g_type_from_name (name) == 0, 0);
306
1
307
1
  type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
308
1
309
1
  /* install proxy functions upon successful registration */
310
1
  if (type)
311
1
    _g_type_boxed_init (type, boxed_copy, boxed_free);
312
1
313
1
  return type;
314
1
}
315
316
/**
317
 * g_boxed_copy:
318
 * @boxed_type: The type of @src_boxed.
319
 * @src_boxed: (not nullable): The boxed structure to be copied.
320
 * 
321
 * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type.
322
 * 
323
 * Returns: (transfer full) (not nullable): The newly created copy of the boxed
324
 *    structure.
325
 */
326
gpointer
327
g_boxed_copy (GType         boxed_type,
328
        gconstpointer src_boxed)
329
0
{
330
0
  GTypeValueTable *value_table;
331
0
  gpointer dest_boxed;
332
0
333
0
  g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
334
0
  g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
335
0
  g_return_val_if_fail (src_boxed != NULL, NULL);
336
0
337
0
  value_table = g_type_value_table_peek (boxed_type);
338
0
  if (!value_table)
339
0
    g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
340
0
341
0
  /* check if our proxying implementation is used, we can short-cut here */
342
0
  if (value_table->value_copy == boxed_proxy_value_copy)
343
0
    dest_boxed = _g_type_boxed_copy (boxed_type, (gpointer) src_boxed);
344
0
  else
345
0
    {
346
0
      GValue src_value, dest_value;
347
0
348
0
      /* we heavily rely on third-party boxed type value vtable
349
0
       * implementations to follow normal boxed value storage
350
0
       * (data[0].v_pointer is the boxed struct, and
351
0
       * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
352
0
       * rest zero).
353
0
       * but then, we can expect that since we laid out the
354
0
       * g_boxed_*() API.
355
0
       * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
356
0
       * after a copy.
357
0
       */
358
0
      /* equiv. to g_value_set_static_boxed() */
359
0
      value_meminit (&src_value, boxed_type);
360
0
      src_value.data[0].v_pointer = (gpointer) src_boxed;
361
0
      src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
362
0
363
0
      /* call third-party code copy function, fingers-crossed */
364
0
      value_meminit (&dest_value, boxed_type);
365
0
      value_table->value_copy (&src_value, &dest_value);
366
0
367
0
      /* double check and grouse if things went wrong */
368
0
      if (dest_value.data[1].v_ulong)
369
0
  g_warning ("the copy_value() implementation of type '%s' seems to make use of reserved GValue fields",
370
0
       g_type_name (boxed_type));
371
0
372
0
      dest_boxed = dest_value.data[0].v_pointer;
373
0
    }
374
0
375
0
  return dest_boxed;
376
0
}
377
378
/**
379
 * g_boxed_free:
380
 * @boxed_type: The type of @boxed.
381
 * @boxed: (not nullable): The boxed structure to be freed.
382
 *
383
 * Free the boxed structure @boxed which is of type @boxed_type.
384
 */
385
void
386
g_boxed_free (GType    boxed_type,
387
        gpointer boxed)
388
0
{
389
0
  GTypeValueTable *value_table;
390
0
391
0
  g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
392
0
  g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
393
0
  g_return_if_fail (boxed != NULL);
394
0
395
0
  value_table = g_type_value_table_peek (boxed_type);
396
0
  if (!value_table)
397
0
    g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
398
0
399
0
  /* check if our proxying implementation is used, we can short-cut here */
400
0
  if (value_table->value_free == boxed_proxy_value_free)
401
0
    _g_type_boxed_free (boxed_type, boxed);
402
0
  else
403
0
    {
404
0
      GValue value;
405
0
406
0
      /* see g_boxed_copy() on why we think we can do this */
407
0
      value_meminit (&value, boxed_type);
408
0
      value.data[0].v_pointer = boxed;
409
0
      value_table->value_free (&value);
410
0
    }
411
0
}
412
413
/**
414
 * g_value_get_boxed:
415
 * @value: a valid #GValue of %G_TYPE_BOXED derived type
416
 *
417
 * Get the contents of a %G_TYPE_BOXED derived #GValue.
418
 *
419
 * Returns: (transfer none): boxed contents of @value
420
 */
421
gpointer
422
g_value_get_boxed (const GValue *value)
423
0
{
424
0
  g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
425
0
  g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
426
0
427
0
  return value->data[0].v_pointer;
428
0
}
429
430
/**
431
 * g_value_dup_boxed: (skip)
432
 * @value: a valid #GValue of %G_TYPE_BOXED derived type
433
 *
434
 * Get the contents of a %G_TYPE_BOXED derived #GValue.  Upon getting,
435
 * the boxed value is duplicated and needs to be later freed with
436
 * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value),
437
 * return_value);
438
 *
439
 * Returns: boxed contents of @value
440
 */
441
gpointer
442
g_value_dup_boxed (const GValue *value)
443
0
{
444
0
  g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
445
0
  g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
446
0
447
0
  return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
448
0
}
449
450
static inline void
451
value_set_boxed_internal (GValue       *value,
452
        gconstpointer boxed,
453
        gboolean      need_copy,
454
        gboolean      need_free)
455
0
{
456
0
  if (!boxed)
457
0
    {
458
0
      /* just resetting to NULL might not be desired, need to
459
0
       * have value reinitialized also (for values defaulting
460
0
       * to other default value states than a NULL data pointer),
461
0
       * g_value_reset() will handle this
462
0
       */
463
0
      g_value_reset (value);
464
0
      return;
465
0
    }
466
0
467
0
  if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
468
0
    g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
469
0
  value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
470
0
  value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : (gpointer) boxed;
471
0
}
472
473
/**
474
 * g_value_set_boxed:
475
 * @value: a valid #GValue of %G_TYPE_BOXED derived type
476
 * @v_boxed: (nullable): boxed value to be set
477
 *
478
 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
479
 */
480
void
481
g_value_set_boxed (GValue       *value,
482
       gconstpointer boxed)
483
0
{
484
0
  g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
485
0
  g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
486
0
487
0
  value_set_boxed_internal (value, boxed, TRUE, TRUE);
488
0
}
489
490
/**
491
 * g_value_set_static_boxed:
492
 * @value: a valid #GValue of %G_TYPE_BOXED derived type
493
 * @v_boxed: (nullable): static boxed value to be set
494
 *
495
 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
496
 * The boxed value is assumed to be static, and is thus not duplicated
497
 * when setting the #GValue.
498
 */
499
void
500
g_value_set_static_boxed (GValue       *value,
501
        gconstpointer boxed)
502
0
{
503
0
  g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
504
0
  g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
505
0
506
0
  value_set_boxed_internal (value, boxed, FALSE, FALSE);
507
0
}
508
509
/**
510
 * g_value_set_boxed_take_ownership:
511
 * @value: a valid #GValue of %G_TYPE_BOXED derived type
512
 * @v_boxed: (nullable): duplicated unowned boxed value to be set
513
 *
514
 * This is an internal function introduced mainly for C marshallers.
515
 *
516
 * Deprecated: 2.4: Use g_value_take_boxed() instead.
517
 */
518
void
519
g_value_set_boxed_take_ownership (GValue       *value,
520
          gconstpointer boxed)
521
0
{
522
0
  g_value_take_boxed (value, boxed);
523
0
}
524
525
/**
526
 * g_value_take_boxed:
527
 * @value: a valid #GValue of %G_TYPE_BOXED derived type
528
 * @v_boxed: (nullable): duplicated unowned boxed value to be set
529
 *
530
 * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed
531
 * and takes over the ownership of the callers reference to @v_boxed;
532
 * the caller doesn't have to unref it any more.
533
 *
534
 * Since: 2.4
535
 */
536
void
537
g_value_take_boxed (GValue       *value,
538
        gconstpointer boxed)
539
0
{
540
0
  g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
541
0
  g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
542
0
543
0
  value_set_boxed_internal (value, boxed, FALSE, TRUE);
544
0
}