]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkrbtree.c
Unify handling of GtkWindow::resizable property
[~andy/gtk] / gtk / gtkrbtree.c
index 0b83f71282bbd8f054b456afd6e23c3b35552af1..b351eb7fc3ffa74a2253262612072e9f91665abc 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
-#include <config.h>
+#include "config.h"
 #include "gtkrbtree.h"
 #include "gtkdebug.h"
-#include "gtkalias.h"
 
-static void        _gtk_rbnode_validate_allocator (GAllocator *allocator);
 static GtkRBNode * _gtk_rbnode_new                (GtkRBTree  *tree,
                                                   gint        height);
 static void        _gtk_rbnode_free               (GtkRBNode  *node);
@@ -34,8 +32,6 @@ static void        _gtk_rbtree_insert_fixup       (GtkRBTree  *tree,
                                                   GtkRBNode  *node);
 static void        _gtk_rbtree_remove_node_fixup  (GtkRBTree  *tree,
                                                   GtkRBNode  *node);
-static gint        _count_nodes                   (GtkRBTree  *tree,
-                                                  GtkRBNode  *node);
 static inline void _fixup_validation              (GtkRBTree  *tree,
                                                   GtkRBNode  *node);
 static inline void _fixup_parity                  (GtkRBTree  *tree,
@@ -43,76 +39,11 @@ static inline void _fixup_parity                  (GtkRBTree  *tree,
 
 
 
-/* node allocation
- */
-struct _GAllocator /* from gmem.c */
-{
-  gchar      *name;
-  guint16     n_preallocs;
-  guint       is_unused : 1;
-  guint       type : 4;
-  GAllocator *last;
-  GMemChunk  *mem_chunk;
-  GtkRBNode  *free_nodes; /* implementation specific */
-};
-
-
-G_LOCK_DEFINE_STATIC (current_allocator);
-static GAllocator *current_allocator = NULL;
-
-
-/* HOLDS: current_allocator_lock */
-static void
-_gtk_rbnode_validate_allocator (GAllocator *allocator)
-{
-  g_return_if_fail (allocator != NULL);
-  g_return_if_fail (allocator->is_unused == TRUE);
-
-  if (allocator->type != G_ALLOCATOR_NODE)
-    {
-      allocator->type = G_ALLOCATOR_NODE;
-      if (allocator->mem_chunk)
-       {
-         g_mem_chunk_destroy (allocator->mem_chunk);
-         allocator->mem_chunk = NULL;
-       }
-    }
-
-  if (!allocator->mem_chunk)
-    {
-      allocator->mem_chunk = g_mem_chunk_new (allocator->name,
-                                             sizeof (GtkRBNode),
-                                             sizeof (GtkRBNode) * allocator->n_preallocs,
-                                             G_ALLOC_ONLY);
-      allocator->free_nodes = NULL;
-    }
-
-  allocator->is_unused = FALSE;
-}
-
 static GtkRBNode *
 _gtk_rbnode_new (GtkRBTree *tree,
                 gint       height)
 {
-  GtkRBNode *node;
-
-  G_LOCK (current_allocator);
-  if (!current_allocator)
-    {
-      GAllocator *allocator = g_allocator_new ("GTK+ default GtkRBNode allocator",
-                                              128);
-      _gtk_rbnode_validate_allocator (allocator);
-      allocator->last = NULL;
-      current_allocator = allocator;
-    }
-  if (!current_allocator->free_nodes)
-    node = g_chunk_new (GtkRBNode, current_allocator->mem_chunk);
-  else
-    {
-      node = current_allocator->free_nodes;
-      current_allocator->free_nodes = node->left;
-    }
-  G_UNLOCK (current_allocator);
+  GtkRBNode *node = g_slice_new (GtkRBNode);
 
   node->left = tree->nil;
   node->right = tree->nil;
@@ -128,21 +59,16 @@ _gtk_rbnode_new (GtkRBTree *tree,
 static void
 _gtk_rbnode_free (GtkRBNode *node)
 {
-  G_LOCK (current_allocator);
-  node->left = current_allocator->free_nodes;
-  current_allocator->free_nodes = node;
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     {
-      /* unfortunately node->left has to continue to point to
-       * a node...
-       */
+      node->left = (gpointer) 0xdeadbeef;
       node->right = (gpointer) 0xdeadbeef;
       node->parent = (gpointer) 0xdeadbeef;
       node->offset = 56789;
       node->count = 56789;
       node->flags = 0;
     }
-  G_UNLOCK (current_allocator);
+  g_slice_free (GtkRBNode, node);
 }
 
 static void
@@ -400,43 +326,16 @@ _gtk_rbtree_remove_node_fixup (GtkRBTree *tree,
   GTK_RBNODE_SET_COLOR (node, GTK_RBNODE_BLACK);
 }
 
-/* Public functions */
-void
-_gtk_rbnode_push_allocator (GAllocator *allocator)
-{
-  G_LOCK (current_allocator);
-  _gtk_rbnode_validate_allocator ( allocator );
-  allocator->last = current_allocator;
-  current_allocator = allocator;
-  G_UNLOCK (current_allocator);
-}
-
-void
-_gtk_rbnode_pop_allocator (void)
-{
-  G_LOCK (current_allocator);
-  if (current_allocator)
-    {
-      GAllocator *allocator;
-
-      allocator = current_allocator;
-      current_allocator = allocator->last;
-      allocator->last = NULL;
-      allocator->is_unused = TRUE;
-    }
-  G_UNLOCK (current_allocator);
-}
-
 GtkRBTree *
 _gtk_rbtree_new (void)
 {
   GtkRBTree *retval;
 
-  retval = (GtkRBTree *) g_new (GtkRBTree, 1);
+  retval = g_new (GtkRBTree, 1);
   retval->parent_tree = NULL;
   retval->parent_node = NULL;
 
-  retval->nil = g_new0 (GtkRBNode, 1);
+  retval->nil = g_slice_new (GtkRBNode);
   retval->nil->left = NULL;
   retval->nil->right = NULL;
   retval->nil->parent = NULL;
@@ -485,7 +384,7 @@ _gtk_rbtree_remove (GtkRBTree *tree)
   gint height = tree->root->offset;
 
 #ifdef G_ENABLE_DEBUG  
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     _gtk_rbtree_test (G_STRLOC, tree);
 #endif
   
@@ -518,7 +417,7 @@ _gtk_rbtree_remove (GtkRBTree *tree)
   _gtk_rbtree_free (tree);
 
 #ifdef G_ENABLE_DEBUG  
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     _gtk_rbtree_test (G_STRLOC, tmp_tree);
 #endif
 }
@@ -536,7 +435,7 @@ _gtk_rbtree_insert_after (GtkRBTree *tree,
   GtkRBTree *tmp_tree;  
 
 #ifdef G_ENABLE_DEBUG  
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     {
       g_print ("\n\n_gtk_rbtree_insert_after: %p\n", current);
       _gtk_rbtree_debug_spew (tree);
@@ -597,7 +496,7 @@ _gtk_rbtree_insert_after (GtkRBTree *tree,
   _gtk_rbtree_insert_fixup (tree, node);
 
 #ifdef G_ENABLE_DEBUG  
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     {
       g_print ("_gtk_rbtree_insert_after finished...\n");
       _gtk_rbtree_debug_spew (tree);
@@ -621,7 +520,7 @@ _gtk_rbtree_insert_before (GtkRBTree *tree,
   GtkRBTree *tmp_tree;
 
 #ifdef G_ENABLE_DEBUG  
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     {
       g_print ("\n\n_gtk_rbtree_insert_before: %p\n", current);
       _gtk_rbtree_debug_spew (tree);
@@ -683,7 +582,7 @@ _gtk_rbtree_insert_before (GtkRBTree *tree,
   _gtk_rbtree_insert_fixup (tree, node);
 
 #ifdef G_ENABLE_DEBUG  
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     {
       g_print ("_gtk_rbtree_insert_before finished...\n");
       _gtk_rbtree_debug_spew (tree);
@@ -740,7 +639,7 @@ _gtk_rbtree_node_set_height (GtkRBTree *tree,
        }
     }
 #ifdef G_ENABLE_DEBUG  
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     _gtk_rbtree_test (G_STRLOC, tree);
 #endif
 }
@@ -895,7 +794,8 @@ _gtk_rbtree_mark_invalid (GtkRBTree *tree)
 
 void
 _gtk_rbtree_set_fixed_height (GtkRBTree *tree,
-                             gint       height)
+                             gint       height,
+                             gboolean   mark_valid)
 {
   GtkRBNode *node;
 
@@ -911,10 +811,14 @@ _gtk_rbtree_set_fixed_height (GtkRBTree *tree,
   do
     {
       if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID))
-       _gtk_rbtree_node_set_height (tree, node, height);
+        {
+         _gtk_rbtree_node_set_height (tree, node, height);
+         if (mark_valid)
+           _gtk_rbtree_node_mark_valid (tree, node);
+       }
 
       if (node->children)
-       _gtk_rbtree_set_fixed_height (node->children, height);
+       _gtk_rbtree_set_fixed_height (node->children, height, mark_valid);
     }
   while ((node = _gtk_rbtree_next (tree, node)) != NULL);
 }
@@ -1208,7 +1112,6 @@ _gtk_rbtree_remove_node (GtkRBTree *tree,
   GtkRBNode *x, *y;
   GtkRBTree *tmp_tree;
   GtkRBNode *tmp_node;
-  gint node_height;
   gint y_height;
   
   g_return_if_fail (tree != NULL);
@@ -1216,7 +1119,7 @@ _gtk_rbtree_remove_node (GtkRBTree *tree,
 
   
 #ifdef G_ENABLE_DEBUG
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     {
       g_print ("\n\n_gtk_rbtree_remove_node: %p\n", node);
       _gtk_rbtree_debug_spew (tree);
@@ -1230,7 +1133,7 @@ _gtk_rbtree_remove_node (GtkRBTree *tree,
   g_return_if_fail (x == tree->root);
 
 #ifdef G_ENABLE_DEBUG  
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     _gtk_rbtree_test (G_STRLOC, tree);
 #endif
   
@@ -1254,7 +1157,6 @@ _gtk_rbtree_remove_node (GtkRBTree *tree,
 
   /* offsets and parity adjust all the way up through parent trees */
   y_height = GTK_RBNODE_GET_HEIGHT (y);
-  node_height = GTK_RBNODE_GET_HEIGHT (node) + (node->children?node->children->root->offset:0);
 
   tmp_tree = tree;
   tmp_node = y;
@@ -1360,7 +1262,7 @@ _gtk_rbtree_remove_node (GtkRBTree *tree,
   _gtk_rbnode_free (y);
 
 #ifdef G_ENABLE_DEBUG  
-  if (gtk_debug_flags & GTK_DEBUG_TREE)
+  if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     {
       g_print ("_gtk_rbtree_remove_node finished...\n");
       _gtk_rbtree_debug_spew (tree);
@@ -1564,25 +1466,6 @@ _gtk_rbtree_traverse (GtkRBTree             *tree,
     }
 }
 
-static gint
-_count_nodes (GtkRBTree *tree,
-             GtkRBNode *node)
-{
-  gint res;
-  if (node == tree->nil)
-    return 0;
-
-  g_assert (node->left);
-  g_assert (node->right);
-  
-  res = (_count_nodes (tree, node->left) +
-        _count_nodes (tree, node->right) + 1);
-
-  if (res != node->count)
-    g_print ("Tree failed\n");
-  return res;
-}
-
 static inline
 void _fixup_validation (GtkRBTree *tree,
                        GtkRBNode *node)
@@ -1662,11 +1545,30 @@ count_parity (GtkRBTree *tree,
     g_print ("parity incorrect for node\n");
 
   if (get_parity (node) != 1)
-    g_error ("Node has incorrect parity %d", get_parity (node));
+    g_error ("Node has incorrect parity %u", get_parity (node));
   
   return res;
 }
 
+static gint
+_count_nodes (GtkRBTree *tree,
+              GtkRBNode *node)
+{
+  gint res;
+  if (node == tree->nil)
+    return 0;
+
+  g_assert (node->left);
+  g_assert (node->right);
+
+  res = (_count_nodes (tree, node->left) +
+         _count_nodes (tree, node->right) + 1);
+
+  if (res != node->count)
+    g_print ("Tree failed\n");
+  return res;
+}
+
 static void
 _gtk_rbtree_test_height (GtkRBTree *tree,
                          GtkRBNode *node)
@@ -1772,7 +1674,7 @@ _gtk_rbtree_test_structure (GtkRBTree *tree)
   g_assert (tree->root->parent == tree->nil);
   _gtk_rbtree_test_structure_helper (tree, tree->root);
 }
-                           
+
 void
 _gtk_rbtree_test (const gchar *where,
                   GtkRBTree   *tree)