Blame glib/glib/ghook.c

Packit db3073
/* GLIB - Library of useful routines for C programming
Packit db3073
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit db3073
 *
Packit db3073
 * GHook: Callback maintenance functions
Packit db3073
 * Copyright (C) 1998 Tim Janik
Packit db3073
 *
Packit db3073
 * This library is free software; you can redistribute it and/or
Packit db3073
 * modify it under the terms of the GNU Lesser General Public
Packit db3073
 * License as published by the Free Software Foundation; either
Packit db3073
 * version 2 of the License, or (at your option) any later version.
Packit db3073
 *
Packit db3073
 * This library is distributed in the hope that it will be useful,
Packit db3073
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit db3073
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit db3073
 * Lesser General Public License for more details.
Packit db3073
 *
Packit db3073
 * You should have received a copy of the GNU Lesser General Public
Packit db3073
 * License along with this library; if not, write to the
Packit db3073
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit db3073
 * Boston, MA 02111-1307, USA.
Packit db3073
 */
Packit db3073
Packit db3073
/*
Packit db3073
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit db3073
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit db3073
 * files for a list of changes.  These files are distributed with
Packit db3073
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit db3073
 */
Packit db3073
Packit db3073
/*
Packit db3073
 * MT safe
Packit db3073
 */
Packit db3073
Packit db3073
#include "config.h"
Packit db3073
Packit db3073
#include "ghook.h"
Packit db3073
Packit db3073
#include "gtestutils.h"
Packit db3073
#include "gslice.h"
Packit db3073
Packit db3073
/**
Packit db3073
 * SECTION:hooks
Packit db3073
 * @title: Hook Functions
Packit db3073
 * @short_description: support for manipulating lists of hook functions
Packit db3073
 *
Packit db3073
 * The #GHookList, #GHook and their related functions provide support for
Packit db3073
 * lists of hook functions. Functions can be added and removed from the lists,
Packit db3073
 * and the list of hook functions can be invoked.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * GHookList:
Packit db3073
 * @seq_id: the next free #GHook id
Packit db3073
 * @hook_size: the size of the #GHookList elements, in bytes
Packit db3073
 * @is_setup: 1 if the #GHookList has been initialized
Packit db3073
 * @hooks: the first #GHook element in the list
Packit db3073
 * @dummy3: unused
Packit db3073
 * @finalize_hook: the function to call to finalize a #GHook element.
Packit db3073
 *     The default behaviour is to call the hooks @destroy function
Packit db3073
 * @dummy: unused
Packit db3073
 *
Packit db3073
 * The <structname>GHookList</structname> struct represents a
Packit db3073
 * list of hook functions.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * GHookFinalizeFunc:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook: the hook in @hook_list that gets finalized
Packit db3073
 *
Packit db3073
 * Defines the type of function to be called when a hook in a
Packit db3073
 * list of hooks gets finalized.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * GHookFlagMask:
Packit db3073
 * @G_HOOK_FLAG_ACTIVE: set if the hook has not been destroyed
Packit db3073
 * @G_HOOK_FLAG_IN_CALL: set if the hook is currently being run
Packit db3073
 * @G_HOOK_FLAG_MASK: A mask covering all bits reserved for
Packit db3073
 *   hook flags; see %G_HOOK_FLAG_USER_SHIFT
Packit db3073
 *
Packit db3073
 * Flags used internally in the #GHook implementation.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_HOOK_FLAGS:
Packit db3073
 * @hook: a #GHook
Packit db3073
 *
Packit db3073
 * Gets the flags of a hook.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_HOOK_FLAG_USER_SHIFT:
Packit db3073
 *
Packit db3073
 * The position of the first bit which is not reserved for internal
Packit db3073
 * use be the #GHook implementation, i.e.
Packit db3073
 * <literal>1 << G_HOOK_FLAG_USER_SHIFT</literal> is the first
Packit db3073
 * bit which can be used for application-defined flags.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_HOOK:
Packit db3073
 * @hook: a pointer
Packit db3073
 *
Packit db3073
 * Casts a pointer to a <literal>GHook*</literal>.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_HOOK_IS_VALID:
Packit db3073
 * @hook: a #GHook
Packit db3073
 *
Packit db3073
 * Returns %TRUE if the #GHook is valid, i.e. it is in a #GHookList,
Packit db3073
 * it is active and it has not been destroyed.
Packit db3073
 *
Packit db3073
 * Returns: %TRUE if the #GHook is valid
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_HOOK_ACTIVE:
Packit db3073
 * @hook: a #GHook
Packit db3073
 *
Packit db3073
 * Returns %TRUE if the #GHook is active, which is normally the case
Packit db3073
 * until the #GHook is destroyed.
Packit db3073
 *
Packit db3073
 * Returns: %TRUE if the #GHook is active
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_HOOK_IN_CALL:
Packit db3073
 * @hook: a #GHook
Packit db3073
 *
Packit db3073
 * Returns %TRUE if the #GHook function is currently executing.
Packit db3073
 *
Packit db3073
 * Returns: %TRUE if the #GHook function is currently executing
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_HOOK_IS_UNLINKED:
Packit db3073
 * @hook: a #GHook
Packit db3073
 *
Packit db3073
 * Returns %TRUE if the #GHook is not in a #GHookList.
Packit db3073
 
Packit db3073
 * Returns: %TRUE if the #GHook is not in a #GHookList
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * GHook:
Packit db3073
 * @data: data which is passed to func when this hook is invoked
Packit db3073
 * @next: pointer to the next hook in the list
Packit db3073
 * @prev: pointer to the previous hook in the list
Packit db3073
 * @ref_count: the reference count of this hook
Packit db3073
 * @hook_id: the id of this hook, which is unique within its list
Packit db3073
 * @flags: flags which are set for this hook. See #GHookFlagMask for
Packit db3073
 *     predefined flags
Packit db3073
 * @func: the function to call when this hook is invoked. The possible
Packit db3073
 *     signatures for this function are #GHookFunc and #GHookCheckFunc
Packit db3073
 * @destroy: the default @finalize_hook function of a #GHookList calls
Packit db3073
 *     this member of the hook that is being finalized
Packit db3073
 *
Packit db3073
 * The <structname>GHook</structname> struct represents a single hook
Packit db3073
 * function in a #GHookList.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * GHookFunc:
Packit db3073
 * @data: the data field of the #GHook is passed to the hook function here
Packit db3073
 *
Packit db3073
 * Defines the type of a hook function that can be invoked
Packit db3073
 * by g_hook_list_invoke().
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * GHookCheckFunc:
Packit db3073
 * @data: the data field of the #GHook is passed to the hook function here
Packit db3073
 *
Packit db3073
 * Defines the type of a hook function that can be invoked
Packit db3073
 * by g_hook_list_invoke_check().
Packit db3073
 *
Packit db3073
 * Returns: %FALSE if the #GHook should be destroyed
Packit db3073
 */
Packit db3073
Packit db3073
/* --- functions --- */
Packit db3073
static void
Packit db3073
default_finalize_hook (GHookList *hook_list,
Packit db3073
		       GHook     *hook)
Packit db3073
{
Packit db3073
  GDestroyNotify destroy = hook->destroy;
Packit db3073
Packit db3073
  if (destroy)
Packit db3073
    {
Packit db3073
      hook->destroy = NULL;
Packit db3073
      destroy (hook->data);
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_list_init:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook_size: the size of each element in the #GHookList,
Packit db3073
 *     typically <literal>sizeof (GHook)</literal>
Packit db3073
 *
Packit db3073
 * Initializes a #GHookList.
Packit db3073
 * This must be called before the #GHookList is used.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_list_init (GHookList *hook_list,
Packit db3073
		  guint	     hook_size)
Packit db3073
{
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  g_return_if_fail (hook_size >= sizeof (GHook));
Packit db3073
  
Packit db3073
  hook_list->seq_id = 1;
Packit db3073
  hook_list->hook_size = hook_size;
Packit db3073
  hook_list->is_setup = TRUE;
Packit db3073
  hook_list->hooks = NULL;
Packit db3073
  hook_list->dummy3 = NULL;
Packit db3073
  hook_list->finalize_hook = default_finalize_hook;
Packit db3073
  hook_list->dummy[0] = NULL;
Packit db3073
  hook_list->dummy[1] = NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_list_clear:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 *
Packit db3073
 * Removes all the #GHook elements from a #GHookList.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_list_clear (GHookList *hook_list)
Packit db3073
{
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  
Packit db3073
  if (hook_list->is_setup)
Packit db3073
    {
Packit db3073
      GHook *hook;
Packit db3073
      
Packit db3073
      hook_list->is_setup = FALSE;
Packit db3073
      
Packit db3073
      hook = hook_list->hooks;
Packit db3073
      if (!hook)
Packit db3073
	{
Packit db3073
	  /* destroy hook_list->hook_memchunk */
Packit db3073
	}
Packit db3073
      else
Packit db3073
	do
Packit db3073
	  {
Packit db3073
	    GHook *tmp;
Packit db3073
	    
Packit db3073
	    g_hook_ref (hook_list, hook);
Packit db3073
	    g_hook_destroy_link (hook_list, hook);
Packit db3073
	    tmp = hook->next;
Packit db3073
	    g_hook_unref (hook_list, hook);
Packit db3073
	    hook = tmp;
Packit db3073
	  }
Packit db3073
	while (hook);
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_alloc:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 *
Packit db3073
 * Allocates space for a #GHook and initializes it.
Packit db3073
 *
Packit db3073
 * Returns: a new #GHook
Packit db3073
 */
Packit db3073
GHook*
Packit db3073
g_hook_alloc (GHookList *hook_list)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_val_if_fail (hook_list != NULL, NULL);
Packit db3073
  g_return_val_if_fail (hook_list->is_setup, NULL);
Packit db3073
  
Packit db3073
  hook = g_slice_alloc0 (hook_list->hook_size);
Packit db3073
  hook->data = NULL;
Packit db3073
  hook->next = NULL;
Packit db3073
  hook->prev = NULL;
Packit db3073
  hook->flags = G_HOOK_FLAG_ACTIVE;
Packit db3073
  hook->ref_count = 0;
Packit db3073
  hook->hook_id = 0;
Packit db3073
  hook->func = NULL;
Packit db3073
  hook->destroy = NULL;
Packit db3073
  
Packit db3073
  return hook;
Packit db3073
}
Packit db3073
/**
Packit db3073
 * g_hook_free:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook: the #GHook to free
Packit db3073
 *
Packit db3073
 * Calls the #GHookList @finalize_hook function if it exists,
Packit db3073
 * and frees the memory allocated for the #GHook.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_free (GHookList *hook_list,
Packit db3073
	     GHook     *hook)
Packit db3073
{
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  g_return_if_fail (hook_list->is_setup);
Packit db3073
  g_return_if_fail (hook != NULL);
Packit db3073
  g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
Packit db3073
  g_return_if_fail (!G_HOOK_IN_CALL (hook));
Packit db3073
Packit db3073
  if(hook_list->finalize_hook != NULL)
Packit db3073
      hook_list->finalize_hook (hook_list, hook);
Packit db3073
  g_slice_free1 (hook_list->hook_size, hook);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_destroy_link:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook: the #GHook to remove
Packit db3073
 *
Packit db3073
 * Removes one #GHook from a #GHookList, marking it
Packit db3073
 * inactive and calling g_hook_unref() on it.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_destroy_link (GHookList *hook_list,
Packit db3073
		     GHook     *hook)
Packit db3073
{
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  g_return_if_fail (hook != NULL);
Packit db3073
Packit db3073
  hook->flags &= ~G_HOOK_FLAG_ACTIVE;
Packit db3073
  if (hook->hook_id)
Packit db3073
    {
Packit db3073
      hook->hook_id = 0;
Packit db3073
      g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_destroy:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook_id: a hook ID
Packit db3073
 *
Packit db3073
 * Destroys a #GHook, given its ID.
Packit db3073
 *
Packit db3073
 * Returns: %TRUE if the #GHook was found in the #GHookList and destroyed
Packit db3073
 */
Packit db3073
gboolean
Packit db3073
g_hook_destroy (GHookList   *hook_list,
Packit db3073
		gulong	     hook_id)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_val_if_fail (hook_list != NULL, FALSE);
Packit db3073
  g_return_val_if_fail (hook_id > 0, FALSE);
Packit db3073
  
Packit db3073
  hook = g_hook_get (hook_list, hook_id);
Packit db3073
  if (hook)
Packit db3073
    {
Packit db3073
      g_hook_destroy_link (hook_list, hook);
Packit db3073
      return TRUE;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return FALSE;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_unref:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook: the #GHook to unref
Packit db3073
 *
Packit db3073
 * Decrements the reference count of a #GHook.
Packit db3073
 * If the reference count falls to 0, the #GHook is removed
Packit db3073
 * from the #GHookList and g_hook_free() is called to free it.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_unref (GHookList *hook_list,
Packit db3073
	      GHook	*hook)
Packit db3073
{
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  g_return_if_fail (hook != NULL);
Packit db3073
  g_return_if_fail (hook->ref_count > 0);
Packit db3073
  
Packit db3073
  hook->ref_count--;
Packit db3073
  if (!hook->ref_count)
Packit db3073
    {
Packit db3073
      g_return_if_fail (hook->hook_id == 0);
Packit db3073
      g_return_if_fail (!G_HOOK_IN_CALL (hook));
Packit db3073
Packit db3073
      if (hook->prev)
Packit db3073
	hook->prev->next = hook->next;
Packit db3073
      else
Packit db3073
	hook_list->hooks = hook->next;
Packit db3073
      if (hook->next)
Packit db3073
	{
Packit db3073
	  hook->next->prev = hook->prev;
Packit db3073
	  hook->next = NULL;
Packit db3073
	}
Packit db3073
      hook->prev = NULL;
Packit db3073
Packit db3073
      if (!hook_list->is_setup)
Packit db3073
	{
Packit db3073
	  hook_list->is_setup = TRUE;
Packit db3073
	  g_hook_free (hook_list, hook);
Packit db3073
	  hook_list->is_setup = FALSE;
Packit db3073
      
Packit db3073
	  if (!hook_list->hooks)
Packit db3073
	    {
Packit db3073
	      /* destroy hook_list->hook_memchunk */
Packit db3073
	    }
Packit db3073
	}
Packit db3073
      else
Packit db3073
	g_hook_free (hook_list, hook);
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_ref:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook: the #GHook to increment the reference count of
Packit db3073
 *
Packit db3073
 * Increments the reference count for a #GHook.
Packit db3073
 *
Packit db3073
 * Returns: the @hook that was passed in (since 2.6)
Packit db3073
 */
Packit db3073
GHook *
Packit db3073
g_hook_ref (GHookList *hook_list,
Packit db3073
	    GHook     *hook)
Packit db3073
{
Packit db3073
  g_return_val_if_fail (hook_list != NULL, NULL);
Packit db3073
  g_return_val_if_fail (hook != NULL, NULL);
Packit db3073
  g_return_val_if_fail (hook->ref_count > 0, NULL);
Packit db3073
  
Packit db3073
  hook->ref_count++;
Packit db3073
Packit db3073
  return hook;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_append:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook: the #GHook to add to the end of @hook_list
Packit db3073
 *
Packit db3073
 * Appends a #GHook onto the end of a #GHookList.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_prepend:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook: the #GHook to add to the start of @hook_list
Packit db3073
 *
Packit db3073
 * Prepends a #GHook on the start of a #GHookList.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_prepend (GHookList *hook_list,
Packit db3073
		GHook	  *hook)
Packit db3073
{
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  
Packit db3073
  g_hook_insert_before (hook_list, hook_list->hooks, hook);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_insert_before:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @sibling: the #GHook to insert the new #GHook before
Packit db3073
 * @hook: the #GHook to insert
Packit db3073
 *
Packit db3073
 * Inserts a #GHook into a #GHookList, before a given #GHook.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_insert_before (GHookList *hook_list,
Packit db3073
		      GHook	*sibling,
Packit db3073
		      GHook	*hook)
Packit db3073
{
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  g_return_if_fail (hook_list->is_setup);
Packit db3073
  g_return_if_fail (hook != NULL);
Packit db3073
  g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
Packit db3073
  g_return_if_fail (hook->ref_count == 0);
Packit db3073
  
Packit db3073
  hook->hook_id = hook_list->seq_id++;
Packit db3073
  hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
Packit db3073
  
Packit db3073
  if (sibling)
Packit db3073
    {
Packit db3073
      if (sibling->prev)
Packit db3073
	{
Packit db3073
	  hook->prev = sibling->prev;
Packit db3073
	  hook->prev->next = hook;
Packit db3073
	  hook->next = sibling;
Packit db3073
	  sibling->prev = hook;
Packit db3073
	}
Packit db3073
      else
Packit db3073
	{
Packit db3073
	  hook_list->hooks = hook;
Packit db3073
	  hook->next = sibling;
Packit db3073
	  sibling->prev = hook;
Packit db3073
	}
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
      if (hook_list->hooks)
Packit db3073
	{
Packit db3073
	  sibling = hook_list->hooks;
Packit db3073
	  while (sibling->next)
Packit db3073
	    sibling = sibling->next;
Packit db3073
	  hook->prev = sibling;
Packit db3073
	  sibling->next = hook;
Packit db3073
	}
Packit db3073
      else
Packit db3073
	hook_list->hooks = hook;
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_list_invoke:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @may_recurse: %TRUE if functions which are already running
Packit db3073
 *     (e.g. in another thread) can be called. If set to %FALSE,
Packit db3073
 *     these are skipped
Packit db3073
 *
Packit db3073
 * Calls all of the #GHook functions in a #GHookList.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_list_invoke (GHookList *hook_list,
Packit db3073
		    gboolean   may_recurse)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  g_return_if_fail (hook_list->is_setup);
Packit db3073
Packit db3073
  hook = g_hook_first_valid (hook_list, may_recurse);
Packit db3073
  while (hook)
Packit db3073
    {
Packit db3073
      GHookFunc func;
Packit db3073
      gboolean was_in_call;
Packit db3073
      
Packit db3073
      func = (GHookFunc) hook->func;
Packit db3073
      
Packit db3073
      was_in_call = G_HOOK_IN_CALL (hook);
Packit db3073
      hook->flags |= G_HOOK_FLAG_IN_CALL;
Packit db3073
      func (hook->data);
Packit db3073
      if (!was_in_call)
Packit db3073
	hook->flags &= ~G_HOOK_FLAG_IN_CALL;
Packit db3073
      
Packit db3073
      hook = g_hook_next_valid (hook_list, hook, may_recurse);
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_list_invoke_check:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @may_recurse: %TRUE if functions which are already running
Packit db3073
 *     (e.g. in another thread) can be called. If set to %FALSE,
Packit db3073
 *     these are skipped
Packit db3073
 *
Packit db3073
 * Calls all of the #GHook functions in a #GHookList.
Packit db3073
 * Any function which returns %FALSE is removed from the #GHookList.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_list_invoke_check (GHookList *hook_list,
Packit db3073
			  gboolean   may_recurse)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  g_return_if_fail (hook_list->is_setup);
Packit db3073
  
Packit db3073
  hook = g_hook_first_valid (hook_list, may_recurse);
Packit db3073
  while (hook)
Packit db3073
    {
Packit db3073
      GHookCheckFunc func;
Packit db3073
      gboolean was_in_call;
Packit db3073
      gboolean need_destroy;
Packit db3073
      
Packit db3073
      func = (GHookCheckFunc) hook->func;
Packit db3073
      
Packit db3073
      was_in_call = G_HOOK_IN_CALL (hook);
Packit db3073
      hook->flags |= G_HOOK_FLAG_IN_CALL;
Packit db3073
      need_destroy = !func (hook->data);
Packit db3073
      if (!was_in_call)
Packit db3073
	hook->flags &= ~G_HOOK_FLAG_IN_CALL;
Packit db3073
      if (need_destroy)
Packit db3073
	g_hook_destroy_link (hook_list, hook);
Packit db3073
      
Packit db3073
      hook = g_hook_next_valid (hook_list, hook, may_recurse);
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * GHookCheckMarshaller:
Packit db3073
 * @hook: a #GHook
Packit db3073
 * @marshal_data: user data
Packit db3073
 *
Packit db3073
 * Defines the type of function used by g_hook_list_marshal_check().
Packit db3073
 *
Packit db3073
 * Returns: %FALSE if @hook should be destroyed
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_list_marshal_check:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @may_recurse: %TRUE if hooks which are currently running
Packit db3073
 *     (e.g. in another thread) are considered valid. If set to %FALSE,
Packit db3073
 *     these are skipped
Packit db3073
 * @marshaller: the function to call for each #GHook
Packit db3073
 * @marshal_data: data to pass to @marshaller
Packit db3073
 *
Packit db3073
 * Calls a function on each valid #GHook and destroys it if the
Packit db3073
 * function returns %FALSE.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_list_marshal_check (GHookList	       *hook_list,
Packit db3073
			   gboolean		may_recurse,
Packit db3073
			   GHookCheckMarshaller marshaller,
Packit db3073
			   gpointer		data)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  g_return_if_fail (hook_list->is_setup);
Packit db3073
  g_return_if_fail (marshaller != NULL);
Packit db3073
  
Packit db3073
  hook = g_hook_first_valid (hook_list, may_recurse);
Packit db3073
  while (hook)
Packit db3073
    {
Packit db3073
      gboolean was_in_call;
Packit db3073
      gboolean need_destroy;
Packit db3073
      
Packit db3073
      was_in_call = G_HOOK_IN_CALL (hook);
Packit db3073
      hook->flags |= G_HOOK_FLAG_IN_CALL;
Packit db3073
      need_destroy = !marshaller (hook, data);
Packit db3073
      if (!was_in_call)
Packit db3073
	hook->flags &= ~G_HOOK_FLAG_IN_CALL;
Packit db3073
      if (need_destroy)
Packit db3073
	g_hook_destroy_link (hook_list, hook);
Packit db3073
      
Packit db3073
      hook = g_hook_next_valid (hook_list, hook, may_recurse);
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * GHookMarshaller:
Packit db3073
 * @hook: a #GHook
Packit db3073
 * @marshal_data: user data
Packit db3073
 *
Packit db3073
 * Defines the type of function used by g_hook_list_marshal().
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_list_marshal:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @may_recurse: %TRUE if hooks which are currently running
Packit db3073
 *     (e.g. in another thread) are considered valid. If set to %FALSE,
Packit db3073
 *     these are skipped
Packit db3073
 * @marshaller: the function to call for each #GHook
Packit db3073
 * @marshal_data: data to pass to @marshaller
Packit db3073
 *
Packit db3073
 * Calls a function on each valid #GHook.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_list_marshal (GHookList		     *hook_list,
Packit db3073
		     gboolean		      may_recurse,
Packit db3073
		     GHookMarshaller	      marshaller,
Packit db3073
		     gpointer		      data)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  g_return_if_fail (hook_list->is_setup);
Packit db3073
  g_return_if_fail (marshaller != NULL);
Packit db3073
  
Packit db3073
  hook = g_hook_first_valid (hook_list, may_recurse);
Packit db3073
  while (hook)
Packit db3073
    {
Packit db3073
      gboolean was_in_call;
Packit db3073
      
Packit db3073
      was_in_call = G_HOOK_IN_CALL (hook);
Packit db3073
      hook->flags |= G_HOOK_FLAG_IN_CALL;
Packit db3073
      marshaller (hook, data);
Packit db3073
      if (!was_in_call)
Packit db3073
	hook->flags &= ~G_HOOK_FLAG_IN_CALL;
Packit db3073
      
Packit db3073
      hook = g_hook_next_valid (hook_list, hook, may_recurse);
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_first_valid:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @may_be_in_call: %TRUE if hooks which are currently running
Packit db3073
 *     (e.g. in another thread) are considered valid. If set to %FALSE,
Packit db3073
 *     these are skipped
Packit db3073
 *
Packit db3073
 * Returns the first #GHook in a #GHookList which has not been destroyed.
Packit db3073
 * The reference count for the #GHook is incremented, so you must call
Packit db3073
 * g_hook_unref() to restore it when no longer needed. (Or call
Packit db3073
 * g_hook_next_valid() if you are stepping through the #GHookList.)
Packit db3073
 *
Packit db3073
 * Returns: the first valid #GHook, or %NULL if none are valid
Packit db3073
 */
Packit db3073
GHook*
Packit db3073
g_hook_first_valid (GHookList *hook_list,
Packit db3073
		    gboolean   may_be_in_call)
Packit db3073
{
Packit db3073
  g_return_val_if_fail (hook_list != NULL, NULL);
Packit db3073
  
Packit db3073
  if (hook_list->is_setup)
Packit db3073
    {
Packit db3073
      GHook *hook;
Packit db3073
      
Packit db3073
      hook = hook_list->hooks;
Packit db3073
      if (hook)
Packit db3073
	{
Packit db3073
	  g_hook_ref (hook_list, hook);
Packit db3073
	  if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
Packit db3073
	    return hook;
Packit db3073
	  else
Packit db3073
	    return g_hook_next_valid (hook_list, hook, may_be_in_call);
Packit db3073
	}
Packit db3073
    }
Packit db3073
  
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_next_valid:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook: the current #GHook
Packit db3073
 * @may_be_in_call: %TRUE if hooks which are currently running
Packit db3073
 *     (e.g. in another thread) are considered valid. If set to %FALSE,
Packit db3073
 *     these are skipped
Packit db3073
 *
Packit db3073
 * Returns the next #GHook in a #GHookList which has not been destroyed.
Packit db3073
 * The reference count for the #GHook is incremented, so you must call
Packit db3073
 * g_hook_unref() to restore it when no longer needed. (Or continue to call
Packit db3073
 * g_hook_next_valid() until %NULL is returned.)
Packit db3073
 *
Packit db3073
 * Returns: the next valid #GHook, or %NULL if none are valid
Packit db3073
 */
Packit db3073
GHook*
Packit db3073
g_hook_next_valid (GHookList *hook_list,
Packit db3073
		   GHook     *hook,
Packit db3073
		   gboolean   may_be_in_call)
Packit db3073
{
Packit db3073
  GHook *ohook = hook;
Packit db3073
Packit db3073
  g_return_val_if_fail (hook_list != NULL, NULL);
Packit db3073
Packit db3073
  if (!hook)
Packit db3073
    return NULL;
Packit db3073
  
Packit db3073
  hook = hook->next;
Packit db3073
  while (hook)
Packit db3073
    {
Packit db3073
      if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
Packit db3073
	{
Packit db3073
	  g_hook_ref (hook_list, hook);
Packit db3073
	  g_hook_unref (hook_list, ohook);
Packit db3073
	  
Packit db3073
	  return hook;
Packit db3073
	}
Packit db3073
      hook = hook->next;
Packit db3073
    }
Packit db3073
  g_hook_unref (hook_list, ohook);
Packit db3073
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_get:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook_id: a hook id
Packit db3073
 *
Packit db3073
 * Returns the #GHook with the given id, or %NULL if it is not found.
Packit db3073
 *
Packit db3073
 * Returns: the #GHook with the given id, or %NULL if it is not found
Packit db3073
 */
Packit db3073
GHook*
Packit db3073
g_hook_get (GHookList *hook_list,
Packit db3073
	    gulong     hook_id)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_val_if_fail (hook_list != NULL, NULL);
Packit db3073
  g_return_val_if_fail (hook_id > 0, NULL);
Packit db3073
  
Packit db3073
  hook = hook_list->hooks;
Packit db3073
  while (hook)
Packit db3073
    {
Packit db3073
      if (hook->hook_id == hook_id)
Packit db3073
	return hook;
Packit db3073
      hook = hook->next;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * GHookFindFunc:
Packit db3073
 * @hook: a #GHook
Packit db3073
 * @data: user data passed to g_hook_find_func()
Packit db3073
 *
Packit db3073
 * Defines the type of the function passed to g_hook_find().
Packit db3073
 *
Packit db3073
 * Returns: %TRUE if the required #GHook has been found
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_find:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @need_valids: %TRUE if #GHook elements which have been destroyed
Packit db3073
 *     should be skipped
Packit db3073
 * @func: the function to call for each #GHook, which should return
Packit db3073
 *     %TRUE when the #GHook has been found
Packit db3073
 * @data: the data to pass to @func
Packit db3073
 *
Packit db3073
 * Finds a #GHook in a #GHookList using the given function to
Packit db3073
 * test for a match.
Packit db3073
 *
Packit db3073
 * Returns: the found #GHook or %NULL if no matching #GHook is found
Packit db3073
 */
Packit db3073
GHook*
Packit db3073
g_hook_find (GHookList	  *hook_list,
Packit db3073
	     gboolean	   need_valids,
Packit db3073
	     GHookFindFunc func,
Packit db3073
	     gpointer	   data)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_val_if_fail (hook_list != NULL, NULL);
Packit db3073
  g_return_val_if_fail (func != NULL, NULL);
Packit db3073
  
Packit db3073
  hook = hook_list->hooks;
Packit db3073
  while (hook)
Packit db3073
    {
Packit db3073
      GHook *tmp;
Packit db3073
Packit db3073
      /* test only non-destroyed hooks */
Packit db3073
      if (!hook->hook_id)
Packit db3073
	{
Packit db3073
	  hook = hook->next;
Packit db3073
	  continue;
Packit db3073
	}
Packit db3073
      
Packit db3073
      g_hook_ref (hook_list, hook);
Packit db3073
      
Packit db3073
      if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
Packit db3073
	{
Packit db3073
	  g_hook_unref (hook_list, hook);
Packit db3073
	  
Packit db3073
	  return hook;
Packit db3073
	}
Packit db3073
Packit db3073
      tmp = hook->next;
Packit db3073
      g_hook_unref (hook_list, hook);
Packit db3073
      hook = tmp;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_find_data:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @need_valids: %TRUE if #GHook elements which have been destroyed
Packit db3073
 *     should be skipped
Packit db3073
 * @data: the data to find
Packit db3073
 *
Packit db3073
 * Finds a #GHook in a #GHookList with the given data.
Packit db3073
 *
Packit db3073
 * Returns: the #GHook with the given @data or %NULL if no matching
Packit db3073
 *     #GHook is found
Packit db3073
 */
Packit db3073
GHook*
Packit db3073
g_hook_find_data (GHookList *hook_list,
Packit db3073
		  gboolean   need_valids,
Packit db3073
		  gpointer   data)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_val_if_fail (hook_list != NULL, NULL);
Packit db3073
  
Packit db3073
  hook = hook_list->hooks;
Packit db3073
  while (hook)
Packit db3073
    {
Packit db3073
      /* test only non-destroyed hooks */
Packit db3073
      if (hook->data == data &&
Packit db3073
	  hook->hook_id &&
Packit db3073
	  (!need_valids || G_HOOK_ACTIVE (hook)))
Packit db3073
	return hook;
Packit db3073
Packit db3073
      hook = hook->next;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_find_func:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @need_valids: %TRUE if #GHook elements which have been destroyed
Packit db3073
 *     should be skipped
Packit db3073
 * @func: the function to find
Packit db3073
 *
Packit db3073
 * Finds a #GHook in a #GHookList with the given function.
Packit db3073
 *
Packit db3073
 * Returns: the #GHook with the given @func or %NULL if no matching
Packit db3073
 *     #GHook is found
Packit db3073
 */
Packit db3073
GHook*
Packit db3073
g_hook_find_func (GHookList *hook_list,
Packit db3073
		  gboolean   need_valids,
Packit db3073
		  gpointer   func)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_val_if_fail (hook_list != NULL, NULL);
Packit db3073
  g_return_val_if_fail (func != NULL, NULL);
Packit db3073
  
Packit db3073
  hook = hook_list->hooks;
Packit db3073
  while (hook)
Packit db3073
    {
Packit db3073
      /* test only non-destroyed hooks */
Packit db3073
      if (hook->func == func &&
Packit db3073
	  hook->hook_id &&
Packit db3073
	  (!need_valids || G_HOOK_ACTIVE (hook)))
Packit db3073
	return hook;
Packit db3073
Packit db3073
      hook = hook->next;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_find_func_data:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @need_valids: %TRUE if #GHook elements which have been destroyed
Packit db3073
 *     should be skipped
Packit db3073
 * @func: the function to find
Packit db3073
 * @data: the data to find
Packit db3073
 *
Packit db3073
 * Finds a #GHook in a #GHookList with the given function and data.
Packit db3073
 *
Packit db3073
 * Returns: the #GHook with the given @func and @data or %NULL if
Packit db3073
 *     no matching #GHook is found
Packit db3073
 */
Packit db3073
GHook*
Packit db3073
g_hook_find_func_data (GHookList *hook_list,
Packit db3073
		       gboolean	  need_valids,
Packit db3073
		       gpointer	  func,
Packit db3073
		       gpointer	  data)
Packit db3073
{
Packit db3073
  GHook *hook;
Packit db3073
  
Packit db3073
  g_return_val_if_fail (hook_list != NULL, NULL);
Packit db3073
  g_return_val_if_fail (func != NULL, NULL);
Packit db3073
  
Packit db3073
  hook = hook_list->hooks;
Packit db3073
  while (hook)
Packit db3073
    {
Packit db3073
      /* test only non-destroyed hooks */
Packit db3073
      if (hook->data == data &&
Packit db3073
	  hook->func == func &&
Packit db3073
	  hook->hook_id &&
Packit db3073
	  (!need_valids || G_HOOK_ACTIVE (hook)))
Packit db3073
	return hook;
Packit db3073
Packit db3073
      hook = hook->next;
Packit db3073
    }
Packit db3073
  
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * GHookCompareFunc:
Packit db3073
 * @new_hook: the #GHook being inserted
Packit db3073
 * @sibling: the #GHook to compare with @new_hook
Packit db3073
 *
Packit db3073
 * Defines the type of function used to compare #GHook elements in
Packit db3073
 * g_hook_insert_sorted().
Packit db3073
 *
Packit db3073
 * Returns: a value <= 0 if @new_hook should be before @sibling
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_insert_sorted:
Packit db3073
 * @hook_list: a #GHookList
Packit db3073
 * @hook: the #GHook to insert
Packit db3073
 * @func: the comparison function used to sort the #GHook elements
Packit db3073
 *
Packit db3073
 * Inserts a #GHook into a #GHookList, sorted by the given function.
Packit db3073
 */
Packit db3073
void
Packit db3073
g_hook_insert_sorted (GHookList	      *hook_list,
Packit db3073
		      GHook	      *hook,
Packit db3073
		      GHookCompareFunc func)
Packit db3073
{
Packit db3073
  GHook *sibling;
Packit db3073
  
Packit db3073
  g_return_if_fail (hook_list != NULL);
Packit db3073
  g_return_if_fail (hook_list->is_setup);
Packit db3073
  g_return_if_fail (hook != NULL);
Packit db3073
  g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
Packit db3073
  g_return_if_fail (hook->func != NULL);
Packit db3073
  g_return_if_fail (func != NULL);
Packit db3073
Packit db3073
  /* first non-destroyed hook */
Packit db3073
  sibling = hook_list->hooks;
Packit db3073
  while (sibling && !sibling->hook_id)
Packit db3073
    sibling = sibling->next;
Packit db3073
  
Packit db3073
  while (sibling)
Packit db3073
    {
Packit db3073
      GHook *tmp;
Packit db3073
      
Packit db3073
      g_hook_ref (hook_list, sibling);
Packit db3073
      if (func (hook, sibling) <= 0 && sibling->hook_id)
Packit db3073
	{
Packit db3073
	  g_hook_unref (hook_list, sibling);
Packit db3073
	  break;
Packit db3073
	}
Packit db3073
Packit db3073
      /* next non-destroyed hook */
Packit db3073
      tmp = sibling->next;
Packit db3073
      while (tmp && !tmp->hook_id)
Packit db3073
	tmp = tmp->next;
Packit db3073
Packit db3073
      g_hook_unref (hook_list, sibling);
Packit db3073
      sibling = tmp;
Packit db3073
   
Packit db3073
 }
Packit db3073
  
Packit db3073
  g_hook_insert_before (hook_list, sibling, hook);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_hook_compare_ids:
Packit db3073
 * @new_hook: a #GHook
Packit db3073
 * @sibling: a #GHook to compare with @new_hook
Packit db3073
 *
Packit db3073
 * Compares the ids of two #GHook elements, returning a negative value
Packit db3073
 * if the second id is greater than the first.
Packit db3073
 *
Packit db3073
 * Returns: a value <= 0 if the id of @sibling is >= the id of @new_hook
Packit db3073
 */
Packit db3073
gint
Packit db3073
g_hook_compare_ids (GHook *new_hook,
Packit db3073
		    GHook *sibling)
Packit db3073
{
Packit db3073
  if (new_hook->hook_id < sibling->hook_id)
Packit db3073
    return -1;
Packit db3073
  else if (new_hook->hook_id > sibling->hook_id)
Packit db3073
    return 1;
Packit db3073
  
Packit db3073
  return 0;
Packit db3073
}