Forgot to adjust default window size
[claws.git] / src / common / stringtable.c
blobe9451817533a7e47fa9845cece2e9427447ebfaa
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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 3 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, see <http://www.gnu.org/licenses/>.
20 #include <glib.h>
21 #include <string.h>
23 #include "stringtable.h"
24 #include "utils.h"
26 /* alfons - hashed string table (I wasn't content with GStringChunk;
27 * can't recall why :-) */
29 #if 0
30 #define XXX_DEBUG \
31 debug_print
32 #else
33 #define XXX_DEBUG \
34 if (0) debug_print
35 #endif
37 typedef struct StringEntry_ {
38 gint ref_count;
39 gchar *string;
40 } StringEntry;
42 static StringEntry *string_entry_new(const gchar *str)
44 StringEntry *entry;
46 entry = g_new0(StringEntry, 1);
47 entry->ref_count = 1;
48 entry->string = g_strdup(str);
49 return entry;
52 static void string_entry_free(StringEntry *entry)
54 cm_return_if_fail(entry != NULL);
56 g_free(entry->string);
57 g_free(entry);
60 StringTable *string_table_new(void)
62 StringTable *strtable;
64 strtable = g_new0(StringTable, 1);
65 cm_return_val_if_fail(strtable != NULL, NULL);
66 strtable->hash_table = g_hash_table_new(g_str_hash, g_str_equal);
67 cm_return_val_if_fail(strtable->hash_table, NULL);
68 return strtable;
71 gchar *string_table_insert_string(StringTable *table, const gchar *str)
73 StringEntry *entry;
75 entry = g_hash_table_lookup(table->hash_table, str);
77 if (entry) {
78 entry->ref_count++;
79 XXX_DEBUG ("ref++ for %s (%d)\n", entry->string,
80 entry->ref_count);
81 } else {
82 entry = string_entry_new(str);
83 XXX_DEBUG ("inserting %s\n", str);
84 /* insert entry->string instead of str, since it can be
85 * invalid pointer after this. */
86 g_hash_table_insert(table->hash_table, entry->string, entry);
89 return entry->string;
92 void string_table_free_string(StringTable *table, const gchar *str)
94 StringEntry *entry;
96 entry = g_hash_table_lookup(table->hash_table, str);
98 if (entry) {
99 entry->ref_count--;
100 if (entry->ref_count <= 0) {
101 XXX_DEBUG ("refcount of string %s dropped to zero\n",
102 entry->string);
103 g_hash_table_remove(table->hash_table, str);
104 string_entry_free(entry);
105 } else {
106 XXX_DEBUG ("ref-- for %s (%d)\n", entry->string,
107 entry->ref_count);
112 static gboolean string_table_remove_for_each_fn(gchar *key, StringEntry *entry,
113 gpointer user_data)
115 cm_return_val_if_fail(key != NULL, TRUE);
116 cm_return_val_if_fail(entry != NULL, TRUE);
118 string_entry_free(entry);
120 return TRUE;
123 void string_table_free(StringTable *table)
125 cm_return_if_fail(table != NULL);
126 cm_return_if_fail(table->hash_table != NULL);
128 g_hash_table_foreach_remove(table->hash_table,
129 (GHRFunc)string_table_remove_for_each_fn,
130 NULL);
131 g_hash_table_destroy(table->hash_table);
132 g_free(table);
135 static void string_table_stats_for_each_fn(gchar *key, StringEntry *entry,
136 guint *totals)
138 if (entry->ref_count > 1) {
139 *totals += strlen(key) * (entry->ref_count - 1);
143 void string_table_get_stats(StringTable *table)
145 guint totals = 0;
147 g_hash_table_foreach(table->hash_table,
148 (GHFunc)string_table_stats_for_each_fn, &totals);
149 XXX_DEBUG ("TOTAL UNSPILLED %d (%dK)\n", totals, totals / 1024);