* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
*
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
+
+/*
+ * Modified by the GLib Team and others 1997-2000. See the AUTHORS
+ * file for a list of people on the GLib Team. See the ChangeLog
+ * files for a list of changes. These files are distributed with
+ * GLib at ftp://ftp.gtk.org/pub/gtk/.
+ */
+
+/*
+ * MT safe
+ */
+
#include "glib.h"
};
static GAllocator *current_allocator = NULL;
+G_LOCK_DEFINE_STATIC (current_allocator);
-void
-g_list_push_allocator (GAllocator *allocator)
+/* HOLDS: current_allocator_lock */
+static void
+g_list_validate_allocator (GAllocator *allocator)
{
g_return_if_fail (allocator != NULL);
g_return_if_fail (allocator->is_unused == TRUE);
}
allocator->is_unused = FALSE;
+}
+
+void
+g_list_push_allocator(GAllocator *allocator)
+{
+ G_LOCK (current_allocator);
+ g_list_validate_allocator (allocator);
allocator->last = current_allocator;
current_allocator = allocator;
+ G_UNLOCK (current_allocator);
}
void
g_list_pop_allocator (void)
{
+ G_LOCK (current_allocator);
if (current_allocator)
{
GAllocator *allocator;
allocator->last = NULL;
allocator->is_unused = TRUE;
}
+ G_UNLOCK (current_allocator);
}
-GList*
-g_list_alloc (void)
+static inline GList*
+_g_list_alloc (void)
{
GList *list;
+ G_LOCK (current_allocator);
if (!current_allocator)
- g_list_push_allocator (g_allocator_new ("GLib default GList allocator", 1024));
-
+ {
+ GAllocator *allocator = g_allocator_new ("GLib default GList allocator",
+ 128);
+ g_list_validate_allocator (allocator);
+ allocator->last = NULL;
+ current_allocator = allocator;
+ }
if (!current_allocator->free_lists)
{
list = g_chunk_new (GList, current_allocator->mem_chunk);
current_allocator->free_lists = list->next;
}
}
+ G_UNLOCK (current_allocator);
list->next = NULL;
list->prev = NULL;
return list;
}
+GList*
+g_list_alloc (void)
+{
+ return _g_list_alloc ();
+}
+
void
g_list_free (GList *list)
{
if (list)
{
- list->data = list->next;
- list->next = current_allocator->free_lists;
+ GList *last_node = list;
+
+#ifdef ENABLE_GC_FRIENDLY
+ while (last_node->next)
+ {
+ last_node->data = NULL;
+ last_node->prev = NULL;
+ last_node = last_node->next;
+ }
+ last_node->data = NULL
+ last_node->prev = NULL
+#else /* !ENABLE_GC_FRIENDLY */
+ list->data = list->next;
+#endif /* ENABLE_GC_FRIENDLY */
+
+ G_LOCK (current_allocator);
+ last_node->next = current_allocator->free_lists;
current_allocator->free_lists = list;
+ G_UNLOCK (current_allocator);
}
}
-void
-g_list_free_1 (GList *list)
+static inline void
+_g_list_free_1 (GList *list)
{
if (list)
{
- list->data = NULL;
+ list->data = NULL;
+
+#ifdef ENABLE_GC_FRIENDLY
+ list->prev = NULL;
+#endif /* ENABLE_GC_FRIENDLY */
+
+ G_LOCK (current_allocator);
list->next = current_allocator->free_lists;
current_allocator->free_lists = list;
+ G_UNLOCK (current_allocator);
}
}
+void
+g_list_free_1 (GList *list)
+{
+ _g_list_free_1 (list);
+}
+
GList*
g_list_append (GList *list,
gpointer data)
GList *new_list;
GList *last;
- new_list = g_list_alloc ();
+ new_list = _g_list_alloc ();
new_list->data = data;
if (list)
{
GList *new_list;
- new_list = g_list_alloc ();
+ new_list = _g_list_alloc ();
new_list->data = data;
if (list)
if (!tmp_list)
return g_list_append (list, data);
- new_list = g_list_alloc ();
+ new_list = _g_list_alloc ();
new_list->data = data;
if (tmp_list->prev)
}
GList*
-g_list_remove (GList *list,
- gpointer data)
+g_list_remove (GList *list,
+ gconstpointer data)
{
GList *tmp;
if (list == tmp)
list = list->next;
- g_list_free_1 (tmp);
+ _g_list_free_1 (tmp);
break;
}
return list;
}
-GList*
-g_list_remove_link (GList *list,
- GList *link)
+static inline GList*
+_g_list_remove_link (GList *list,
+ GList *link)
{
if (link)
{
}
GList*
+g_list_remove_link (GList *list,
+ GList *link)
+{
+ return _g_list_remove_link (list, link);
+}
+
+GList*
+g_list_delete_link (GList *list,
+ GList *link)
+{
+ list = _g_list_remove_link (list, link);
+ _g_list_free_1 (link);
+
+ return list;
+}
+
+GList*
g_list_copy (GList *list)
{
GList *new_list = NULL;
{
GList *last;
- new_list = g_list_alloc ();
+ new_list = _g_list_alloc ();
new_list->data = list->data;
last = new_list;
list = list->next;
while (list)
{
- last->next = g_list_alloc ();
+ last->next = _g_list_alloc ();
last->next->prev = last;
last = last->next;
last->data = list->data;
}
GList*
-g_list_find (GList *list,
- gpointer data)
+g_list_find (GList *list,
+ gconstpointer data)
{
while (list)
{
}
GList*
-g_list_find_custom (GList *list,
- gpointer data,
- GCompareFunc func)
+g_list_find_custom (GList *list,
+ gconstpointer data,
+ GCompareFunc func)
{
g_return_val_if_fail (func != NULL, list);
}
gint
-g_list_index (GList *list,
- gpointer data)
+g_list_index (GList *list,
+ gconstpointer data)
{
gint i;
if (!list)
{
- new_list = g_list_alloc();
+ new_list = _g_list_alloc ();
new_list->data = data;
return new_list;
}
cmp = (*func) (data, tmp_list->data);
}
- new_list = g_list_alloc();
+ new_list = _g_list_alloc ();
new_list->data = data;
if ((!tmp_list->next) && (cmp > 0))