forked from irssi/irssi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefstrings.c
More file actions
129 lines (105 loc) · 2.07 KB
/
refstrings.c
File metadata and controls
129 lines (105 loc) · 2.07 KB
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
#include <glib.h>
#include <string.h>
#include <irssi/src/core/refstrings.h>
#if GLIB_CHECK_VERSION(2, 58, 0)
void i_refstr_init(void)
{
/* nothing */
}
char *i_refstr_intern(const char *str)
{
if (str == NULL) {
return NULL;
}
return g_ref_string_new_intern(str);
}
void i_refstr_release(char *str)
{
if (str == NULL) {
return;
}
g_ref_string_release(str);
}
void i_refstr_deinit(void)
{
/* nothing */
}
char *i_refstr_table_size_info(void)
{
/* not available */
return NULL;
}
#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