1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and 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 /*
19 * MT safe
20 */
21
22 #include "config.h"
23
24 #include "../glib/gvalgrind.h"
25 #include <string.h>
26
27 #include "gtype.h"
28 #include "gtype-private.h"
29 #include "gtypeplugin.h"
30 #include "gvaluecollector.h"
31 #include "gatomicarray.h"
32 #include "gobject_trace.h"
33
34 #include "glib-private.h"
35 #include "gconstructor.h"
36
37 #ifdef G_OS_WIN32
38 #include <windows.h>
39 #endif
40
41 #ifdef G_ENABLE_DEBUG
42 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
43 #endif
44
45 /**
46 * SECTION:gtype
47 * @short_description: The GLib Runtime type identification and
48 * management system
49 * @title:Type Information
50 *
51 * The GType API is the foundation of the GObject system. It provides the
52 * facilities for registering and managing all fundamental data types,
53 * user-defined object and interface types.
54 *
55 * For type creation and registration purposes, all types fall into one of
56 * two categories: static or dynamic. Static types are never loaded or
57 * unloaded at run-time as dynamic types may be. Static types are created
58 * with g_type_register_static() that gets type specific information passed
59 * in via a #GTypeInfo structure.
60 *
61 * Dynamic types are created with g_type_register_dynamic() which takes a
62 * #GTypePlugin structure instead. The remaining type information (the
63 * #GTypeInfo structure) is retrieved during runtime through #GTypePlugin
64 * and the g_type_plugin_*() API.
65 *
66 * These registration functions are usually called only once from a
67 * function whose only purpose is to return the type identifier for a
68 * specific class. Once the type (or class or interface) is registered,
69 * it may be instantiated, inherited, or implemented depending on exactly
70 * what sort of type it is.
71 *
72 * There is also a third registration function for registering fundamental
73 * types called g_type_register_fundamental() which requires both a #GTypeInfo
74 * structure and a #GTypeFundamentalInfo structure but it is seldom used
75 * since most fundamental types are predefined rather than user-defined.
76 *
77 * Type instance and class structs are limited to a total of 64 KiB,
78 * including all parent types. Similarly, type instances' private data
79 * (as created by G_ADD_PRIVATE()) are limited to a total of
80 * 64 KiB. If a type instance needs a large static buffer, allocate it
81 * separately (typically by using #GArray or #GPtrArray) and put a pointer
82 * to the buffer in the structure.
83 *
84 * As mentioned in the [GType conventions][gtype-conventions], type names must
85 * be at least three characters long. There is no upper length limit. The first
86 * character must be a letter (a–z or A–Z) or an underscore (‘_’). Subsequent
87 * characters can be letters, numbers or any of ‘-_+’.
88 */
89
90
91 /* NOTE: some functions (some internal variants and exported ones)
92 * invalidate data portions of the TypeNodes. if external functions/callbacks
93 * are called, pointers to memory maintained by TypeNodes have to be looked up
94 * again. this affects most of the struct TypeNode fields, e.g. ->children or
95 * CLASSED_NODE_IFACES_ENTRIES() respectively IFACE_NODE_PREREQUISITES() (but
96 * not ->supers[]), as all those memory portions can get realloc()ed during
97 * callback invocation.
98 *
99 * LOCKING:
100 * lock handling issues when calling static functions are indicated by
101 * uppercase letter postfixes, all static functions have to have
102 * one of the below postfixes:
103 * - _I: [Indifferent about locking]
104 * function doesn't care about locks at all
105 * - _U: [Unlocked invocation]
106 * no read or write lock has to be held across function invocation
107 * (locks may be acquired and released during invocation though)
108 * - _L: [Locked invocation]
109 * a write lock or more than 0 read locks have to be held across
110 * function invocation
111 * - _W: [Write-locked invocation]
112 * a write lock has to be held across function invocation
113 * - _Wm: [Write-locked invocation, mutatable]
114 * like _W, but the write lock might be released and reacquired
115 * during invocation, watch your pointers
116 * - _WmREC: [Write-locked invocation, mutatable, recursive]
117 * like _Wm, but also acquires recursive mutex class_init_rec_mutex
118 */
119
120 #ifdef LOCK_DEBUG
121 #define G_READ_LOCK(rw_lock) do { g_printerr (G_STRLOC ": readL++\n"); g_rw_lock_reader_lock (rw_lock); } while (0)
122 #define G_READ_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": readL--\n"); g_rw_lock_reader_unlock (rw_lock); } while (0)
123 #define G_WRITE_LOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL++\n"); g_rw_lock_writer_lock (rw_lock); } while (0)
124 #define G_WRITE_UNLOCK(rw_lock) do { g_printerr (G_STRLOC ": writeL--\n"); g_rw_lock_writer_unlock (rw_lock); } while (0)
125 #else
126 #define G_READ_LOCK(rw_lock) g_rw_lock_reader_lock (rw_lock)
127 #define G_READ_UNLOCK(rw_lock) g_rw_lock_reader_unlock (rw_lock)
128 #define G_WRITE_LOCK(rw_lock) g_rw_lock_writer_lock (rw_lock)
129 #define G_WRITE_UNLOCK(rw_lock) g_rw_lock_writer_unlock (rw_lock)
130 #endif
131 #define INVALID_RECURSION(func, arg, type_name) G_STMT_START{ \
132 static const gchar _action[] = " invalidly modified type "; \
133 gpointer _arg = (gpointer) (arg); const gchar *_tname = (type_name), *_fname = (func); \
134 if (_arg) \
135 g_error ("%s(%p)%s'%s'", _fname, _arg, _action, _tname); \
136 else \
137 g_error ("%s()%s'%s'", _fname, _action, _tname); \
138 }G_STMT_END
139 #define g_assert_type_system_initialized() \
140 g_assert (static_quark_type_flags)
141
142 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
143 G_TYPE_FLAG_INSTANTIATABLE | \
144 G_TYPE_FLAG_DERIVABLE | \
145 G_TYPE_FLAG_DEEP_DERIVABLE)
146 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT)
147 #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \
148 sizeof (gpointer)), \
149 sizeof (glong)))
150
151 /* The 2*sizeof(size_t) alignment here is borrowed from
152 * GNU libc, so it should be good most everywhere.
153 * It is more conservative than is needed on some 64-bit
154 * platforms, but ia64 does require a 16-byte alignment.
155 * The SIMD extensions for x86 and ppc32 would want a
156 * larger alignment than this, but we don't need to
157 * do better than malloc.
158 */
159 #define STRUCT_ALIGNMENT (2 * sizeof (gsize))
160 #define ALIGN_STRUCT(offset) \
161 ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
162
163
164 /* --- typedefs --- */
165 typedef struct _TypeNode TypeNode;
166 typedef struct _CommonData CommonData;
167 typedef struct _BoxedData BoxedData;
168 typedef struct _IFaceData IFaceData;
169 typedef struct _ClassData ClassData;
170 typedef struct _InstanceData InstanceData;
171 typedef union _TypeData TypeData;
172 typedef struct _IFaceEntries IFaceEntries;
173 typedef struct _IFaceEntry IFaceEntry;
174 typedef struct _IFaceHolder IFaceHolder;
175
176
177 /* --- prototypes --- */
178 static inline GTypeFundamentalInfo* type_node_fundamental_info_I (TypeNode *node);
179 static void type_add_flags_W (TypeNode *node,
180 GTypeFlags flags);
181 static void type_data_make_W (TypeNode *node,
182 const GTypeInfo *info,
183 const GTypeValueTable *value_table);
184 static inline void type_data_ref_Wm (TypeNode *node);
185 static inline void type_data_unref_U (TypeNode *node,
186 gboolean uncached);
187 static void type_data_last_unref_Wm (TypeNode * node,
188 gboolean uncached);
189 static inline gpointer type_get_qdata_L (TypeNode *node,
190 GQuark quark);
191 static inline void type_set_qdata_W (TypeNode *node,
192 GQuark quark,
193 gpointer data);
194 static IFaceHolder* type_iface_peek_holder_L (TypeNode *iface,
195 GType instance_type);
196 static gboolean type_iface_vtable_base_init_Wm (TypeNode *iface,
197 TypeNode *node);
198 static void type_iface_vtable_iface_init_Wm (TypeNode *iface,
199 TypeNode *node);
200 static gboolean type_node_is_a_L (TypeNode *node,
201 TypeNode *iface_node);
202
203
204 /* --- enumeration --- */
205
206 /* The InitState enumeration is used to track the progress of initializing
207 * both classes and interface vtables. Keeping the state of initialization
208 * is necessary to handle new interfaces being added while we are initializing
209 * the class or other interfaces.
210 */
211 typedef enum
212 {
213 UNINITIALIZED,
214 BASE_CLASS_INIT,
215 BASE_IFACE_INIT,
216 CLASS_INIT,
217 IFACE_INIT,
218 INITIALIZED
219 } InitState;
220
221 /* --- structures --- */
222 struct _TypeNode
223 {
224 guint volatile ref_count;
225 #ifdef G_ENABLE_DEBUG
226 guint volatile instance_count;
227 #endif
228 GTypePlugin *plugin;
229 guint n_children; /* writable with lock */
230 guint n_supers : 8;
231 guint n_prerequisites : 9;
232 guint is_classed : 1;
233 guint is_instantiatable : 1;
234 guint mutatable_check_cache : 1; /* combines some common path checks */
235 GType *children; /* writable with lock */
236 TypeData * volatile data;
237 GQuark qname;
238 GData *global_gdata;
239 union {
240 GAtomicArray iface_entries; /* for !iface types */
241 GAtomicArray offsets;
242 } _prot;
243 GType *prerequisites;
244 GType supers[1]; /* flexible array */
245 };
246
247 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
248 #define MAX_N_SUPERS (255)
249 #define MAX_N_CHILDREN (G_MAXUINT)
250 #define MAX_N_INTERFACES (255) /* Limited by offsets being 8 bits */
251 #define MAX_N_PREREQUISITES (511)
252 #define NODE_TYPE(node) (node->supers[0])
253 #define NODE_PARENT_TYPE(node) (node->supers[1])
254 #define NODE_FUNDAMENTAL_TYPE(node) (node->supers[node->n_supers])
255 #define NODE_NAME(node) (g_quark_to_string (node->qname))
256 #define NODE_REFCOUNT(node) ((guint) g_atomic_int_get ((int *) &(node)->ref_count))
257 #define NODE_IS_BOXED(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_BOXED)
258 #define NODE_IS_IFACE(node) (NODE_FUNDAMENTAL_TYPE (node) == G_TYPE_INTERFACE)
259 #define CLASSED_NODE_IFACES_ENTRIES(node) (&(node)->_prot.iface_entries)
260 #define CLASSED_NODE_IFACES_ENTRIES_LOCKED(node)(G_ATOMIC_ARRAY_GET_LOCKED(CLASSED_NODE_IFACES_ENTRIES((node)), IFaceEntries))
261 #define IFACE_NODE_N_PREREQUISITES(node) ((node)->n_prerequisites)
262 #define IFACE_NODE_PREREQUISITES(node) ((node)->prerequisites)
263 #define iface_node_get_holders_L(node) ((IFaceHolder*) type_get_qdata_L ((node), static_quark_iface_holder))
264 #define iface_node_set_holders_W(node, holders) (type_set_qdata_W ((node), static_quark_iface_holder, (holders)))
265 #define iface_node_get_dependants_array_L(n) ((GType*) type_get_qdata_L ((n), static_quark_dependants_array))
266 #define iface_node_set_dependants_array_W(n,d) (type_set_qdata_W ((n), static_quark_dependants_array, (d)))
267 #define TYPE_ID_MASK ((GType) ((1 << G_TYPE_FUNDAMENTAL_SHIFT) - 1))
268
269 #define NODE_IS_ANCESTOR(ancestor, node) \
270 ((ancestor)->n_supers <= (node)->n_supers && \
271 (node)->supers[(node)->n_supers - (ancestor)->n_supers] == NODE_TYPE (ancestor))
272
273 struct _IFaceHolder
274 {
275 GType instance_type;
276 GInterfaceInfo *info;
277 GTypePlugin *plugin;
278 IFaceHolder *next;
279 };
280
281 struct _IFaceEntry
282 {
283 GType iface_type;
284 GTypeInterface *vtable;
285 InitState init_state;
286 };
287
288 struct _IFaceEntries {
289 guint offset_index;
290 IFaceEntry entry[1];
291 };
292
293 #define IFACE_ENTRIES_HEADER_SIZE (sizeof(IFaceEntries) - sizeof(IFaceEntry))
294 #define IFACE_ENTRIES_N_ENTRIES(_entries) ( (G_ATOMIC_ARRAY_DATA_SIZE((_entries)) - IFACE_ENTRIES_HEADER_SIZE) / sizeof(IFaceEntry) )
295
296 struct _CommonData
297 {
298 GTypeValueTable *value_table;
299 };
300
301 struct _BoxedData
302 {
303 CommonData data;
304 GBoxedCopyFunc copy_func;
305 GBoxedFreeFunc free_func;
306 };
307
308 struct _IFaceData
309 {
310 CommonData common;
311 guint16 vtable_size;
312 GBaseInitFunc vtable_init_base;
313 GBaseFinalizeFunc vtable_finalize_base;
314 GClassInitFunc dflt_init;
315 GClassFinalizeFunc dflt_finalize;
316 gconstpointer dflt_data;
317 gpointer dflt_vtable;
318 };
319
320 struct _ClassData
321 {
322 CommonData common;
323 guint16 class_size;
324 guint16 class_private_size;
325 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
326 GBaseInitFunc class_init_base;
327 GBaseFinalizeFunc class_finalize_base;
328 GClassInitFunc class_init;
329 GClassFinalizeFunc class_finalize;
330 gconstpointer class_data;
331 gpointer class;
332 };
333
334 struct _InstanceData
335 {
336 CommonData common;
337 guint16 class_size;
338 guint16 class_private_size;
339 int volatile init_state; /* atomic - g_type_class_ref reads it unlocked */
340 GBaseInitFunc class_init_base;
341 GBaseFinalizeFunc class_finalize_base;
342 GClassInitFunc class_init;
343 GClassFinalizeFunc class_finalize;
344 gconstpointer class_data;
345 gpointer class;
346 guint16 instance_size;
347 guint16 private_size;
348 guint16 n_preallocs;
349 GInstanceInitFunc instance_init;
350 };
351
352 union _TypeData
353 {
354 CommonData common;
355 BoxedData boxed;
356 IFaceData iface;
357 ClassData class;
358 InstanceData instance;
359 };
360
361 typedef struct {
362 gpointer cache_data;
363 GTypeClassCacheFunc cache_func;
364 } ClassCacheFunc;
365
366 typedef struct {
367 gpointer check_data;
368 GTypeInterfaceCheckFunc check_func;
369 } IFaceCheckFunc;
370
371
372 /* --- variables --- */
373 static GRWLock type_rw_lock;
374 static GRecMutex class_init_rec_mutex;
375 static guint static_n_class_cache_funcs = 0;
376 static ClassCacheFunc *static_class_cache_funcs = NULL;
377 static guint static_n_iface_check_funcs = 0;
378 static IFaceCheckFunc *static_iface_check_funcs = NULL;
379 static GQuark static_quark_type_flags = 0;
380 static GQuark static_quark_iface_holder = 0;
381 static GQuark static_quark_dependants_array = 0;
382 static guint type_registration_serial = 0;
383
384 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
385 GTypeDebugFlags _g_type_debug_flags = 0;
386 G_GNUC_END_IGNORE_DEPRECATIONS
387
388 /* --- type nodes --- */
389 static GHashTable *static_type_nodes_ht = NULL;
390 static TypeNode *static_fundamental_type_nodes[(G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT) + 1] = { NULL, };
391 static GType static_fundamental_next = G_TYPE_RESERVED_USER_FIRST;
392
393 static inline TypeNode*
lookup_type_node_I(GType utype)394 lookup_type_node_I (GType utype)
395 {
396 if (utype > G_TYPE_FUNDAMENTAL_MAX)
397 return (TypeNode*) (utype & ~TYPE_ID_MASK);
398 else
399 return static_fundamental_type_nodes[utype >> G_TYPE_FUNDAMENTAL_SHIFT];
400 }
401
402 /**
403 * g_type_get_type_registration_serial:
404 *
405 * Returns an opaque serial number that represents the state of the set
406 * of registered types. Any time a type is registered this serial changes,
407 * which means you can cache information based on type lookups (such as
408 * g_type_from_name()) and know if the cache is still valid at a later
409 * time by comparing the current serial with the one at the type lookup.
410 *
411 * Since: 2.36
412 *
413 * Returns: An unsigned int, representing the state of type registrations
414 */
415 guint
g_type_get_type_registration_serial(void)416 g_type_get_type_registration_serial (void)
417 {
418 return (guint)g_atomic_int_get ((gint *)&type_registration_serial);
419 }
420
421 static TypeNode*
type_node_any_new_W(TypeNode * pnode,GType ftype,const gchar * name,GTypePlugin * plugin,GTypeFundamentalFlags type_flags)422 type_node_any_new_W (TypeNode *pnode,
423 GType ftype,
424 const gchar *name,
425 GTypePlugin *plugin,
426 GTypeFundamentalFlags type_flags)
427 {
428 guint n_supers;
429 GType type;
430 TypeNode *node;
431 guint i, node_size = 0;
432
433 n_supers = pnode ? pnode->n_supers + 1 : 0;
434
435 if (!pnode)
436 node_size += SIZEOF_FUNDAMENTAL_INFO; /* fundamental type info */
437 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
438 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + (0) for ->supers[] */
439 node = g_malloc0 (node_size);
440 if (!pnode) /* offset fundamental types */
441 {
442 node = G_STRUCT_MEMBER_P (node, SIZEOF_FUNDAMENTAL_INFO);
443 static_fundamental_type_nodes[ftype >> G_TYPE_FUNDAMENTAL_SHIFT] = node;
444 type = ftype;
445 }
446 else
447 type = (GType) node;
448
449 g_assert ((type & TYPE_ID_MASK) == 0);
450
451 node->n_supers = n_supers;
452 if (!pnode)
453 {
454 node->supers[0] = type;
455 node->supers[1] = 0;
456
457 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
458 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
459
460 if (NODE_IS_IFACE (node))
461 {
462 IFACE_NODE_N_PREREQUISITES (node) = 0;
463 IFACE_NODE_PREREQUISITES (node) = NULL;
464 }
465 else
466 _g_atomic_array_init (CLASSED_NODE_IFACES_ENTRIES (node));
467 }
468 else
469 {
470 node->supers[0] = type;
471 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
472
473 node->is_classed = pnode->is_classed;
474 node->is_instantiatable = pnode->is_instantiatable;
475
476 if (NODE_IS_IFACE (node))
477 {
478 IFACE_NODE_N_PREREQUISITES (node) = 0;
479 IFACE_NODE_PREREQUISITES (node) = NULL;
480 }
481 else
482 {
483 guint j;
484 IFaceEntries *entries;
485
486 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (pnode),
487 IFACE_ENTRIES_HEADER_SIZE,
488 0);
489 if (entries)
490 {
491 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
492 {
493 entries->entry[j].vtable = NULL;
494 entries->entry[j].init_state = UNINITIALIZED;
495 }
496 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node),
497 entries);
498 }
499 }
500
501 i = pnode->n_children++;
502 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
503 pnode->children[i] = type;
504 }
505
506 TRACE(GOBJECT_TYPE_NEW(name, node->supers[1], type));
507
508 node->plugin = plugin;
509 node->n_children = 0;
510 node->children = NULL;
511 node->data = NULL;
512 node->qname = g_quark_from_string (name);
513 node->global_gdata = NULL;
514 g_hash_table_insert (static_type_nodes_ht,
515 (gpointer) g_quark_to_string (node->qname),
516 (gpointer) type);
517
518 g_atomic_int_inc ((gint *)&type_registration_serial);
519
520 return node;
521 }
522
523 static inline GTypeFundamentalInfo*
type_node_fundamental_info_I(TypeNode * node)524 type_node_fundamental_info_I (TypeNode *node)
525 {
526 GType ftype = NODE_FUNDAMENTAL_TYPE (node);
527
528 if (ftype != NODE_TYPE (node))
529 node = lookup_type_node_I (ftype);
530
531 return node ? G_STRUCT_MEMBER_P (node, -SIZEOF_FUNDAMENTAL_INFO) : NULL;
532 }
533
534 static TypeNode*
type_node_fundamental_new_W(GType ftype,const gchar * name,GTypeFundamentalFlags type_flags)535 type_node_fundamental_new_W (GType ftype,
536 const gchar *name,
537 GTypeFundamentalFlags type_flags)
538 {
539 GTypeFundamentalInfo *finfo;
540 TypeNode *node;
541
542 g_assert ((ftype & TYPE_ID_MASK) == 0);
543 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX);
544
545 if (ftype >> G_TYPE_FUNDAMENTAL_SHIFT == static_fundamental_next)
546 static_fundamental_next++;
547
548 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
549
550 node = type_node_any_new_W (NULL, ftype, name, NULL, type_flags);
551
552 finfo = type_node_fundamental_info_I (node);
553 finfo->type_flags = type_flags;
554
555 return node;
556 }
557
558 static TypeNode*
type_node_new_W(TypeNode * pnode,const gchar * name,GTypePlugin * plugin)559 type_node_new_W (TypeNode *pnode,
560 const gchar *name,
561 GTypePlugin *plugin)
562
563 {
564 g_assert (pnode);
565 g_assert (pnode->n_supers < MAX_N_SUPERS);
566 g_assert (pnode->n_children < MAX_N_CHILDREN);
567
568 return type_node_any_new_W (pnode, NODE_FUNDAMENTAL_TYPE (pnode), name, plugin, 0);
569 }
570
571 static inline IFaceEntry*
lookup_iface_entry_I(volatile IFaceEntries * entries,TypeNode * iface_node)572 lookup_iface_entry_I (volatile IFaceEntries *entries,
573 TypeNode *iface_node)
574 {
575 guint8 *offsets;
576 guint offset_index;
577 IFaceEntry *check;
578 int index;
579 IFaceEntry *entry;
580
581 if (entries == NULL)
582 return NULL;
583
584 G_ATOMIC_ARRAY_DO_TRANSACTION
585 (&iface_node->_prot.offsets, guint8,
586
587 entry = NULL;
588 offsets = transaction_data;
589 offset_index = entries->offset_index;
590 if (offsets != NULL &&
591 offset_index < G_ATOMIC_ARRAY_DATA_SIZE(offsets))
592 {
593 index = offsets[offset_index];
594 if (index > 0)
595 {
596 /* zero means unset, subtract one to get real index */
597 index -= 1;
598
599 if (index < IFACE_ENTRIES_N_ENTRIES (entries))
600 {
601 check = (IFaceEntry *)&entries->entry[index];
602 if (check->iface_type == NODE_TYPE (iface_node))
603 entry = check;
604 }
605 }
606 }
607 );
608
609 return entry;
610 }
611
612 static inline IFaceEntry*
type_lookup_iface_entry_L(TypeNode * node,TypeNode * iface_node)613 type_lookup_iface_entry_L (TypeNode *node,
614 TypeNode *iface_node)
615 {
616 if (!NODE_IS_IFACE (iface_node))
617 return NULL;
618
619 return lookup_iface_entry_I (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node),
620 iface_node);
621 }
622
623
624 static inline gboolean
type_lookup_iface_vtable_I(TypeNode * node,TypeNode * iface_node,gpointer * vtable_ptr)625 type_lookup_iface_vtable_I (TypeNode *node,
626 TypeNode *iface_node,
627 gpointer *vtable_ptr)
628 {
629 IFaceEntry *entry;
630 gboolean res;
631
632 if (!NODE_IS_IFACE (iface_node))
633 {
634 if (vtable_ptr)
635 *vtable_ptr = NULL;
636 return FALSE;
637 }
638
639 G_ATOMIC_ARRAY_DO_TRANSACTION
640 (CLASSED_NODE_IFACES_ENTRIES (node), IFaceEntries,
641
642 entry = lookup_iface_entry_I (transaction_data, iface_node);
643 res = entry != NULL;
644 if (vtable_ptr)
645 {
646 if (entry)
647 *vtable_ptr = entry->vtable;
648 else
649 *vtable_ptr = NULL;
650 }
651 );
652
653 return res;
654 }
655
656 static inline gboolean
type_lookup_prerequisite_L(TypeNode * iface,GType prerequisite_type)657 type_lookup_prerequisite_L (TypeNode *iface,
658 GType prerequisite_type)
659 {
660 if (NODE_IS_IFACE (iface) && IFACE_NODE_N_PREREQUISITES (iface))
661 {
662 GType *prerequisites = IFACE_NODE_PREREQUISITES (iface) - 1;
663 guint n_prerequisites = IFACE_NODE_N_PREREQUISITES (iface);
664
665 do
666 {
667 guint i;
668 GType *check;
669
670 i = (n_prerequisites + 1) >> 1;
671 check = prerequisites + i;
672 if (prerequisite_type == *check)
673 return TRUE;
674 else if (prerequisite_type > *check)
675 {
676 n_prerequisites -= i;
677 prerequisites = check;
678 }
679 else /* if (prerequisite_type < *check) */
680 n_prerequisites = i - 1;
681 }
682 while (n_prerequisites);
683 }
684 return FALSE;
685 }
686
687 static const gchar*
type_descriptive_name_I(GType type)688 type_descriptive_name_I (GType type)
689 {
690 if (type)
691 {
692 TypeNode *node = lookup_type_node_I (type);
693
694 return node ? NODE_NAME (node) : "<unknown>";
695 }
696 else
697 return "<invalid>";
698 }
699
700
701 /* --- type consistency checks --- */
702 static gboolean
check_plugin_U(GTypePlugin * plugin,gboolean need_complete_type_info,gboolean need_complete_interface_info,const gchar * type_name)703 check_plugin_U (GTypePlugin *plugin,
704 gboolean need_complete_type_info,
705 gboolean need_complete_interface_info,
706 const gchar *type_name)
707 {
708 /* G_IS_TYPE_PLUGIN() and G_TYPE_PLUGIN_GET_CLASS() are external calls: _U
709 */
710 if (!plugin)
711 {
712 g_warning ("plugin handle for type '%s' is NULL",
713 type_name);
714 return FALSE;
715 }
716 if (!G_IS_TYPE_PLUGIN (plugin))
717 {
718 g_warning ("plugin pointer (%p) for type '%s' is invalid",
719 plugin, type_name);
720 return FALSE;
721 }
722 if (need_complete_type_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_type_info)
723 {
724 g_warning ("plugin for type '%s' has no complete_type_info() implementation",
725 type_name);
726 return FALSE;
727 }
728 if (need_complete_interface_info && !G_TYPE_PLUGIN_GET_CLASS (plugin)->complete_interface_info)
729 {
730 g_warning ("plugin for type '%s' has no complete_interface_info() implementation",
731 type_name);
732 return FALSE;
733 }
734 return TRUE;
735 }
736
737 static gboolean
check_type_name_I(const gchar * type_name)738 check_type_name_I (const gchar *type_name)
739 {
740 static const gchar extra_chars[] = "-_+";
741 const gchar *p = type_name;
742 gboolean name_valid;
743
744 if (!type_name[0] || !type_name[1] || !type_name[2])
745 {
746 g_warning ("type name '%s' is too short", type_name);
747 return FALSE;
748 }
749 /* check the first letter */
750 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
751 for (p = type_name + 1; *p; p++)
752 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
753 (p[0] >= 'a' && p[0] <= 'z') ||
754 (p[0] >= '0' && p[0] <= '9') ||
755 strchr (extra_chars, p[0]));
756 if (!name_valid)
757 {
758 g_warning ("type name '%s' contains invalid characters", type_name);
759 return FALSE;
760 }
761 if (g_type_from_name (type_name))
762 {
763 g_warning ("cannot register existing type '%s'", type_name);
764 return FALSE;
765 }
766
767 return TRUE;
768 }
769
770 static gboolean
check_derivation_I(GType parent_type,const gchar * type_name)771 check_derivation_I (GType parent_type,
772 const gchar *type_name)
773 {
774 TypeNode *pnode;
775 GTypeFundamentalInfo* finfo;
776
777 pnode = lookup_type_node_I (parent_type);
778 if (!pnode)
779 {
780 g_warning ("cannot derive type '%s' from invalid parent type '%s'",
781 type_name,
782 type_descriptive_name_I (parent_type));
783 return FALSE;
784 }
785 finfo = type_node_fundamental_info_I (pnode);
786 /* ensure flat derivability */
787 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
788 {
789 g_warning ("cannot derive '%s' from non-derivable parent type '%s'",
790 type_name,
791 NODE_NAME (pnode));
792 return FALSE;
793 }
794 /* ensure deep derivability */
795 if (parent_type != NODE_FUNDAMENTAL_TYPE (pnode) &&
796 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
797 {
798 g_warning ("cannot derive '%s' from non-fundamental parent type '%s'",
799 type_name,
800 NODE_NAME (pnode));
801 return FALSE;
802 }
803
804 return TRUE;
805 }
806
807 static gboolean
check_collect_format_I(const gchar * collect_format)808 check_collect_format_I (const gchar *collect_format)
809 {
810 const gchar *p = collect_format;
811 gchar valid_format[] = { G_VALUE_COLLECT_INT, G_VALUE_COLLECT_LONG,
812 G_VALUE_COLLECT_INT64, G_VALUE_COLLECT_DOUBLE,
813 G_VALUE_COLLECT_POINTER, 0 };
814
815 while (*p)
816 if (!strchr (valid_format, *p++))
817 return FALSE;
818 return p - collect_format <= G_VALUE_COLLECT_FORMAT_MAX_LENGTH;
819 }
820
821 static gboolean
check_value_table_I(const gchar * type_name,const GTypeValueTable * value_table)822 check_value_table_I (const gchar *type_name,
823 const GTypeValueTable *value_table)
824 {
825 if (!value_table)
826 return FALSE;
827 else if (value_table->value_init == NULL)
828 {
829 if (value_table->value_free || value_table->value_copy ||
830 value_table->value_peek_pointer ||
831 value_table->collect_format || value_table->collect_value ||
832 value_table->lcopy_format || value_table->lcopy_value)
833 g_warning ("cannot handle uninitializable values of type '%s'",
834 type_name);
835 return FALSE;
836 }
837 else /* value_table->value_init != NULL */
838 {
839 if (!value_table->value_free)
840 {
841 /* +++ optional +++
842 * g_warning ("missing 'value_free()' for type '%s'", type_name);
843 * return FALSE;
844 */
845 }
846 if (!value_table->value_copy)
847 {
848 g_warning ("missing 'value_copy()' for type '%s'", type_name);
849 return FALSE;
850 }
851 if ((value_table->collect_format || value_table->collect_value) &&
852 (!value_table->collect_format || !value_table->collect_value))
853 {
854 g_warning ("one of 'collect_format' and 'collect_value()' is unspecified for type '%s'",
855 type_name);
856 return FALSE;
857 }
858 if (value_table->collect_format && !check_collect_format_I (value_table->collect_format))
859 {
860 g_warning ("the '%s' specification for type '%s' is too long or invalid",
861 "collect_format",
862 type_name);
863 return FALSE;
864 }
865 if ((value_table->lcopy_format || value_table->lcopy_value) &&
866 (!value_table->lcopy_format || !value_table->lcopy_value))
867 {
868 g_warning ("one of 'lcopy_format' and 'lcopy_value()' is unspecified for type '%s'",
869 type_name);
870 return FALSE;
871 }
872 if (value_table->lcopy_format && !check_collect_format_I (value_table->lcopy_format))
873 {
874 g_warning ("the '%s' specification for type '%s' is too long or invalid",
875 "lcopy_format",
876 type_name);
877 return FALSE;
878 }
879 }
880 return TRUE;
881 }
882
883 static gboolean
check_type_info_I(TypeNode * pnode,GType ftype,const gchar * type_name,const GTypeInfo * info)884 check_type_info_I (TypeNode *pnode,
885 GType ftype,
886 const gchar *type_name,
887 const GTypeInfo *info)
888 {
889 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (lookup_type_node_I (ftype));
890 gboolean is_interface = ftype == G_TYPE_INTERFACE;
891
892 g_assert (ftype <= G_TYPE_FUNDAMENTAL_MAX && !(ftype & TYPE_ID_MASK));
893
894 /* check instance members */
895 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
896 (info->instance_size || info->n_preallocs || info->instance_init))
897 {
898 if (pnode)
899 g_warning ("cannot instantiate '%s', derived from non-instantiatable parent type '%s'",
900 type_name,
901 NODE_NAME (pnode));
902 else
903 g_warning ("cannot instantiate '%s' as non-instantiatable fundamental",
904 type_name);
905 return FALSE;
906 }
907 /* check class & interface members */
908 if (!((finfo->type_flags & G_TYPE_FLAG_CLASSED) || is_interface) &&
909 (info->class_init || info->class_finalize || info->class_data ||
910 info->class_size || info->base_init || info->base_finalize))
911 {
912 if (pnode)
913 g_warning ("cannot create class for '%s', derived from non-classed parent type '%s'",
914 type_name,
915 NODE_NAME (pnode));
916 else
917 g_warning ("cannot create class for '%s' as non-classed fundamental",
918 type_name);
919 return FALSE;
920 }
921 /* check interface size */
922 if (is_interface && info->class_size < sizeof (GTypeInterface))
923 {
924 g_warning ("specified interface size for type '%s' is smaller than 'GTypeInterface' size",
925 type_name);
926 return FALSE;
927 }
928 /* check class size */
929 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
930 {
931 if (info->class_size < sizeof (GTypeClass))
932 {
933 g_warning ("specified class size for type '%s' is smaller than 'GTypeClass' size",
934 type_name);
935 return FALSE;
936 }
937 if (pnode && info->class_size < pnode->data->class.class_size)
938 {
939 g_warning ("specified class size for type '%s' is smaller "
940 "than the parent type's '%s' class size",
941 type_name,
942 NODE_NAME (pnode));
943 return FALSE;
944 }
945 }
946 /* check instance size */
947 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
948 {
949 if (info->instance_size < sizeof (GTypeInstance))
950 {
951 g_warning ("specified instance size for type '%s' is smaller than 'GTypeInstance' size",
952 type_name);
953 return FALSE;
954 }
955 if (pnode && info->instance_size < pnode->data->instance.instance_size)
956 {
957 g_warning ("specified instance size for type '%s' is smaller "
958 "than the parent type's '%s' instance size",
959 type_name,
960 NODE_NAME (pnode));
961 return FALSE;
962 }
963 }
964
965 return TRUE;
966 }
967
968 static TypeNode*
find_conforming_child_type_L(TypeNode * pnode,TypeNode * iface)969 find_conforming_child_type_L (TypeNode *pnode,
970 TypeNode *iface)
971 {
972 TypeNode *node = NULL;
973 guint i;
974
975 if (type_lookup_iface_entry_L (pnode, iface))
976 return pnode;
977
978 for (i = 0; i < pnode->n_children && !node; i++)
979 node = find_conforming_child_type_L (lookup_type_node_I (pnode->children[i]), iface);
980
981 return node;
982 }
983
984 static gboolean
check_add_interface_L(GType instance_type,GType iface_type)985 check_add_interface_L (GType instance_type,
986 GType iface_type)
987 {
988 TypeNode *node = lookup_type_node_I (instance_type);
989 TypeNode *iface = lookup_type_node_I (iface_type);
990 IFaceEntry *entry;
991 TypeNode *tnode;
992 GType *prerequisites;
993 guint i;
994
995
996 if (!node || !node->is_instantiatable)
997 {
998 g_warning ("cannot add interfaces to invalid (non-instantiatable) type '%s'",
999 type_descriptive_name_I (instance_type));
1000 return FALSE;
1001 }
1002 if (!iface || !NODE_IS_IFACE (iface))
1003 {
1004 g_warning ("cannot add invalid (non-interface) type '%s' to type '%s'",
1005 type_descriptive_name_I (iface_type),
1006 NODE_NAME (node));
1007 return FALSE;
1008 }
1009 if (node->data && node->data->class.class)
1010 {
1011 g_warning ("attempting to add an interface (%s) to class (%s) after class_init",
1012 NODE_NAME (iface), NODE_NAME (node));
1013 return FALSE;
1014 }
1015 tnode = lookup_type_node_I (NODE_PARENT_TYPE (iface));
1016 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry_L (node, tnode))
1017 {
1018 /* 2001/7/31:timj: erk, i guess this warning is junk as interface derivation is flat */
1019 g_warning ("cannot add sub-interface '%s' to type '%s' which does not conform to super-interface '%s'",
1020 NODE_NAME (iface),
1021 NODE_NAME (node),
1022 NODE_NAME (tnode));
1023 return FALSE;
1024 }
1025 /* allow overriding of interface type introduced for parent type */
1026 entry = type_lookup_iface_entry_L (node, iface);
1027 if (entry && entry->vtable == NULL && !type_iface_peek_holder_L (iface, NODE_TYPE (node)))
1028 {
1029 /* ok, we do conform to this interface already, but the interface vtable was not
1030 * yet intialized, and we just conform to the interface because it got added to
1031 * one of our parents. so we allow overriding of holder info here.
1032 */
1033 return TRUE;
1034 }
1035 /* check whether one of our children already conforms (or whether the interface
1036 * got added to this node already)
1037 */
1038 tnode = find_conforming_child_type_L (node, iface); /* tnode is_a node */
1039 if (tnode)
1040 {
1041 g_warning ("cannot add interface type '%s' to type '%s', since type '%s' already conforms to interface",
1042 NODE_NAME (iface),
1043 NODE_NAME (node),
1044 NODE_NAME (tnode));
1045 return FALSE;
1046 }
1047 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1048 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1049 {
1050 tnode = lookup_type_node_I (prerequisites[i]);
1051 if (!type_node_is_a_L (node, tnode))
1052 {
1053 g_warning ("cannot add interface type '%s' to type '%s' which does not conform to prerequisite '%s'",
1054 NODE_NAME (iface),
1055 NODE_NAME (node),
1056 NODE_NAME (tnode));
1057 return FALSE;
1058 }
1059 }
1060 return TRUE;
1061 }
1062
1063 static gboolean
check_interface_info_I(TypeNode * iface,GType instance_type,const GInterfaceInfo * info)1064 check_interface_info_I (TypeNode *iface,
1065 GType instance_type,
1066 const GInterfaceInfo *info)
1067 {
1068 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
1069 {
1070 g_warning ("interface type '%s' for type '%s' comes without initializer",
1071 NODE_NAME (iface),
1072 type_descriptive_name_I (instance_type));
1073 return FALSE;
1074 }
1075
1076 return TRUE;
1077 }
1078
1079 /* --- type info (type node data) --- */
1080 static void
type_data_make_W(TypeNode * node,const GTypeInfo * info,const GTypeValueTable * value_table)1081 type_data_make_W (TypeNode *node,
1082 const GTypeInfo *info,
1083 const GTypeValueTable *value_table)
1084 {
1085 TypeData *data;
1086 GTypeValueTable *vtable = NULL;
1087 guint vtable_size = 0;
1088
1089 g_assert (node->data == NULL && info != NULL);
1090
1091 if (!value_table)
1092 {
1093 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1094
1095 if (pnode)
1096 vtable = pnode->data->common.value_table;
1097 else
1098 {
1099 static const GTypeValueTable zero_vtable = { NULL, };
1100
1101 value_table = &zero_vtable;
1102 }
1103 }
1104 if (value_table)
1105 {
1106 /* need to setup vtable_size since we have to allocate it with data in one chunk */
1107 vtable_size = sizeof (GTypeValueTable);
1108 if (value_table->collect_format)
1109 vtable_size += strlen (value_table->collect_format);
1110 if (value_table->lcopy_format)
1111 vtable_size += strlen (value_table->lcopy_format);
1112 vtable_size += 2;
1113 }
1114
1115 if (node->is_instantiatable) /* careful, is_instantiatable is also is_classed */
1116 {
1117 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1118
1119 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
1120 if (vtable_size)
1121 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
1122 data->instance.class_size = info->class_size;
1123 data->instance.class_init_base = info->base_init;
1124 data->instance.class_finalize_base = info->base_finalize;
1125 data->instance.class_init = info->class_init;
1126 data->instance.class_finalize = info->class_finalize;
1127 data->instance.class_data = info->class_data;
1128 data->instance.class = NULL;
1129 data->instance.init_state = UNINITIALIZED;
1130 data->instance.instance_size = info->instance_size;
1131 /* We'll set the final value for data->instance.private size
1132 * after the parent class has been initialized
1133 */
1134 data->instance.private_size = 0;
1135 data->instance.class_private_size = 0;
1136 if (pnode)
1137 data->instance.class_private_size = pnode->data->instance.class_private_size;
1138 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
1139 data->instance.instance_init = info->instance_init;
1140 }
1141 else if (node->is_classed) /* only classed */
1142 {
1143 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1144
1145 data = g_malloc0 (sizeof (ClassData) + vtable_size);
1146 if (vtable_size)
1147 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
1148 data->class.class_size = info->class_size;
1149 data->class.class_init_base = info->base_init;
1150 data->class.class_finalize_base = info->base_finalize;
1151 data->class.class_init = info->class_init;
1152 data->class.class_finalize = info->class_finalize;
1153 data->class.class_data = info->class_data;
1154 data->class.class = NULL;
1155 data->class.class_private_size = 0;
1156 if (pnode)
1157 data->class.class_private_size = pnode->data->class.class_private_size;
1158 data->class.init_state = UNINITIALIZED;
1159 }
1160 else if (NODE_IS_IFACE (node))
1161 {
1162 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
1163 if (vtable_size)
1164 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
1165 data->iface.vtable_size = info->class_size;
1166 data->iface.vtable_init_base = info->base_init;
1167 data->iface.vtable_finalize_base = info->base_finalize;
1168 data->iface.dflt_init = info->class_init;
1169 data->iface.dflt_finalize = info->class_finalize;
1170 data->iface.dflt_data = info->class_data;
1171 data->iface.dflt_vtable = NULL;
1172 }
1173 else if (NODE_IS_BOXED (node))
1174 {
1175 data = g_malloc0 (sizeof (BoxedData) + vtable_size);
1176 if (vtable_size)
1177 vtable = G_STRUCT_MEMBER_P (data, sizeof (BoxedData));
1178 }
1179 else
1180 {
1181 data = g_malloc0 (sizeof (CommonData) + vtable_size);
1182 if (vtable_size)
1183 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
1184 }
1185
1186 node->data = data;
1187
1188 if (vtable_size)
1189 {
1190 gchar *p;
1191
1192 /* we allocate the vtable and its strings together with the type data, so
1193 * children can take over their parent's vtable pointer, and we don't
1194 * need to worry freeing it or not when the child data is destroyed
1195 */
1196 *vtable = *value_table;
1197 p = G_STRUCT_MEMBER_P (vtable, sizeof (*vtable));
1198 p[0] = 0;
1199 vtable->collect_format = p;
1200 if (value_table->collect_format)
1201 {
1202 strcat (p, value_table->collect_format);
1203 p += strlen (value_table->collect_format);
1204 }
1205 p++;
1206 p[0] = 0;
1207 vtable->lcopy_format = p;
1208 if (value_table->lcopy_format)
1209 strcat (p, value_table->lcopy_format);
1210 }
1211 node->data->common.value_table = vtable;
1212 node->mutatable_check_cache = (node->data->common.value_table->value_init != NULL &&
1213 !((G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_ABSTRACT) &
1214 GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))));
1215
1216 g_assert (node->data->common.value_table != NULL); /* paranoid */
1217
1218 g_atomic_int_set ((int *) &node->ref_count, 1);
1219 }
1220
1221 static inline void
type_data_ref_Wm(TypeNode * node)1222 type_data_ref_Wm (TypeNode *node)
1223 {
1224 if (!node->data)
1225 {
1226 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
1227 GTypeInfo tmp_info;
1228 GTypeValueTable tmp_value_table;
1229
1230 g_assert (node->plugin != NULL);
1231
1232 if (pnode)
1233 {
1234 type_data_ref_Wm (pnode);
1235 if (node->data)
1236 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1237 }
1238
1239 memset (&tmp_info, 0, sizeof (tmp_info));
1240 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
1241
1242 G_WRITE_UNLOCK (&type_rw_lock);
1243 g_type_plugin_use (node->plugin);
1244 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
1245 G_WRITE_LOCK (&type_rw_lock);
1246 if (node->data)
1247 INVALID_RECURSION ("g_type_plugin_*", node->plugin, NODE_NAME (node));
1248
1249 check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (node), NODE_NAME (node), &tmp_info);
1250 type_data_make_W (node, &tmp_info,
1251 check_value_table_I (NODE_NAME (node),
1252 &tmp_value_table) ? &tmp_value_table : NULL);
1253 }
1254 else
1255 {
1256 g_assert (NODE_REFCOUNT (node) > 0);
1257
1258 g_atomic_int_inc ((int *) &node->ref_count);
1259 }
1260 }
1261
1262 static inline gboolean
type_data_ref_U(TypeNode * node)1263 type_data_ref_U (TypeNode *node)
1264 {
1265 guint current;
1266
1267 do {
1268 current = NODE_REFCOUNT (node);
1269
1270 if (current < 1)
1271 return FALSE;
1272 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current + 1));
1273
1274 return TRUE;
1275 }
1276
1277 static gboolean
iface_node_has_available_offset_L(TypeNode * iface_node,int offset,int for_index)1278 iface_node_has_available_offset_L (TypeNode *iface_node,
1279 int offset,
1280 int for_index)
1281 {
1282 guint8 *offsets;
1283
1284 offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1285 if (offsets == NULL)
1286 return TRUE;
1287
1288 if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
1289 return TRUE;
1290
1291 if (offsets[offset] == 0 ||
1292 offsets[offset] == for_index+1)
1293 return TRUE;
1294
1295 return FALSE;
1296 }
1297
1298 static int
find_free_iface_offset_L(IFaceEntries * entries)1299 find_free_iface_offset_L (IFaceEntries *entries)
1300 {
1301 IFaceEntry *entry;
1302 TypeNode *iface_node;
1303 int offset;
1304 int i;
1305 int n_entries;
1306
1307 n_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1308 offset = -1;
1309 do
1310 {
1311 offset++;
1312 for (i = 0; i < n_entries; i++)
1313 {
1314 entry = &entries->entry[i];
1315 iface_node = lookup_type_node_I (entry->iface_type);
1316
1317 if (!iface_node_has_available_offset_L (iface_node, offset, i))
1318 break;
1319 }
1320 }
1321 while (i != n_entries);
1322
1323 return offset;
1324 }
1325
1326 static void
iface_node_set_offset_L(TypeNode * iface_node,int offset,int index)1327 iface_node_set_offset_L (TypeNode *iface_node,
1328 int offset,
1329 int index)
1330 {
1331 guint8 *offsets, *old_offsets;
1332 int new_size, old_size;
1333 int i;
1334
1335 old_offsets = G_ATOMIC_ARRAY_GET_LOCKED (&iface_node->_prot.offsets, guint8);
1336 if (old_offsets == NULL)
1337 old_size = 0;
1338 else
1339 {
1340 old_size = G_ATOMIC_ARRAY_DATA_SIZE (old_offsets);
1341 if (offset < old_size &&
1342 old_offsets[offset] == index + 1)
1343 return; /* Already set to this index, return */
1344 }
1345 new_size = MAX (old_size, offset + 1);
1346
1347 offsets = _g_atomic_array_copy (&iface_node->_prot.offsets,
1348 0, new_size - old_size);
1349
1350 /* Mark new area as unused */
1351 for (i = old_size; i < new_size; i++)
1352 offsets[i] = 0;
1353
1354 offsets[offset] = index + 1;
1355
1356 _g_atomic_array_update (&iface_node->_prot.offsets, offsets);
1357 }
1358
1359 static void
type_node_add_iface_entry_W(TypeNode * node,GType iface_type,IFaceEntry * parent_entry)1360 type_node_add_iface_entry_W (TypeNode *node,
1361 GType iface_type,
1362 IFaceEntry *parent_entry)
1363 {
1364 IFaceEntries *entries;
1365 IFaceEntry *entry;
1366 TypeNode *iface_node;
1367 guint i, j;
1368 int num_entries;
1369
1370 g_assert (node->is_instantiatable);
1371
1372 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
1373 if (entries != NULL)
1374 {
1375 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1376
1377 g_assert (num_entries < MAX_N_INTERFACES);
1378
1379 for (i = 0; i < num_entries; i++)
1380 {
1381 entry = &entries->entry[i];
1382 if (entry->iface_type == iface_type)
1383 {
1384 /* this can happen in two cases:
1385 * - our parent type already conformed to iface_type and node
1386 * got its own holder info. here, our children already have
1387 * entries and NULL vtables, since this will only work for
1388 * uninitialized classes.
1389 * - an interface type is added to an ancestor after it was
1390 * added to a child type.
1391 */
1392 if (!parent_entry)
1393 g_assert (entry->vtable == NULL && entry->init_state == UNINITIALIZED);
1394 else
1395 {
1396 /* sick, interface is added to ancestor *after* child type;
1397 * nothing todo, the entry and our children were already setup correctly
1398 */
1399 }
1400 return;
1401 }
1402 }
1403 }
1404
1405 entries = _g_atomic_array_copy (CLASSED_NODE_IFACES_ENTRIES (node),
1406 IFACE_ENTRIES_HEADER_SIZE,
1407 sizeof (IFaceEntry));
1408 num_entries = IFACE_ENTRIES_N_ENTRIES (entries);
1409 i = num_entries - 1;
1410 if (i == 0)
1411 entries->offset_index = 0;
1412 entries->entry[i].iface_type = iface_type;
1413 entries->entry[i].vtable = NULL;
1414 entries->entry[i].init_state = UNINITIALIZED;
1415
1416 if (parent_entry)
1417 {
1418 if (node->data && node->data->class.init_state >= BASE_IFACE_INIT)
1419 {
1420 entries->entry[i].init_state = INITIALIZED;
1421 entries->entry[i].vtable = parent_entry->vtable;
1422 }
1423 }
1424
1425 /* Update offsets in iface */
1426 iface_node = lookup_type_node_I (iface_type);
1427
1428 if (iface_node_has_available_offset_L (iface_node,
1429 entries->offset_index,
1430 i))
1431 {
1432 iface_node_set_offset_L (iface_node,
1433 entries->offset_index, i);
1434 }
1435 else
1436 {
1437 entries->offset_index =
1438 find_free_iface_offset_L (entries);
1439 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (entries); j++)
1440 {
1441 entry = &entries->entry[j];
1442 iface_node =
1443 lookup_type_node_I (entry->iface_type);
1444 iface_node_set_offset_L (iface_node,
1445 entries->offset_index, j);
1446 }
1447 }
1448
1449 _g_atomic_array_update (CLASSED_NODE_IFACES_ENTRIES (node), entries);
1450
1451 if (parent_entry)
1452 {
1453 for (i = 0; i < node->n_children; i++)
1454 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), iface_type, &entries->entry[i]);
1455 }
1456 }
1457
1458 static void
type_add_interface_Wm(TypeNode * node,TypeNode * iface,const GInterfaceInfo * info,GTypePlugin * plugin)1459 type_add_interface_Wm (TypeNode *node,
1460 TypeNode *iface,
1461 const GInterfaceInfo *info,
1462 GTypePlugin *plugin)
1463 {
1464 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
1465 IFaceEntry *entry;
1466 guint i;
1467
1468 g_assert (node->is_instantiatable && NODE_IS_IFACE (iface) && ((info && !plugin) || (!info && plugin)));
1469
1470 iholder->next = iface_node_get_holders_L (iface);
1471 iface_node_set_holders_W (iface, iholder);
1472 iholder->instance_type = NODE_TYPE (node);
1473 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
1474 iholder->plugin = plugin;
1475
1476 /* create an iface entry for this type */
1477 type_node_add_iface_entry_W (node, NODE_TYPE (iface), NULL);
1478
1479 /* if the class is already (partly) initialized, we may need to base
1480 * initalize and/or initialize the new interface.
1481 */
1482 if (node->data)
1483 {
1484 InitState class_state = node->data->class.init_state;
1485
1486 if (class_state >= BASE_IFACE_INIT)
1487 type_iface_vtable_base_init_Wm (iface, node);
1488
1489 if (class_state >= IFACE_INIT)
1490 type_iface_vtable_iface_init_Wm (iface, node);
1491 }
1492
1493 /* create iface entries for children of this type */
1494 entry = type_lookup_iface_entry_L (node, iface);
1495 for (i = 0; i < node->n_children; i++)
1496 type_node_add_iface_entry_W (lookup_type_node_I (node->children[i]), NODE_TYPE (iface), entry);
1497 }
1498
1499 static void
type_iface_add_prerequisite_W(TypeNode * iface,TypeNode * prerequisite_node)1500 type_iface_add_prerequisite_W (TypeNode *iface,
1501 TypeNode *prerequisite_node)
1502 {
1503 GType prerequisite_type = NODE_TYPE (prerequisite_node);
1504 GType *prerequisites, *dependants;
1505 guint n_dependants, i;
1506
1507 g_assert (NODE_IS_IFACE (iface) &&
1508 IFACE_NODE_N_PREREQUISITES (iface) < MAX_N_PREREQUISITES &&
1509 (prerequisite_node->is_instantiatable || NODE_IS_IFACE (prerequisite_node)));
1510
1511 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1512 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1513 if (prerequisites[i] == prerequisite_type)
1514 return; /* we already have that prerequisiste */
1515 else if (prerequisites[i] > prerequisite_type)
1516 break;
1517 IFACE_NODE_N_PREREQUISITES (iface) += 1;
1518 IFACE_NODE_PREREQUISITES (iface) = g_renew (GType,
1519 IFACE_NODE_PREREQUISITES (iface),
1520 IFACE_NODE_N_PREREQUISITES (iface));
1521 prerequisites = IFACE_NODE_PREREQUISITES (iface);
1522 memmove (prerequisites + i + 1, prerequisites + i,
1523 sizeof (prerequisites[0]) * (IFACE_NODE_N_PREREQUISITES (iface) - i - 1));
1524 prerequisites[i] = prerequisite_type;
1525
1526 /* we want to get notified when prerequisites get added to prerequisite_node */
1527 if (NODE_IS_IFACE (prerequisite_node))
1528 {
1529 dependants = iface_node_get_dependants_array_L (prerequisite_node);
1530 n_dependants = dependants ? dependants[0] : 0;
1531 n_dependants += 1;
1532 dependants = g_renew (GType, dependants, n_dependants + 1);
1533 dependants[n_dependants] = NODE_TYPE (iface);
1534 dependants[0] = n_dependants;
1535 iface_node_set_dependants_array_W (prerequisite_node, dependants);
1536 }
1537
1538 /* we need to notify all dependants */
1539 dependants = iface_node_get_dependants_array_L (iface);
1540 n_dependants = dependants ? dependants[0] : 0;
1541 for (i = 1; i <= n_dependants; i++)
1542 type_iface_add_prerequisite_W (lookup_type_node_I (dependants[i]), prerequisite_node);
1543 }
1544
1545 /**
1546 * g_type_interface_add_prerequisite:
1547 * @interface_type: #GType value of an interface type
1548 * @prerequisite_type: #GType value of an interface or instantiatable type
1549 *
1550 * Adds @prerequisite_type to the list of prerequisites of @interface_type.
1551 * This means that any type implementing @interface_type must also implement
1552 * @prerequisite_type. Prerequisites can be thought of as an alternative to
1553 * interface derivation (which GType doesn't support). An interface can have
1554 * at most one instantiatable prerequisite type.
1555 */
1556 void
g_type_interface_add_prerequisite(GType interface_type,GType prerequisite_type)1557 g_type_interface_add_prerequisite (GType interface_type,
1558 GType prerequisite_type)
1559 {
1560 TypeNode *iface, *prerequisite_node;
1561 IFaceHolder *holders;
1562
1563 g_return_if_fail (G_TYPE_IS_INTERFACE (interface_type)); /* G_TYPE_IS_INTERFACE() is an external call: _U */
1564 g_return_if_fail (!g_type_is_a (interface_type, prerequisite_type));
1565 g_return_if_fail (!g_type_is_a (prerequisite_type, interface_type));
1566
1567 iface = lookup_type_node_I (interface_type);
1568 prerequisite_node = lookup_type_node_I (prerequisite_type);
1569 if (!iface || !prerequisite_node || !NODE_IS_IFACE (iface))
1570 {
1571 g_warning ("interface type '%s' or prerequisite type '%s' invalid",
1572 type_descriptive_name_I (interface_type),
1573 type_descriptive_name_I (prerequisite_type));
1574 return;
1575 }
1576 G_WRITE_LOCK (&type_rw_lock);
1577 holders = iface_node_get_holders_L (iface);
1578 if (holders)
1579 {
1580 G_WRITE_UNLOCK (&type_rw_lock);
1581 g_warning ("unable to add prerequisite '%s' to interface '%s' which is already in use for '%s'",
1582 type_descriptive_name_I (prerequisite_type),
1583 type_descriptive_name_I (interface_type),
1584 type_descriptive_name_I (holders->instance_type));
1585 return;
1586 }
1587 if (prerequisite_node->is_instantiatable)
1588 {
1589 guint i;
1590
1591 /* can have at most one publicly installable instantiatable prerequisite */
1592 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1593 {
1594 TypeNode *prnode = lookup_type_node_I (IFACE_NODE_PREREQUISITES (iface)[i]);
1595
1596 if (prnode->is_instantiatable)
1597 {
1598 G_WRITE_UNLOCK (&type_rw_lock);
1599 g_warning ("adding prerequisite '%s' to interface '%s' conflicts with existing prerequisite '%s'",
1600 type_descriptive_name_I (prerequisite_type),
1601 type_descriptive_name_I (interface_type),
1602 type_descriptive_name_I (NODE_TYPE (prnode)));
1603 return;
1604 }
1605 }
1606
1607 for (i = 0; i < prerequisite_node->n_supers + 1; i++)
1608 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisite_node->supers[i]));
1609 G_WRITE_UNLOCK (&type_rw_lock);
1610 }
1611 else if (NODE_IS_IFACE (prerequisite_node))
1612 {
1613 GType *prerequisites;
1614 guint i;
1615
1616 prerequisites = IFACE_NODE_PREREQUISITES (prerequisite_node);
1617 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (prerequisite_node); i++)
1618 type_iface_add_prerequisite_W (iface, lookup_type_node_I (prerequisites[i]));
1619 type_iface_add_prerequisite_W (iface, prerequisite_node);
1620 G_WRITE_UNLOCK (&type_rw_lock);
1621 }
1622 else
1623 {
1624 G_WRITE_UNLOCK (&type_rw_lock);
1625 g_warning ("prerequisite '%s' for interface '%s' is neither instantiatable nor interface",
1626 type_descriptive_name_I (prerequisite_type),
1627 type_descriptive_name_I (interface_type));
1628 }
1629 }
1630
1631 /**
1632 * g_type_interface_prerequisites:
1633 * @interface_type: an interface type
1634 * @n_prerequisites: (out) (optional): location to return the number
1635 * of prerequisites, or %NULL
1636 *
1637 * Returns the prerequisites of an interfaces type.
1638 *
1639 * Since: 2.2
1640 *
1641 * Returns: (array length=n_prerequisites) (transfer full): a
1642 * newly-allocated zero-terminated array of #GType containing
1643 * the prerequisites of @interface_type
1644 */
1645 GType*
g_type_interface_prerequisites(GType interface_type,guint * n_prerequisites)1646 g_type_interface_prerequisites (GType interface_type,
1647 guint *n_prerequisites)
1648 {
1649 TypeNode *iface;
1650
1651 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL);
1652
1653 iface = lookup_type_node_I (interface_type);
1654 if (iface)
1655 {
1656 GType *types;
1657 TypeNode *inode = NULL;
1658 guint i, n = 0;
1659
1660 G_READ_LOCK (&type_rw_lock);
1661 types = g_new0 (GType, IFACE_NODE_N_PREREQUISITES (iface) + 1);
1662 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (iface); i++)
1663 {
1664 GType prerequisite = IFACE_NODE_PREREQUISITES (iface)[i];
1665 TypeNode *node = lookup_type_node_I (prerequisite);
1666 if (node->is_instantiatable)
1667 {
1668 if (!inode || type_node_is_a_L (node, inode))
1669 inode = node;
1670 }
1671 else
1672 types[n++] = NODE_TYPE (node);
1673 }
1674 if (inode)
1675 types[n++] = NODE_TYPE (inode);
1676
1677 if (n_prerequisites)
1678 *n_prerequisites = n;
1679 G_READ_UNLOCK (&type_rw_lock);
1680
1681 return types;
1682 }
1683 else
1684 {
1685 if (n_prerequisites)
1686 *n_prerequisites = 0;
1687
1688 return NULL;
1689 }
1690 }
1691
1692
1693 static IFaceHolder*
type_iface_peek_holder_L(TypeNode * iface,GType instance_type)1694 type_iface_peek_holder_L (TypeNode *iface,
1695 GType instance_type)
1696 {
1697 IFaceHolder *iholder;
1698
1699 g_assert (NODE_IS_IFACE (iface));
1700
1701 iholder = iface_node_get_holders_L (iface);
1702 while (iholder && iholder->instance_type != instance_type)
1703 iholder = iholder->next;
1704 return iholder;
1705 }
1706
1707 static IFaceHolder*
type_iface_retrieve_holder_info_Wm(TypeNode * iface,GType instance_type,gboolean need_info)1708 type_iface_retrieve_holder_info_Wm (TypeNode *iface,
1709 GType instance_type,
1710 gboolean need_info)
1711 {
1712 IFaceHolder *iholder = type_iface_peek_holder_L (iface, instance_type);
1713
1714 if (iholder && !iholder->info && need_info)
1715 {
1716 GInterfaceInfo tmp_info;
1717
1718 g_assert (iholder->plugin != NULL);
1719
1720 type_data_ref_Wm (iface);
1721 if (iholder->info)
1722 INVALID_RECURSION ("g_type_plugin_*", iface->plugin, NODE_NAME (iface));
1723
1724 memset (&tmp_info, 0, sizeof (tmp_info));
1725
1726 G_WRITE_UNLOCK (&type_rw_lock);
1727 g_type_plugin_use (iholder->plugin);
1728 g_type_plugin_complete_interface_info (iholder->plugin, instance_type, NODE_TYPE (iface), &tmp_info);
1729 G_WRITE_LOCK (&type_rw_lock);
1730 if (iholder->info)
1731 INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
1732
1733 check_interface_info_I (iface, instance_type, &tmp_info);
1734 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
1735 }
1736
1737 return iholder; /* we don't modify write lock upon returning NULL */
1738 }
1739
1740 static void
type_iface_blow_holder_info_Wm(TypeNode * iface,GType instance_type)1741 type_iface_blow_holder_info_Wm (TypeNode *iface,
1742 GType instance_type)
1743 {
1744 IFaceHolder *iholder = iface_node_get_holders_L (iface);
1745
1746 g_assert (NODE_IS_IFACE (iface));
1747
1748 while (iholder->instance_type != instance_type)
1749 iholder = iholder->next;
1750
1751 if (iholder->info && iholder->plugin)
1752 {
1753 g_free (iholder->info);
1754 iholder->info = NULL;
1755
1756 G_WRITE_UNLOCK (&type_rw_lock);
1757 g_type_plugin_unuse (iholder->plugin);
1758 type_data_unref_U (iface, FALSE);
1759 G_WRITE_LOCK (&type_rw_lock);
1760 }
1761 }
1762
1763 /**
1764 * g_type_create_instance: (skip)
1765 * @type: an instantiatable type to create an instance for
1766 *
1767 * Creates and initializes an instance of @type if @type is valid and
1768 * can be instantiated. The type system only performs basic allocation
1769 * and structure setups for instances: actual instance creation should
1770 * happen through functions supplied by the type's fundamental type
1771 * implementation. So use of g_type_create_instance() is reserved for
1772 * implementators of fundamental types only. E.g. instances of the
1773 * #GObject hierarchy should be created via g_object_new() and never
1774 * directly through g_type_create_instance() which doesn't handle things
1775 * like singleton objects or object construction.
1776 *
1777 * The extended members of the returned instance are guaranteed to be filled
1778 * with zeros.
1779 *
1780 * Note: Do not use this function, unless you're implementing a
1781 * fundamental type. Also language bindings should not use this
1782 * function, but g_object_new() instead.
1783 *
1784 * Returns: an allocated and initialized instance, subject to further
1785 * treatment by the fundamental type implementation
1786 */
1787 GTypeInstance*
g_type_create_instance(GType type)1788 g_type_create_instance (GType type)
1789 {
1790 TypeNode *node;
1791 GTypeInstance *instance;
1792 GTypeClass *class;
1793 gchar *allocated;
1794 gint private_size;
1795 gint ivar_size;
1796 guint i;
1797
1798 node = lookup_type_node_I (type);
1799 if (!node || !node->is_instantiatable)
1800 {
1801 g_error ("cannot create new instance of invalid (non-instantiatable) type '%s'",
1802 type_descriptive_name_I (type));
1803 }
1804 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1805 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
1806 {
1807 g_error ("cannot create instance of abstract (non-instantiatable) type '%s'",
1808 type_descriptive_name_I (type));
1809 }
1810
1811 class = g_type_class_ref (type);
1812
1813 /* We allocate the 'private' areas before the normal instance data, in
1814 * reverse order. This allows the private area of a particular class
1815 * to always be at a constant relative address to the instance data.
1816 * If we stored the private data after the instance data this would
1817 * not be the case (since a subclass that added more instance
1818 * variables would push the private data further along).
1819 *
1820 * This presents problems for valgrindability, of course, so we do a
1821 * workaround for that case. We identify the start of the object to
1822 * valgrind as an allocated block (so that pointers to objects show up
1823 * as 'reachable' instead of 'possibly lost'). We then add an extra
1824 * pointer at the end of the object, after all instance data, back to
1825 * the start of the private area so that it is also recorded as
1826 * reachable. We also add extra private space at the start because
1827 * valgrind doesn't seem to like us claiming to have allocated an
1828 * address that it saw allocated by malloc().
1829 */
1830 private_size = node->data->instance.private_size;
1831 ivar_size = node->data->instance.instance_size;
1832
1833 #ifdef ENABLE_VALGRIND
1834 if (private_size && RUNNING_ON_VALGRIND)
1835 {
1836 private_size += ALIGN_STRUCT (1);
1837
1838 /* Allocate one extra pointer size... */
1839 allocated = g_slice_alloc0 (private_size + ivar_size + sizeof (gpointer));
1840 /* ... and point it back to the start of the private data. */
1841 *(gpointer *) (allocated + private_size + ivar_size) = allocated + ALIGN_STRUCT (1);
1842
1843 /* Tell valgrind that it should treat the object itself as such */
1844 VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, ivar_size + sizeof (gpointer), 0, TRUE);
1845 VALGRIND_MALLOCLIKE_BLOCK (allocated + ALIGN_STRUCT (1), private_size - ALIGN_STRUCT (1), 0, TRUE);
1846 }
1847 else
1848 #endif
1849 allocated = g_slice_alloc0 (private_size + ivar_size);
1850
1851 instance = (GTypeInstance *) (allocated + private_size);
1852
1853 for (i = node->n_supers; i > 0; i--)
1854 {
1855 TypeNode *pnode;
1856
1857 pnode = lookup_type_node_I (node->supers[i]);
1858 if (pnode->data->instance.instance_init)
1859 {
1860 instance->g_class = pnode->data->instance.class;
1861 pnode->data->instance.instance_init (instance, class);
1862 }
1863 }
1864
1865 instance->g_class = class;
1866 if (node->data->instance.instance_init)
1867 node->data->instance.instance_init (instance, class);
1868
1869 #ifdef G_ENABLE_DEBUG
1870 IF_DEBUG (INSTANCE_COUNT)
1871 {
1872 g_atomic_int_inc ((int *) &node->instance_count);
1873 }
1874 #endif
1875
1876 TRACE(GOBJECT_OBJECT_NEW(instance, type));
1877
1878 return instance;
1879 }
1880
1881 /**
1882 * g_type_free_instance:
1883 * @instance: an instance of a type
1884 *
1885 * Frees an instance of a type, returning it to the instance pool for
1886 * the type, if there is one.
1887 *
1888 * Like g_type_create_instance(), this function is reserved for
1889 * implementors of fundamental types.
1890 */
1891 void
g_type_free_instance(GTypeInstance * instance)1892 g_type_free_instance (GTypeInstance *instance)
1893 {
1894 TypeNode *node;
1895 GTypeClass *class;
1896 gchar *allocated;
1897 gint private_size;
1898 gint ivar_size;
1899
1900 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1901
1902 class = instance->g_class;
1903 node = lookup_type_node_I (class->g_type);
1904 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1905 {
1906 g_warning ("cannot free instance of invalid (non-instantiatable) type '%s'",
1907 type_descriptive_name_I (class->g_type));
1908 return;
1909 }
1910 /* G_TYPE_IS_ABSTRACT() is an external call: _U */
1911 if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1912 {
1913 g_warning ("cannot free instance of abstract (non-instantiatable) type '%s'",
1914 NODE_NAME (node));
1915 return;
1916 }
1917
1918 instance->g_class = NULL;
1919 private_size = node->data->instance.private_size;
1920 ivar_size = node->data->instance.instance_size;
1921 allocated = ((gchar *) instance) - private_size;
1922
1923 #ifdef G_ENABLE_DEBUG
1924 memset (allocated, 0xaa, ivar_size + private_size);
1925 #endif
1926
1927 #ifdef ENABLE_VALGRIND
1928 /* See comment in g_type_create_instance() about what's going on here.
1929 * We're basically unwinding what we put into motion there.
1930 */
1931 if (private_size && RUNNING_ON_VALGRIND)
1932 {
1933 private_size += ALIGN_STRUCT (1);
1934 allocated -= ALIGN_STRUCT (1);
1935
1936 /* Clear out the extra pointer... */
1937 *(gpointer *) (allocated + private_size + ivar_size) = NULL;
1938 /* ... and ensure we include it in the size we free. */
1939 g_slice_free1 (private_size + ivar_size + sizeof (gpointer), allocated);
1940
1941 VALGRIND_FREELIKE_BLOCK (allocated + ALIGN_STRUCT (1), 0);
1942 VALGRIND_FREELIKE_BLOCK (instance, 0);
1943 }
1944 else
1945 #endif
1946 g_slice_free1 (private_size + ivar_size, allocated);
1947
1948 #ifdef G_ENABLE_DEBUG
1949 IF_DEBUG (INSTANCE_COUNT)
1950 {
1951 g_atomic_int_add ((int *) &node->instance_count, -1);
1952 }
1953 #endif
1954
1955 g_type_class_unref (class);
1956 }
1957
1958 static void
type_iface_ensure_dflt_vtable_Wm(TypeNode * iface)1959 type_iface_ensure_dflt_vtable_Wm (TypeNode *iface)
1960 {
1961 g_assert (iface->data);
1962
1963 if (!iface->data->iface.dflt_vtable)
1964 {
1965 GTypeInterface *vtable = g_malloc0 (iface->data->iface.vtable_size);
1966 iface->data->iface.dflt_vtable = vtable;
1967 vtable->g_type = NODE_TYPE (iface);
1968 vtable->g_instance_type = 0;
1969 if (iface->data->iface.vtable_init_base ||
1970 iface->data->iface.dflt_init)
1971 {
1972 G_WRITE_UNLOCK (&type_rw_lock);
1973 if (iface->data->iface.vtable_init_base)
1974 iface->data->iface.vtable_init_base (vtable);
1975 if (iface->data->iface.dflt_init)
1976 iface->data->iface.dflt_init (vtable, (gpointer) iface->data->iface.dflt_data);
1977 G_WRITE_LOCK (&type_rw_lock);
1978 }
1979 }
1980 }
1981
1982
1983 /* This is called to allocate and do the first part of initializing
1984 * the interface vtable; type_iface_vtable_iface_init_Wm() does the remainder.
1985 *
1986 * A FALSE return indicates that we didn't find an init function for
1987 * this type/iface pair, so the vtable from the parent type should
1988 * be used. Note that the write lock is not modified upon a FALSE
1989 * return.
1990 */
1991 static gboolean
type_iface_vtable_base_init_Wm(TypeNode * iface,TypeNode * node)1992 type_iface_vtable_base_init_Wm (TypeNode *iface,
1993 TypeNode *node)
1994 {
1995 IFaceEntry *entry;
1996 IFaceHolder *iholder;
1997 GTypeInterface *vtable = NULL;
1998 TypeNode *pnode;
1999
2000 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2001 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), TRUE);
2002 if (!iholder)
2003 return FALSE; /* we don't modify write lock upon FALSE */
2004
2005 type_iface_ensure_dflt_vtable_Wm (iface);
2006
2007 entry = type_lookup_iface_entry_L (node, iface);
2008
2009 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
2010
2011 entry->init_state = IFACE_INIT;
2012
2013 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2014 if (pnode) /* want to copy over parent iface contents */
2015 {
2016 IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
2017
2018 if (pentry)
2019 vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
2020 }
2021 if (!vtable)
2022 vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
2023 entry->vtable = vtable;
2024 vtable->g_type = NODE_TYPE (iface);
2025 vtable->g_instance_type = NODE_TYPE (node);
2026
2027 if (iface->data->iface.vtable_init_base)
2028 {
2029 G_WRITE_UNLOCK (&type_rw_lock);
2030 iface->data->iface.vtable_init_base (vtable);
2031 G_WRITE_LOCK (&type_rw_lock);
2032 }
2033 return TRUE; /* initialized the vtable */
2034 }
2035
2036 /* Finishes what type_iface_vtable_base_init_Wm started by
2037 * calling the interface init function.
2038 * this function may only be called for types with their
2039 * own interface holder info, i.e. types for which
2040 * g_type_add_interface*() was called and not children thereof.
2041 */
2042 static void
type_iface_vtable_iface_init_Wm(TypeNode * iface,TypeNode * node)2043 type_iface_vtable_iface_init_Wm (TypeNode *iface,
2044 TypeNode *node)
2045 {
2046 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2047 IFaceHolder *iholder = type_iface_peek_holder_L (iface, NODE_TYPE (node));
2048 GTypeInterface *vtable = NULL;
2049 guint i;
2050
2051 /* iholder->info should have been filled in by type_iface_vtable_base_init_Wm() */
2052 g_assert (iface->data && entry && iholder && iholder->info);
2053 g_assert (entry->init_state == IFACE_INIT); /* assert prior base_init() */
2054
2055 entry->init_state = INITIALIZED;
2056
2057 vtable = entry->vtable;
2058
2059 if (iholder->info->interface_init)
2060 {
2061 G_WRITE_UNLOCK (&type_rw_lock);
2062 if (iholder->info->interface_init)
2063 iholder->info->interface_init (vtable, iholder->info->interface_data);
2064 G_WRITE_LOCK (&type_rw_lock);
2065 }
2066
2067 for (i = 0; i < static_n_iface_check_funcs; i++)
2068 {
2069 GTypeInterfaceCheckFunc check_func = static_iface_check_funcs[i].check_func;
2070 gpointer check_data = static_iface_check_funcs[i].check_data;
2071
2072 G_WRITE_UNLOCK (&type_rw_lock);
2073 check_func (check_data, (gpointer)vtable);
2074 G_WRITE_LOCK (&type_rw_lock);
2075 }
2076 }
2077
2078 static gboolean
type_iface_vtable_finalize_Wm(TypeNode * iface,TypeNode * node,GTypeInterface * vtable)2079 type_iface_vtable_finalize_Wm (TypeNode *iface,
2080 TypeNode *node,
2081 GTypeInterface *vtable)
2082 {
2083 IFaceEntry *entry = type_lookup_iface_entry_L (node, iface);
2084 IFaceHolder *iholder;
2085
2086 /* type_iface_retrieve_holder_info_Wm() doesn't modify write lock for returning NULL */
2087 iholder = type_iface_retrieve_holder_info_Wm (iface, NODE_TYPE (node), FALSE);
2088 if (!iholder)
2089 return FALSE; /* we don't modify write lock upon FALSE */
2090
2091 g_assert (entry && entry->vtable == vtable && iholder->info);
2092
2093 entry->vtable = NULL;
2094 entry->init_state = UNINITIALIZED;
2095 if (iholder->info->interface_finalize || iface->data->iface.vtable_finalize_base)
2096 {
2097 G_WRITE_UNLOCK (&type_rw_lock);
2098 if (iholder->info->interface_finalize)
2099 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
2100 if (iface->data->iface.vtable_finalize_base)
2101 iface->data->iface.vtable_finalize_base (vtable);
2102 G_WRITE_LOCK (&type_rw_lock);
2103 }
2104 vtable->g_type = 0;
2105 vtable->g_instance_type = 0;
2106 g_free (vtable);
2107
2108 type_iface_blow_holder_info_Wm (iface, NODE_TYPE (node));
2109
2110 return TRUE; /* write lock modified */
2111 }
2112
2113 static void
type_class_init_Wm(TypeNode * node,GTypeClass * pclass)2114 type_class_init_Wm (TypeNode *node,
2115 GTypeClass *pclass)
2116 {
2117 GSList *slist, *init_slist = NULL;
2118 GTypeClass *class;
2119 IFaceEntries *entries;
2120 IFaceEntry *entry;
2121 TypeNode *bnode, *pnode;
2122 guint i;
2123
2124 /* Accessing data->class will work for instantiable types
2125 * too because ClassData is a subset of InstanceData
2126 */
2127 g_assert (node->is_classed && node->data &&
2128 node->data->class.class_size &&
2129 !node->data->class.class &&
2130 node->data->class.init_state == UNINITIALIZED);
2131 if (node->data->class.class_private_size)
2132 class = g_malloc0 (ALIGN_STRUCT (node->data->class.class_size) + node->data->class.class_private_size);
2133 else
2134 class = g_malloc0 (node->data->class.class_size);
2135 node->data->class.class = class;
2136 g_atomic_int_set (&node->data->class.init_state, BASE_CLASS_INIT);
2137
2138 if (pclass)
2139 {
2140 TypeNode *pnode = lookup_type_node_I (pclass->g_type);
2141
2142 memcpy (class, pclass, pnode->data->class.class_size);
2143 memcpy (G_STRUCT_MEMBER_P (class, ALIGN_STRUCT (node->data->class.class_size)), G_STRUCT_MEMBER_P (pclass, ALIGN_STRUCT (pnode->data->class.class_size)), pnode->data->class.class_private_size);
2144
2145 if (node->is_instantiatable)
2146 {
2147 /* We need to initialize the private_size here rather than in
2148 * type_data_make_W() since the class init for the parent
2149 * class may have changed pnode->data->instance.private_size.
2150 */
2151 node->data->instance.private_size = pnode->data->instance.private_size;
2152 }
2153 }
2154 class->g_type = NODE_TYPE (node);
2155
2156 G_WRITE_UNLOCK (&type_rw_lock);
2157
2158 /* stack all base class initialization functions, so we
2159 * call them in ascending order.
2160 */
2161 for (bnode = node; bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2162 if (bnode->data->class.class_init_base)
2163 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
2164 for (slist = init_slist; slist; slist = slist->next)
2165 {
2166 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
2167
2168 class_init_base (class);
2169 }
2170 g_slist_free (init_slist);
2171
2172 G_WRITE_LOCK (&type_rw_lock);
2173
2174 g_atomic_int_set (&node->data->class.init_state, BASE_IFACE_INIT);
2175
2176 /* Before we initialize the class, base initialize all interfaces, either
2177 * from parent, or through our holder info
2178 */
2179 pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
2180
2181 i = 0;
2182 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL &&
2183 i < IFACE_ENTRIES_N_ENTRIES (entries))
2184 {
2185 entry = &entries->entry[i];
2186 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2187 entry->init_state == IFACE_INIT)
2188 {
2189 entry++;
2190 i++;
2191 }
2192
2193 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2194 break;
2195
2196 if (!type_iface_vtable_base_init_Wm (lookup_type_node_I (entry->iface_type), node))
2197 {
2198 guint j;
2199 IFaceEntries *pentries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (pnode);
2200
2201 /* need to get this interface from parent, type_iface_vtable_base_init_Wm()
2202 * doesn't modify write lock upon FALSE, so entry is still valid;
2203 */
2204 g_assert (pnode != NULL);
2205
2206 if (pentries)
2207 for (j = 0; j < IFACE_ENTRIES_N_ENTRIES (pentries); j++)
2208 {
2209 IFaceEntry *pentry = &pentries->entry[j];
2210
2211 if (pentry->iface_type == entry->iface_type)
2212 {
2213 entry->vtable = pentry->vtable;
2214 entry->init_state = INITIALIZED;
2215 break;
2216 }
2217 }
2218 g_assert (entry->vtable != NULL);
2219 }
2220
2221 /* If the write lock was released, additional interface entries might
2222 * have been inserted into CLASSED_NODE_IFACES_ENTRIES (node); they'll
2223 * be base-initialized when inserted, so we don't have to worry that
2224 * we might miss them. Uninitialized entries can only be moved higher
2225 * when new ones are inserted.
2226 */
2227 i++;
2228 }
2229
2230 g_atomic_int_set (&node->data->class.init_state, CLASS_INIT);
2231
2232 G_WRITE_UNLOCK (&type_rw_lock);
2233
2234 if (node->data->class.class_init)
2235 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
2236
2237 G_WRITE_LOCK (&type_rw_lock);
2238
2239 g_atomic_int_set (&node->data->class.init_state, IFACE_INIT);
2240
2241 /* finish initializing the interfaces through our holder info.
2242 * inherited interfaces are already init_state == INITIALIZED, because
2243 * they either got setup in the above base_init loop, or during
2244 * class_init from within type_add_interface_Wm() for this or
2245 * an anchestor type.
2246 */
2247 i = 0;
2248 while ((entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node)) != NULL)
2249 {
2250 entry = &entries->entry[i];
2251 while (i < IFACE_ENTRIES_N_ENTRIES (entries) &&
2252 entry->init_state == INITIALIZED)
2253 {
2254 entry++;
2255 i++;
2256 }
2257
2258 if (i == IFACE_ENTRIES_N_ENTRIES (entries))
2259 break;
2260
2261 type_iface_vtable_iface_init_Wm (lookup_type_node_I (entry->iface_type), node);
2262
2263 /* As in the loop above, additional initialized entries might be inserted
2264 * if the write lock is released, but that's harmless because the entries
2265 * we need to initialize only move higher in the list.
2266 */
2267 i++;
2268 }
2269
2270 g_atomic_int_set (&node->data->class.init_state, INITIALIZED);
2271 }
2272
2273 static void
type_data_finalize_class_ifaces_Wm(TypeNode * node)2274 type_data_finalize_class_ifaces_Wm (TypeNode *node)
2275 {
2276 guint i;
2277 IFaceEntries *entries;
2278
2279 g_assert (node->is_instantiatable && node->data && node->data->class.class && NODE_REFCOUNT (node) == 0);
2280
2281 reiterate:
2282 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
2283 for (i = 0; entries != NULL && i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
2284 {
2285 IFaceEntry *entry = &entries->entry[i];
2286 if (entry->vtable)
2287 {
2288 if (type_iface_vtable_finalize_Wm (lookup_type_node_I (entry->iface_type), node, entry->vtable))
2289 {
2290 /* refetch entries, IFACES_ENTRIES might be modified */
2291 goto reiterate;
2292 }
2293 else
2294 {
2295 /* type_iface_vtable_finalize_Wm() doesn't modify write lock upon FALSE,
2296 * iface vtable came from parent
2297 */
2298 entry->vtable = NULL;
2299 entry->init_state = UNINITIALIZED;
2300 }
2301 }
2302 }
2303 }
2304
2305 static void
type_data_finalize_class_U(TypeNode * node,ClassData * cdata)2306 type_data_finalize_class_U (TypeNode *node,
2307 ClassData *cdata)
2308 {
2309 GTypeClass *class = cdata->class;
2310 TypeNode *bnode;
2311
2312 g_assert (cdata->class && NODE_REFCOUNT (node) == 0);
2313
2314 if (cdata->class_finalize)
2315 cdata->class_finalize (class, (gpointer) cdata->class_data);
2316
2317 /* call all base class destruction functions in descending order
2318 */
2319 if (cdata->class_finalize_base)
2320 cdata->class_finalize_base (class);
2321 for (bnode = lookup_type_node_I (NODE_PARENT_TYPE (node)); bnode; bnode = lookup_type_node_I (NODE_PARENT_TYPE (bnode)))
2322 if (bnode->data->class.class_finalize_base)
2323 bnode->data->class.class_finalize_base (class);
2324
2325 g_free (cdata->class);
2326 }
2327
2328 static void
type_data_last_unref_Wm(TypeNode * node,gboolean uncached)2329 type_data_last_unref_Wm (TypeNode *node,
2330 gboolean uncached)
2331 {
2332 g_return_if_fail (node != NULL && node->plugin != NULL);
2333
2334 if (!node->data || NODE_REFCOUNT (node) == 0)
2335 {
2336 g_warning ("cannot drop last reference to unreferenced type '%s'",
2337 NODE_NAME (node));
2338 return;
2339 }
2340
2341 /* call class cache hooks */
2342 if (node->is_classed && node->data && node->data->class.class && static_n_class_cache_funcs && !uncached)
2343 {
2344 guint i;
2345
2346 G_WRITE_UNLOCK (&type_rw_lock);
2347 G_READ_LOCK (&type_rw_lock);
2348 for (i = 0; i < static_n_class_cache_funcs; i++)
2349 {
2350 GTypeClassCacheFunc cache_func = static_class_cache_funcs[i].cache_func;
2351 gpointer cache_data = static_class_cache_funcs[i].cache_data;
2352 gboolean need_break;
2353
2354 G_READ_UNLOCK (&type_rw_lock);
2355 need_break = cache_func (cache_data, node->data->class.class);
2356 G_READ_LOCK (&type_rw_lock);
2357 if (!node->data || NODE_REFCOUNT (node) == 0)
2358 INVALID_RECURSION ("GType class cache function ", cache_func, NODE_NAME (node));
2359 if (need_break)
2360 break;
2361 }
2362 G_READ_UNLOCK (&type_rw_lock);
2363 G_WRITE_LOCK (&type_rw_lock);
2364 }
2365
2366 /* may have been re-referenced meanwhile */
2367 if (g_atomic_int_dec_and_test ((int *) &node->ref_count))
2368 {
2369 GType ptype = NODE_PARENT_TYPE (node);
2370 TypeData *tdata;
2371
2372 if (node->is_instantiatable)
2373 {
2374 /* destroy node->data->instance.mem_chunk */
2375 }
2376
2377 tdata = node->data;
2378 if (node->is_classed && tdata->class.class)
2379 {
2380 if (CLASSED_NODE_IFACES_ENTRIES_LOCKED (node) != NULL)
2381 type_data_finalize_class_ifaces_Wm (node);
2382 node->mutatable_check_cache = FALSE;
2383 node->data = NULL;
2384 G_WRITE_UNLOCK (&type_rw_lock);
2385 type_data_finalize_class_U (node, &tdata->class);
2386 G_WRITE_LOCK (&type_rw_lock);
2387 }
2388 else if (NODE_IS_IFACE (node) && tdata->iface.dflt_vtable)
2389 {
2390 node->mutatable_check_cache = FALSE;
2391 node->data = NULL;
2392 if (tdata->iface.dflt_finalize || tdata->iface.vtable_finalize_base)
2393 {
2394 G_WRITE_UNLOCK (&type_rw_lock);
2395 if (tdata->iface.dflt_finalize)
2396 tdata->iface.dflt_finalize (tdata->iface.dflt_vtable, (gpointer) tdata->iface.dflt_data);
2397 if (tdata->iface.vtable_finalize_base)
2398 tdata->iface.vtable_finalize_base (tdata->iface.dflt_vtable);
2399 G_WRITE_LOCK (&type_rw_lock);
2400 }
2401 g_free (tdata->iface.dflt_vtable);
2402 }
2403 else
2404 {
2405 node->mutatable_check_cache = FALSE;
2406 node->data = NULL;
2407 }
2408
2409 /* freeing tdata->common.value_table and its contents is taken care of
2410 * by allocating it in one chunk with tdata
2411 */
2412 g_free (tdata);
2413
2414 G_WRITE_UNLOCK (&type_rw_lock);
2415 g_type_plugin_unuse (node->plugin);
2416 if (ptype)
2417 type_data_unref_U (lookup_type_node_I (ptype), FALSE);
2418 G_WRITE_LOCK (&type_rw_lock);
2419 }
2420 }
2421
2422 static inline void
type_data_unref_U(TypeNode * node,gboolean uncached)2423 type_data_unref_U (TypeNode *node,
2424 gboolean uncached)
2425 {
2426 guint current;
2427
2428 do {
2429 current = NODE_REFCOUNT (node);
2430
2431 if (current <= 1)
2432 {
2433 if (!node->plugin)
2434 {
2435 g_warning ("static type '%s' unreferenced too often",
2436 NODE_NAME (node));
2437 return;
2438 }
2439 else
2440 {
2441 /* This is the last reference of a type from a plugin. We are
2442 * experimentally disabling support for unloading type
2443 * plugins, so don't allow the last ref to drop.
2444 */
2445 return;
2446 }
2447
2448 g_assert (current > 0);
2449
2450 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2451 G_WRITE_LOCK (&type_rw_lock);
2452 type_data_last_unref_Wm (node, uncached);
2453 G_WRITE_UNLOCK (&type_rw_lock);
2454 g_rec_mutex_unlock (&class_init_rec_mutex);
2455 return;
2456 }
2457 } while (!g_atomic_int_compare_and_exchange ((int *) &node->ref_count, current, current - 1));
2458 }
2459
2460 /**
2461 * g_type_add_class_cache_func: (skip)
2462 * @cache_data: data to be passed to @cache_func
2463 * @cache_func: a #GTypeClassCacheFunc
2464 *
2465 * Adds a #GTypeClassCacheFunc to be called before the reference count of a
2466 * class goes from one to zero. This can be used to prevent premature class
2467 * destruction. All installed #GTypeClassCacheFunc functions will be chained
2468 * until one of them returns %TRUE. The functions have to check the class id
2469 * passed in to figure whether they actually want to cache the class of this
2470 * type, since all classes are routed through the same #GTypeClassCacheFunc
2471 * chain.
2472 */
2473 void
g_type_add_class_cache_func(gpointer cache_data,GTypeClassCacheFunc cache_func)2474 g_type_add_class_cache_func (gpointer cache_data,
2475 GTypeClassCacheFunc cache_func)
2476 {
2477 guint i;
2478
2479 g_return_if_fail (cache_func != NULL);
2480
2481 G_WRITE_LOCK (&type_rw_lock);
2482 i = static_n_class_cache_funcs++;
2483 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2484 static_class_cache_funcs[i].cache_data = cache_data;
2485 static_class_cache_funcs[i].cache_func = cache_func;
2486 G_WRITE_UNLOCK (&type_rw_lock);
2487 }
2488
2489 /**
2490 * g_type_remove_class_cache_func: (skip)
2491 * @cache_data: data that was given when adding @cache_func
2492 * @cache_func: a #GTypeClassCacheFunc
2493 *
2494 * Removes a previously installed #GTypeClassCacheFunc. The cache
2495 * maintained by @cache_func has to be empty when calling
2496 * g_type_remove_class_cache_func() to avoid leaks.
2497 */
2498 void
g_type_remove_class_cache_func(gpointer cache_data,GTypeClassCacheFunc cache_func)2499 g_type_remove_class_cache_func (gpointer cache_data,
2500 GTypeClassCacheFunc cache_func)
2501 {
2502 gboolean found_it = FALSE;
2503 guint i;
2504
2505 g_return_if_fail (cache_func != NULL);
2506
2507 G_WRITE_LOCK (&type_rw_lock);
2508 for (i = 0; i < static_n_class_cache_funcs; i++)
2509 if (static_class_cache_funcs[i].cache_data == cache_data &&
2510 static_class_cache_funcs[i].cache_func == cache_func)
2511 {
2512 static_n_class_cache_funcs--;
2513 memmove (static_class_cache_funcs + i,
2514 static_class_cache_funcs + i + 1,
2515 sizeof (static_class_cache_funcs[0]) * (static_n_class_cache_funcs - i));
2516 static_class_cache_funcs = g_renew (ClassCacheFunc, static_class_cache_funcs, static_n_class_cache_funcs);
2517 found_it = TRUE;
2518 break;
2519 }
2520 G_WRITE_UNLOCK (&type_rw_lock);
2521
2522 if (!found_it)
2523 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
2524 cache_func, cache_data);
2525 }
2526
2527
2528 /**
2529 * g_type_add_interface_check: (skip)
2530 * @check_data: data to pass to @check_func
2531 * @check_func: function to be called after each interface
2532 * is initialized
2533 *
2534 * Adds a function to be called after an interface vtable is
2535 * initialized for any class (i.e. after the @interface_init
2536 * member of #GInterfaceInfo has been called).
2537 *
2538 * This function is useful when you want to check an invariant
2539 * that depends on the interfaces of a class. For instance, the
2540 * implementation of #GObject uses this facility to check that an
2541 * object implements all of the properties that are defined on its
2542 * interfaces.
2543 *
2544 * Since: 2.4
2545 */
2546 void
g_type_add_interface_check(gpointer check_data,GTypeInterfaceCheckFunc check_func)2547 g_type_add_interface_check (gpointer check_data,
2548 GTypeInterfaceCheckFunc check_func)
2549 {
2550 guint i;
2551
2552 g_return_if_fail (check_func != NULL);
2553
2554 G_WRITE_LOCK (&type_rw_lock);
2555 i = static_n_iface_check_funcs++;
2556 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2557 static_iface_check_funcs[i].check_data = check_data;
2558 static_iface_check_funcs[i].check_func = check_func;
2559 G_WRITE_UNLOCK (&type_rw_lock);
2560 }
2561
2562 /**
2563 * g_type_remove_interface_check: (skip)
2564 * @check_data: callback data passed to g_type_add_interface_check()
2565 * @check_func: callback function passed to g_type_add_interface_check()
2566 *
2567 * Removes an interface check function added with
2568 * g_type_add_interface_check().
2569 *
2570 * Since: 2.4
2571 */
2572 void
g_type_remove_interface_check(gpointer check_data,GTypeInterfaceCheckFunc check_func)2573 g_type_remove_interface_check (gpointer check_data,
2574 GTypeInterfaceCheckFunc check_func)
2575 {
2576 gboolean found_it = FALSE;
2577 guint i;
2578
2579 g_return_if_fail (check_func != NULL);
2580
2581 G_WRITE_LOCK (&type_rw_lock);
2582 for (i = 0; i < static_n_iface_check_funcs; i++)
2583 if (static_iface_check_funcs[i].check_data == check_data &&
2584 static_iface_check_funcs[i].check_func == check_func)
2585 {
2586 static_n_iface_check_funcs--;
2587 memmove (static_iface_check_funcs + i,
2588 static_iface_check_funcs + i + 1,
2589 sizeof (static_iface_check_funcs[0]) * (static_n_iface_check_funcs - i));
2590 static_iface_check_funcs = g_renew (IFaceCheckFunc, static_iface_check_funcs, static_n_iface_check_funcs);
2591 found_it = TRUE;
2592 break;
2593 }
2594 G_WRITE_UNLOCK (&type_rw_lock);
2595
2596 if (!found_it)
2597 g_warning (G_STRLOC ": cannot remove unregistered class check func %p with data %p",
2598 check_func, check_data);
2599 }
2600
2601 /* --- type registration --- */
2602 /**
2603 * g_type_register_fundamental:
2604 * @type_id: a predefined type identifier
2605 * @type_name: 0-terminated string used as the name of the new type
2606 * @info: #GTypeInfo structure for this type
2607 * @finfo: #GTypeFundamentalInfo structure for this type
2608 * @flags: bitwise combination of #GTypeFlags values
2609 *
2610 * Registers @type_id as the predefined identifier and @type_name as the
2611 * name of a fundamental type. If @type_id is already registered, or a
2612 * type named @type_name is already registered, the behaviour is undefined.
2613 * The type system uses the information contained in the #GTypeInfo structure
2614 * pointed to by @info and the #GTypeFundamentalInfo structure pointed to by
2615 * @finfo to manage the type and its instances. The value of @flags determines
2616 * additional characteristics of the fundamental type.
2617 *
2618 * Returns: the predefined type identifier
2619 */
2620 GType
g_type_register_fundamental(GType type_id,const gchar * type_name,const GTypeInfo * info,const GTypeFundamentalInfo * finfo,GTypeFlags flags)2621 g_type_register_fundamental (GType type_id,
2622 const gchar *type_name,
2623 const GTypeInfo *info,
2624 const GTypeFundamentalInfo *finfo,
2625 GTypeFlags flags)
2626 {
2627 TypeNode *node;
2628
2629 g_assert_type_system_initialized ();
2630 g_return_val_if_fail (type_id > 0, 0);
2631 g_return_val_if_fail (type_name != NULL, 0);
2632 g_return_val_if_fail (info != NULL, 0);
2633 g_return_val_if_fail (finfo != NULL, 0);
2634
2635 if (!check_type_name_I (type_name))
2636 return 0;
2637 if ((type_id & TYPE_ID_MASK) ||
2638 type_id > G_TYPE_FUNDAMENTAL_MAX)
2639 {
2640 g_warning ("attempt to register fundamental type '%s' with invalid type id (%" G_GSIZE_FORMAT ")",
2641 type_name,
2642 type_id);
2643 return 0;
2644 }
2645 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
2646 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
2647 {
2648 g_warning ("cannot register instantiatable fundamental type '%s' as non-classed",
2649 type_name);
2650 return 0;
2651 }
2652 if (lookup_type_node_I (type_id))
2653 {
2654 g_warning ("cannot register existing fundamental type '%s' (as '%s')",
2655 type_descriptive_name_I (type_id),
2656 type_name);
2657 return 0;
2658 }
2659
2660 G_WRITE_LOCK (&type_rw_lock);
2661 node = type_node_fundamental_new_W (type_id, type_name, finfo->type_flags);
2662 type_add_flags_W (node, flags);
2663
2664 if (check_type_info_I (NULL, NODE_FUNDAMENTAL_TYPE (node), type_name, info))
2665 type_data_make_W (node, info,
2666 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2667 G_WRITE_UNLOCK (&type_rw_lock);
2668
2669 return NODE_TYPE (node);
2670 }
2671
2672 /**
2673 * g_type_register_static_simple: (skip)
2674 * @parent_type: type from which this type will be derived
2675 * @type_name: 0-terminated string used as the name of the new type
2676 * @class_size: size of the class structure (see #GTypeInfo)
2677 * @class_init: location of the class initialization function (see #GTypeInfo)
2678 * @instance_size: size of the instance structure (see #GTypeInfo)
2679 * @instance_init: location of the instance initialization function (see #GTypeInfo)
2680 * @flags: bitwise combination of #GTypeFlags values
2681 *
2682 * Registers @type_name as the name of a new static type derived from
2683 * @parent_type. The value of @flags determines the nature (e.g.
2684 * abstract or not) of the type. It works by filling a #GTypeInfo
2685 * struct and calling g_type_register_static().
2686 *
2687 * Since: 2.12
2688 *
2689 * Returns: the new type identifier
2690 */
2691 GType
g_type_register_static_simple(GType parent_type,const gchar * type_name,guint class_size,GClassInitFunc class_init,guint instance_size,GInstanceInitFunc instance_init,GTypeFlags flags)2692 g_type_register_static_simple (GType parent_type,
2693 const gchar *type_name,
2694 guint class_size,
2695 GClassInitFunc class_init,
2696 guint instance_size,
2697 GInstanceInitFunc instance_init,
2698 GTypeFlags flags)
2699 {
2700 GTypeInfo info;
2701
2702 /* Instances are not allowed to be larger than this. If you have a big
2703 * fixed-length array or something, point to it instead.
2704 */
2705 g_return_val_if_fail (class_size <= G_MAXUINT16, G_TYPE_INVALID);
2706 g_return_val_if_fail (instance_size <= G_MAXUINT16, G_TYPE_INVALID);
2707
2708 info.class_size = class_size;
2709 info.base_init = NULL;
2710 info.base_finalize = NULL;
2711 info.class_init = class_init;
2712 info.class_finalize = NULL;
2713 info.class_data = NULL;
2714 info.instance_size = instance_size;
2715 info.n_preallocs = 0;
2716 info.instance_init = instance_init;
2717 info.value_table = NULL;
2718
2719 return g_type_register_static (parent_type, type_name, &info, flags);
2720 }
2721
2722 /**
2723 * g_type_register_static:
2724 * @parent_type: type from which this type will be derived
2725 * @type_name: 0-terminated string used as the name of the new type
2726 * @info: #GTypeInfo structure for this type
2727 * @flags: bitwise combination of #GTypeFlags values
2728 *
2729 * Registers @type_name as the name of a new static type derived from
2730 * @parent_type. The type system uses the information contained in the
2731 * #GTypeInfo structure pointed to by @info to manage the type and its
2732 * instances (if not abstract). The value of @flags determines the nature
2733 * (e.g. abstract or not) of the type.
2734 *
2735 * Returns: the new type identifier
2736 */
2737 GType
g_type_register_static(GType parent_type,const gchar * type_name,const GTypeInfo * info,GTypeFlags flags)2738 g_type_register_static (GType parent_type,
2739 const gchar *type_name,
2740 const GTypeInfo *info,
2741 GTypeFlags flags)
2742 {
2743 TypeNode *pnode, *node;
2744 GType type = 0;
2745
2746 g_assert_type_system_initialized ();
2747 g_return_val_if_fail (parent_type > 0, 0);
2748 g_return_val_if_fail (type_name != NULL, 0);
2749 g_return_val_if_fail (info != NULL, 0);
2750
2751 if (!check_type_name_I (type_name) ||
2752 !check_derivation_I (parent_type, type_name))
2753 return 0;
2754 if (info->class_finalize)
2755 {
2756 g_warning ("class finalizer specified for static type '%s'",
2757 type_name);
2758 return 0;
2759 }
2760
2761 pnode = lookup_type_node_I (parent_type);
2762 G_WRITE_LOCK (&type_rw_lock);
2763 type_data_ref_Wm (pnode);
2764 if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info))
2765 {
2766 node = type_node_new_W (pnode, type_name, NULL);
2767 type_add_flags_W (node, flags);
2768 type = NODE_TYPE (node);
2769 type_data_make_W (node, info,
2770 check_value_table_I (type_name, info->value_table) ? info->value_table : NULL);
2771 }
2772 G_WRITE_UNLOCK (&type_rw_lock);
2773
2774 return type;
2775 }
2776
2777 /**
2778 * g_type_register_dynamic:
2779 * @parent_type: type from which this type will be derived
2780 * @type_name: 0-terminated string used as the name of the new type
2781 * @plugin: #GTypePlugin structure to retrieve the #GTypeInfo from
2782 * @flags: bitwise combination of #GTypeFlags values
2783 *
2784 * Registers @type_name as the name of a new dynamic type derived from
2785 * @parent_type. The type system uses the information contained in the
2786 * #GTypePlugin structure pointed to by @plugin to manage the type and its
2787 * instances (if not abstract). The value of @flags determines the nature
2788 * (e.g. abstract or not) of the type.
2789 *
2790 * Returns: the new type identifier or #G_TYPE_INVALID if registration failed
2791 */
2792 GType
g_type_register_dynamic(GType parent_type,const gchar * type_name,GTypePlugin * plugin,GTypeFlags flags)2793 g_type_register_dynamic (GType parent_type,
2794 const gchar *type_name,
2795 GTypePlugin *plugin,
2796 GTypeFlags flags)
2797 {
2798 TypeNode *pnode, *node;
2799 GType type;
2800
2801 g_assert_type_system_initialized ();
2802 g_return_val_if_fail (parent_type > 0, 0);
2803 g_return_val_if_fail (type_name != NULL, 0);
2804 g_return_val_if_fail (plugin != NULL, 0);
2805
2806 if (!check_type_name_I (type_name) ||
2807 !check_derivation_I (parent_type, type_name) ||
2808 !check_plugin_U (plugin, TRUE, FALSE, type_name))
2809 return 0;
2810
2811 G_WRITE_LOCK (&type_rw_lock);
2812 pnode = lookup_type_node_I (parent_type);
2813 node = type_node_new_W (pnode, type_name, plugin);
2814 type_add_flags_W (node, flags);
2815 type = NODE_TYPE (node);
2816 G_WRITE_UNLOCK (&type_rw_lock);
2817
2818 return type;
2819 }
2820
2821 /**
2822 * g_type_add_interface_static:
2823 * @instance_type: #GType value of an instantiable type
2824 * @interface_type: #GType value of an interface type
2825 * @info: #GInterfaceInfo structure for this
2826 * (@instance_type, @interface_type) combination
2827 *
2828 * Adds the static @interface_type to @instantiable_type.
2829 * The information contained in the #GInterfaceInfo structure
2830 * pointed to by @info is used to manage the relationship.
2831 */
2832 void
g_type_add_interface_static(GType instance_type,GType interface_type,const GInterfaceInfo * info)2833 g_type_add_interface_static (GType instance_type,
2834 GType interface_type,
2835 const GInterfaceInfo *info)
2836 {
2837 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2838 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2839 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2840
2841 /* we only need to lock class_init_rec_mutex if instance_type already has its
2842 * class initialized, however this function is rarely enough called to take
2843 * the simple route and always acquire class_init_rec_mutex.
2844 */
2845 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2846 G_WRITE_LOCK (&type_rw_lock);
2847 if (check_add_interface_L (instance_type, interface_type))
2848 {
2849 TypeNode *node = lookup_type_node_I (instance_type);
2850 TypeNode *iface = lookup_type_node_I (interface_type);
2851 if (check_interface_info_I (iface, NODE_TYPE (node), info))
2852 type_add_interface_Wm (node, iface, info, NULL);
2853 }
2854 G_WRITE_UNLOCK (&type_rw_lock);
2855 g_rec_mutex_unlock (&class_init_rec_mutex);
2856 }
2857
2858 /**
2859 * g_type_add_interface_dynamic:
2860 * @instance_type: #GType value of an instantiable type
2861 * @interface_type: #GType value of an interface type
2862 * @plugin: #GTypePlugin structure to retrieve the #GInterfaceInfo from
2863 *
2864 * Adds the dynamic @interface_type to @instantiable_type. The information
2865 * contained in the #GTypePlugin structure pointed to by @plugin
2866 * is used to manage the relationship.
2867 */
2868 void
g_type_add_interface_dynamic(GType instance_type,GType interface_type,GTypePlugin * plugin)2869 g_type_add_interface_dynamic (GType instance_type,
2870 GType interface_type,
2871 GTypePlugin *plugin)
2872 {
2873 TypeNode *node;
2874 /* G_TYPE_IS_INSTANTIATABLE() is an external call: _U */
2875 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
2876 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
2877
2878 node = lookup_type_node_I (instance_type);
2879 if (!check_plugin_U (plugin, FALSE, TRUE, NODE_NAME (node)))
2880 return;
2881
2882 /* see comment in g_type_add_interface_static() about class_init_rec_mutex */
2883 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2884 G_WRITE_LOCK (&type_rw_lock);
2885 if (check_add_interface_L (instance_type, interface_type))
2886 {
2887 TypeNode *iface = lookup_type_node_I (interface_type);
2888 type_add_interface_Wm (node, iface, NULL, plugin);
2889 }
2890 G_WRITE_UNLOCK (&type_rw_lock);
2891 g_rec_mutex_unlock (&class_init_rec_mutex);
2892 }
2893
2894
2895 /* --- public API functions --- */
2896 /**
2897 * g_type_class_ref:
2898 * @type: type ID of a classed type
2899 *
2900 * Increments the reference count of the class structure belonging to
2901 * @type. This function will demand-create the class if it doesn't
2902 * exist already.
2903 *
2904 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
2905 * structure for the given type ID
2906 */
2907 gpointer
g_type_class_ref(GType type)2908 g_type_class_ref (GType type)
2909 {
2910 TypeNode *node;
2911 GType ptype;
2912 gboolean holds_ref;
2913 GTypeClass *pclass;
2914
2915 /* optimize for common code path */
2916 node = lookup_type_node_I (type);
2917 if (!node || !node->is_classed)
2918 {
2919 g_warning ("cannot retrieve class for invalid (unclassed) type '%s'",
2920 type_descriptive_name_I (type));
2921 return NULL;
2922 }
2923
2924 if (G_LIKELY (type_data_ref_U (node)))
2925 {
2926 if (G_LIKELY (g_atomic_int_get (&node->data->class.init_state) == INITIALIZED))
2927 return node->data->class.class;
2928 holds_ref = TRUE;
2929 }
2930 else
2931 holds_ref = FALSE;
2932
2933 /* here, we either have node->data->class.class == NULL, or a recursive
2934 * call to g_type_class_ref() with a partly initialized class, or
2935 * node->data->class.init_state == INITIALIZED, because any
2936 * concurrently running initialization was guarded by class_init_rec_mutex.
2937 */
2938 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
2939
2940 /* we need an initialized parent class for initializing derived classes */
2941 ptype = NODE_PARENT_TYPE (node);
2942 pclass = ptype ? g_type_class_ref (ptype) : NULL;
2943
2944 G_WRITE_LOCK (&type_rw_lock);
2945
2946 if (!holds_ref)
2947 type_data_ref_Wm (node);
2948
2949 if (!node->data->class.class) /* class uninitialized */
2950 type_class_init_Wm (node, pclass);
2951
2952 G_WRITE_UNLOCK (&type_rw_lock);
2953
2954 if (pclass)
2955 g_type_class_unref (pclass);
2956
2957 g_rec_mutex_unlock (&class_init_rec_mutex);
2958
2959 return node->data->class.class;
2960 }
2961
2962 /**
2963 * g_type_class_unref:
2964 * @g_class: (type GObject.TypeClass): a #GTypeClass structure to unref
2965 *
2966 * Decrements the reference count of the class structure being passed in.
2967 * Once the last reference count of a class has been released, classes
2968 * may be finalized by the type system, so further dereferencing of a
2969 * class pointer after g_type_class_unref() are invalid.
2970 */
2971 void
g_type_class_unref(gpointer g_class)2972 g_type_class_unref (gpointer g_class)
2973 {
2974 TypeNode *node;
2975 GTypeClass *class = g_class;
2976
2977 g_return_if_fail (g_class != NULL);
2978
2979 node = lookup_type_node_I (class->g_type);
2980 if (node && node->is_classed && NODE_REFCOUNT (node))
2981 type_data_unref_U (node, FALSE);
2982 else
2983 g_warning ("cannot unreference class of invalid (unclassed) type '%s'",
2984 type_descriptive_name_I (class->g_type));
2985 }
2986
2987 /**
2988 * g_type_class_unref_uncached: (skip)
2989 * @g_class: (type GObject.TypeClass): a #GTypeClass structure to unref
2990 *
2991 * A variant of g_type_class_unref() for use in #GTypeClassCacheFunc
2992 * implementations. It unreferences a class without consulting the chain
2993 * of #GTypeClassCacheFuncs, avoiding the recursion which would occur
2994 * otherwise.
2995 */
2996 void
g_type_class_unref_uncached(gpointer g_class)2997 g_type_class_unref_uncached (gpointer g_class)
2998 {
2999 TypeNode *node;
3000 GTypeClass *class = g_class;
3001
3002 g_return_if_fail (g_class != NULL);
3003
3004 node = lookup_type_node_I (class->g_type);
3005 if (node && node->is_classed && NODE_REFCOUNT (node))
3006 type_data_unref_U (node, TRUE);
3007 else
3008 g_warning ("cannot unreference class of invalid (unclassed) type '%s'",
3009 type_descriptive_name_I (class->g_type));
3010 }
3011
3012 /**
3013 * g_type_class_peek:
3014 * @type: type ID of a classed type
3015 *
3016 * This function is essentially the same as g_type_class_ref(),
3017 * except that the classes reference count isn't incremented.
3018 * As a consequence, this function may return %NULL if the class
3019 * of the type passed in does not currently exist (hasn't been
3020 * referenced before).
3021 *
3022 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
3023 * structure for the given type ID or %NULL if the class does not
3024 * currently exist
3025 */
3026 gpointer
g_type_class_peek(GType type)3027 g_type_class_peek (GType type)
3028 {
3029 TypeNode *node;
3030 gpointer class;
3031
3032 node = lookup_type_node_I (type);
3033 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3034 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3035 /* ref_count _may_ be 0 */
3036 class = node->data->class.class;
3037 else
3038 class = NULL;
3039
3040 return class;
3041 }
3042
3043 /**
3044 * g_type_class_peek_static:
3045 * @type: type ID of a classed type
3046 *
3047 * A more efficient version of g_type_class_peek() which works only for
3048 * static types.
3049 *
3050 * Returns: (type GObject.TypeClass) (transfer none): the #GTypeClass
3051 * structure for the given type ID or %NULL if the class does not
3052 * currently exist or is dynamically loaded
3053 *
3054 * Since: 2.4
3055 */
3056 gpointer
g_type_class_peek_static(GType type)3057 g_type_class_peek_static (GType type)
3058 {
3059 TypeNode *node;
3060 gpointer class;
3061
3062 node = lookup_type_node_I (type);
3063 if (node && node->is_classed && NODE_REFCOUNT (node) &&
3064 /* peek only static types: */ node->plugin == NULL &&
3065 g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)
3066 /* ref_count _may_ be 0 */
3067 class = node->data->class.class;
3068 else
3069 class = NULL;
3070
3071 return class;
3072 }
3073
3074 /**
3075 * g_type_class_peek_parent:
3076 * @g_class: (type GObject.TypeClass): the #GTypeClass structure to
3077 * retrieve the parent class for
3078 *
3079 * This is a convenience function often needed in class initializers.
3080 * It returns the class structure of the immediate parent type of the
3081 * class passed in. Since derived classes hold a reference count on
3082 * their parent classes as long as they are instantiated, the returned
3083 * class will always exist.
3084 *
3085 * This function is essentially equivalent to:
3086 * g_type_class_peek (g_type_parent (G_TYPE_FROM_CLASS (g_class)))
3087 *
3088 * Returns: (type GObject.TypeClass) (transfer none): the parent class
3089 * of @g_class
3090 */
3091 gpointer
g_type_class_peek_parent(gpointer g_class)3092 g_type_class_peek_parent (gpointer g_class)
3093 {
3094 TypeNode *node;
3095 gpointer class = NULL;
3096
3097 g_return_val_if_fail (g_class != NULL, NULL);
3098
3099 node = lookup_type_node_I (G_TYPE_FROM_CLASS (g_class));
3100 /* We used to acquire a read lock here. That is not necessary, since
3101 * parent->data->class.class is constant as long as the derived class
3102 * exists.
3103 */
3104 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
3105 {
3106 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3107 class = node->data->class.class;
3108 }
3109 else if (NODE_PARENT_TYPE (node))
3110 g_warning (G_STRLOC ": invalid class pointer '%p'", g_class);
3111
3112 return class;
3113 }
3114
3115 /**
3116 * g_type_interface_peek:
3117 * @instance_class: (type GObject.TypeClass): a #GTypeClass structure
3118 * @iface_type: an interface ID which this class conforms to
3119 *
3120 * Returns the #GTypeInterface structure of an interface to which the
3121 * passed in class conforms.
3122 *
3123 * Returns: (type GObject.TypeInterface) (transfer none): the #GTypeInterface
3124 * structure of @iface_type if implemented by @instance_class, %NULL
3125 * otherwise
3126 */
3127 gpointer
g_type_interface_peek(gpointer instance_class,GType iface_type)3128 g_type_interface_peek (gpointer instance_class,
3129 GType iface_type)
3130 {
3131 TypeNode *node;
3132 TypeNode *iface;
3133 gpointer vtable = NULL;
3134 GTypeClass *class = instance_class;
3135
3136 g_return_val_if_fail (instance_class != NULL, NULL);
3137
3138 node = lookup_type_node_I (class->g_type);
3139 iface = lookup_type_node_I (iface_type);
3140 if (node && node->is_instantiatable && iface)
3141 type_lookup_iface_vtable_I (node, iface, &vtable);
3142 else
3143 g_warning (G_STRLOC ": invalid class pointer '%p'", class);
3144
3145 return vtable;
3146 }
3147
3148 /**
3149 * g_type_interface_peek_parent:
3150 * @g_iface: (type GObject.TypeInterface): a #GTypeInterface structure
3151 *
3152 * Returns the corresponding #GTypeInterface structure of the parent type
3153 * of the instance type to which @g_iface belongs. This is useful when
3154 * deriving the implementation of an interface from the parent type and
3155 * then possibly overriding some methods.
3156 *
3157 * Returns: (transfer none) (type GObject.TypeInterface): the
3158 * corresponding #GTypeInterface structure of the parent type of the
3159 * instance type to which @g_iface belongs, or %NULL if the parent
3160 * type doesn't conform to the interface
3161 */
3162 gpointer
g_type_interface_peek_parent(gpointer g_iface)3163 g_type_interface_peek_parent (gpointer g_iface)
3164 {
3165 TypeNode *node;
3166 TypeNode *iface;
3167 gpointer vtable = NULL;
3168 GTypeInterface *iface_class = g_iface;
3169
3170 g_return_val_if_fail (g_iface != NULL, NULL);
3171
3172 iface = lookup_type_node_I (iface_class->g_type);
3173 node = lookup_type_node_I (iface_class->g_instance_type);
3174 if (node)
3175 node = lookup_type_node_I (NODE_PARENT_TYPE (node));
3176 if (node && node->is_instantiatable && iface)
3177 type_lookup_iface_vtable_I (node, iface, &vtable);
3178 else if (node)
3179 g_warning (G_STRLOC ": invalid interface pointer '%p'", g_iface);
3180
3181 return vtable;
3182 }
3183
3184 /**
3185 * g_type_default_interface_ref:
3186 * @g_type: an interface type
3187 *
3188 * Increments the reference count for the interface type @g_type,
3189 * and returns the default interface vtable for the type.
3190 *
3191 * If the type is not currently in use, then the default vtable
3192 * for the type will be created and initalized by calling
3193 * the base interface init and default vtable init functions for
3194 * the type (the @base_init and @class_init members of #GTypeInfo).
3195 * Calling g_type_default_interface_ref() is useful when you
3196 * want to make sure that signals and properties for an interface
3197 * have been installed.
3198 *
3199 * Since: 2.4
3200 *
3201 * Returns: (type GObject.TypeInterface) (transfer none): the default
3202 * vtable for the interface; call g_type_default_interface_unref()
3203 * when you are done using the interface.
3204 */
3205 gpointer
g_type_default_interface_ref(GType g_type)3206 g_type_default_interface_ref (GType g_type)
3207 {
3208 TypeNode *node;
3209 gpointer dflt_vtable;
3210
3211 G_WRITE_LOCK (&type_rw_lock);
3212
3213 node = lookup_type_node_I (g_type);
3214 if (!node || !NODE_IS_IFACE (node) ||
3215 (node->data && NODE_REFCOUNT (node) == 0))
3216 {
3217 G_WRITE_UNLOCK (&type_rw_lock);
3218 g_warning ("cannot retrieve default vtable for invalid or non-interface type '%s'",
3219 type_descriptive_name_I (g_type));
3220 return NULL;
3221 }
3222
3223 if (!node->data || !node->data->iface.dflt_vtable)
3224 {
3225 G_WRITE_UNLOCK (&type_rw_lock);
3226 g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
3227 G_WRITE_LOCK (&type_rw_lock);
3228 node = lookup_type_node_I (g_type);
3229 type_data_ref_Wm (node);
3230 type_iface_ensure_dflt_vtable_Wm (node);
3231 g_rec_mutex_unlock (&class_init_rec_mutex);
3232 }
3233 else
3234 type_data_ref_Wm (node); /* ref_count >= 1 already */
3235
3236 dflt_vtable = node->data->iface.dflt_vtable;
3237 G_WRITE_UNLOCK (&type_rw_lock);
3238
3239 return dflt_vtable;
3240 }
3241
3242 /**
3243 * g_type_default_interface_peek:
3244 * @g_type: an interface type
3245 *
3246 * If the interface type @g_type is currently in use, returns its
3247 * default interface vtable.
3248 *
3249 * Since: 2.4
3250 *
3251 * Returns: (type GObject.TypeInterface) (transfer none): the default
3252 * vtable for the interface, or %NULL if the type is not currently
3253 * in use
3254 */
3255 gpointer
g_type_default_interface_peek(GType g_type)3256 g_type_default_interface_peek (GType g_type)
3257 {
3258 TypeNode *node;
3259 gpointer vtable;
3260
3261 node = lookup_type_node_I (g_type);
3262 if (node && NODE_IS_IFACE (node) && NODE_REFCOUNT (node))
3263 vtable = node->data->iface.dflt_vtable;
3264 else
3265 vtable = NULL;
3266
3267 return vtable;
3268 }
3269
3270 /**
3271 * g_type_default_interface_unref:
3272 * @g_iface: (type GObject.TypeInterface): the default vtable
3273 * structure for an interface, as returned by g_type_default_interface_ref()
3274 *
3275 * Decrements the reference count for the type corresponding to the
3276 * interface default vtable @g_iface. If the type is dynamic, then
3277 * when no one is using the interface and all references have
3278 * been released, the finalize function for the interface's default
3279 * vtable (the @class_finalize member of #GTypeInfo) will be called.
3280 *
3281 * Since: 2.4
3282 */
3283 void
g_type_default_interface_unref(gpointer g_iface)3284 g_type_default_interface_unref (gpointer g_iface)
3285 {
3286 TypeNode *node;
3287 GTypeInterface *vtable = g_iface;
3288
3289 g_return_if_fail (g_iface != NULL);
3290
3291 node = lookup_type_node_I (vtable->g_type);
3292 if (node && NODE_IS_IFACE (node))
3293 type_data_unref_U (node, FALSE);
3294 else
3295 g_warning ("cannot unreference invalid interface default vtable for '%s'",
3296 type_descriptive_name_I (vtable->g_type));
3297 }
3298
3299 /**
3300 * g_type_name:
3301 * @type: type to return name for
3302 *
3303 * Get the unique name that is assigned to a type ID. Note that this
3304 * function (like all other GType API) cannot cope with invalid type
3305 * IDs. %G_TYPE_INVALID may be passed to this function, as may be any
3306 * other validly registered type ID, but randomized type IDs should
3307 * not be passed in and will most likely lead to a crash.
3308 *
3309 * Returns: static type name or %NULL
3310 */
3311 const gchar *
g_type_name(GType type)3312 g_type_name (GType type)
3313 {
3314 TypeNode *node;
3315
3316 g_assert_type_system_initialized ();
3317
3318 node = lookup_type_node_I (type);
3319
3320 return node ? NODE_NAME (node) : NULL;
3321 }
3322
3323 /**
3324 * g_type_qname:
3325 * @type: type to return quark of type name for
3326 *
3327 * Get the corresponding quark of the type IDs name.
3328 *
3329 * Returns: the type names quark or 0
3330 */
3331 GQuark
g_type_qname(GType type)3332 g_type_qname (GType type)
3333 {
3334 TypeNode *node;
3335
3336 node = lookup_type_node_I (type);
3337
3338 return node ? node->qname : 0;
3339 }
3340
3341 /**
3342 * g_type_from_name:
3343 * @name: type name to look up
3344 *
3345 * Look up the type ID from a given type name, returning 0 if no type
3346 * has been registered under this name (this is the preferred method
3347 * to find out by name whether a specific type has been registered
3348 * yet).
3349 *
3350 * Returns: corresponding type ID or 0
3351 */
3352 GType
g_type_from_name(const gchar * name)3353 g_type_from_name (const gchar *name)
3354 {
3355 GType type = 0;
3356
3357 g_return_val_if_fail (name != NULL, 0);
3358
3359 G_READ_LOCK (&type_rw_lock);
3360 type = (GType) g_hash_table_lookup (static_type_nodes_ht, name);
3361 G_READ_UNLOCK (&type_rw_lock);
3362
3363 return type;
3364 }
3365
3366 /**
3367 * g_type_parent:
3368 * @type: the derived type
3369 *
3370 * Return the direct parent type of the passed in type. If the passed
3371 * in type has no parent, i.e. is a fundamental type, 0 is returned.
3372 *
3373 * Returns: the parent type
3374 */
3375 GType
g_type_parent(GType type)3376 g_type_parent (GType type)
3377 {
3378 TypeNode *node;
3379
3380 node = lookup_type_node_I (type);
3381
3382 return node ? NODE_PARENT_TYPE (node) : 0;
3383 }
3384
3385 /**
3386 * g_type_depth:
3387 * @type: a #GType
3388 *
3389 * Returns the length of the ancestry of the passed in type. This
3390 * includes the type itself, so that e.g. a fundamental type has depth 1.
3391 *
3392 * Returns: the depth of @type
3393 */
3394 guint
g_type_depth(GType type)3395 g_type_depth (GType type)
3396 {
3397 TypeNode *node;
3398
3399 node = lookup_type_node_I (type);
3400
3401 return node ? node->n_supers + 1 : 0;
3402 }
3403
3404 /**
3405 * g_type_next_base:
3406 * @leaf_type: descendant of @root_type and the type to be returned
3407 * @root_type: immediate parent of the returned type
3408 *
3409 * Given a @leaf_type and a @root_type which is contained in its
3410 * anchestry, return the type that @root_type is the immediate parent
3411 * of. In other words, this function determines the type that is
3412 * derived directly from @root_type which is also a base class of
3413 * @leaf_type. Given a root type and a leaf type, this function can
3414 * be used to determine the types and order in which the leaf type is
3415 * descended from the root type.
3416 *
3417 * Returns: immediate child of @root_type and anchestor of @leaf_type
3418 */
3419 GType
g_type_next_base(GType type,GType base_type)3420 g_type_next_base (GType type,
3421 GType base_type)
3422 {
3423 GType atype = 0;
3424 TypeNode *node;
3425
3426 node = lookup_type_node_I (type);
3427 if (node)
3428 {
3429 TypeNode *base_node = lookup_type_node_I (base_type);
3430
3431 if (base_node && base_node->n_supers < node->n_supers)
3432 {
3433 guint n = node->n_supers - base_node->n_supers;
3434
3435 if (node->supers[n] == base_type)
3436 atype = node->supers[n - 1];
3437 }
3438 }
3439
3440 return atype;
3441 }
3442
3443 static inline gboolean
type_node_check_conformities_UorL(TypeNode * node,TypeNode * iface_node,gboolean support_interfaces,gboolean support_prerequisites,gboolean have_lock)3444 type_node_check_conformities_UorL (TypeNode *node,
3445 TypeNode *iface_node,
3446 /* support_inheritance */
3447 gboolean support_interfaces,
3448 gboolean support_prerequisites,
3449 gboolean have_lock)
3450 {
3451 gboolean match;
3452
3453 if (/* support_inheritance && */
3454 NODE_IS_ANCESTOR (iface_node, node))
3455 return TRUE;
3456
3457 support_interfaces = support_interfaces && node->is_instantiatable && NODE_IS_IFACE (iface_node);
3458 support_prerequisites = support_prerequisites && NODE_IS_IFACE (node);
3459 match = FALSE;
3460 if (support_interfaces)
3461 {
3462 if (have_lock)
3463 {
3464 if (type_lookup_iface_entry_L (node, iface_node))
3465 match = TRUE;
3466 }
3467 else
3468 {
3469 if (type_lookup_iface_vtable_I (node, iface_node, NULL))
3470 match = TRUE;
3471 }
3472 }
3473 if (!match &&
3474 support_prerequisites)
3475 {
3476 if (!have_lock)
3477 G_READ_LOCK (&type_rw_lock);
3478 if (support_prerequisites && type_lookup_prerequisite_L (node, NODE_TYPE (iface_node)))
3479 match = TRUE;
3480 if (!have_lock)
3481 G_READ_UNLOCK (&type_rw_lock);
3482 }
3483 return match;
3484 }
3485
3486 static gboolean
type_node_is_a_L(TypeNode * node,TypeNode * iface_node)3487 type_node_is_a_L (TypeNode *node,
3488 TypeNode *iface_node)
3489 {
3490 return type_node_check_conformities_UorL (node, iface_node, TRUE, TRUE, TRUE);
3491 }
3492
3493 static inline gboolean
type_node_conforms_to_U(TypeNode * node,TypeNode * iface_node,gboolean support_interfaces,gboolean support_prerequisites)3494 type_node_conforms_to_U (TypeNode *node,
3495 TypeNode *iface_node,
3496 gboolean support_interfaces,
3497 gboolean support_prerequisites)
3498 {
3499 return type_node_check_conformities_UorL (node, iface_node, support_interfaces, support_prerequisites, FALSE);
3500 }
3501
3502 /**
3503 * g_type_is_a:
3504 * @type: type to check anchestry for
3505 * @is_a_type: possible anchestor of @type or interface that @type
3506 * could conform to
3507 *
3508 * If @is_a_type is a derivable type, check whether @type is a
3509 * descendant of @is_a_type. If @is_a_type is an interface, check
3510 * whether @type conforms to it.
3511 *
3512 * Returns: %TRUE if @type is a @is_a_type
3513 */
3514 gboolean
g_type_is_a(GType type,GType iface_type)3515 g_type_is_a (GType type,
3516 GType iface_type)
3517 {
3518 TypeNode *node, *iface_node;
3519 gboolean is_a;
3520
3521 if (type == iface_type)
3522 return TRUE;
3523
3524 node = lookup_type_node_I (type);
3525 iface_node = lookup_type_node_I (iface_type);
3526 is_a = node && iface_node && type_node_conforms_to_U (node, iface_node, TRUE, TRUE);
3527
3528 return is_a;
3529 }
3530
3531 /**
3532 * g_type_children:
3533 * @type: the parent type
3534 * @n_children: (out) (optional): location to store the length of
3535 * the returned array, or %NULL
3536 *
3537 * Return a newly allocated and 0-terminated array of type IDs, listing
3538 * the child types of @type.
3539 *
3540 * Returns: (array length=n_children) (transfer full): Newly allocated
3541 * and 0-terminated array of child types, free with g_free()
3542 */
3543 GType*
g_type_children(GType type,guint * n_children)3544 g_type_children (GType type,
3545 guint *n_children)
3546 {
3547 TypeNode *node;
3548
3549 node = lookup_type_node_I (type);
3550 if (node)
3551 {
3552 GType *children;
3553
3554 G_READ_LOCK (&type_rw_lock); /* ->children is relocatable */
3555 children = g_new (GType, node->n_children + 1);
3556 if (node->n_children != 0)
3557 memcpy (children, node->children, sizeof (GType) * node->n_children);
3558 children[node->n_children] = 0;
3559
3560 if (n_children)
3561 *n_children = node->n_children;
3562 G_READ_UNLOCK (&type_rw_lock);
3563
3564 return children;
3565 }
3566 else
3567 {
3568 if (n_children)
3569 *n_children = 0;
3570
3571 return NULL;
3572 }
3573 }
3574
3575 /**
3576 * g_type_interfaces:
3577 * @type: the type to list interface types for
3578 * @n_interfaces: (out) (optional): location to store the length of
3579 * the returned array, or %NULL
3580 *
3581 * Return a newly allocated and 0-terminated array of type IDs, listing
3582 * the interface types that @type conforms to.
3583 *
3584 * Returns: (array length=n_interfaces) (transfer full): Newly allocated
3585 * and 0-terminated array of interface types, free with g_free()
3586 */
3587 GType*
g_type_interfaces(GType type,guint * n_interfaces)3588 g_type_interfaces (GType type,
3589 guint *n_interfaces)
3590 {
3591 TypeNode *node;
3592
3593 node = lookup_type_node_I (type);
3594 if (node && node->is_instantiatable)
3595 {
3596 IFaceEntries *entries;
3597 GType *ifaces;
3598 guint i;
3599
3600 G_READ_LOCK (&type_rw_lock);
3601 entries = CLASSED_NODE_IFACES_ENTRIES_LOCKED (node);
3602 if (entries)
3603 {
3604 ifaces = g_new (GType, IFACE_ENTRIES_N_ENTRIES (entries) + 1);
3605 for (i = 0; i < IFACE_ENTRIES_N_ENTRIES (entries); i++)
3606 ifaces[i] = entries->entry[i].iface_type;
3607 }
3608 else
3609 {
3610 ifaces = g_new (GType, 1);
3611 i = 0;
3612 }
3613 ifaces[i] = 0;
3614
3615 if (n_interfaces)
3616 *n_interfaces = i;
3617 G_READ_UNLOCK (&type_rw_lock);
3618
3619 return ifaces;
3620 }
3621 else
3622 {
3623 if (n_interfaces)
3624 *n_interfaces = 0;
3625
3626 return NULL;
3627 }
3628 }
3629
3630 typedef struct _QData QData;
3631 struct _GData
3632 {
3633 guint n_qdatas;
3634 QData *qdatas;
3635 };
3636 struct _QData
3637 {
3638 GQuark quark;
3639 gpointer data;
3640 };
3641
3642 static inline gpointer
type_get_qdata_L(TypeNode * node,GQuark quark)3643 type_get_qdata_L (TypeNode *node,
3644 GQuark quark)
3645 {
3646 GData *gdata = node->global_gdata;
3647
3648 if (quark && gdata && gdata->n_qdatas)
3649 {
3650 QData *qdatas = gdata->qdatas - 1;
3651 guint n_qdatas = gdata->n_qdatas;
3652
3653 do
3654 {
3655 guint i;
3656 QData *check;
3657
3658 i = (n_qdatas + 1) / 2;
3659 check = qdatas + i;
3660 if (quark == check->quark)
3661 return check->data;
3662 else if (quark > check->quark)
3663 {
3664 n_qdatas -= i;
3665 qdatas = check;
3666 }
3667 else /* if (quark < check->quark) */
3668 n_qdatas = i - 1;
3669 }
3670 while (n_qdatas);
3671 }
3672 return NULL;
3673 }
3674
3675 /**
3676 * g_type_get_qdata:
3677 * @type: a #GType
3678 * @quark: a #GQuark id to identify the data
3679 *
3680 * Obtains data which has previously been attached to @type
3681 * with g_type_set_qdata().
3682 *
3683 * Note that this does not take subtyping into account; data
3684 * attached to one type with g_type_set_qdata() cannot
3685 * be retrieved from a subtype using g_type_get_qdata().
3686 *
3687 * Returns: (transfer none): the data, or %NULL if no data was found
3688 */
3689 gpointer
g_type_get_qdata(GType type,GQuark quark)3690 g_type_get_qdata (GType type,
3691 GQuark quark)
3692 {
3693 TypeNode *node;
3694 gpointer data;
3695
3696 node = lookup_type_node_I (type);
3697 if (node)
3698 {
3699 G_READ_LOCK (&type_rw_lock);
3700 data = type_get_qdata_L (node, quark);
3701 G_READ_UNLOCK (&type_rw_lock);
3702 }
3703 else
3704 {
3705 g_return_val_if_fail (node != NULL, NULL);
3706 data = NULL;
3707 }
3708 return data;
3709 }
3710
3711 static inline void
type_set_qdata_W(TypeNode * node,GQuark quark,gpointer data)3712 type_set_qdata_W (TypeNode *node,
3713 GQuark quark,
3714 gpointer data)
3715 {
3716 GData *gdata;
3717 QData *qdata;
3718 guint i;
3719
3720 /* setup qdata list if necessary */
3721 if (!node->global_gdata)
3722 node->global_gdata = g_new0 (GData, 1);
3723 gdata = node->global_gdata;
3724
3725 /* try resetting old data */
3726 qdata = gdata->qdatas;
3727 for (i = 0; i < gdata->n_qdatas; i++)
3728 if (qdata[i].quark == quark)
3729 {
3730 qdata[i].data = data;
3731 return;
3732 }
3733
3734 /* add new entry */
3735 gdata->n_qdatas++;
3736 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
3737 qdata = gdata->qdatas;
3738 for (i = 0; i < gdata->n_qdatas - 1; i++)
3739 if (qdata[i].quark > quark)
3740 break;
3741 memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
3742 qdata[i].quark = quark;
3743 qdata[i].data = data;
3744 }
3745
3746 /**
3747 * g_type_set_qdata:
3748 * @type: a #GType
3749 * @quark: a #GQuark id to identify the data
3750 * @data: the data
3751 *
3752 * Attaches arbitrary data to a type.
3753 */
3754 void
g_type_set_qdata(GType type,GQuark quark,gpointer data)3755 g_type_set_qdata (GType type,
3756 GQuark quark,
3757 gpointer data)
3758 {
3759 TypeNode *node;
3760
3761 g_return_if_fail (quark != 0);
3762
3763 node = lookup_type_node_I (type);
3764 if (node)
3765 {
3766 G_WRITE_LOCK (&type_rw_lock);
3767 type_set_qdata_W (node, quark, data);
3768 G_WRITE_UNLOCK (&type_rw_lock);
3769 }
3770 else
3771 g_return_if_fail (node != NULL);
3772 }
3773
3774 static void
type_add_flags_W(TypeNode * node,GTypeFlags flags)3775 type_add_flags_W (TypeNode *node,
3776 GTypeFlags flags)
3777 {
3778 guint dflags;
3779
3780 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
3781 g_return_if_fail (node != NULL);
3782
3783 if ((flags & TYPE_FLAG_MASK) && node->is_classed && node->data && node->data->class.class)
3784 g_warning ("tagging type '%s' as abstract after class initialization", NODE_NAME (node));
3785 dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
3786 dflags |= flags;
3787 type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags));
3788 }
3789
3790 /**
3791 * g_type_query:
3792 * @type: #GType of a static, classed type
3793 * @query: (out caller-allocates): a user provided structure that is
3794 * filled in with constant values upon success
3795 *
3796 * Queries the type system for information about a specific type.
3797 * This function will fill in a user-provided structure to hold
3798 * type-specific information. If an invalid #GType is passed in, the
3799 * @type member of the #GTypeQuery is 0. All members filled into the
3800 * #GTypeQuery structure should be considered constant and have to be
3801 * left untouched.
3802 */
3803 void
g_type_query(GType type,GTypeQuery * query)3804 g_type_query (GType type,
3805 GTypeQuery *query)
3806 {
3807 TypeNode *node;
3808
3809 g_return_if_fail (query != NULL);
3810
3811 /* if node is not static and classed, we won't allow query */
3812 query->type = 0;
3813 node = lookup_type_node_I (type);
3814 if (node && node->is_classed && !node->plugin)
3815 {
3816 /* type is classed and probably even instantiatable */
3817 G_READ_LOCK (&type_rw_lock);
3818 if (node->data) /* type is static or referenced */
3819 {
3820 query->type = NODE_TYPE (node);
3821 query->type_name = NODE_NAME (node);
3822 query->class_size = node->data->class.class_size;
3823 query->instance_size = node->is_instantiatable ? node->data->instance.instance_size : 0;
3824 }
3825 G_READ_UNLOCK (&type_rw_lock);
3826 }
3827 }
3828
3829 /**
3830 * g_type_get_instance_count:
3831 * @type: a #GType
3832 *
3833 * Returns the number of instances allocated of the particular type;
3834 * this is only available if GLib is built with debugging support and
3835 * the instance_count debug flag is set (by setting the GOBJECT_DEBUG
3836 * variable to include instance-count).
3837 *
3838 * Returns: the number of instances allocated of the given type;
3839 * if instance counts are not available, returns 0.
3840 *
3841 * Since: 2.44
3842 */
3843 int
g_type_get_instance_count(GType type)3844 g_type_get_instance_count (GType type)
3845 {
3846 #ifdef G_ENABLE_DEBUG
3847 TypeNode *node;
3848
3849 node = lookup_type_node_I (type);
3850 g_return_val_if_fail (node != NULL, 0);
3851
3852 return g_atomic_int_get (&node->instance_count);
3853 #else
3854 return 0;
3855 #endif
3856 }
3857
3858 /* --- implementation details --- */
3859 gboolean
g_type_test_flags(GType type,guint flags)3860 g_type_test_flags (GType type,
3861 guint flags)
3862 {
3863 TypeNode *node;
3864 gboolean result = FALSE;
3865
3866 node = lookup_type_node_I (type);
3867 if (node)
3868 {
3869 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
3870 guint tflags = flags & TYPE_FLAG_MASK;
3871
3872 if (fflags)
3873 {
3874 GTypeFundamentalInfo *finfo = type_node_fundamental_info_I (node);
3875
3876 fflags = (finfo->type_flags & fflags) == fflags;
3877 }
3878 else
3879 fflags = TRUE;
3880
3881 if (tflags)
3882 {
3883 G_READ_LOCK (&type_rw_lock);
3884 tflags = (tflags & GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags))) == tflags;
3885 G_READ_UNLOCK (&type_rw_lock);
3886 }
3887 else
3888 tflags = TRUE;
3889
3890 result = tflags && fflags;
3891 }
3892
3893 return result;
3894 }
3895
3896 /**
3897 * g_type_get_plugin:
3898 * @type: #GType to retrieve the plugin for
3899 *
3900 * Returns the #GTypePlugin structure for @type.
3901 *
3902 * Returns: (transfer none): the corresponding plugin
3903 * if @type is a dynamic type, %NULL otherwise
3904 */
3905 GTypePlugin*
g_type_get_plugin(GType type)3906 g_type_get_plugin (GType type)
3907 {
3908 TypeNode *node;
3909
3910 node = lookup_type_node_I (type);
3911
3912 return node ? node->plugin : NULL;
3913 }
3914
3915 /**
3916 * g_type_interface_get_plugin:
3917 * @instance_type: #GType of an instantiatable type
3918 * @interface_type: #GType of an interface type
3919 *
3920 * Returns the #GTypePlugin structure for the dynamic interface
3921 * @interface_type which has been added to @instance_type, or %NULL
3922 * if @interface_type has not been added to @instance_type or does
3923 * not have a #GTypePlugin structure. See g_type_add_interface_dynamic().
3924 *
3925 * Returns: (transfer none): the #GTypePlugin for the dynamic
3926 * interface @interface_type of @instance_type
3927 */
3928 GTypePlugin*
g_type_interface_get_plugin(GType instance_type,GType interface_type)3929 g_type_interface_get_plugin (GType instance_type,
3930 GType interface_type)
3931 {
3932 TypeNode *node;
3933 TypeNode *iface;
3934
3935 g_return_val_if_fail (G_TYPE_IS_INTERFACE (interface_type), NULL); /* G_TYPE_IS_INTERFACE() is an external call: _U */
3936
3937 node = lookup_type_node_I (instance_type);
3938 iface = lookup_type_node_I (interface_type);
3939 if (node && iface)
3940 {
3941 IFaceHolder *iholder;
3942 GTypePlugin *plugin;
3943
3944 G_READ_LOCK (&type_rw_lock);
3945
3946 iholder = iface_node_get_holders_L (iface);
3947 while (iholder && iholder->instance_type != instance_type)
3948 iholder = iholder->next;
3949 plugin = iholder ? iholder->plugin : NULL;
3950
3951 G_READ_UNLOCK (&type_rw_lock);
3952
3953 return plugin;
3954 }
3955
3956 g_return_val_if_fail (node == NULL, NULL);
3957 g_return_val_if_fail (iface == NULL, NULL);
3958
3959 g_warning (G_STRLOC ": attempt to look up plugin for invalid instance/interface type pair.");
3960
3961 return NULL;
3962 }
3963
3964 /**
3965 * g_type_fundamental_next:
3966 *
3967 * Returns the next free fundamental type id which can be used to
3968 * register a new fundamental type with g_type_register_fundamental().
3969 * The returned type ID represents the highest currently registered
3970 * fundamental type identifier.
3971 *
3972 * Returns: the next available fundamental type ID to be registered,
3973 * or 0 if the type system ran out of fundamental type IDs
3974 */
3975 GType
g_type_fundamental_next(void)3976 g_type_fundamental_next (void)
3977 {
3978 GType type;
3979
3980 G_READ_LOCK (&type_rw_lock);
3981 type = static_fundamental_next;
3982 G_READ_UNLOCK (&type_rw_lock);
3983 type = G_TYPE_MAKE_FUNDAMENTAL (type);
3984 return type <= G_TYPE_FUNDAMENTAL_MAX ? type : 0;
3985 }
3986
3987 /**
3988 * g_type_fundamental:
3989 * @type_id: valid type ID
3990 *
3991 * Internal function, used to extract the fundamental type ID portion.
3992 * Use G_TYPE_FUNDAMENTAL() instead.
3993 *
3994 * Returns: fundamental type ID
3995 */
3996 GType
g_type_fundamental(GType type_id)3997 g_type_fundamental (GType type_id)
3998 {
3999 TypeNode *node = lookup_type_node_I (type_id);
4000
4001 return node ? NODE_FUNDAMENTAL_TYPE (node) : 0;
4002 }
4003
4004 gboolean
g_type_check_instance_is_a(GTypeInstance * type_instance,GType iface_type)4005 g_type_check_instance_is_a (GTypeInstance *type_instance,
4006 GType iface_type)
4007 {
4008 TypeNode *node, *iface;
4009 gboolean check;
4010
4011 if (!type_instance || !type_instance->g_class)
4012 return FALSE;
4013
4014 node = lookup_type_node_I (type_instance->g_class->g_type);
4015 iface = lookup_type_node_I (iface_type);
4016 check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
4017
4018 return check;
4019 }
4020
4021 gboolean
g_type_check_instance_is_fundamentally_a(GTypeInstance * type_instance,GType fundamental_type)4022 g_type_check_instance_is_fundamentally_a (GTypeInstance *type_instance,
4023 GType fundamental_type)
4024 {
4025 TypeNode *node;
4026 if (!type_instance || !type_instance->g_class)
4027 return FALSE;
4028 node = lookup_type_node_I (type_instance->g_class->g_type);
4029 return node && (NODE_FUNDAMENTAL_TYPE(node) == fundamental_type);
4030 }
4031
4032 gboolean
g_type_check_class_is_a(GTypeClass * type_class,GType is_a_type)4033 g_type_check_class_is_a (GTypeClass *type_class,
4034 GType is_a_type)
4035 {
4036 TypeNode *node, *iface;
4037 gboolean check;
4038
4039 if (!type_class)
4040 return FALSE;
4041
4042 node = lookup_type_node_I (type_class->g_type);
4043 iface = lookup_type_node_I (is_a_type);
4044 check = node && node->is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
4045
4046 return check;
4047 }
4048
4049 GTypeInstance*
g_type_check_instance_cast(GTypeInstance * type_instance,GType iface_type)4050 g_type_check_instance_cast (GTypeInstance *type_instance,
4051 GType iface_type)
4052 {
4053 if (type_instance)
4054 {
4055 if (type_instance->g_class)
4056 {
4057 TypeNode *node, *iface;
4058 gboolean is_instantiatable, check;
4059
4060 node = lookup_type_node_I (type_instance->g_class->g_type);
4061 is_instantiatable = node && node->is_instantiatable;
4062 iface = lookup_type_node_I (iface_type);
4063 check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
4064 if (check)
4065 return type_instance;
4066
4067 if (is_instantiatable)
4068 g_warning ("invalid cast from '%s' to '%s'",
4069 type_descriptive_name_I (type_instance->g_class->g_type),
4070 type_descriptive_name_I (iface_type));
4071 else
4072 g_warning ("invalid uninstantiatable type '%s' in cast to '%s'",
4073 type_descriptive_name_I (type_instance->g_class->g_type),
4074 type_descriptive_name_I (iface_type));
4075 }
4076 else
4077 g_warning ("invalid unclassed pointer in cast to '%s'",
4078 type_descriptive_name_I (iface_type));
4079 }
4080
4081 return type_instance;
4082 }
4083
4084 GTypeClass*
g_type_check_class_cast(GTypeClass * type_class,GType is_a_type)4085 g_type_check_class_cast (GTypeClass *type_class,
4086 GType is_a_type)
4087 {
4088 if (type_class)
4089 {
4090 TypeNode *node, *iface;
4091 gboolean is_classed, check;
4092
4093 node = lookup_type_node_I (type_class->g_type);
4094 is_classed = node && node->is_classed;
4095 iface = lookup_type_node_I (is_a_type);
4096 check = is_classed && iface && type_node_conforms_to_U (node, iface, FALSE, FALSE);
4097 if (check)
4098 return type_class;
4099
4100 if (is_classed)
4101 g_warning ("invalid class cast from '%s' to '%s'",
4102 type_descriptive_name_I (type_class->g_type),
4103 type_descriptive_name_I (is_a_type));
4104 else
4105 g_warning ("invalid unclassed type '%s' in class cast to '%s'",
4106 type_descriptive_name_I (type_class->g_type),
4107 type_descriptive_name_I (is_a_type));
4108 }
4109 else
4110 g_warning ("invalid class cast from (NULL) pointer to '%s'",
4111 type_descriptive_name_I (is_a_type));
4112 return type_class;
4113 }
4114
4115 /**
4116 * g_type_check_instance:
4117 * @instance: a valid #GTypeInstance structure
4118 *
4119 * Private helper function to aid implementation of the
4120 * G_TYPE_CHECK_INSTANCE() macro.
4121 *
4122 * Returns: %TRUE if @instance is valid, %FALSE otherwise
4123 */
4124 gboolean
g_type_check_instance(GTypeInstance * type_instance)4125 g_type_check_instance (GTypeInstance *type_instance)
4126 {
4127 /* this function is just here to make the signal system
4128 * conveniently elaborated on instance checks
4129 */
4130 if (type_instance)
4131 {
4132 if (type_instance->g_class)
4133 {
4134 TypeNode *node = lookup_type_node_I (type_instance->g_class->g_type);
4135
4136 if (node && node->is_instantiatable)
4137 return TRUE;
4138
4139 g_warning ("instance of invalid non-instantiatable type '%s'",
4140 type_descriptive_name_I (type_instance->g_class->g_type));
4141 }
4142 else
4143 g_warning ("instance with invalid (NULL) class pointer");
4144 }
4145 else
4146 g_warning ("invalid (NULL) pointer instance");
4147
4148 return FALSE;
4149 }
4150
4151 static inline gboolean
type_check_is_value_type_U(GType type)4152 type_check_is_value_type_U (GType type)
4153 {
4154 GTypeFlags tflags = G_TYPE_FLAG_VALUE_ABSTRACT;
4155 TypeNode *node;
4156
4157 /* common path speed up */
4158 node = lookup_type_node_I (type);
4159 if (node && node->mutatable_check_cache)
4160 return TRUE;
4161
4162 G_READ_LOCK (&type_rw_lock);
4163 restart_check:
4164 if (node)
4165 {
4166 if (node->data && NODE_REFCOUNT (node) > 0 &&
4167 node->data->common.value_table->value_init)
4168 tflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags));
4169 else if (NODE_IS_IFACE (node))
4170 {
4171 guint i;
4172
4173 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4174 {
4175 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4176 TypeNode *prnode = lookup_type_node_I (prtype);
4177
4178 if (prnode->is_instantiatable)
4179 {
4180 type = prtype;
4181 node = lookup_type_node_I (type);
4182 goto restart_check;
4183 }
4184 }
4185 }
4186 }
4187 G_READ_UNLOCK (&type_rw_lock);
4188
4189 return !(tflags & G_TYPE_FLAG_VALUE_ABSTRACT);
4190 }
4191
4192 gboolean
g_type_check_is_value_type(GType type)4193 g_type_check_is_value_type (GType type)
4194 {
4195 return type_check_is_value_type_U (type);
4196 }
4197
4198 gboolean
g_type_check_value(const GValue * value)4199 g_type_check_value (const GValue *value)
4200 {
4201 return value && type_check_is_value_type_U (value->g_type);
4202 }
4203
4204 gboolean
g_type_check_value_holds(const GValue * value,GType type)4205 g_type_check_value_holds (const GValue *value,
4206 GType type)
4207 {
4208 return value && type_check_is_value_type_U (value->g_type) && g_type_is_a (value->g_type, type);
4209 }
4210
4211 /**
4212 * g_type_value_table_peek: (skip)
4213 * @type: a #GType
4214 *
4215 * Returns the location of the #GTypeValueTable associated with @type.
4216 *
4217 * Note that this function should only be used from source code
4218 * that implements or has internal knowledge of the implementation of
4219 * @type.
4220 *
4221 * Returns: location of the #GTypeValueTable associated with @type or
4222 * %NULL if there is no #GTypeValueTable associated with @type
4223 */
4224 GTypeValueTable*
g_type_value_table_peek(GType type)4225 g_type_value_table_peek (GType type)
4226 {
4227 GTypeValueTable *vtable = NULL;
4228 TypeNode *node = lookup_type_node_I (type);
4229 gboolean has_refed_data, has_table;
4230
4231 if (node && NODE_REFCOUNT (node) && node->mutatable_check_cache)
4232 return node->data->common.value_table;
4233
4234 G_READ_LOCK (&type_rw_lock);
4235
4236 restart_table_peek:
4237 has_refed_data = node && node->data && NODE_REFCOUNT (node) > 0;
4238 has_table = has_refed_data && node->data->common.value_table->value_init;
4239 if (has_refed_data)
4240 {
4241 if (has_table)
4242 vtable = node->data->common.value_table;
4243 else if (NODE_IS_IFACE (node))
4244 {
4245 guint i;
4246
4247 for (i = 0; i < IFACE_NODE_N_PREREQUISITES (node); i++)
4248 {
4249 GType prtype = IFACE_NODE_PREREQUISITES (node)[i];
4250 TypeNode *prnode = lookup_type_node_I (prtype);
4251
4252 if (prnode->is_instantiatable)
4253 {
4254 type = prtype;
4255 node = lookup_type_node_I (type);
4256 goto restart_table_peek;
4257 }
4258 }
4259 }
4260 }
4261
4262 G_READ_UNLOCK (&type_rw_lock);
4263
4264 if (vtable)
4265 return vtable;
4266
4267 if (!node)
4268 g_warning (G_STRLOC ": type id '%" G_GSIZE_FORMAT "' is invalid", type);
4269 if (!has_refed_data)
4270 g_warning ("can't peek value table for type '%s' which is not currently referenced",
4271 type_descriptive_name_I (type));
4272
4273 return NULL;
4274 }
4275
4276 const gchar *
g_type_name_from_instance(GTypeInstance * instance)4277 g_type_name_from_instance (GTypeInstance *instance)
4278 {
4279 if (!instance)
4280 return "<NULL-instance>";
4281 else
4282 return g_type_name_from_class (instance->g_class);
4283 }
4284
4285 const gchar *
g_type_name_from_class(GTypeClass * g_class)4286 g_type_name_from_class (GTypeClass *g_class)
4287 {
4288 if (!g_class)
4289 return "<NULL-class>";
4290 else
4291 return g_type_name (g_class->g_type);
4292 }
4293
4294
4295 /* --- private api for gboxed.c --- */
4296 gpointer
_g_type_boxed_copy(GType type,gpointer value)4297 _g_type_boxed_copy (GType type, gpointer value)
4298 {
4299 TypeNode *node = lookup_type_node_I (type);
4300
4301 return node->data->boxed.copy_func (value);
4302 }
4303
4304 void
_g_type_boxed_free(GType type,gpointer value)4305 _g_type_boxed_free (GType type, gpointer value)
4306 {
4307 TypeNode *node = lookup_type_node_I (type);
4308
4309 node->data->boxed.free_func (value);
4310 }
4311
4312 void
_g_type_boxed_init(GType type,GBoxedCopyFunc copy_func,GBoxedFreeFunc free_func)4313 _g_type_boxed_init (GType type,
4314 GBoxedCopyFunc copy_func,
4315 GBoxedFreeFunc free_func)
4316 {
4317 TypeNode *node = lookup_type_node_I (type);
4318
4319 node->data->boxed.copy_func = copy_func;
4320 node->data->boxed.free_func = free_func;
4321 }
4322
4323 /* --- initialization --- */
4324 /**
4325 * g_type_init_with_debug_flags:
4326 * @debug_flags: bitwise combination of #GTypeDebugFlags values for
4327 * debugging purposes
4328 *
4329 * This function used to initialise the type system with debugging
4330 * flags. Since GLib 2.36, the type system is initialised automatically
4331 * and this function does nothing.
4332 *
4333 * If you need to enable debugging features, use the GOBJECT_DEBUG
4334 * environment variable.
4335 *
4336 * Deprecated: 2.36: the type system is now initialised automatically
4337 */
4338 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
4339 void
g_type_init_with_debug_flags(GTypeDebugFlags debug_flags)4340 g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
4341 {
4342 g_assert_type_system_initialized ();
4343
4344 if (debug_flags)
4345 g_message ("g_type_init_with_debug_flags() is no longer supported. Use the GOBJECT_DEBUG environment variable.");
4346 }
4347 G_GNUC_END_IGNORE_DEPRECATIONS
4348
4349 /**
4350 * g_type_init:
4351 *
4352 * This function used to initialise the type system. Since GLib 2.36,
4353 * the type system is initialised automatically and this function does
4354 * nothing.
4355 *
4356 * Deprecated: 2.36: the type system is now initialised automatically
4357 */
4358 void
g_type_init(void)4359 g_type_init (void)
4360 {
4361 g_assert_type_system_initialized ();
4362 }
4363
4364 static void
gobject_init(void)4365 gobject_init (void)
4366 {
4367 const gchar *env_string;
4368 GTypeInfo info;
4369 TypeNode *node;
4370 GType type G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
4371
4372 /* Ensure GLib is initialized first, see
4373 * https://bugzilla.gnome.org/show_bug.cgi?id=756139
4374 */
4375 GLIB_PRIVATE_CALL (glib_init) ();
4376
4377 G_WRITE_LOCK (&type_rw_lock);
4378
4379 /* setup GObject library wide debugging flags */
4380 env_string = g_getenv ("GOBJECT_DEBUG");
4381 if (env_string != NULL)
4382 {
4383 GDebugKey debug_keys[] = {
4384 { "objects", G_TYPE_DEBUG_OBJECTS },
4385 { "instance-count", G_TYPE_DEBUG_INSTANCE_COUNT },
4386 { "signals", G_TYPE_DEBUG_SIGNALS },
4387 };
4388
4389 _g_type_debug_flags = g_parse_debug_string (env_string, debug_keys, G_N_ELEMENTS (debug_keys));
4390 }
4391
4392 /* quarks */
4393 static_quark_type_flags = g_quark_from_static_string ("-g-type-private--GTypeFlags");
4394 static_quark_iface_holder = g_quark_from_static_string ("-g-type-private--IFaceHolder");
4395 static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
4396
4397 /* type qname hash table */
4398 static_type_nodes_ht = g_hash_table_new (g_str_hash, g_str_equal);
4399
4400 /* invalid type G_TYPE_INVALID (0)
4401 */
4402 static_fundamental_type_nodes[0] = NULL;
4403
4404 /* void type G_TYPE_NONE
4405 */
4406 node = type_node_fundamental_new_W (G_TYPE_NONE, g_intern_static_string ("void"), 0);
4407 type = NODE_TYPE (node);
4408 g_assert (type == G_TYPE_NONE);
4409
4410 /* interface fundamental type G_TYPE_INTERFACE (!classed)
4411 */
4412 memset (&info, 0, sizeof (info));
4413 node = type_node_fundamental_new_W (G_TYPE_INTERFACE, g_intern_static_string ("GInterface"), G_TYPE_FLAG_DERIVABLE);
4414 type = NODE_TYPE (node);
4415 type_data_make_W (node, &info, NULL);
4416 g_assert (type == G_TYPE_INTERFACE);
4417
4418 G_WRITE_UNLOCK (&type_rw_lock);
4419
4420 _g_value_c_init ();
4421
4422 /* G_TYPE_TYPE_PLUGIN
4423 */
4424 g_type_ensure (g_type_plugin_get_type ());
4425
4426 /* G_TYPE_* value types
4427 */
4428 _g_value_types_init ();
4429
4430 /* G_TYPE_ENUM & G_TYPE_FLAGS
4431 */
4432 _g_enum_types_init ();
4433
4434 /* G_TYPE_BOXED
4435 */
4436 _g_boxed_type_init ();
4437
4438 /* G_TYPE_PARAM
4439 */
4440 _g_param_type_init ();
4441
4442 /* G_TYPE_OBJECT
4443 */
4444 _g_object_type_init ();
4445
4446 /* G_TYPE_PARAM_* pspec types
4447 */
4448 _g_param_spec_types_init ();
4449
4450 /* Value Transformations
4451 */
4452 _g_value_transforms_init ();
4453
4454 /* Signal system
4455 */
4456 _g_signal_init ();
4457 }
4458
4459 #if defined (G_OS_WIN32)
4460
4461 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
4462 DWORD fdwReason,
4463 LPVOID lpvReserved);
4464
4465 BOOL WINAPI
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)4466 DllMain (HINSTANCE hinstDLL,
4467 DWORD fdwReason,
4468 LPVOID lpvReserved)
4469 {
4470 switch (fdwReason)
4471 {
4472 case DLL_PROCESS_ATTACH:
4473 gobject_init ();
4474 break;
4475
4476 default:
4477 /* do nothing */
4478 ;
4479 }
4480
4481 return TRUE;
4482 }
4483
4484 #elif defined (G_HAS_CONSTRUCTORS)
4485 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
4486 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gobject_init_ctor)
4487 #endif
G_DEFINE_CONSTRUCTOR(gobject_init_ctor)4488 G_DEFINE_CONSTRUCTOR(gobject_init_ctor)
4489
4490 static void
4491 gobject_init_ctor (void)
4492 {
4493 gobject_init ();
4494 }
4495
4496 #else
4497 # error Your platform/compiler is missing constructor support
4498 #endif
4499
4500 /**
4501 * g_type_class_add_private:
4502 * @g_class: (type GObject.TypeClass): class structure for an instantiatable
4503 * type
4504 * @private_size: size of private structure
4505 *
4506 * Registers a private structure for an instantiatable type.
4507 *
4508 * When an object is allocated, the private structures for
4509 * the type and all of its parent types are allocated
4510 * sequentially in the same memory block as the public
4511 * structures, and are zero-filled.
4512 *
4513 * Note that the accumulated size of the private structures of
4514 * a type and all its parent types cannot exceed 64 KiB.
4515 *
4516 * This function should be called in the type's class_init() function.
4517 * The private structure can be retrieved using the
4518 * G_TYPE_INSTANCE_GET_PRIVATE() macro.
4519 *
4520 * The following example shows attaching a private structure
4521 * MyObjectPrivate to an object MyObject defined in the standard
4522 * GObject fashion in the type's class_init() function.
4523 *
4524 * Note the use of a structure member "priv" to avoid the overhead
4525 * of repeatedly calling MY_OBJECT_GET_PRIVATE().
4526 *
4527 * |[<!-- language="C" -->
4528 * typedef struct _MyObject MyObject;
4529 * typedef struct _MyObjectPrivate MyObjectPrivate;
4530 *
4531 * struct _MyObject {
4532 * GObject parent;
4533 *
4534 * MyObjectPrivate *priv;
4535 * };
4536 *
4537 * struct _MyObjectPrivate {
4538 * int some_field;
4539 * };
4540 *
4541 * static void
4542 * my_object_class_init (MyObjectClass *klass)
4543 * {
4544 * g_type_class_add_private (klass, sizeof (MyObjectPrivate));
4545 * }
4546 *
4547 * static void
4548 * my_object_init (MyObject *my_object)
4549 * {
4550 * my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
4551 * MY_TYPE_OBJECT,
4552 * MyObjectPrivate);
4553 * // my_object->priv->some_field will be automatically initialised to 0
4554 * }
4555 *
4556 * static int
4557 * my_object_get_some_field (MyObject *my_object)
4558 * {
4559 * MyObjectPrivate *priv;
4560 *
4561 * g_return_val_if_fail (MY_IS_OBJECT (my_object), 0);
4562 *
4563 * priv = my_object->priv;
4564 *
4565 * return priv->some_field;
4566 * }
4567 * ]|
4568 *
4569 * Since: 2.4
4570 * Deprecated: 2.58: Use the G_ADD_PRIVATE() macro with the `G_DEFINE_*`
4571 * family of macros to add instance private data to a type
4572 */
4573 void
g_type_class_add_private(gpointer g_class,gsize private_size)4574 g_type_class_add_private (gpointer g_class,
4575 gsize private_size)
4576 {
4577 GType instance_type = ((GTypeClass *)g_class)->g_type;
4578 TypeNode *node = lookup_type_node_I (instance_type);
4579
4580 g_return_if_fail (private_size > 0);
4581 g_return_if_fail (private_size <= 0xffff);
4582
4583 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
4584 {
4585 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4586 type_descriptive_name_I (instance_type));
4587 return;
4588 }
4589
4590 if (NODE_PARENT_TYPE (node))
4591 {
4592 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4593 if (node->data->instance.private_size != pnode->data->instance.private_size)
4594 {
4595 g_warning ("g_type_class_add_private() called multiple times for the same type");
4596 return;
4597 }
4598 }
4599
4600 G_WRITE_LOCK (&type_rw_lock);
4601
4602 private_size = ALIGN_STRUCT (node->data->instance.private_size + private_size);
4603 g_assert (private_size <= 0xffff);
4604 node->data->instance.private_size = private_size;
4605
4606 G_WRITE_UNLOCK (&type_rw_lock);
4607 }
4608
4609 /* semi-private, called only by the G_ADD_PRIVATE macro */
4610 gint
g_type_add_instance_private(GType class_gtype,gsize private_size)4611 g_type_add_instance_private (GType class_gtype,
4612 gsize private_size)
4613 {
4614 TypeNode *node = lookup_type_node_I (class_gtype);
4615
4616 g_return_val_if_fail (private_size > 0, 0);
4617 g_return_val_if_fail (private_size <= 0xffff, 0);
4618
4619 if (!node || !node->is_classed || !node->is_instantiatable || !node->data)
4620 {
4621 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4622 type_descriptive_name_I (class_gtype));
4623 return 0;
4624 }
4625
4626 if (node->plugin != NULL)
4627 {
4628 g_warning ("cannot use g_type_add_instance_private() with dynamic type '%s'",
4629 type_descriptive_name_I (class_gtype));
4630 return 0;
4631 }
4632
4633 /* in the future, we want to register the private data size of a type
4634 * directly from the get_type() implementation so that we can take full
4635 * advantage of the type definition macros that we already have.
4636 *
4637 * unfortunately, this does not behave correctly if a class in the middle
4638 * of the type hierarchy uses the "old style" of private data registration
4639 * from the class_init() implementation, as the private data offset is not
4640 * going to be known until the full class hierarchy is initialized.
4641 *
4642 * in order to transition our code to the Glorious New Future™, we proceed
4643 * with a two-step implementation: first, we provide this new function to
4644 * register the private data size in the get_type() implementation and we
4645 * hide it behind a macro. the function will return the private size, instead
4646 * of the offset, which will be stored inside a static variable defined by
4647 * the G_DEFINE_TYPE_EXTENDED macro. the G_DEFINE_TYPE_EXTENDED macro will
4648 * check the variable and call g_type_class_add_instance_private(), which
4649 * will use the data size and actually register the private data, then
4650 * return the computed offset of the private data, which will be stored
4651 * inside the static variable, so we can use it to retrieve the pointer
4652 * to the private data structure.
4653 *
4654 * once all our code has been migrated to the new idiomatic form of private
4655 * data registration, we will change the g_type_add_instance_private()
4656 * function to actually perform the registration and return the offset
4657 * of the private data; g_type_class_add_instance_private() already checks
4658 * if the passed argument is negative (meaning that it's an offset in the
4659 * GTypeInstance allocation) and becomes a no-op if that's the case. this
4660 * should make the migration fully transparent even if we're effectively
4661 * copying this macro into everybody's code.
4662 */
4663 return private_size;
4664 }
4665
4666 /* semi-private function, should only be used by G_DEFINE_TYPE_EXTENDED */
4667 void
g_type_class_adjust_private_offset(gpointer g_class,gint * private_size_or_offset)4668 g_type_class_adjust_private_offset (gpointer g_class,
4669 gint *private_size_or_offset)
4670 {
4671 GType class_gtype = ((GTypeClass *) g_class)->g_type;
4672 TypeNode *node = lookup_type_node_I (class_gtype);
4673 gssize private_size;
4674
4675 g_return_if_fail (private_size_or_offset != NULL);
4676
4677 /* if we have been passed the offset instead of the private data size,
4678 * then we consider this as a no-op, and just return the value. see the
4679 * comment in g_type_add_instance_private() for the full explanation.
4680 */
4681 if (*private_size_or_offset > 0)
4682 g_return_if_fail (*private_size_or_offset <= 0xffff);
4683 else
4684 return;
4685
4686 if (!node || !node->is_classed || !node->is_instantiatable || !node->data)
4687 {
4688 g_warning ("cannot add private field to invalid (non-instantiatable) type '%s'",
4689 type_descriptive_name_I (class_gtype));
4690 *private_size_or_offset = 0;
4691 return;
4692 }
4693
4694 if (NODE_PARENT_TYPE (node))
4695 {
4696 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4697 if (node->data->instance.private_size != pnode->data->instance.private_size)
4698 {
4699 g_warning ("g_type_add_instance_private() called multiple times for the same type");
4700 *private_size_or_offset = 0;
4701 return;
4702 }
4703 }
4704
4705 G_WRITE_LOCK (&type_rw_lock);
4706
4707 private_size = ALIGN_STRUCT (node->data->instance.private_size + *private_size_or_offset);
4708 g_assert (private_size <= 0xffff);
4709 node->data->instance.private_size = private_size;
4710
4711 *private_size_or_offset = -(gint) node->data->instance.private_size;
4712
4713 G_WRITE_UNLOCK (&type_rw_lock);
4714 }
4715
4716 gpointer
g_type_instance_get_private(GTypeInstance * instance,GType private_type)4717 g_type_instance_get_private (GTypeInstance *instance,
4718 GType private_type)
4719 {
4720 TypeNode *node;
4721
4722 g_return_val_if_fail (instance != NULL && instance->g_class != NULL, NULL);
4723
4724 node = lookup_type_node_I (private_type);
4725 if (G_UNLIKELY (!node || !node->is_instantiatable))
4726 {
4727 g_warning ("instance of invalid non-instantiatable type '%s'",
4728 type_descriptive_name_I (instance->g_class->g_type));
4729 return NULL;
4730 }
4731
4732 return ((gchar *) instance) - node->data->instance.private_size;
4733 }
4734
4735 /**
4736 * g_type_class_get_instance_private_offset: (skip)
4737 * @g_class: (type GObject.TypeClass): a #GTypeClass
4738 *
4739 * Gets the offset of the private data for instances of @g_class.
4740 *
4741 * This is how many bytes you should add to the instance pointer of a
4742 * class in order to get the private data for the type represented by
4743 * @g_class.
4744 *
4745 * You can only call this function after you have registered a private
4746 * data area for @g_class using g_type_class_add_private().
4747 *
4748 * Returns: the offset, in bytes
4749 *
4750 * Since: 2.38
4751 **/
4752 gint
g_type_class_get_instance_private_offset(gpointer g_class)4753 g_type_class_get_instance_private_offset (gpointer g_class)
4754 {
4755 GType instance_type;
4756 guint16 parent_size;
4757 TypeNode *node;
4758
4759 g_assert (g_class != NULL);
4760
4761 instance_type = ((GTypeClass *) g_class)->g_type;
4762 node = lookup_type_node_I (instance_type);
4763
4764 g_assert (node != NULL);
4765 g_assert (node->is_instantiatable);
4766
4767 if (NODE_PARENT_TYPE (node))
4768 {
4769 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4770
4771 parent_size = pnode->data->instance.private_size;
4772 }
4773 else
4774 parent_size = 0;
4775
4776 if (node->data->instance.private_size == parent_size)
4777 g_error ("g_type_class_get_instance_private_offset() called on class %s but it has no private data",
4778 g_type_name (instance_type));
4779
4780 return -(gint) node->data->instance.private_size;
4781 }
4782
4783 /**
4784 * g_type_add_class_private:
4785 * @class_type: GType of a classed type
4786 * @private_size: size of private structure
4787 *
4788 * Registers a private class structure for a classed type;
4789 * when the class is allocated, the private structures for
4790 * the class and all of its parent types are allocated
4791 * sequentially in the same memory block as the public
4792 * structures, and are zero-filled.
4793 *
4794 * This function should be called in the
4795 * type's get_type() function after the type is registered.
4796 * The private structure can be retrieved using the
4797 * G_TYPE_CLASS_GET_PRIVATE() macro.
4798 *
4799 * Since: 2.24
4800 */
4801 void
g_type_add_class_private(GType class_type,gsize private_size)4802 g_type_add_class_private (GType class_type,
4803 gsize private_size)
4804 {
4805 TypeNode *node = lookup_type_node_I (class_type);
4806 gsize offset;
4807
4808 g_return_if_fail (private_size > 0);
4809
4810 if (!node || !node->is_classed || !node->data)
4811 {
4812 g_warning ("cannot add class private field to invalid type '%s'",
4813 type_descriptive_name_I (class_type));
4814 return;
4815 }
4816
4817 if (NODE_PARENT_TYPE (node))
4818 {
4819 TypeNode *pnode = lookup_type_node_I (NODE_PARENT_TYPE (node));
4820 if (node->data->class.class_private_size != pnode->data->class.class_private_size)
4821 {
4822 g_warning ("g_type_add_class_private() called multiple times for the same type");
4823 return;
4824 }
4825 }
4826
4827 G_WRITE_LOCK (&type_rw_lock);
4828
4829 offset = ALIGN_STRUCT (node->data->class.class_private_size);
4830 node->data->class.class_private_size = offset + private_size;
4831
4832 G_WRITE_UNLOCK (&type_rw_lock);
4833 }
4834
4835 gpointer
g_type_class_get_private(GTypeClass * klass,GType private_type)4836 g_type_class_get_private (GTypeClass *klass,
4837 GType private_type)
4838 {
4839 TypeNode *class_node;
4840 TypeNode *private_node;
4841 TypeNode *parent_node;
4842 gsize offset;
4843
4844 g_return_val_if_fail (klass != NULL, NULL);
4845
4846 class_node = lookup_type_node_I (klass->g_type);
4847 if (G_UNLIKELY (!class_node || !class_node->is_classed))
4848 {
4849 g_warning ("class of invalid type '%s'",
4850 type_descriptive_name_I (klass->g_type));
4851 return NULL;
4852 }
4853
4854 private_node = lookup_type_node_I (private_type);
4855 if (G_UNLIKELY (!private_node || !NODE_IS_ANCESTOR (private_node, class_node)))
4856 {
4857 g_warning ("attempt to retrieve private data for invalid type '%s'",
4858 type_descriptive_name_I (private_type));
4859 return NULL;
4860 }
4861
4862 offset = ALIGN_STRUCT (class_node->data->class.class_size);
4863
4864 if (NODE_PARENT_TYPE (private_node))
4865 {
4866 parent_node = lookup_type_node_I (NODE_PARENT_TYPE (private_node));
4867 g_assert (parent_node->data && NODE_REFCOUNT (parent_node) > 0);
4868
4869 if (G_UNLIKELY (private_node->data->class.class_private_size == parent_node->data->class.class_private_size))
4870 {
4871 g_warning ("g_type_instance_get_class_private() requires a prior call to g_type_add_class_private()");
4872 return NULL;
4873 }
4874
4875 offset += ALIGN_STRUCT (parent_node->data->class.class_private_size);
4876 }
4877
4878 return G_STRUCT_MEMBER_P (klass, offset);
4879 }
4880
4881 /**
4882 * g_type_ensure:
4883 * @type: a #GType
4884 *
4885 * Ensures that the indicated @type has been registered with the
4886 * type system, and its _class_init() method has been run.
4887 *
4888 * In theory, simply calling the type's _get_type() method (or using
4889 * the corresponding macro) is supposed take care of this. However,
4890 * _get_type() methods are often marked %G_GNUC_CONST for performance
4891 * reasons, even though this is technically incorrect (since
4892 * %G_GNUC_CONST requires that the function not have side effects,
4893 * which _get_type() methods do on the first call). As a result, if
4894 * you write a bare call to a _get_type() macro, it may get optimized
4895 * out by the compiler. Using g_type_ensure() guarantees that the
4896 * type's _get_type() method is called.
4897 *
4898 * Since: 2.34
4899 */
4900 void
g_type_ensure(GType type)4901 g_type_ensure (GType type)
4902 {
4903 /* In theory, @type has already been resolved and so there's nothing
4904 * to do here. But this protects us in the case where the function
4905 * gets inlined (as it might in gobject_init_ctor() above).
4906 */
4907 if (G_UNLIKELY (type == (GType)-1))
4908 g_error ("can't happen");
4909 }
4910
4911