Add a note about g_strreverse() and UTF-8.
[platform/upstream/glib.git] / glib / ghook.c
index a3e3a78..d2b0fcd 100644 (file)
@@ -5,23 +5,23 @@
  * Copyright (C) 1998 Tim Janik
  *
  * 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-1999.  See the AUTHORS
+ * 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/. 
 
 
 /* --- functions --- */
+static void
+default_finalize_hook (GHookList *hook_list,
+                      GHook     *hook)
+{
+  GDestroyNotify destroy = hook->destroy;
+
+  if (destroy)
+    {
+      hook->destroy = NULL;
+      destroy (hook->data);
+    }
+}
+
 void
 g_hook_list_init (GHookList *hook_list,
                  guint      hook_size)
 {
   g_return_if_fail (hook_list != NULL);
   g_return_if_fail (hook_size >= sizeof (GHook));
+  g_return_if_fail (hook_size < 65536);
   
   hook_list->seq_id = 1;
   hook_list->hook_size = hook_size;
@@ -54,8 +68,9 @@ g_hook_list_init (GHookList *hook_list,
                                              hook_size,
                                              hook_size * G_HOOKS_PREALLOC,
                                              G_ALLOC_AND_FREE);
-  hook_list->hook_free = NULL;
-  hook_list->hook_destroy = NULL;
+  hook_list->finalize_hook = default_finalize_hook;
+  hook_list->dummy[0] = NULL;
+  hook_list->dummy[1] = NULL;
 }
 
 void
@@ -87,6 +102,8 @@ g_hook_list_clear (GHookList *hook_list)
            hook = tmp;
          }
        while (hook);
+      if (hook_list->hook_memchunk)
+       g_warning (G_STRLOC ": failed to clear hooklist, unconsolidated references on hooks left");
     }
 }
 
@@ -119,10 +136,9 @@ g_hook_free (GHookList *hook_list,
   g_return_if_fail (hook_list->is_setup);
   g_return_if_fail (hook != NULL);
   g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
+  g_return_if_fail (!G_HOOK_IN_CALL (hook));
 
-  if (hook_list->hook_free)
-    hook_list->hook_free (hook_list, hook);
-  
+  hook_list->finalize_hook (hook_list, hook);
   g_chunk_free (hook, hook_list->hook_memchunk);
 }
 
@@ -132,30 +148,18 @@ g_hook_destroy_link (GHookList *hook_list,
 {
   g_return_if_fail (hook_list != NULL);
   g_return_if_fail (hook != NULL);
-  
+
+  hook->flags &= ~G_HOOK_FLAG_ACTIVE;
   if (hook->hook_id)
     {
       hook->hook_id = 0;
-      hook->flags &= ~G_HOOK_FLAG_ACTIVE;
-      if (hook_list->hook_destroy)
-       {
-         if (hook_list->hook_destroy != G_HOOK_DEFERRED_DESTROY)
-           hook_list->hook_destroy (hook_list, hook);
-       }
-      else if (hook->destroy)
-       {
-         hook->destroy (hook->data);
-         hook->data = NULL;
-         hook->func = NULL;
-         hook->destroy = NULL;
-       }
       g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
     }
 }
 
 gboolean
 g_hook_destroy (GHookList   *hook_list,
-               guint        hook_id)
+               gulong       hook_id)
 {
   GHook *hook;
   
@@ -177,6 +181,7 @@ g_hook_unref (GHookList *hook_list,
              GHook     *hook)
 {
   g_return_if_fail (hook_list != NULL);
+  g_return_if_fail (hook_list->hook_memchunk != NULL);
   g_return_if_fail (hook != NULL);
   g_return_if_fail (hook->ref_count > 0);
   
@@ -185,7 +190,7 @@ g_hook_unref (GHookList *hook_list,
     {
       g_return_if_fail (hook->hook_id == 0);
       g_return_if_fail (!G_HOOK_IN_CALL (hook));
-      
+
       if (hook->prev)
        hook->prev->next = hook->next;
       else
@@ -196,15 +201,21 @@ g_hook_unref (GHookList *hook_list,
          hook->next = NULL;
        }
       hook->prev = NULL;
-      
-      g_hook_free (hook_list, hook);
-      
-      if (!hook_list->hooks &&
-         !hook_list->is_setup)
+
+      if (!hook_list->is_setup)
        {
-         g_mem_chunk_destroy (hook_list->hook_memchunk);
-         hook_list->hook_memchunk = NULL;
+         hook_list->is_setup = TRUE;
+         g_hook_free (hook_list, hook);
+         hook_list->is_setup = FALSE;
+      
+         if (!hook_list->hooks)
+           {
+             g_mem_chunk_destroy (hook_list->hook_memchunk);
+             hook_list->hook_memchunk = NULL;
+           }
        }
+      else
+       g_hook_free (hook_list, hook);
     }
 }
 
@@ -237,7 +248,7 @@ g_hook_insert_before (GHookList *hook_list,
   g_return_if_fail (hook_list->is_setup);
   g_return_if_fail (hook != NULL);
   g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
-  g_return_if_fail (hook->func != NULL);
+  g_return_if_fail (hook->ref_count == 0);
   
   hook->hook_id = hook_list->seq_id++;
   hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
@@ -442,7 +453,7 @@ g_hook_next_valid (GHookList *hook_list,
 
 GHook*
 g_hook_get (GHookList *hook_list,
-           guint      hook_id)
+           gulong     hook_id)
 {
   GHook *hook;
   
@@ -622,5 +633,10 @@ gint
 g_hook_compare_ids (GHook *new_hook,
                    GHook *sibling)
 {
-  return ((glong) new_hook->hook_id) - ((glong) sibling->hook_id);
+  if (new_hook->hook_id < sibling->hook_id)
+    return -1;
+  else if (new_hook->hook_id > sibling->hook_id)
+    return 1;
+  
+  return 0;
 }