From 9c1692c2604bf147d2b08877d8cebb1f077658e0 Mon Sep 17 00:00:00 2001 From: Tim Janik Date: Tue, 24 Nov 1998 12:18:22 +0000 Subject: removed the GListAllocator type and its g_*_allocator_*() function Tue Nov 24 09:40:00 1998 Tim Janik * glib.h: removed the GListAllocator type and its g_*_allocator_*() function variants (which weren't working anyways) in favour of a generic GAllocator type. new functions: g_allocator_new, g_allocator_free, g_slist_push_allocator, g_slist_pop_allocator, g_list_push_allocator, g_list_pop_allocator, g_node_push_allocator and g_node_pop_allocator. * gstring.c: removed bogus slist allocator code. * gtree.c: maintain own list of free tree nodes and don't waste GSLists for that, removed bogus slist allocator code. * glist.c: use GAllocators for node allocation. * gslist.c: use GAllocators for node allocation. * gnode.c: use GAllocators for node allocation. * gdataset.c: cleanups wrt automatic initialization. --- gtree.c | 101 +++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 49 insertions(+), 52 deletions(-) (limited to 'gtree.c') diff --git a/gtree.c b/gtree.c index 0d7f78cf9..4d9e98a0b 100644 --- a/gtree.c +++ b/gtree.c @@ -79,7 +79,51 @@ static void g_tree_node_check (GTreeNode *node); static GMemChunk *node_mem_chunk = NULL; -static GSList *node_free_list = NULL; +static GTreeNode *node_free_list = NULL; + + +static GTreeNode* +g_tree_node_new (gpointer key, + gpointer value) +{ + GTreeNode *node; + + if (node_free_list) + { + node = node_free_list; + node_free_list = node->right; + } + else + { + if (!node_mem_chunk) + node_mem_chunk = g_mem_chunk_new ("GLib GTreeNode mem chunk", + sizeof (GTreeNode), + 1024, + G_ALLOC_ONLY); + + node = g_chunk_new (GTreeNode, node_mem_chunk); + } + + node->balance = 0; + node->left = NULL; + node->right = NULL; + node->key = key; + node->value = value; + + return node; +} + +static void +g_tree_node_destroy (GTreeNode *node) +{ + if (node) + { + g_tree_node_destroy (node->right); + g_tree_node_destroy (node->left); + node->right = node_free_list; + node_free_list = node; + } +} GTree* @@ -230,55 +274,6 @@ g_tree_nnodes (GTree *tree) return 0; } - -static GTreeNode* -g_tree_node_new (gpointer key, - gpointer value) -{ - GTreeNode *node; - GSList *tmp_list; - - if (node_free_list) - { - tmp_list = node_free_list; - node_free_list = node_free_list->next; - - node = tmp_list->data; - - { - GListAllocator *tmp_allocator = g_list_set_allocator (NULL); - g_slist_free_1 (tmp_list); - g_list_set_allocator (tmp_allocator); - } - } - else - { - if (!node_mem_chunk) - node_mem_chunk = g_mem_chunk_new ("tree node mem chunk", sizeof (GTreeNode), 1024, G_ALLOC_ONLY); - - node = g_chunk_new (GTreeNode, node_mem_chunk); - } - - node->balance = 0; - node->left = NULL; - node->right = NULL; - node->key = key; - node->value = value; - - return node; -} - -static void -g_tree_node_destroy (GTreeNode *node) -{ - if (node) - { - node_free_list = g_slist_prepend (node_free_list, node); - g_tree_node_destroy (node->right); - g_tree_node_destroy (node->left); - } -} - static GTreeNode* g_tree_node_insert (GTreeNode *node, GCompareFunc compare, @@ -352,7 +347,6 @@ g_tree_node_remove (GTreeNode *node, GCompareFunc compare, gpointer key) { - GTreeNode *garbage; GTreeNode *new_root; gint old_balance; gint cmp; @@ -363,6 +357,8 @@ g_tree_node_remove (GTreeNode *node, cmp = (* compare) (key, node->key); if (cmp == 0) { + GTreeNode *garbage; + garbage = node; if (!node->right) @@ -379,7 +375,8 @@ g_tree_node_remove (GTreeNode *node, node = g_tree_node_restore_right_balance (new_root, old_balance); } - node_free_list = g_slist_prepend (node_free_list, garbage); + garbage->right = node_free_list; + node_free_list = garbage; } else if (cmp < 0) { -- cgit v1.2.3