[Gnucash-changes] Patch from Chris Shoemaker to privatize the
gnucash string cache code
David Hampton
hampton at cvs.gnucash.org
Fri Oct 14 08:54:21 EDT 2005
Log Message:
-----------
Patch from Chris Shoemaker to privatize the gnucash string cache code
and force all usage through gnc_string_cache_{remove,insert}()
functions. This makes debugging various uses (and misuses) of the
cache easier.
Tags:
----
gnucash-gnome2-dev
Modified Files:
--------------
gnucash/src/engine:
gnc-engine-util.c
gnc-engine-util.h
Revision Data
-------------
Index: gnc-engine-util.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/gnc-engine-util.h,v
retrieving revision 1.32.4.10
retrieving revision 1.32.4.11
diff -Lsrc/engine/gnc-engine-util.h -Lsrc/engine/gnc-engine-util.h -u -r1.32.4.10 -r1.32.4.11
--- src/engine/gnc-engine-util.h
+++ src/engine/gnc-engine-util.h
@@ -211,38 +211,48 @@
* return that number. (Leading whitespace is ignored). */
int qof_util_bool_to_int (const char * val);
-/** Many strings used throughout the engine are likely to be duplicated.
+
+/** Gnucash's String Cache:
+ *
+ * Many strings used throughout the engine are likely to be duplicated.
* So we provide a reference counted cache system for the strings, which
* shares strings whenever possible.
*
- * Use g_cache_insert to insert a string into the cache (it will return a
- * pointer to the cached string).
- * Basically you should use this instead of g_strdup.
- *
- * Use g_cache_remove (giving it a pointer to a cached string) if the string
- * is unused. If this is the last reference to the string it will be
- * removed from the cache, otherwise it will just decrement the
- * reference count.
- * Basically you should use this instead of g_free.
+ * Use gnc_string_cache_insert to insert a string into the cache (it
+ * will return a pointer to the cached string). Basically you should
+ * use this instead of g_strdup.
+ *
+ * Use gnc_string_cache_remove (giving it a pointer to a cached
+ * string) if the string is unused. If this is the last reference to
+ * the string it will be removed from the cache, otherwise it will
+ * just decrement the reference count. Basically you should use this
+ * instead of g_free.
+ *
+ * Just in case it's not clear: The remove function must NOT be called
+ * for the string you passed INTO the insert function. It must be
+ * called for the _cached_ string that is _returned_ by the insert
+ * function.
*
* Note that all the work is done when inserting or removing. Once
* cached the strings are just plain C strings.
- */
-
-/** Get the gnc_string_cache. Create it if it doesn't exist already
-
-\todo hide the gcache as a static */
-GCache* gnc_engine_get_string_cache(void);
+ *
+ * The string cache is demand-created on first use.
+ *
+ **/
+/** Destroy the gnc_string_cache */
void gnc_engine_string_cache_destroy (void);
/* You can use this function as a destroy notifier for a GHashTable
that uses common strings as keys (or values, for that matter.) */
-void gnc_string_cache_remove(gpointer key);
+void gnc_string_cache_remove(gconstpointer key);
/* You can use this function with g_hash_table_insert(), or the key
(or value), as long as you use the destroy notifier above. */
gpointer gnc_string_cache_insert(gpointer key);
+#define CACHE_INSERT(str) gnc_string_cache_insert((gpointer)(str));
+#define CACHE_REMOVE(str) gnc_string_cache_remove((str));
+
#endif /* QOF_UTIL_H */
/** @} */
Index: gnc-engine-util.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/gnc-engine-util.c,v
retrieving revision 1.29.4.6
retrieving revision 1.29.4.7
diff -Lsrc/engine/gnc-engine-util.c -Lsrc/engine/gnc-engine-util.c -u -r1.29.4.6 -r1.29.4.7
--- src/engine/gnc-engine-util.c
+++ src/engine/gnc-engine-util.c
@@ -221,16 +221,43 @@
static GCache * gnc_string_cache = NULL;
-/* maybe better to make this function static */
-GCache*
+#ifdef THESE_CAN_BE_USEFUL_FOR_DEGUGGING
+static guint g_str_hash_KEY(gconstpointer v) {
+ return g_str_hash(v);
+}
+static guint g_str_hash_VAL(gconstpointer v) {
+ return g_str_hash(v);
+}
+static gpointer g_strdup_VAL(gpointer v) {
+ return g_strdup(v);
+}
+static gpointer g_strdup_KEY(gpointer v) {
+ return g_strdup(v);
+}
+static void g_free_VAL(gpointer v) {
+ return g_free(v);
+}
+static void g_free_KEY(gpointer v) {
+ return g_free(v);
+}
+static gboolean gnc_str_equal(gconstpointer v, gconstpointer v2)
+{
+ return (v && v2) ? g_str_equal(v, v2) : FALSE;
+}
+#endif
+
+static GCache*
gnc_engine_get_string_cache(void)
{
- if(!gnc_string_cache)
- {
+ if(!gnc_string_cache) {
gnc_string_cache = g_cache_new(
- (GCacheNewFunc) g_strdup, g_free,
- (GCacheDupFunc) g_strdup, g_free, g_str_hash,
- g_str_hash, g_str_equal);
+ (GCacheNewFunc) g_strdup, /* value_new_func */
+ g_free, /* value_destroy_func */
+ (GCacheDupFunc) g_strdup, /* key_dup_func */
+ g_free, /* key_destroy_func */
+ g_str_hash, /* hash_key_func */
+ g_str_hash, /* hash_value_func */
+ g_str_equal); /* key_equal_func */
}
return gnc_string_cache;
}
@@ -238,18 +265,22 @@
void
gnc_engine_string_cache_destroy (void)
{
- g_cache_destroy (gnc_string_cache);
- gnc_string_cache = NULL;
+ if (gnc_string_cache)
+ g_cache_destroy (gnc_string_cache);
+ gnc_string_cache = NULL;
}
-void gnc_string_cache_remove(gpointer key)
+void
+gnc_string_cache_remove(gconstpointer key)
{
- g_cache_remove(gnc_engine_get_string_cache(), key);
+ g_cache_remove(gnc_engine_get_string_cache(), key);
}
-gpointer gnc_string_cache_insert(gpointer key)
+
+gpointer
+gnc_string_cache_insert(gpointer key)
{
- return g_cache_insert(gnc_engine_get_string_cache(), key);
+ return g_cache_insert(gnc_engine_get_string_cache(), key);
}
void
More information about the gnucash-changes
mailing list