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 Lesser 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 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser 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.
23 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
24 * file for a list of people on the GLib Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 * MT safe ; FIXME: might still freeze, watch out, not thoroughly
42 #define G_QUARK_BLOCK_SIZE (512)
43 #define G_DATA_MEM_CHUNK_PREALLOC (128)
44 #define G_DATA_CACHE_MAX (512)
45 #define G_DATASET_MEM_CHUNK_PREALLOC (32)
48 /* --- structures --- */
49 typedef struct _GDataset GDataset;
55 GDestroyNotify destroy_func;
60 gconstpointer location;
65 /* --- prototypes --- */
66 static inline GDataset* g_dataset_lookup (gconstpointer dataset_location);
67 static inline void g_datalist_clear_i (GData **datalist);
68 static void g_dataset_destroy_internal (GDataset *dataset);
69 static inline gpointer g_data_set_internal (GData **datalist,
72 GDestroyNotify destroy_func,
74 static void g_data_initialize (void);
75 static inline GQuark g_quark_new (gchar *string);
78 /* --- variables --- */
79 G_LOCK_DEFINE_STATIC (g_dataset_global);
80 static GHashTable *g_dataset_location_ht = NULL;
81 static GDataset *g_dataset_cached = NULL; /* should this be
83 static GMemChunk *g_dataset_mem_chunk = NULL;
84 static GMemChunk *g_data_mem_chunk = NULL;
85 static GData *g_data_cache = NULL;
86 static guint g_data_cache_length = 0;
88 G_LOCK_DEFINE_STATIC (g_quark_global);
89 static GHashTable *g_quark_ht = NULL;
90 static gchar **g_quarks = NULL;
91 static GQuark g_quark_seq_id = 0;
94 /* --- functions --- */
96 /* HOLDS: g_dataset_global_lock */
98 g_datalist_clear_i (GData **datalist)
100 register GData *list;
102 /* unlink *all* items before walking their destructors
109 register GData *prev;
114 if (prev->destroy_func)
116 G_UNLOCK (g_dataset_global);
117 prev->destroy_func (prev->data);
118 G_LOCK (g_dataset_global);
121 if (g_data_cache_length < G_DATA_CACHE_MAX)
123 prev->next = g_data_cache;
125 g_data_cache_length++;
128 g_mem_chunk_free (g_data_mem_chunk, prev);
133 g_datalist_clear (GData **datalist)
135 g_return_if_fail (datalist != NULL);
137 G_LOCK (g_dataset_global);
138 if (!g_dataset_location_ht)
139 g_data_initialize ();
142 g_datalist_clear_i (datalist);
143 G_UNLOCK (g_dataset_global);
146 /* HOLDS: g_dataset_global_lock */
147 static inline GDataset*
148 g_dataset_lookup (gconstpointer dataset_location)
150 register GDataset *dataset;
152 if (g_dataset_cached && g_dataset_cached->location == dataset_location)
153 return g_dataset_cached;
155 dataset = g_hash_table_lookup (g_dataset_location_ht, dataset_location);
157 g_dataset_cached = dataset;
162 /* HOLDS: g_dataset_global_lock */
164 g_dataset_destroy_internal (GDataset *dataset)
166 register gconstpointer dataset_location;
168 dataset_location = dataset->location;
171 if (!dataset->datalist)
173 if (dataset == g_dataset_cached)
174 g_dataset_cached = NULL;
175 g_hash_table_remove (g_dataset_location_ht, dataset_location);
176 g_mem_chunk_free (g_dataset_mem_chunk, dataset);
180 g_datalist_clear_i (&dataset->datalist);
181 dataset = g_dataset_lookup (dataset_location);
186 g_dataset_destroy (gconstpointer dataset_location)
188 g_return_if_fail (dataset_location != NULL);
190 G_LOCK (g_dataset_global);
191 if (g_dataset_location_ht)
193 register GDataset *dataset;
195 dataset = g_dataset_lookup (dataset_location);
197 g_dataset_destroy_internal (dataset);
199 G_UNLOCK (g_dataset_global);
202 /* HOLDS: g_dataset_global_lock */
203 static inline gpointer
204 g_data_set_internal (GData **datalist,
207 GDestroyNotify destroy_func,
210 register GData *list;
215 register GData *prev;
220 if (list->id == key_id)
222 gpointer ret_data = NULL;
225 prev->next = list->next;
228 *datalist = list->next;
230 /* the dataset destruction *must* be done
231 * prior to invokation of the data destroy function
233 if (!*datalist && dataset)
234 g_dataset_destroy_internal (dataset);
237 /* the GData struct *must* already be unlinked
238 * when invoking the destroy function.
239 * we use (data==NULL && destroy_func!=NULL) as
240 * a special hint combination to "steal"
241 * data without destroy notification
243 if (list->destroy_func && !destroy_func)
245 G_UNLOCK (g_dataset_global);
246 list->destroy_func (list->data);
247 G_LOCK (g_dataset_global);
250 ret_data = list->data;
252 if (g_data_cache_length < G_DATA_CACHE_MAX)
254 list->next = g_data_cache;
256 g_data_cache_length++;
259 g_mem_chunk_free (g_data_mem_chunk, list);
272 if (list->id == key_id)
274 if (!list->destroy_func)
277 list->destroy_func = destroy_func;
281 register GDestroyNotify dfunc;
282 register gpointer ddata;
284 dfunc = list->destroy_func;
287 list->destroy_func = destroy_func;
289 /* we need to have updated all structures prior to
290 * invokation of the destroy function
292 G_UNLOCK (g_dataset_global);
294 G_LOCK (g_dataset_global);
306 g_data_cache = list->next;
307 g_data_cache_length--;
310 list = g_chunk_new (GData, g_data_mem_chunk);
311 list->next = *datalist;
314 list->destroy_func = destroy_func;
322 g_dataset_id_set_data_full (gconstpointer dataset_location,
325 GDestroyNotify destroy_func)
327 register GDataset *dataset;
329 g_return_if_fail (dataset_location != NULL);
331 g_return_if_fail (destroy_func == NULL);
335 g_return_if_fail (key_id > 0);
340 G_LOCK (g_dataset_global);
341 if (!g_dataset_location_ht)
342 g_data_initialize ();
344 dataset = g_dataset_lookup (dataset_location);
347 dataset = g_chunk_new (GDataset, g_dataset_mem_chunk);
348 dataset->location = dataset_location;
349 g_datalist_init (&dataset->datalist);
350 g_hash_table_insert (g_dataset_location_ht,
351 (gpointer) dataset->location,
355 g_data_set_internal (&dataset->datalist, key_id, data, destroy_func, dataset);
356 G_UNLOCK (g_dataset_global);
360 g_datalist_id_set_data_full (GData **datalist,
363 GDestroyNotify destroy_func)
365 g_return_if_fail (datalist != NULL);
367 g_return_if_fail (destroy_func == NULL);
371 g_return_if_fail (key_id > 0);
376 G_LOCK (g_dataset_global);
377 if (!g_dataset_location_ht)
378 g_data_initialize ();
380 g_data_set_internal (datalist, key_id, data, destroy_func, NULL);
381 G_UNLOCK (g_dataset_global);
385 g_dataset_id_remove_no_notify (gconstpointer dataset_location,
388 gpointer ret_data = NULL;
390 g_return_val_if_fail (dataset_location != NULL, NULL);
392 G_LOCK (g_dataset_global);
393 if (key_id && g_dataset_location_ht)
397 dataset = g_dataset_lookup (dataset_location);
399 ret_data = g_data_set_internal (&dataset->datalist, key_id, NULL, (GDestroyNotify) 42, dataset);
401 G_UNLOCK (g_dataset_global);
407 g_datalist_id_remove_no_notify (GData **datalist,
410 gpointer ret_data = NULL;
412 g_return_val_if_fail (datalist != NULL, NULL);
414 G_LOCK (g_dataset_global);
415 if (key_id && g_dataset_location_ht)
416 ret_data = g_data_set_internal (datalist, key_id, NULL, (GDestroyNotify) 42, NULL);
417 G_UNLOCK (g_dataset_global);
423 g_dataset_id_get_data (gconstpointer dataset_location,
426 g_return_val_if_fail (dataset_location != NULL, NULL);
428 G_LOCK (g_dataset_global);
429 if (key_id && g_dataset_location_ht)
431 register GDataset *dataset;
433 dataset = g_dataset_lookup (dataset_location);
436 register GData *list;
438 for (list = dataset->datalist; list; list = list->next)
439 if (list->id == key_id)
441 G_UNLOCK (g_dataset_global);
446 G_UNLOCK (g_dataset_global);
452 g_datalist_id_get_data (GData **datalist,
455 g_return_val_if_fail (datalist != NULL, NULL);
459 register GData *list;
461 for (list = *datalist; list; list = list->next)
462 if (list->id == key_id)
470 g_dataset_foreach (gconstpointer dataset_location,
471 GDataForeachFunc func,
474 register GDataset *dataset;
476 g_return_if_fail (dataset_location != NULL);
477 g_return_if_fail (func != NULL);
479 G_LOCK (g_dataset_global);
480 if (g_dataset_location_ht)
482 dataset = g_dataset_lookup (dataset_location);
483 G_UNLOCK (g_dataset_global);
486 register GData *list, *next;
488 for (list = dataset->datalist; list; list = next)
491 func (list->id, list->data, user_data);
497 G_UNLOCK (g_dataset_global);
502 g_datalist_foreach (GData **datalist,
503 GDataForeachFunc func,
506 register GData *list, *next;
508 g_return_if_fail (datalist != NULL);
509 g_return_if_fail (func != NULL);
511 for (list = *datalist; list; list = next)
514 func (list->id, list->data, user_data);
519 g_datalist_init (GData **datalist)
521 g_return_if_fail (datalist != NULL);
526 /* HOLDS: g_dataset_global_lock */
528 g_data_initialize (void)
530 g_return_if_fail (g_dataset_location_ht == NULL);
532 g_dataset_location_ht = g_hash_table_new (g_direct_hash, NULL);
533 g_dataset_cached = NULL;
534 g_dataset_mem_chunk =
535 g_mem_chunk_new ("GDataset MemChunk",
537 sizeof (GDataset) * G_DATASET_MEM_CHUNK_PREALLOC,
540 g_mem_chunk_new ("GData MemChunk",
542 sizeof (GData) * G_DATA_MEM_CHUNK_PREALLOC,
547 g_quark_try_string (const gchar *string)
550 g_return_val_if_fail (string != NULL, 0);
552 G_LOCK (g_quark_global);
554 quark = GPOINTER_TO_UINT (g_hash_table_lookup (g_quark_ht, string));
555 G_UNLOCK (g_quark_global);
561 g_quark_from_string (const gchar *string)
565 g_return_val_if_fail (string != NULL, 0);
567 G_LOCK (g_quark_global);
569 quark = (gulong) g_hash_table_lookup (g_quark_ht, string);
572 g_quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
577 quark = g_quark_new (g_strdup (string));
578 G_UNLOCK (g_quark_global);
584 g_quark_from_static_string (const gchar *string)
588 g_return_val_if_fail (string != NULL, 0);
590 G_LOCK (g_quark_global);
592 quark = (gulong) g_hash_table_lookup (g_quark_ht, string);
595 g_quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
600 quark = g_quark_new ((gchar*) string);
601 G_UNLOCK (g_quark_global);
606 G_CONST_RETURN gchar*
607 g_quark_to_string (GQuark quark)
609 gchar* result = NULL;
610 G_LOCK (g_quark_global);
611 if (quark > 0 && quark <= g_quark_seq_id)
612 result = g_quarks[quark - 1];
613 G_UNLOCK (g_quark_global);
618 /* HOLDS: g_quark_global_lock */
620 g_quark_new (gchar *string)
624 if (g_quark_seq_id % G_QUARK_BLOCK_SIZE == 0)
625 g_quarks = g_renew (gchar*, g_quarks, g_quark_seq_id + G_QUARK_BLOCK_SIZE);
627 g_quarks[g_quark_seq_id] = string;
629 quark = g_quark_seq_id;
630 g_hash_table_insert (g_quark_ht, string, GUINT_TO_POINTER (quark));