glib/glib/gcache.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* GLIB - Library of useful routines for C programming
       
     2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       
     3  * Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
     4  *
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Lesser General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Lesser General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Lesser General Public
       
    16  * License along with this library; if not, write to the
       
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    18  * Boston, MA 02111-1307, USA.
       
    19  */
       
    20 
       
    21 /*
       
    22  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
       
    23  * file for a list of people on the GLib Team.  See the ChangeLog
       
    24  * files for a list of changes.  These files are distributed with
       
    25  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
       
    26  */
       
    27 
       
    28 /* 
       
    29  * MT safe
       
    30  */
       
    31 
       
    32 #include "config.h"
       
    33 
       
    34 #include "glib.h"
       
    35 #include "galias.h"
       
    36 
       
    37 
       
    38 typedef struct _GCacheNode  GCacheNode;
       
    39 
       
    40 struct _GCacheNode
       
    41 {
       
    42   /* A reference counted node */
       
    43   gpointer value;
       
    44   gint ref_count;
       
    45 };
       
    46 
       
    47 struct _GCache
       
    48 {
       
    49   /* Called to create a value from a key */
       
    50   GCacheNewFunc value_new_func;
       
    51 
       
    52   /* Called to destroy a value */
       
    53   GCacheDestroyFunc value_destroy_func;
       
    54 
       
    55   /* Called to duplicate a key */
       
    56   GCacheDupFunc key_dup_func;
       
    57 
       
    58   /* Called to destroy a key */
       
    59   GCacheDestroyFunc key_destroy_func;
       
    60 
       
    61   /* Associates keys with nodes */
       
    62   GHashTable *key_table;
       
    63 
       
    64   /* Associates nodes with keys */
       
    65   GHashTable *value_table;
       
    66 };
       
    67 
       
    68 static inline GCacheNode*
       
    69 g_cache_node_new (gpointer value)
       
    70 {
       
    71   GCacheNode *node = g_slice_new (GCacheNode);
       
    72   node->value = value;
       
    73   node->ref_count = 1;
       
    74   return node;
       
    75 }
       
    76 
       
    77 static inline void
       
    78 g_cache_node_destroy (GCacheNode *node)
       
    79 {
       
    80   g_slice_free (GCacheNode, node);
       
    81 }
       
    82 
       
    83 EXPORT_C GCache*
       
    84 g_cache_new (GCacheNewFunc      value_new_func,
       
    85 	     GCacheDestroyFunc  value_destroy_func,
       
    86 	     GCacheDupFunc      key_dup_func,
       
    87 	     GCacheDestroyFunc  key_destroy_func,
       
    88 	     GHashFunc          hash_key_func,
       
    89 	     GHashFunc          hash_value_func,
       
    90 	     GEqualFunc         key_equal_func)
       
    91 {
       
    92   GCache *cache;
       
    93 
       
    94   g_return_val_if_fail (value_new_func != NULL, NULL);
       
    95   g_return_val_if_fail (value_destroy_func != NULL, NULL);
       
    96   g_return_val_if_fail (key_dup_func != NULL, NULL);
       
    97   g_return_val_if_fail (key_destroy_func != NULL, NULL);
       
    98   g_return_val_if_fail (hash_key_func != NULL, NULL);
       
    99   g_return_val_if_fail (hash_value_func != NULL, NULL);
       
   100   g_return_val_if_fail (key_equal_func != NULL, NULL);
       
   101 
       
   102   cache = g_slice_new (GCache);
       
   103   cache->value_new_func = value_new_func;
       
   104   cache->value_destroy_func = value_destroy_func;
       
   105   cache->key_dup_func = key_dup_func;
       
   106   cache->key_destroy_func = key_destroy_func;
       
   107   cache->key_table = g_hash_table_new (hash_key_func, key_equal_func);
       
   108   cache->value_table = g_hash_table_new (hash_value_func, NULL);
       
   109 
       
   110   return cache;
       
   111 }
       
   112 
       
   113 EXPORT_C void
       
   114 g_cache_destroy (GCache *cache)
       
   115 {
       
   116   g_return_if_fail (cache != NULL);
       
   117 
       
   118   g_hash_table_destroy (cache->key_table);
       
   119   g_hash_table_destroy (cache->value_table);
       
   120   g_slice_free (GCache, cache);
       
   121 }
       
   122 
       
   123 EXPORT_C gpointer
       
   124 g_cache_insert (GCache   *cache,
       
   125 		gpointer  key)
       
   126 {
       
   127   GCacheNode *node;
       
   128   gpointer value;
       
   129 
       
   130   g_return_val_if_fail (cache != NULL, NULL);
       
   131 
       
   132   node = g_hash_table_lookup (cache->key_table, key);
       
   133   if (node)
       
   134     {
       
   135       node->ref_count += 1;
       
   136       return node->value;
       
   137     }
       
   138 
       
   139   key = (* cache->key_dup_func) (key);
       
   140   value = (* cache->value_new_func) (key);
       
   141   node = g_cache_node_new (value);
       
   142 
       
   143   g_hash_table_insert (cache->key_table, key, node);
       
   144   g_hash_table_insert (cache->value_table, value, key);
       
   145 
       
   146   return node->value;
       
   147 }
       
   148 
       
   149 EXPORT_C void
       
   150 g_cache_remove (GCache        *cache,
       
   151 		gconstpointer  value)
       
   152 {
       
   153   GCacheNode *node;
       
   154   gpointer key;
       
   155 
       
   156   g_return_if_fail (cache != NULL);
       
   157 
       
   158   key = g_hash_table_lookup (cache->value_table, value);
       
   159   node = g_hash_table_lookup (cache->key_table, key);
       
   160 
       
   161   g_return_if_fail (node != NULL);
       
   162 
       
   163   node->ref_count -= 1;
       
   164   if (node->ref_count == 0)
       
   165     {
       
   166       g_hash_table_remove (cache->value_table, value);
       
   167       g_hash_table_remove (cache->key_table, key);
       
   168 
       
   169       (* cache->key_destroy_func) (key);
       
   170       (* cache->value_destroy_func) (node->value);
       
   171       g_cache_node_destroy (node);
       
   172     }
       
   173 }
       
   174 
       
   175 EXPORT_C void
       
   176 g_cache_key_foreach (GCache   *cache,
       
   177 		     GHFunc    func,
       
   178 		     gpointer  user_data)
       
   179 {
       
   180   g_return_if_fail (cache != NULL);
       
   181   g_return_if_fail (func != NULL);
       
   182 
       
   183   g_hash_table_foreach (cache->value_table, func, user_data);
       
   184 }
       
   185 
       
   186 EXPORT_C void
       
   187 g_cache_value_foreach (GCache   *cache,
       
   188 		       GHFunc    func,
       
   189 		       gpointer  user_data)
       
   190 {
       
   191   g_return_if_fail (cache != NULL);
       
   192   g_return_if_fail (func != NULL);
       
   193 
       
   194   g_hash_table_foreach (cache->key_table, func, user_data);
       
   195 }
       
   196 
       
   197 #define __G_CACHE_C__
       
   198 #include "galiasdef.c"