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