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: 2025-02-07 16:25:45 Functions: 100.0 % 6 6
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     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                 :      767567 : qof_get_string_cache(void)
      51                 :             : {
      52                 :      767567 :     if (!qof_string_cache)
      53                 :             :     {
      54                 :         115 :         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                 :      767567 :     return qof_string_cache;
      61                 :             : }
      62                 :             : 
      63                 :             : void
      64                 :         115 : qof_string_cache_init(void)
      65                 :             : {
      66                 :         115 :     (void)qof_get_string_cache();
      67                 :         115 : }
      68                 :             : 
      69                 :             : void
      70                 :          42 : qof_string_cache_destroy (void)
      71                 :             : {
      72                 :          42 :     if (qof_string_cache)
      73                 :             :     {
      74                 :          42 :         g_hash_table_destroy(qof_string_cache);
      75                 :             :     }
      76                 :          42 :     qof_string_cache = NULL;
      77                 :          42 : }
      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                 :      626920 : qof_string_cache_remove(const char * key)
      83                 :             : {
      84                 :      626920 :     if (key && key[0] != 0)
      85                 :             :     {
      86                 :      307691 :         GHashTable* cache = qof_get_string_cache();
      87                 :             :         gpointer value;
      88                 :             :         gpointer cache_key;
      89                 :      307691 :         if (g_hash_table_lookup_extended(cache, key, &cache_key, &value))
      90                 :             :         {
      91                 :      307687 :             guint* refcount = (guint*)value;
      92                 :      307687 :             if (*refcount == 1)
      93                 :             :             {
      94                 :      103000 :                 g_hash_table_remove(cache, key);
      95                 :             :             }
      96                 :             :             else
      97                 :             :             {
      98                 :      204687 :                 --(*refcount);
      99                 :             :             }
     100                 :             :         }
     101                 :             :     }
     102                 :      626920 : }
     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                 :      796831 : qof_string_cache_insert(const char * key)
     108                 :             : {
     109                 :      796831 :     if (key)
     110                 :             :     {
     111                 :      789272 :         if (key[0] == 0)
     112                 :             :         {
     113                 :      329511 :             return "";
     114                 :             :         }
     115                 :             : 
     116                 :      459761 :         GHashTable* cache = qof_get_string_cache();
     117                 :             :         gpointer value;
     118                 :             :         gpointer cache_key;
     119                 :      459761 :         if (g_hash_table_lookup_extended(cache, key, &cache_key, &value))
     120                 :             :         {
     121                 :      324687 :             guint* refcount = (guint*)value;
     122                 :      324687 :             ++(*refcount);
     123                 :      324687 :             return static_cast <char *> (cache_key);
     124                 :             :         }
     125                 :             :         else
     126                 :             :         {
     127                 :      135074 :             gpointer new_key = g_strdup(static_cast<const char*>(key));
     128                 :      135074 :             guint* refcount = static_cast<unsigned int*>(g_malloc(sizeof(guint)));
     129                 :      135074 :             *refcount = 1;
     130                 :      135074 :             g_hash_table_insert(cache, new_key, refcount);
     131                 :      135074 :             return static_cast <char *> (new_key);
     132                 :             :         }
     133                 :             :     }
     134                 :        7559 :     return NULL;
     135                 :             : }
     136                 :             : 
     137                 :             : const char *
     138                 :        8131 : qof_string_cache_replace(char const * dst, char const * src)
     139                 :             : {
     140                 :        8131 :     const char * tmp {qof_string_cache_insert (src)};
     141                 :        8131 :     qof_string_cache_remove (dst);
     142                 :        8131 :     return tmp;
     143                 :             : }
     144                 :             : /* ************************ END OF FILE ***************************** */
        

Generated by: LCOV version 2.0-1