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