nexmon – Blame information for rev 1

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