Revamp to be like makefile.mingw.in, make the MSVC build actually work
[platform/upstream/glib.git] / glib / glist.c
index e74dee8..0423212 100644 (file)
@@ -2,21 +2,28 @@
  * 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
  */
@@ -36,7 +43,7 @@ struct _GAllocator /* from gmem.c */
 };
 
 static GAllocator      *current_allocator = NULL;
-static G_LOCK_DEFINE(current_allocator);
+G_LOCK_DEFINE_STATIC (current_allocator);
 
 /* HOLDS: current_allocator_lock */
 static void
@@ -70,17 +77,17 @@ g_list_validate_allocator (GAllocator *allocator)
 void
 g_list_push_allocator(GAllocator *allocator)
 {
-  g_list_validate_allocator ( allocator );
-  g_lock (current_allocator);
+  G_LOCK (current_allocator);
+  g_list_validate_allocator (allocator);
   allocator->last = current_allocator;
   current_allocator = allocator;
-  g_unlock (current_allocator);
+  G_UNLOCK (current_allocator);
 }
 
 void
 g_list_pop_allocator (void)
 {
-  g_lock (current_allocator);
+  G_LOCK (current_allocator);
   if (current_allocator)
     {
       GAllocator *allocator;
@@ -90,19 +97,19 @@ g_list_pop_allocator (void)
       allocator->last = NULL;
       allocator->is_unused = TRUE;
     }
-  g_unlock (current_allocator);
+  G_UNLOCK (current_allocator);
 }
 
-GList*
-g_list_alloc (void)
+static inline GList*
+_g_list_alloc (void)
 {
   GList *list;
 
-  g_lock (current_allocator);
+  G_LOCK (current_allocator);
   if (!current_allocator)
     {
       GAllocator *allocator = g_allocator_new ("GLib default GList allocator",
-                                              1024);
+                                              128);
       g_list_validate_allocator (allocator);
       allocator->last = NULL;
       current_allocator = allocator;
@@ -126,41 +133,68 @@ g_list_alloc (void)
          current_allocator->free_lists = list->next;
        }
     }
-  g_unlock (current_allocator);
+  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 0
   if (list)
     {
+      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;  
-      g_lock (current_allocator);
-      list->next = current_allocator->free_lists;
+#endif /* ENABLE_GC_FRIENDLY */
+
+      G_LOCK (current_allocator);
+      last_node->next = current_allocator->free_lists;
       current_allocator->free_lists = list;
-      g_unlock (current_allocator);
+      G_UNLOCK (current_allocator);
     }
-#endif
 }
 
-void
-g_list_free_1 (GList *list)
+static inline void
+_g_list_free_1 (GList *list)
 {
-#if 0  
   if (list)
     {
       list->data = NULL;  
-      g_lock (current_allocator);
+
+#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);
+      G_UNLOCK (current_allocator);
     }
-#endif  
+}
+
+void
+g_list_free_1 (GList *list)
+{
+  _g_list_free_1 (list);
 }
 
 GList*
@@ -170,7 +204,7 @@ g_list_append (GList        *list,
   GList *new_list;
   GList *last;
   
-  new_list = g_list_alloc ();
+  new_list = _g_list_alloc ();
   new_list->data = data;
   
   if (list)
@@ -192,7 +226,7 @@ g_list_prepend (GList        *list,
 {
   GList *new_list;
   
-  new_list = g_list_alloc ();
+  new_list = _g_list_alloc ();
   new_list->data = data;
   
   if (list)
@@ -226,7 +260,7 @@ g_list_insert (GList        *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)
@@ -262,8 +296,8 @@ g_list_concat (GList *list1, GList *list2)
 }
 
 GList*
-g_list_remove (GList   *list,
-              gpointer  data)
+g_list_remove (GList        *list,
+              gconstpointer  data)
 {
   GList *tmp;
   
@@ -282,7 +316,7 @@ g_list_remove (GList        *list,
          if (list == tmp)
            list = list->next;
          
-         g_list_free_1 (tmp);
+         _g_list_free_1 (tmp);
          
          break;
        }
@@ -290,9 +324,9 @@ g_list_remove (GList        *list,
   return list;
 }
 
-GList*
-g_list_remove_link (GList *list,
-                   GList *link)
+static inline GList*
+_g_list_remove_link (GList *list,
+                    GList *link)
 {
   if (link)
     {
@@ -312,6 +346,23 @@ g_list_remove_link (GList *list,
 }
 
 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;
@@ -320,13 +371,13 @@ g_list_copy (GList *list)
     {
       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;
@@ -375,8 +426,8 @@ g_list_nth_data (GList     *list,
 }
 
 GList*
-g_list_find (GList    *list,
-            gpointer  data)
+g_list_find (GList         *list,
+            gconstpointer  data)
 {
   while (list)
     {
@@ -389,9 +440,9 @@ g_list_find (GList    *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);
 
@@ -425,8 +476,8 @@ g_list_position (GList *list,
 }
 
 gint
-g_list_index (GList   *list,
-             gpointer data)
+g_list_index (GList         *list,
+             gconstpointer  data)
 {
   gint i;
 
@@ -507,7 +558,7 @@ g_list_insert_sorted (GList        *list,
   
   if (!list) 
     {
-      new_list = g_list_alloc();
+      new_list = _g_list_alloc ();
       new_list->data = data;
       return new_list;
     }
@@ -520,7 +571,7 @@ g_list_insert_sorted (GList        *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))