diff options
author | Tim Janik <timj@gtk.org> | 1998-11-24 12:18:22 +0000 |
---|---|---|
committer | Tim Janik <timj@src.gnome.org> | 1998-11-24 12:18:22 +0000 |
commit | 9c1692c2604bf147d2b08877d8cebb1f077658e0 (patch) | |
tree | fc6f6f3a2e46a638f65e448d71eff427716c2808 /glib/gslist.c | |
parent | eabb208b306f159264ed14d0362b56b7749a70e2 (diff) | |
download | glib-9c1692c2604bf147d2b08877d8cebb1f077658e0.tar.gz |
removed the GListAllocator type and its g_*_allocator_*() function
Tue Nov 24 09:40:00 1998 Tim Janik <timj@gtk.org>
* 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.
Diffstat (limited to 'glib/gslist.c')
-rw-r--r-- | glib/gslist.c | 111 |
1 files changed, 70 insertions, 41 deletions
diff --git a/glib/gslist.c b/glib/gslist.c index 6a76d22a6..4efe2a575 100644 --- a/glib/gslist.c +++ b/glib/gslist.c @@ -19,75 +19,103 @@ #include "glib.h" -typedef struct _GRealListAllocator GRealListAllocator; - -struct _GRealListAllocator +struct _GAllocator /* from gmem.c */ { - GMemChunk *list_mem_chunk; - GSList *free_list; + gchar *name; + guint16 n_preallocs; + guint is_unused : 1; + guint type : 4; + GAllocator *last; + GMemChunk *mem_chunk; + GSList *free_lists; /* implementation specific */ }; +static GAllocator *current_allocator = NULL; -static GRealListAllocator *default_allocator = NULL; -static GRealListAllocator *current_allocator = NULL; - -GListAllocator* -g_slist_set_allocator (GListAllocator* fallocator) +void +g_slist_push_allocator (GAllocator *allocator) { - GRealListAllocator* allocator = (GRealListAllocator *) fallocator; - GRealListAllocator* old_allocator = current_allocator; + g_return_if_fail (allocator != NULL); + g_return_if_fail (allocator->is_unused == TRUE); - if (allocator) - current_allocator = allocator; - else + if (allocator->type != G_ALLOCATOR_SLIST) { - if (!default_allocator) - default_allocator = (GRealListAllocator*) g_list_allocator_new (); - current_allocator = default_allocator; + allocator->type = G_ALLOCATOR_SLIST; + if (allocator->mem_chunk) + { + g_mem_chunk_destroy (allocator->mem_chunk); + allocator->mem_chunk = NULL; + } } - if (!current_allocator->list_mem_chunk) - current_allocator->list_mem_chunk = g_mem_chunk_new ("slist mem chunk", - sizeof (GSList), - 1024, - G_ALLOC_ONLY); + if (!allocator->mem_chunk) + { + allocator->mem_chunk = g_mem_chunk_new (allocator->name, + sizeof (GSList), + sizeof (GSList) * allocator->n_preallocs, + G_ALLOC_ONLY); + allocator->free_lists = NULL; + } - return (GListAllocator*) (old_allocator == default_allocator ? NULL : old_allocator); + allocator->is_unused = FALSE; + allocator->last = current_allocator; + current_allocator = allocator; } +void +g_slist_pop_allocator (void) +{ + if (current_allocator) + { + GAllocator *allocator; + + allocator = current_allocator; + current_allocator = allocator->last; + allocator->last = NULL; + allocator->is_unused = TRUE; + } +} GSList* g_slist_alloc (void) { - GSList *new_list; + GSList *list; + + if (!current_allocator) + g_slist_push_allocator (g_allocator_new ("GLib default GSList allocator", 1024)); - g_slist_set_allocator (NULL); - if (current_allocator->free_list) + if (!current_allocator->free_lists) { - new_list = current_allocator->free_list; - current_allocator->free_list = current_allocator->free_list->next; + list = g_chunk_new (GSList, current_allocator->mem_chunk); + list->data = NULL; } else { - new_list = g_chunk_new (GSList, current_allocator->list_mem_chunk); + if (current_allocator->free_lists->data) + { + list = current_allocator->free_lists->data; + current_allocator->free_lists->data = list->next; + list->data = NULL; + } + else + { + list = current_allocator->free_lists; + current_allocator->free_lists = list->next; + } } + list->next = NULL; - new_list->data = NULL; - new_list->next = NULL; - - return new_list; + return list; } void g_slist_free (GSList *list) { - GSList *last; - if (list) { - last = g_slist_last (list); - last->next = current_allocator->free_list; - current_allocator->free_list = list; + list->data = list->next; + list->next = current_allocator->free_lists; + current_allocator->free_lists = list; } } @@ -96,8 +124,9 @@ g_slist_free_1 (GSList *list) { if (list) { - list->next = current_allocator->free_list; - current_allocator->free_list = list; + list->data = NULL; + list->next = current_allocator->free_lists; + current_allocator->free_lists = list; } } |