[glib] Add a note about size limits of private structures



commit 42080449d078c27f2098419b27d0f63269ee2d70
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Sep 3 18:11:08 2010 -0400

    Add a note about size limits of private structures
    
    Also add some assertions to check these limits, instead of
    failing silently.  Bug 604479.

 gobject/gtype.c |   21 ++++++++++++++++-----
 1 files changed, 16 insertions(+), 5 deletions(-)
---
diff --git a/gobject/gtype.c b/gobject/gtype.c
index bf89d91..60f850c 100644
--- a/gobject/gtype.c
+++ b/gobject/gtype.c
@@ -4372,13 +4372,20 @@ g_type_init (void)
  * @g_class: class structure for an instantiatable type
  * @private_size: size of private structure.
  *
- * Registers a private structure for an instantiatable type;
- * when an object is allocated, the private structures for
+ * Registers a private structure for an instantiatable type.
+ *
+ * When an object is allocated, the private structures for
  * the type and all of its parent types are allocated
  * sequentially in the same memory block as the public
- * structures. This function should be called in the
- * type's class_init() function. The private structure can
- * be retrieved using the G_TYPE_INSTANCE_GET_PRIVATE() macro.
+ * structures.
+ *
+ * Note that the accumulated size of the private structures of
+ * a type and all its parent types cannot excced 64kB.
+ *
+ * This function should be called in the type's class_init() function.
+ * The private structure can be retrieved using the
+ * G_TYPE_INSTANCE_GET_PRIVATE() macro.
+ *
  * The following example shows attaching a private structure
  * <structname>MyObjectPrivate</structname> to an object
  * <structname>MyObject</structname> defined in the standard GObject
@@ -4433,6 +4440,7 @@ g_type_class_add_private (gpointer g_class,
   gsize offset;
 
   g_return_if_fail (private_size > 0);
+  g_return_if_fail (private_size <= 0xffff);
 
   if (!node || !node->is_instantiatable || !node->data || node->data->class.class != g_class)
     {
@@ -4454,6 +4462,9 @@ g_type_class_add_private (gpointer g_class,
   G_WRITE_LOCK (&type_rw_lock);
 
   offset = ALIGN_STRUCT (node->data->instance.private_size);
+
+  g_assert (offset + private_size <= 0xffff);
+
   node->data->instance.private_size = offset + private_size;
   
   G_WRITE_UNLOCK (&type_rw_lock);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]