[gtk+] rbtree: Replace nil node allocation with a preallocated nil



commit 73a834336fd8d67cde6c9739e88744f96af57ceb
Author: Benjamin Otte <otte redhat com>
Date:   Tue Nov 22 01:32:28 2011 +0100

    rbtree: Replace nil node allocation with a preallocated nil
    
    This has one major caveat: The new value is const, so read-only memory.
    Any attempt to write to it will cause a crash. Note that we are not ever
    supposed to write to it, but bugs happen...

 gtk/gtkrbtree.c |   15 ++++++---------
 1 files changed, 6 insertions(+), 9 deletions(-)
---
diff --git a/gtk/gtkrbtree.c b/gtk/gtkrbtree.c
index c9ad445..a9ec703 100644
--- a/gtk/gtkrbtree.c
+++ b/gtk/gtkrbtree.c
@@ -43,6 +43,11 @@ static void        _gtk_rbtree_test               (const gchar *where,
 static void        _gtk_rbtree_debug_spew         (GtkRBTree  *tree);
 #endif
 
+static const GtkRBNode nil = {
+  /* .flags = */ GTK_RBNODE_BLACK,
+
+  /* rest is NULL */
+};
 
 
 static GtkRBNode *
@@ -347,14 +352,7 @@ _gtk_rbtree_new (void)
   retval->parent_tree = NULL;
   retval->parent_node = NULL;
 
-  retval->nil = g_slice_new (GtkRBNode);
-  retval->nil->left = NULL;
-  retval->nil->right = NULL;
-  retval->nil->parent = NULL;
-  retval->nil->flags = GTK_RBNODE_BLACK;
-  retval->nil->count = 0;
-  retval->nil->offset = 0;
-  retval->nil->total_count = 0;
+  retval->nil = (GtkRBNode *) &nil;
 
   retval->root = retval->nil;
   return retval;
@@ -383,7 +381,6 @@ _gtk_rbtree_free (GtkRBTree *tree)
   if (tree->parent_node &&
       tree->parent_node->children == tree)
     tree->parent_node->children = NULL;
-  _gtk_rbnode_free (tree->nil);
   g_free (tree);
 }
 



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