2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "gtktreedatalist.h"
21 #include "gobject/gvalue.h"
25 struct _GAllocator /* from gmem.c */
33 GtkTreeDataList *free_nodes;
37 G_LOCK_DEFINE_STATIC (current_allocator);
38 static GAllocator *current_allocator = NULL;
40 /* HOLDS: current_allocator_lock */
42 gtk_tree_data_list_validate_allocator (GAllocator *allocator)
44 g_return_if_fail (allocator != NULL);
45 g_return_if_fail (allocator->is_unused == TRUE);
47 if (allocator->type != G_ALLOCATOR_NODE)
49 allocator->type = G_ALLOCATOR_NODE;
50 if (allocator->mem_chunk)
52 g_mem_chunk_destroy (allocator->mem_chunk);
53 allocator->mem_chunk = NULL;
57 if (!allocator->mem_chunk)
59 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
60 sizeof (GtkTreeDataList),
61 sizeof (GtkTreeDataList) * allocator->n_preallocs,
63 allocator->free_nodes = NULL;
66 allocator->is_unused = FALSE;
70 gtk_tree_data_list_push_allocator (GAllocator *allocator)
72 G_LOCK (current_allocator);
73 gtk_tree_data_list_validate_allocator ( allocator );
74 allocator->last = current_allocator;
75 current_allocator = allocator;
76 G_UNLOCK (current_allocator);
80 gtk_tree_data_list_pop_allocator (void)
82 G_LOCK (current_allocator);
83 if (current_allocator)
85 GAllocator *allocator;
87 allocator = current_allocator;
88 current_allocator = allocator->last;
89 allocator->last = NULL;
90 allocator->is_unused = TRUE;
92 G_UNLOCK (current_allocator);
96 gtk_tree_data_list_alloc (void)
98 GtkTreeDataList *list;
100 G_LOCK (current_allocator);
101 if (!current_allocator)
103 GAllocator *allocator = g_allocator_new ("GTK+ default GtkTreeDataList allocator",
105 gtk_tree_data_list_validate_allocator (allocator);
106 allocator->last = NULL;
107 current_allocator = allocator;
109 if (!current_allocator->free_nodes)
110 list = g_chunk_new (GtkTreeDataList, current_allocator->mem_chunk);
113 list = current_allocator->free_nodes;
114 current_allocator->free_nodes = list->next;
116 G_UNLOCK (current_allocator);
122 gtk_tree_data_list_free (GtkTreeDataList *list)
124 G_LOCK (current_allocator);
125 list->next = current_allocator->free_nodes;
126 current_allocator->free_nodes = list;
127 G_UNLOCK (current_allocator);
131 gtk_tree_data_list_node_to_value (GtkTreeDataList *list,
135 g_value_init (value, type);
140 g_value_set_string (value, (gchar *) list->data.v_pointer);
146 gtk_tree_data_list_value_to_node (GtkTreeDataList *list,
149 switch (value->g_type)
152 list->data.v_pointer = g_value_dup_string (value);
155 g_warning ("Unsupported type (%s) stored.", g_type_name (value->g_type));