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