2 * Claws Mail -- 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/>.
25 #include "stringtable.h"
28 /* alfons - hashed string table (I wasn't content with GStringChunk;
29 * can't recall why :-) */
39 typedef struct StringEntry_
{
44 static StringEntry
*string_entry_new(const gchar
*str
)
48 entry
= g_new0(StringEntry
, 1);
50 entry
->string
= g_strdup(str
);
54 static void string_entry_free(StringEntry
*entry
)
56 cm_return_if_fail(entry
!= NULL
);
58 g_free(entry
->string
);
62 StringTable
*string_table_new(void)
64 StringTable
*strtable
;
66 strtable
= g_new0(StringTable
, 1);
67 cm_return_val_if_fail(strtable
!= NULL
, NULL
);
68 strtable
->hash_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
69 if (strtable
->hash_table
== NULL
) {
76 gchar
*string_table_insert_string(StringTable
*table
, const gchar
*str
)
80 entry
= g_hash_table_lookup(table
->hash_table
, str
);
84 XXX_DEBUG ("ref++ for %s (%d)\n", entry
->string
,
87 entry
= string_entry_new(str
);
88 XXX_DEBUG ("inserting %s\n", str
);
89 /* insert entry->string instead of str, since it can be
90 * invalid pointer after this. */
91 g_hash_table_insert(table
->hash_table
, entry
->string
, entry
);
97 void string_table_free_string(StringTable
*table
, const gchar
*str
)
101 entry
= g_hash_table_lookup(table
->hash_table
, str
);
105 if (entry
->ref_count
<= 0) {
106 XXX_DEBUG ("refcount of string %s dropped to zero\n",
108 g_hash_table_remove(table
->hash_table
, str
);
109 string_entry_free(entry
);
111 XXX_DEBUG ("ref-- for %s (%d)\n", entry
->string
,
117 static gboolean
string_table_remove_for_each_fn(gchar
*key
, StringEntry
*entry
,
120 cm_return_val_if_fail(key
!= NULL
, TRUE
);
121 cm_return_val_if_fail(entry
!= NULL
, TRUE
);
123 string_entry_free(entry
);
128 void string_table_free(StringTable
*table
)
130 cm_return_if_fail(table
!= NULL
);
131 cm_return_if_fail(table
->hash_table
!= NULL
);
133 g_hash_table_foreach_remove(table
->hash_table
,
134 (GHRFunc
)string_table_remove_for_each_fn
,
136 g_hash_table_destroy(table
->hash_table
);
140 static void string_table_stats_for_each_fn(gchar
*key
, StringEntry
*entry
,
143 if (entry
->ref_count
> 1) {
144 *totals
+= strlen(key
) * (entry
->ref_count
- 1);
148 void string_table_get_stats(StringTable
*table
)
152 g_hash_table_foreach(table
->hash_table
,
153 (GHFunc
)string_table_stats_for_each_fn
, &totals
);
154 XXX_DEBUG ("TOTAL UNSPILLED %d (%dK)\n", totals
, totals
/ 1024);