Correct string cache code

string cache replace was incorrect and covered by gpointer casting.
This commit is contained in:
lmat 2017-12-12 09:34:44 -08:00
parent 805549ba24
commit a23438d5fb
4 changed files with 19 additions and 19 deletions

View File

@ -1108,7 +1108,7 @@ gnc_account_create_root (QofBook *book)
rpriv = GET_PRIVATE(root);
xaccAccountBeginEdit(root);
rpriv->type = ACCT_TYPE_ROOT;
qof_string_cache_replace((void const **)(&rpriv->accountName), "Root Account");
rpriv->accountName = qof_string_cache_replace(rpriv->accountName, "Root Account");
mark_account (root);
xaccAccountCommitEdit(root);
gnc_book_set_root_account(book, root);
@ -2245,7 +2245,7 @@ xaccAccountSetName (Account *acc, const char *str)
return;
xaccAccountBeginEdit(acc);
qof_string_cache_replace((void const **)(&priv->accountName), str);
priv->accountName = qof_string_cache_replace(priv->accountName, str);
mark_account (acc);
xaccAccountCommitEdit(acc);
}
@ -2264,7 +2264,7 @@ xaccAccountSetCode (Account *acc, const char *str)
return;
xaccAccountBeginEdit(acc);
qof_string_cache_replace((void const **)(&priv->accountCode), str ? str : "");
priv->accountCode = qof_string_cache_replace(priv->accountCode, str ? str : "");
mark_account (acc);
xaccAccountCommitEdit(acc);
}
@ -2283,7 +2283,7 @@ xaccAccountSetDescription (Account *acc, const char *str)
return;
xaccAccountBeginEdit(acc);
qof_string_cache_replace((void const **)(&priv->description), str ? str : "");
priv->description = qof_string_cache_replace(priv->description, str ? str : "");
mark_account (acc);
xaccAccountCommitEdit(acc);
}

View File

@ -82,7 +82,7 @@ qof_string_cache_destroy (void)
/* If the key exists in the cache, check the refcount. If 1, just
* remove the key. Otherwise, decrement the refcount */
void
qof_string_cache_remove(gconstpointer key)
qof_string_cache_remove(const char * key)
{
if (key)
{
@ -106,8 +106,8 @@ qof_string_cache_remove(gconstpointer key)
/* If the key exists in the cache, increment the refcount. Otherwise,
* add it with a refcount of 1. */
gpointer
qof_string_cache_insert(gconstpointer key)
char *
qof_string_cache_insert(const char * key)
{
if (key)
{
@ -118,7 +118,7 @@ qof_string_cache_insert(gconstpointer key)
{
guint* refcount = (guint*)value;
++(*refcount);
return cache_key;
return static_cast <char *> (cache_key);
}
else
{
@ -126,17 +126,17 @@ qof_string_cache_insert(gconstpointer key)
guint* refcount = static_cast<unsigned int*>(g_malloc(sizeof(guint)));
*refcount = 1;
g_hash_table_insert(cache, new_key, refcount);
return new_key;
return static_cast <char *> (new_key);
}
}
return NULL;
}
void
qof_string_cache_replace(gconstpointer * dst, gconstpointer src)
char *
qof_string_cache_replace(char const * dst, char const * src)
{
gpointer tmp {qof_string_cache_insert(src)};
qof_string_cache_remove(&dst);
*dst = tmp;
char * tmp {qof_string_cache_insert (src)};
qof_string_cache_remove (dst);
return tmp;
}
/* ************************ END OF FILE ***************************** */

View File

@ -79,18 +79,18 @@ void qof_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 qof_string_cache_remove(gconstpointer key);
void qof_string_cache_remove(const char * key);
/** You can use this function with g_hash_table_insert(), for the key
(or value), as long as you use the destroy notifier above.
*/
gpointer qof_string_cache_insert(gconstpointer key);
char * qof_string_cache_insert(const char * key);
/** Same as CACHE_REPLACE below, but safe to call from C++.
*/
void qof_string_cache_replace(gconstpointer * dst, gconstpointer src);
char * qof_string_cache_replace(const char * dst, const char * src);
#define CACHE_INSERT(str) qof_string_cache_insert((gconstpointer)(str))
#define CACHE_INSERT(str) qof_string_cache_insert((str))
#define CACHE_REMOVE(str) qof_string_cache_remove((str))
/* Replace cached string currently in 'dst' with string in 'src'.

View File

@ -563,7 +563,7 @@ qof_book_get_collection (const QofBook *book, QofIdType entity_type)
col = qof_collection_new (entity_type);
g_hash_table_insert(
book->hash_of_collections,
qof_string_cache_insert((gpointer) entity_type), col);
qof_string_cache_insert(entity_type), col);
}
return col;
}