-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathstring-cache.c
153 lines (140 loc) · 4.22 KB
/
string-cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @file string-cache.c
* Provide a cache of strings to reduce memory allocations
*
* Copyright 2024, Allied Telesis Labs New Zealand, Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <malloc.h>
#include <assert.h>
#include "hashtree.h"
#include "string-cache.h"
/* Structure with a flexible array member for the string.
* When we allocate this it will be done with space for the string. */
typedef struct
{
uint64_t refcount;
char str[0];
} StringCacheEntry;
static GHashTable *string_cache = NULL;
static pthread_mutex_t string_cache_lock = PTHREAD_MUTEX_INITIALIZER;
/* Function to create a new cache entry. */
static StringCacheEntry *
string_cache_entry_new (const char *str)
{
/* Include space for null terminator. */
size_t len = strlen (str) + 1;
StringCacheEntry *entry = g_malloc (sizeof (StringCacheEntry) + len);
entry->refcount = 1;
/* Copy the string into the allocated memory (including null terminator). */
memcpy (entry->str, str, len);
return entry;
}
/* Function to free a cache entry. */
static void
string_cache_entry_free (StringCacheEntry *entry)
{
assert (entry->refcount == 0);
if (entry)
{
g_free (entry);
}
}
/* Retrieve a string from the cache or add it if it doesn't exist */
const char *
string_cache_get (const char *str)
{
if (!str)
{
return NULL;
}
pthread_mutex_lock (&string_cache_lock);
StringCacheEntry *entry = g_hash_table_lookup (string_cache, str);
if (entry)
{
entry->refcount++;
}
else
{
entry = string_cache_entry_new (str);
/* Use entry->str as the key to ensure hash/equal functions operate on the string */
g_hash_table_insert (string_cache, entry->str, entry);
}
pthread_mutex_unlock (&string_cache_lock);
return entry->str;
}
/* Decrease the reference count of a string and remove it if refcount reaches 0 */
void
string_cache_release (const char *str)
{
if (!str)
{
return;
}
pthread_mutex_lock (&string_cache_lock);
StringCacheEntry *entry = g_hash_table_lookup (string_cache, str);
if (entry)
{
entry->refcount--;
if (entry->refcount == 0)
{
g_hash_table_remove (string_cache, str);
}
}
pthread_mutex_unlock (&string_cache_lock);
}
/* Amount of actual memory used to store this string */
uint64_t
string_cache_memuse (const char *str, bool pss)
{
StringCacheEntry *entry;
uint64_t size = 0;
if (str && (entry = g_hash_table_lookup (string_cache, str)))
{
/* Ignore the string-cache hashtable structure but
include the pointers to key and value and the hash */
size = (sizeof (gpointer) + sizeof (gpointer) + sizeof (guint));
/* 8 bytes malloc header + actual malloced size */
size += 8 + malloc_usable_size (entry);
/* Proportional size is one references share of the total */
if (pss)
size /= entry->refcount;
}
return size;
}
void
string_cache_init ()
{
pthread_mutex_lock (&string_cache_lock);
if (!string_cache)
{
string_cache =
g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
(GDestroyNotify) string_cache_entry_free);
}
pthread_mutex_unlock (&string_cache_lock);
}
/* This function should only be called when *all* strings have been released. */
void
string_cache_deinit ()
{
pthread_mutex_lock (&string_cache_lock);
g_hash_table_destroy (string_cache);
string_cache = NULL;
pthread_mutex_unlock (&string_cache_lock);
}