1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gdataset.c: Generic dataset mechanism, similar to GtkObject data.
5 * Copyright (C) 1998 Tim Janik
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
26 #define G_DATASET_BLOCK_SIZE (512)
27 #define G_DATASET_MEM_CHUNK_PREALLOC (64)
28 #define G_DATASET_DATA_MEM_CHUNK_PREALLOC (128)
31 /* --- structures --- */
32 typedef struct _GDatasetData GDatasetData;
33 typedef struct _GDataset GDataset;
39 GDestroyNotify destroy_func;
44 gconstpointer location;
45 GDatasetData *data_list;
49 /* --- prototypes --- */
50 static inline GDataset* g_dataset_lookup (gconstpointer dataset_location);
51 static inline void g_dataset_destroy_i (GDataset *dataset);
52 static void g_dataset_initialize (void);
53 static void g_dataset_alloc_key (const gchar *string,
58 /* --- variables --- */
59 static GHashTable *g_dataset_location_ht = NULL;
60 static GHashTable *g_dataset_key_ht = NULL;
61 static GDataset *g_dataset_cached = NULL;
62 static GMemChunk *g_dataset_mem_chunk = NULL;
63 static GMemChunk *g_dataset_data_mem_chunk = NULL;
64 static gchar **g_dataset_key_array = NULL;
65 static guint g_dataset_seq_id = 0;
69 /* --- functions --- */
70 static inline GDataset*
71 g_dataset_lookup (gconstpointer dataset_location)
73 register GDataset *dataset;
75 if (g_dataset_cached && g_dataset_cached->location == dataset_location)
76 return g_dataset_cached;
78 if (!g_dataset_location_ht)
79 g_dataset_initialize ();
81 dataset = g_hash_table_lookup (g_dataset_location_ht, dataset_location);
83 g_dataset_cached = dataset;
89 g_dataset_destroy_i (GDataset *dataset)
91 register GDatasetData *list;
93 if (dataset == g_dataset_cached)
94 g_dataset_cached = NULL;
95 g_hash_table_remove (g_dataset_location_ht, dataset->location);
97 list = dataset->data_list;
98 g_mem_chunk_free (g_dataset_mem_chunk, dataset);
102 register GDatasetData *prev;
107 if (prev->destroy_func)
108 prev->destroy_func (prev->data);
110 g_mem_chunk_free (g_dataset_data_mem_chunk, prev);
115 g_dataset_destroy (gconstpointer dataset_location)
117 register GDataset *dataset;
119 g_return_if_fail (dataset_location != NULL);
121 dataset = g_dataset_lookup (dataset_location);
123 g_dataset_destroy_i (dataset);
127 g_dataset_id_set_destroy (gconstpointer dataset_location,
129 GDestroyNotify destroy_func)
131 g_return_if_fail (dataset_location != NULL);
135 register GDataset *dataset;
137 dataset = g_dataset_lookup (dataset_location);
140 register GDatasetData *list;
142 list = dataset->data_list;
145 if (list->id == key_id)
147 list->destroy_func = destroy_func;
156 g_dataset_id_get_data (gconstpointer dataset_location,
159 g_return_val_if_fail (dataset_location != NULL, NULL);
163 register GDataset *dataset;
165 dataset = g_dataset_lookup (dataset_location);
168 register GDatasetData *list;
170 for (list = dataset->data_list; list; list = list->next)
171 if (list->id == key_id)
180 g_dataset_id_set_data_full (gconstpointer dataset_location,
183 GDestroyNotify destroy_func)
185 register GDataset *dataset;
186 register GDatasetData *list;
188 g_return_if_fail (dataset_location != NULL);
189 g_return_if_fail (key_id > 0);
191 dataset = g_dataset_lookup (dataset_location);
194 dataset = g_chunk_new (GDataset, g_dataset_mem_chunk);
195 dataset->location = dataset_location;
196 dataset->data_list = NULL;
197 g_hash_table_insert (g_dataset_location_ht,
198 (gpointer) dataset->location, /* Yuck */
202 list = dataset->data_list;
205 register GDatasetData *prev;
210 if (list->id == key_id)
213 prev->next = list->next;
216 dataset->data_list = list->next;
218 if (!dataset->data_list)
219 g_dataset_destroy_i (dataset);
222 /* we need to have unlinked before invoking the destroy function
224 if (list->destroy_func)
225 list->destroy_func (list->data);
227 g_mem_chunk_free (g_dataset_data_mem_chunk, list);
237 register GDatasetData *prev;
242 if (list->id == key_id)
245 prev->next = list->next;
247 dataset->data_list = list->next;
249 /* we need to have unlinked before invoking the destroy function
251 if (list->destroy_func)
252 list->destroy_func (list->data);
262 list = g_chunk_new (GDatasetData, g_dataset_data_mem_chunk);
263 list->next = dataset->data_list;
266 list->destroy_func = destroy_func;
267 dataset->data_list = list;
272 g_dataset_try_key (const gchar *key)
275 g_return_val_if_fail (key != NULL, 0);
277 if (g_dataset_key_ht)
279 id = g_hash_table_lookup (g_dataset_key_ht, (gpointer) key);
289 g_dataset_force_id (const gchar *key)
293 g_return_val_if_fail (key != NULL, 0);
295 if (!g_dataset_key_ht)
296 g_dataset_initialize ();
298 id = g_hash_table_lookup (g_dataset_key_ht, (gpointer) key);
303 g_dataset_alloc_key (key, &id, &new_key);
304 g_hash_table_insert (g_dataset_key_ht, new_key, id);
311 g_dataset_initialize (void)
313 if (!g_dataset_location_ht)
315 g_dataset_location_ht = g_hash_table_new (g_direct_hash, NULL);
316 g_dataset_key_ht = g_hash_table_new (g_str_hash, g_str_equal);
317 g_dataset_cached = NULL;
318 g_dataset_mem_chunk =
319 g_mem_chunk_new ("GDataset MemChunk",
321 sizeof (GDataset) * G_DATASET_MEM_CHUNK_PREALLOC,
323 g_dataset_data_mem_chunk =
324 g_mem_chunk_new ("GDatasetData MemChunk",
325 sizeof (GDatasetData),
326 sizeof (GDatasetData) * G_DATASET_DATA_MEM_CHUNK_PREALLOC,
332 g_dataset_retrive_key (guint id)
334 if (id > 0 && id <= g_dataset_seq_id)
335 return g_dataset_key_array[id - 1];
340 g_dataset_alloc_key (const gchar *string,
344 if (g_dataset_seq_id % G_DATASET_BLOCK_SIZE == 0)
345 g_dataset_key_array = g_realloc (g_dataset_key_array,
346 (g_dataset_seq_id + G_DATASET_BLOCK_SIZE) * sizeof (gchar*));
348 *key = g_new (gchar, sizeof (guint) + strlen (string) + 1);
350 *key += sizeof (guint);
351 strcpy (*key, string);
352 g_dataset_key_array[g_dataset_seq_id] = *key;
354 **id = g_dataset_seq_id;