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