LCOV - code coverage report
Current view: top level - libgnucash/engine - qof-string-cache.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 100.0 % 40 40
Test Date: 2026-11-02 18:19:43 Functions: 100.0 % 6 6
Legend: Lines:     hit not hit

            Line data    Source code
       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              : #include <config.h>
      31              : 
      32              : #include <ctype.h>
      33              : #include <stdlib.h>
      34              : #include <string.h>
      35              : #include "qof.h"
      36              : 
      37              : /* Uncomment if you need to log anything.
      38              : static QofLogModule log_module = QOF_MOD_UTIL;
      39              : */
      40              : /* =================================================================== */
      41              : /* The QOF string cache                                                */
      42              : /*                                                                     */
      43              : /* The cache is a GHashTable where a copy of the string is the key,    */
      44              : /* and a ref count is the value                                        */
      45              : /* =================================================================== */
      46              : 
      47              : static GHashTable* qof_string_cache = NULL;
      48              : 
      49              : static GHashTable*
      50       848882 : qof_get_string_cache(void)
      51              : {
      52       848882 :     if (!qof_string_cache)
      53              :     {
      54          118 :         qof_string_cache = g_hash_table_new_full(
      55              :                                g_str_hash,               /* hash_func          */
      56              :                                g_str_equal,              /* key_equal_func     */
      57              :                                g_free,                   /* key_destroy_func   */
      58              :                                g_free);                  /* value_destroy_func */
      59              :     }
      60       848882 :     return qof_string_cache;
      61              : }
      62              : 
      63              : void
      64          117 : qof_string_cache_init(void)
      65              : {
      66          117 :     (void)qof_get_string_cache();
      67          117 : }
      68              : 
      69              : void
      70           43 : qof_string_cache_destroy (void)
      71              : {
      72           43 :     if (qof_string_cache)
      73              :     {
      74           43 :         g_hash_table_destroy(qof_string_cache);
      75              :     }
      76           43 :     qof_string_cache = NULL;
      77           43 : }
      78              : 
      79              : /* If the key exists in the cache, check the refcount.  If 1, just
      80              :  * remove the key.  Otherwise, decrement the refcount */
      81              : void
      82       675332 : qof_string_cache_remove(const char * key)
      83              : {
      84       675332 :     if (key && key[0] != 0)
      85              :     {
      86       334413 :         GHashTable* cache = qof_get_string_cache();
      87              :         gpointer value;
      88              :         gpointer cache_key;
      89       334413 :         if (g_hash_table_lookup_extended(cache, key, &cache_key, &value))
      90              :         {
      91       334409 :             guint* refcount = (guint*)value;
      92       334409 :             if (*refcount == 1)
      93              :             {
      94       113687 :                 g_hash_table_remove(cache, key);
      95              :             }
      96              :             else
      97              :             {
      98       220722 :                 --(*refcount);
      99              :             }
     100              :         }
     101              :     }
     102       675332 : }
     103              : 
     104              : /* If the key exists in the cache, increment the refcount.  Otherwise,
     105              :  * add it with a refcount of 1. */
     106              : const char *
     107       878498 : qof_string_cache_insert(const char * key)
     108              : {
     109       878498 :     if (key)
     110              :     {
     111       877865 :         if (key[0] == 0)
     112              :         {
     113       363513 :             return "";
     114              :         }
     115              : 
     116       514352 :         GHashTable* cache = qof_get_string_cache();
     117              :         gpointer value;
     118              :         gpointer cache_key;
     119       514352 :         if (g_hash_table_lookup_extended(cache, key, &cache_key, &value))
     120              :         {
     121       368432 :             guint* refcount = (guint*)value;
     122       368432 :             ++(*refcount);
     123       368432 :             return static_cast <char *> (cache_key);
     124              :         }
     125              :         else
     126              :         {
     127       145920 :             gpointer new_key = g_strdup(static_cast<const char*>(key));
     128       145920 :             guint* refcount = static_cast<unsigned int*>(g_malloc(sizeof(guint)));
     129       145920 :             *refcount = 1;
     130       145920 :             g_hash_table_insert(cache, new_key, refcount);
     131       145920 :             return static_cast <char *> (new_key);
     132              :         }
     133              :     }
     134          633 :     return NULL;
     135              : }
     136              : 
     137              : const char *
     138         8375 : qof_string_cache_replace(char const * dst, char const * src)
     139              : {
     140         8375 :     const char * tmp {qof_string_cache_insert (src)};
     141         8375 :     qof_string_cache_remove (dst);
     142         8375 :     return tmp;
     143              : }
     144              : /* ************************ END OF FILE ***************************** */
        

Generated by: LCOV version 2.0-1