GnuCash  4.8a-132-gcdaeb421d+
qof-string-cache.cpp
1 /********************************************************************\
2  * qof-string-cache.c -- QOF string cache functions *
3  * Copyright (C) 1997 Robin D. Clark *
4  * Copyright (C) 1997-2001,2004 Linas Vepstas <linas@linas.org> *
5  * Copyright 2006 Neil Williams <linux@codehelp.co.uk> *
6  * *
7  * This program is free software; you can redistribute it and/or *
8  * modify it under the terms of the GNU General Public License as *
9  * published by the Free Software Foundation; either version 2 of *
10  * the License, or (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License*
18  * along with this program; if not, contact: *
19  * *
20  * Free Software Foundation Voice: +1-617-542-5942 *
21  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
22  * Boston, MA 02110-1301, USA gnu@gnu.org *
23  * *
24  * Author: Rob Clark (rclark@cs.hmc.edu) *
25  * Author: Linas Vepstas (linas@linas.org) *
26  * Author: Phil Longstaff (phil.longstaff@yahoo.ca) *
27 \********************************************************************/
28 #include <glib.h>
29 
30 extern "C"
31 {
32 #include <config.h>
33 
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include "qof.h"
38 }
39 
40 /* Uncomment if you need to log anything.
41 static QofLogModule log_module = QOF_MOD_UTIL;
42 */
43 /* =================================================================== */
44 /* The QOF string cache */
45 /* */
46 /* The cache is a GHashTable where a copy of the string is the key, */
47 /* and a ref count is the value */
48 /* =================================================================== */
49 
50 static GHashTable* qof_string_cache = NULL;
51 
52 static GHashTable*
53 qof_get_string_cache(void)
54 {
55  if (!qof_string_cache)
56  {
57  qof_string_cache = g_hash_table_new_full(
58  g_str_hash, /* hash_func */
59  g_str_equal, /* key_equal_func */
60  g_free, /* key_destroy_func */
61  g_free); /* value_destroy_func */
62  }
63  return qof_string_cache;
64 }
65 
66 void
68 {
69  (void)qof_get_string_cache();
70 }
71 
72 void
74 {
75  if (qof_string_cache)
76  {
77  g_hash_table_destroy(qof_string_cache);
78  }
79  qof_string_cache = NULL;
80 }
81 
82 /* If the key exists in the cache, check the refcount. If 1, just
83  * remove the key. Otherwise, decrement the refcount */
84 void
85 qof_string_cache_remove(const char * key)
86 {
87  if (key && key[0] != 0)
88  {
89  GHashTable* cache = qof_get_string_cache();
90  gpointer value;
91  gpointer cache_key;
92  if (g_hash_table_lookup_extended(cache, key, &cache_key, &value))
93  {
94  guint* refcount = (guint*)value;
95  if (*refcount == 1)
96  {
97  g_hash_table_remove(cache, key);
98  }
99  else
100  {
101  --(*refcount);
102  }
103  }
104  }
105 }
106 
107 /* If the key exists in the cache, increment the refcount. Otherwise,
108  * add it with a refcount of 1. */
109 const char *
110 qof_string_cache_insert(const char * key)
111 {
112  if (key)
113  {
114  if (key[0] == 0)
115  {
116  return "";
117  }
118 
119  GHashTable* cache = qof_get_string_cache();
120  gpointer value;
121  gpointer cache_key;
122  if (g_hash_table_lookup_extended(cache, key, &cache_key, &value))
123  {
124  guint* refcount = (guint*)value;
125  ++(*refcount);
126  return static_cast <char *> (cache_key);
127  }
128  else
129  {
130  gpointer new_key = g_strdup(static_cast<const char*>(key));
131  guint* refcount = static_cast<unsigned int*>(g_malloc(sizeof(guint)));
132  *refcount = 1;
133  g_hash_table_insert(cache, new_key, refcount);
134  return static_cast <char *> (new_key);
135  }
136  }
137  return NULL;
138 }
139 
140 const char *
141 qof_string_cache_replace(char const * dst, char const * src)
142 {
143  const char * tmp {qof_string_cache_insert (src)};
145  return tmp;
146 }
147 /* ************************ END OF FILE ***************************** */
const char * qof_string_cache_replace(char const *dst, char const *src)
Same as CACHE_REPLACE below, but safe to call from C++.
void qof_string_cache_destroy(void)
Destroy the qof_string_cache.
const char * qof_string_cache_insert(const char *key)
You can use this function with g_hash_table_insert(), for the key (or value), as long as you use the ...
void qof_string_cache_remove(const char *key)
You can use this function as a destroy notifier for a GHashTable that uses common strings as keys (or...
void qof_string_cache_init(void)
The QOF String Cache: