2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "stringtable.h"
26 /* alfons - hashed string table (I wasn't content with GStringChunk;
27 * can't recall why :-) */
37 typedef struct StringEntry_ {
42 static StringEntry *string_entry_new(const gchar *str)
46 entry = g_new0(StringEntry, 1);
48 entry->string = g_strdup(str);
52 static void string_entry_free(StringEntry *entry)
54 g_return_if_fail(entry != NULL);
56 g_free(entry->string);
60 StringTable *string_table_new(void)
62 StringTable *strtable;
64 strtable = g_new0(StringTable, 1);
65 g_return_val_if_fail(strtable != NULL, NULL);
66 strtable->hash_table = g_hash_table_new(g_str_hash, g_str_equal);
67 g_return_val_if_fail(strtable->hash_table, NULL);
71 gchar *string_table_lookup_string(StringTable *table, const gchar *str)
75 entry = g_hash_table_lookup(table->hash_table, str);
84 gchar *string_table_insert_string(StringTable *table, const gchar *str)
88 entry = g_hash_table_lookup(table->hash_table, str);
92 XXX_DEBUG ("ref++ for %s (%d)\n", entry->string,
95 entry = string_entry_new(str);
96 XXX_DEBUG ("inserting %s\n", str);
97 /* insert entry->string instead of str, since it can be
98 * invalid pointer after this. */
99 g_hash_table_insert(table->hash_table, entry->string, entry);
102 return entry->string;
105 void string_table_free_string(StringTable *table, const gchar *str)
109 entry = g_hash_table_lookup(table->hash_table, str);
113 if (entry->ref_count <= 0) {
114 XXX_DEBUG ("refcount of string %s dropped to zero\n",
116 g_hash_table_remove(table->hash_table, str);
117 string_entry_free(entry);
119 XXX_DEBUG ("ref-- for %s (%d)\n", entry->string,
125 static gboolean string_table_remove_for_each_fn(gchar *key, StringEntry *entry,
128 g_return_val_if_fail(key != NULL, TRUE);
129 g_return_val_if_fail(entry != NULL, TRUE);
131 string_entry_free(entry);
136 void string_table_free(StringTable *table)
138 g_return_if_fail(table != NULL);
139 g_return_if_fail(table->hash_table != NULL);
141 g_hash_table_foreach_remove(table->hash_table,
142 (GHRFunc)string_table_remove_for_each_fn,
144 g_hash_table_destroy(table->hash_table);
148 static void string_table_stats_for_each_fn(gchar *key, StringEntry *entry,
151 if (entry->ref_count > 1) {
152 *totals += strlen(key) * (entry->ref_count - 1);
156 void string_table_get_stats(StringTable *table)
160 g_hash_table_foreach(table->hash_table,
161 (GHFunc)string_table_stats_for_each_fn, &totals);
162 XXX_DEBUG ("TOTAL UNSPILLED %d (%dK)\n", totals, totals / 1024);