1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
33 #include "gstringchunk.h"
37 #include "gmessages.h"
40 #include "gutilsprivate.h"
43 * SECTION:string_chunks
44 * @title: String Chunks
45 * @short_description: efficient storage of groups of strings
47 * String chunks are used to store groups of strings. Memory is
48 * allocated in blocks, and as strings are added to the #GStringChunk
49 * they are copied into the next free position in a block. When a block
50 * is full a new block is allocated.
52 * When storing a large number of strings, string chunks are more
53 * efficient than using g_strdup() since fewer calls to malloc() are
54 * needed, and less memory is wasted in memory allocation overheads.
56 * By adding strings with g_string_chunk_insert_const() it is also
57 * possible to remove duplicates.
59 * To create a new #GStringChunk use g_string_chunk_new().
61 * To add strings to a #GStringChunk use g_string_chunk_insert().
63 * To add strings to a #GStringChunk, but without duplicating strings
64 * which are already in the #GStringChunk, use
65 * g_string_chunk_insert_const().
67 * To free the entire #GStringChunk use g_string_chunk_free(). It is
68 * not possible to free individual strings.
74 * An opaque data structure representing String Chunks.
75 * It should only be accessed by using the following functions.
79 GHashTable *const_table;
88 * @size: the default size of the blocks of memory which are
89 * allocated to store the strings. If a particular string
90 * is larger than this default size, a larger block of
91 * memory will be allocated for it.
93 * Creates a new #GStringChunk.
95 * Returns: a new #GStringChunk
98 g_string_chunk_new (gsize size)
100 GStringChunk *new_chunk = g_new (GStringChunk, 1);
101 gsize actual_size = 1;
103 actual_size = g_nearest_pow (MAX (1, size));
105 new_chunk->const_table = NULL;
106 new_chunk->storage_list = NULL;
107 new_chunk->storage_next = actual_size;
108 new_chunk->default_size = actual_size;
109 new_chunk->this_size = actual_size;
115 * g_string_chunk_free:
116 * @chunk: a #GStringChunk
118 * Frees all memory allocated by the #GStringChunk.
119 * After calling g_string_chunk_free() it is not safe to
120 * access any of the strings which were contained within it.
123 g_string_chunk_free (GStringChunk *chunk)
125 g_return_if_fail (chunk != NULL);
127 if (chunk->storage_list)
128 g_slist_free_full (chunk->storage_list, g_free);
130 if (chunk->const_table)
131 g_hash_table_destroy (chunk->const_table);
137 * g_string_chunk_clear:
138 * @chunk: a #GStringChunk
140 * Frees all strings contained within the #GStringChunk.
141 * After calling g_string_chunk_clear() it is not safe to
142 * access any of the strings which were contained within it.
147 g_string_chunk_clear (GStringChunk *chunk)
149 g_return_if_fail (chunk != NULL);
151 if (chunk->storage_list)
153 g_slist_free_full (chunk->storage_list, g_free);
155 chunk->storage_list = NULL;
156 chunk->storage_next = chunk->default_size;
157 chunk->this_size = chunk->default_size;
160 if (chunk->const_table)
161 g_hash_table_remove_all (chunk->const_table);
165 * g_string_chunk_insert:
166 * @chunk: a #GStringChunk
167 * @string: the string to add
169 * Adds a copy of @string to the #GStringChunk.
170 * It returns a pointer to the new copy of the string
171 * in the #GStringChunk. The characters in the string
172 * can be changed, if necessary, though you should not
173 * change anything after the end of the string.
175 * Unlike g_string_chunk_insert_const(), this function
176 * does not check for duplicates. Also strings added
177 * with g_string_chunk_insert() will not be searched
178 * by g_string_chunk_insert_const() when looking for
181 * Returns: a pointer to the copy of @string within
185 g_string_chunk_insert (GStringChunk *chunk,
188 g_return_val_if_fail (chunk != NULL, NULL);
190 return g_string_chunk_insert_len (chunk, string, -1);
194 * g_string_chunk_insert_const:
195 * @chunk: a #GStringChunk
196 * @string: the string to add
198 * Adds a copy of @string to the #GStringChunk, unless the same
199 * string has already been added to the #GStringChunk with
200 * g_string_chunk_insert_const().
202 * This function is useful if you need to copy a large number
203 * of strings but do not want to waste space storing duplicates.
204 * But you must remember that there may be several pointers to
205 * the same string, and so any changes made to the strings
206 * should be done very carefully.
208 * Note that g_string_chunk_insert_const() will not return a
209 * pointer to a string added with g_string_chunk_insert(), even
212 * Returns: a pointer to the new or existing copy of @string
213 * within the #GStringChunk
216 g_string_chunk_insert_const (GStringChunk *chunk,
221 g_return_val_if_fail (chunk != NULL, NULL);
223 if (!chunk->const_table)
224 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
226 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
230 lookup = g_string_chunk_insert (chunk, string);
231 g_hash_table_add (chunk->const_table, lookup);
238 * g_string_chunk_insert_len:
239 * @chunk: a #GStringChunk
240 * @string: bytes to insert
241 * @len: number of bytes of @string to insert, or -1 to insert a
242 * nul-terminated string
244 * Adds a copy of the first @len bytes of @string to the #GStringChunk.
245 * The copy is nul-terminated.
247 * Since this function does not stop at nul bytes, it is the caller's
248 * responsibility to ensure that @string has at least @len addressable
251 * The characters in the returned string can be changed, if necessary,
252 * though you should not change anything after the end of the string.
254 * Returns: a pointer to the copy of @string within the #GStringChunk
259 g_string_chunk_insert_len (GStringChunk *chunk,
266 g_return_val_if_fail (chunk != NULL, NULL);
269 size = strlen (string);
273 if ((G_MAXSIZE - chunk->storage_next < size + 1) || (chunk->storage_next + size + 1) > chunk->this_size)
275 gsize new_size = g_nearest_pow (MAX (chunk->default_size, size + 1));
277 /* If size is bigger than G_MAXSIZE / 2 then store it in its own
278 * allocation instead of failing here */
282 chunk->storage_list = g_slist_prepend (chunk->storage_list,
283 g_new (gchar, new_size));
285 chunk->this_size = new_size;
286 chunk->storage_next = 0;
289 pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next;
291 *(pos + size) = '\0';
293 memcpy (pos, string, size);
295 chunk->storage_next += size + 1;