mirror of
https://github.com/irssi/irssi.git
synced 2026-08-10 04:13:32 +02:00
implement reference counted strings
- on glib >=2.58, use the implementation provided by glib - otherwise, a hash table will contain the strings
This commit is contained in:
parent
2f82d9cd33
commit
ad7ad063ca
6 changed files with 133 additions and 0 deletions
94
src/core/refstrings.c
Normal file
94
src/core/refstrings.c
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
#include <string.h>
|
||||
|
||||
#include <irssi/src/core/refstrings.h>
|
||||
|
||||
#if GLIB_CHECK_VERSION(2, 58, 0)
|
||||
/* nothing */
|
||||
#else
|
||||
|
||||
GHashTable *i_refstr_table;
|
||||
|
||||
void i_refstr_init(void)
|
||||
{
|
||||
i_refstr_table = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
}
|
||||
|
||||
char *i_refstr_intern(const char *str)
|
||||
{
|
||||
char *ret;
|
||||
gpointer rc_p, ret_p;
|
||||
size_t rc;
|
||||
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
|
||||
if (g_hash_table_lookup_extended(i_refstr_table, str, &ret_p, &rc_p)) {
|
||||
rc = GPOINTER_TO_SIZE(rc_p);
|
||||
ret = ret_p;
|
||||
} else {
|
||||
rc = 0;
|
||||
ret = g_strdup(str);
|
||||
}
|
||||
|
||||
if (rc + 1 <= G_MAXSIZE) {
|
||||
g_hash_table_insert(i_refstr_table, ret, GSIZE_TO_POINTER(rc + 1));
|
||||
return ret;
|
||||
} else {
|
||||
return g_strdup(str);
|
||||
}
|
||||
}
|
||||
|
||||
void i_refstr_release(char *str)
|
||||
{
|
||||
char *ret;
|
||||
gpointer rc_p, ret_p;
|
||||
size_t rc;
|
||||
|
||||
if (str == NULL)
|
||||
return;
|
||||
|
||||
if (g_hash_table_lookup_extended(i_refstr_table, str, &ret_p, &rc_p)) {
|
||||
rc = GPOINTER_TO_SIZE(rc_p);
|
||||
ret = ret_p;
|
||||
} else {
|
||||
rc = 0;
|
||||
ret = NULL;
|
||||
}
|
||||
|
||||
if (ret == str) {
|
||||
if (rc > 1) {
|
||||
g_hash_table_insert(i_refstr_table, ret, GSIZE_TO_POINTER(rc - 1));
|
||||
} else {
|
||||
g_hash_table_remove(i_refstr_table, ret);
|
||||
g_free(ret);
|
||||
}
|
||||
} else {
|
||||
g_free(str);
|
||||
}
|
||||
}
|
||||
|
||||
void i_refstr_deinit(void)
|
||||
{
|
||||
g_hash_table_foreach(i_refstr_table, (GHFunc) g_free, NULL);
|
||||
g_hash_table_destroy(i_refstr_table);
|
||||
}
|
||||
|
||||
char *i_refstr_table_size_info(void)
|
||||
{
|
||||
GHashTableIter iter;
|
||||
void *k_p, *v_p;
|
||||
size_t count, mem;
|
||||
count = 0;
|
||||
mem = 0;
|
||||
g_hash_table_iter_init(&iter, i_refstr_table);
|
||||
while (g_hash_table_iter_next(&iter, &k_p, &v_p)) {
|
||||
char *key = k_p;
|
||||
count++;
|
||||
mem += sizeof(char) * (strlen(key) + 1) + 2 * sizeof(void *);
|
||||
}
|
||||
|
||||
return g_strdup_printf("Shared strings: %ld, %dkB of data", count,
|
||||
(int) (mem / 1024));
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue