Statistics
| Revision:

root / libsylph / stringtable.c @ 2071

History | View | Annotate | Download (3.9 kB)

1 528 hiro
/*
2 578 hiro
 * LibSylph -- E-Mail client library
3 578 hiro
 * Copyright (C) 1999-2005 Hiroyuki Yamamoto
4 528 hiro
 *
5 578 hiro
 * This library is free software; you can redistribute it and/or
6 578 hiro
 * modify it under the terms of the GNU Lesser General Public
7 578 hiro
 * License as published by the Free Software Foundation; either
8 578 hiro
 * version 2.1 of the License, or (at your option) any later version.
9 528 hiro
 *
10 578 hiro
 * This library is distributed in the hope that it will be useful,
11 528 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 578 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 578 hiro
 * Lesser General Public License for more details.
14 528 hiro
 *
15 578 hiro
 * You should have received a copy of the GNU Lesser General Public
16 578 hiro
 * License along with this library; if not, write to the Free Software
17 578 hiro
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 528 hiro
 */
19 528 hiro
20 528 hiro
#include <glib.h>
21 528 hiro
#include <string.h>
22 528 hiro
23 528 hiro
#include "stringtable.h"
24 528 hiro
#include "utils.h"
25 528 hiro
26 528 hiro
/* alfons - hashed string table (I wasn't content with GStringChunk;
27 528 hiro
 * can't recall why :-) */
28 528 hiro
29 528 hiro
#if 0
30 528 hiro
#define XXX_DEBUG \
31 528 hiro
        debug_print
32 528 hiro
#else
33 528 hiro
#define XXX_DEBUG \
34 528 hiro
        if (0) debug_print
35 528 hiro
#endif
36 528 hiro
37 528 hiro
typedef struct StringEntry_ {
38 528 hiro
        gint        ref_count;
39 528 hiro
        gchar  *string;
40 528 hiro
} StringEntry;
41 528 hiro
42 528 hiro
static StringEntry *string_entry_new(const gchar *str)
43 528 hiro
{
44 528 hiro
        StringEntry *entry;
45 528 hiro
46 528 hiro
        entry = g_new0(StringEntry, 1);
47 528 hiro
        entry->ref_count = 1;
48 528 hiro
        entry->string = g_strdup(str);
49 528 hiro
        return entry;
50 528 hiro
}
51 528 hiro
52 528 hiro
static void string_entry_free(StringEntry *entry)
53 528 hiro
{
54 528 hiro
        g_return_if_fail(entry != NULL);
55 528 hiro
56 528 hiro
        g_free(entry->string);
57 528 hiro
        g_free(entry);
58 528 hiro
}
59 528 hiro
60 528 hiro
StringTable *string_table_new(void)
61 528 hiro
{
62 528 hiro
        StringTable *strtable;
63 528 hiro
64 528 hiro
        strtable = g_new0(StringTable, 1);
65 528 hiro
        g_return_val_if_fail(strtable != NULL, NULL);
66 528 hiro
        strtable->hash_table = g_hash_table_new(g_str_hash, g_str_equal);
67 528 hiro
        g_return_val_if_fail(strtable->hash_table, NULL);
68 528 hiro
        return strtable;
69 528 hiro
}
70 528 hiro
71 528 hiro
gchar *string_table_lookup_string(StringTable *table, const gchar *str)
72 528 hiro
{
73 528 hiro
        StringEntry *entry;
74 528 hiro
75 528 hiro
        entry = g_hash_table_lookup(table->hash_table, str);
76 528 hiro
77 528 hiro
        if (entry) {
78 528 hiro
                return entry->string;
79 528 hiro
        } else {
80 528 hiro
                return NULL;
81 528 hiro
        }
82 528 hiro
}
83 528 hiro
84 528 hiro
gchar *string_table_insert_string(StringTable *table, const gchar *str)
85 528 hiro
{
86 528 hiro
        StringEntry *entry;
87 528 hiro
88 528 hiro
        entry = g_hash_table_lookup(table->hash_table, str);
89 528 hiro
90 528 hiro
        if (entry) {
91 528 hiro
                entry->ref_count++;
92 528 hiro
                XXX_DEBUG ("ref++ for %s (%d)\n", entry->string,
93 528 hiro
                           entry->ref_count);
94 528 hiro
        } else {
95 528 hiro
                entry = string_entry_new(str);
96 528 hiro
                XXX_DEBUG ("inserting %s\n", str);
97 528 hiro
                /* insert entry->string instead of str, since it can be
98 528 hiro
                 * invalid pointer after this. */
99 528 hiro
                g_hash_table_insert(table->hash_table, entry->string, entry);
100 528 hiro
        }
101 528 hiro
102 528 hiro
        return entry->string;
103 528 hiro
}
104 528 hiro
105 528 hiro
void string_table_free_string(StringTable *table, const gchar *str)
106 528 hiro
{
107 528 hiro
        StringEntry *entry;
108 528 hiro
109 528 hiro
        entry = g_hash_table_lookup(table->hash_table, str);
110 528 hiro
111 528 hiro
        if (entry) {
112 528 hiro
                entry->ref_count--;
113 528 hiro
                if (entry->ref_count <= 0) {
114 528 hiro
                        XXX_DEBUG ("refcount of string %s dropped to zero\n",
115 528 hiro
                                   entry->string);
116 528 hiro
                        g_hash_table_remove(table->hash_table, str);
117 528 hiro
                        string_entry_free(entry);
118 528 hiro
                } else {
119 528 hiro
                        XXX_DEBUG ("ref-- for %s (%d)\n", entry->string,
120 528 hiro
                                   entry->ref_count);
121 528 hiro
                }
122 528 hiro
        }
123 528 hiro
}
124 528 hiro
125 528 hiro
static gboolean string_table_remove_for_each_fn(gchar *key, StringEntry *entry,
126 528 hiro
                                                gpointer user_data)
127 528 hiro
{
128 528 hiro
        g_return_val_if_fail(key != NULL, TRUE);
129 528 hiro
        g_return_val_if_fail(entry != NULL, TRUE);
130 528 hiro
131 528 hiro
        string_entry_free(entry);
132 528 hiro
133 528 hiro
        return TRUE;
134 528 hiro
}
135 528 hiro
136 528 hiro
void string_table_free(StringTable *table)
137 528 hiro
{
138 528 hiro
        g_return_if_fail(table != NULL);
139 528 hiro
        g_return_if_fail(table->hash_table != NULL);
140 528 hiro
141 528 hiro
        g_hash_table_foreach_remove(table->hash_table,
142 528 hiro
                                    (GHRFunc)string_table_remove_for_each_fn,
143 528 hiro
                                    NULL);
144 528 hiro
        g_hash_table_destroy(table->hash_table);
145 528 hiro
        g_free(table);
146 528 hiro
}
147 528 hiro
148 528 hiro
static void string_table_stats_for_each_fn(gchar *key, StringEntry *entry,
149 528 hiro
                                           guint *totals)
150 528 hiro
{
151 528 hiro
        if (entry->ref_count > 1) {
152 528 hiro
                *totals += strlen(key) * (entry->ref_count - 1);
153 528 hiro
        }
154 528 hiro
}
155 528 hiro
156 528 hiro
void string_table_get_stats(StringTable *table)
157 528 hiro
{
158 528 hiro
        guint totals = 0;
159 528 hiro
160 528 hiro
        g_hash_table_foreach(table->hash_table,
161 528 hiro
                             (GHFunc)string_table_stats_for_each_fn, &totals);
162 528 hiro
        XXX_DEBUG ("TOTAL UNSPILLED %d (%dK)\n", totals, totals / 1024);
163 528 hiro
}