Blame glib/deprecated/gcache.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gcache.h"
Packit ae235b
Packit ae235b
#include "gslice.h"
Packit ae235b
#include "ghash.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:caches
Packit ae235b
 * @title: Caches
Packit ae235b
 * @short_description: caches allow sharing of complex data structures
Packit ae235b
 *                     to save resources
Packit ae235b
 *
Packit ae235b
 * A #GCache allows sharing of complex data structures, in order to
Packit ae235b
 * save system resources.
Packit ae235b
 *
Packit ae235b
 * GCache uses keys and values. A GCache key describes the properties
Packit ae235b
 * of a particular resource. A GCache value is the actual resource.
Packit ae235b
 *
Packit ae235b
 * GCache has been marked as deprecated, since this API is rarely
Packit ae235b
 * used and not very actively maintained.
Packit ae235b
 */
Packit ae235b
Packit ae235b
typedef struct _GCacheNode  GCacheNode;
Packit ae235b
Packit ae235b
struct _GCacheNode
Packit ae235b
{
Packit ae235b
  /* A reference counted node */
Packit ae235b
  gpointer value;
Packit ae235b
  gint ref_count;
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GCache:
Packit ae235b
 *
Packit ae235b
 * The #GCache struct is an opaque data structure containing
Packit ae235b
 * information about a #GCache. It should only be accessed via the
Packit ae235b
 * following functions.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: Use a #GHashTable instead
Packit ae235b
 */
Packit ae235b
struct _GCache
Packit ae235b
{
Packit ae235b
  /* Called to create a value from a key */
Packit ae235b
  GCacheNewFunc value_new_func;
Packit ae235b
Packit ae235b
  /* Called to destroy a value */
Packit ae235b
  GCacheDestroyFunc value_destroy_func;
Packit ae235b
Packit ae235b
  /* Called to duplicate a key */
Packit ae235b
  GCacheDupFunc key_dup_func;
Packit ae235b
Packit ae235b
  /* Called to destroy a key */
Packit ae235b
  GCacheDestroyFunc key_destroy_func;
Packit ae235b
Packit ae235b
  /* Associates keys with nodes */
Packit ae235b
  GHashTable *key_table;
Packit ae235b
Packit ae235b
  /* Associates nodes with keys */
Packit ae235b
  GHashTable *value_table;
Packit ae235b
};
Packit ae235b
Packit ae235b
static inline GCacheNode*
Packit ae235b
g_cache_node_new (gpointer value)
Packit ae235b
{
Packit ae235b
  GCacheNode *node = g_slice_new (GCacheNode);
Packit ae235b
  node->value = value;
Packit ae235b
  node->ref_count = 1;
Packit ae235b
  return node;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline void
Packit ae235b
g_cache_node_destroy (GCacheNode *node)
Packit ae235b
{
Packit ae235b
  g_slice_free (GCacheNode, node);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cache_new:
Packit ae235b
 * @value_new_func: a function to create a new object given a key.
Packit ae235b
 *                  This is called by g_cache_insert() if an object
Packit ae235b
 *                  with the given key does not already exist
Packit ae235b
 * @value_destroy_func: a function to destroy an object. It is called
Packit ae235b
 *                      by g_cache_remove() when the object is no
Packit ae235b
 *                      longer needed (i.e. its reference count drops
Packit ae235b
 *                      to 0)
Packit ae235b
 * @key_dup_func: a function to copy a key. It is called by
Packit ae235b
 *                g_cache_insert() if the key does not already exist in
Packit ae235b
 *                the #GCache
Packit ae235b
 * @key_destroy_func: a function to destroy a key. It is called by
Packit ae235b
 *                    g_cache_remove() when the object is no longer
Packit ae235b
 *                    needed (i.e. its reference count drops to 0)
Packit ae235b
 * @hash_key_func: a function to create a hash value from a key
Packit ae235b
 * @hash_value_func: a function to create a hash value from a value
Packit ae235b
 * @key_equal_func: a function to compare two keys. It should return
Packit ae235b
 *                  %TRUE if the two keys are equivalent
Packit ae235b
 *
Packit ae235b
 * Creates a new #GCache.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GCache
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: Use a #GHashTable instead
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GCacheNewFunc:
Packit ae235b
 * @key: a #GCache key
Packit ae235b
 *
Packit ae235b
 * Specifies the type of the @value_new_func function passed to
Packit ae235b
 * g_cache_new(). It is passed a #GCache key and should create the
Packit ae235b
 * value corresponding to the key.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GCache value corresponding to the key.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GCacheDestroyFunc:
Packit ae235b
 * @value: the #GCache value to destroy
Packit ae235b
 *
Packit ae235b
 * Specifies the type of the @value_destroy_func and @key_destroy_func
Packit ae235b
 * functions passed to g_cache_new(). The functions are passed a
Packit ae235b
 * pointer to the #GCache key or #GCache value and should free any
Packit ae235b
 * memory and other resources associated with it.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GCacheDupFunc:
Packit ae235b
 * @value: the #GCache key to destroy (__not__ a
Packit ae235b
 *         #GCache value as it seems)
Packit ae235b
 *
Packit ae235b
 * Specifies the type of the @key_dup_func function passed to
Packit ae235b
 * g_cache_new(). The function is passed a key
Packit ae235b
 * (__not__ a value as the prototype implies) and
Packit ae235b
 * should return a duplicate of the key.
Packit ae235b
 *
Packit ae235b
 * Returns: a copy of the #GCache key
Packit ae235b
 */
Packit ae235b
GCache*
Packit ae235b
g_cache_new (GCacheNewFunc      value_new_func,
Packit ae235b
             GCacheDestroyFunc  value_destroy_func,
Packit ae235b
             GCacheDupFunc      key_dup_func,
Packit ae235b
             GCacheDestroyFunc  key_destroy_func,
Packit ae235b
             GHashFunc          hash_key_func,
Packit ae235b
             GHashFunc          hash_value_func,
Packit ae235b
             GEqualFunc         key_equal_func)
Packit ae235b
{
Packit ae235b
  GCache *cache;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (value_new_func != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (value_destroy_func != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (key_dup_func != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (key_destroy_func != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (hash_key_func != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (hash_value_func != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (key_equal_func != NULL, NULL);
Packit ae235b
Packit ae235b
  cache = g_slice_new (GCache);
Packit ae235b
  cache->value_new_func = value_new_func;
Packit ae235b
  cache->value_destroy_func = value_destroy_func;
Packit ae235b
  cache->key_dup_func = key_dup_func;
Packit ae235b
  cache->key_destroy_func = key_destroy_func;
Packit ae235b
  cache->key_table = g_hash_table_new (hash_key_func, key_equal_func);
Packit ae235b
  cache->value_table = g_hash_table_new (hash_value_func, NULL);
Packit ae235b
Packit ae235b
  return cache;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cache_destroy:
Packit ae235b
 * @cache: a #GCache
Packit ae235b
 *
Packit ae235b
 * Frees the memory allocated for the #GCache.
Packit ae235b
 *
Packit ae235b
 * Note that it does not destroy the keys and values which were
Packit ae235b
 * contained in the #GCache.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: Use a #GHashTable instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_cache_destroy (GCache *cache)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (cache != NULL);
Packit ae235b
Packit ae235b
  g_hash_table_destroy (cache->key_table);
Packit ae235b
  g_hash_table_destroy (cache->value_table);
Packit ae235b
  g_slice_free (GCache, cache);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cache_insert:
Packit ae235b
 * @cache: a #GCache
Packit ae235b
 * @key: a key describing a #GCache object
Packit ae235b
 *
Packit ae235b
 * Gets the value corresponding to the given key, creating it if
Packit ae235b
 * necessary. It first checks if the value already exists in the
Packit ae235b
 * #GCache, by using the @key_equal_func function passed to
Packit ae235b
 * g_cache_new(). If it does already exist it is returned, and its
Packit ae235b
 * reference count is increased by one. If the value does not currently
Packit ae235b
 * exist, if is created by calling the @value_new_func. The key is
Packit ae235b
 * duplicated by calling @key_dup_func and the duplicated key and value
Packit ae235b
 * are inserted into the #GCache.
Packit ae235b
 *
Packit ae235b
 * Returns: a pointer to a #GCache value
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: Use a #GHashTable instead
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_cache_insert (GCache   *cache,
Packit ae235b
                gpointer  key)
Packit ae235b
{
Packit ae235b
  GCacheNode *node;
Packit ae235b
  gpointer value;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (cache != NULL, NULL);
Packit ae235b
Packit ae235b
  node = g_hash_table_lookup (cache->key_table, key);
Packit ae235b
  if (node)
Packit ae235b
    {
Packit ae235b
      node->ref_count += 1;
Packit ae235b
      return node->value;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  key = (* cache->key_dup_func) (key);
Packit ae235b
  value = (* cache->value_new_func) (key);
Packit ae235b
  node = g_cache_node_new (value);
Packit ae235b
Packit ae235b
  g_hash_table_insert (cache->key_table, key, node);
Packit ae235b
  g_hash_table_insert (cache->value_table, value, key);
Packit ae235b
Packit ae235b
  return node->value;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cache_remove:
Packit ae235b
 * @cache: a #GCache
Packit ae235b
 * @value: the value to remove
Packit ae235b
 *
Packit ae235b
 * Decreases the reference count of the given value. If it drops to 0
Packit ae235b
 * then the value and its corresponding key are destroyed, using the
Packit ae235b
 * @value_destroy_func and @key_destroy_func passed to g_cache_new().
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: Use a #GHashTable instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_cache_remove (GCache        *cache,
Packit ae235b
                gconstpointer  value)
Packit ae235b
{
Packit ae235b
  GCacheNode *node;
Packit ae235b
  gpointer key;
Packit ae235b
Packit ae235b
  g_return_if_fail (cache != NULL);
Packit ae235b
Packit ae235b
  key = g_hash_table_lookup (cache->value_table, value);
Packit ae235b
  node = g_hash_table_lookup (cache->key_table, key);
Packit ae235b
Packit ae235b
  g_return_if_fail (node != NULL);
Packit ae235b
Packit ae235b
  node->ref_count -= 1;
Packit ae235b
  if (node->ref_count == 0)
Packit ae235b
    {
Packit ae235b
      g_hash_table_remove (cache->value_table, value);
Packit ae235b
      g_hash_table_remove (cache->key_table, key);
Packit ae235b
Packit ae235b
      (* cache->key_destroy_func) (key);
Packit ae235b
      (* cache->value_destroy_func) (node->value);
Packit ae235b
      g_cache_node_destroy (node);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cache_key_foreach:
Packit ae235b
 * @cache: a #GCache
Packit ae235b
 * @func: the function to call with each #GCache key
Packit ae235b
 * @user_data: user data to pass to the function
Packit ae235b
 *
Packit ae235b
 * Calls the given function for each of the keys in the #GCache.
Packit ae235b
 *
Packit ae235b
 * NOTE @func is passed three parameters, the value and key of a cache
Packit ae235b
 * entry and the @user_data. The order of value and key is different
Packit ae235b
 * from the order in which g_hash_table_foreach() passes key-value
Packit ae235b
 * pairs to its callback function !
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: Use a #GHashTable instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_cache_key_foreach (GCache   *cache,
Packit ae235b
                     GHFunc    func,
Packit ae235b
                     gpointer  user_data)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (cache != NULL);
Packit ae235b
  g_return_if_fail (func != NULL);
Packit ae235b
Packit ae235b
  g_hash_table_foreach (cache->value_table, func, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cache_value_foreach:
Packit ae235b
 * @cache: a #GCache
Packit ae235b
 * @func: the function to call with each #GCache value
Packit ae235b
 * @user_data: user data to pass to the function
Packit ae235b
 *
Packit ae235b
 * Calls the given function for each of the values in the #GCache.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.10: The reason is that it passes pointers to internal
Packit ae235b
 *    data structures to @func; use g_cache_key_foreach() instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_cache_value_foreach (GCache   *cache,
Packit ae235b
                       GHFunc    func,
Packit ae235b
                       gpointer  user_data)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (cache != NULL);
Packit ae235b
  g_return_if_fail (func != NULL);
Packit ae235b
Packit ae235b
  g_hash_table_foreach (cache->key_table, func, user_data);
Packit ae235b
}