Add PPTP runtime and GUI
[tomato.git] / release / src / router / glib / gdataset.c
blob2aa4032abc218135a57f99dbb7dd54eec752451b
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.
23 * Modified by the GLib Team and others 1997-1999. 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/.
29 /*
30 * MT safe ; FIXME: might still freeze, watch out, not thoroughly
31 * looked at yet.
34 #include <string.h>
35 #include "glib.h"
39 /* --- defines --- */
40 #define G_QUARK_BLOCK_SIZE (512)
41 #define G_DATA_MEM_CHUNK_PREALLOC (128)
42 #define G_DATA_CACHE_MAX (512)
43 #define G_DATASET_MEM_CHUNK_PREALLOC (32)
46 /* --- structures --- */
47 typedef struct _GDataset GDataset;
48 struct _GData
50 GData *next;
51 GQuark id;
52 gpointer data;
53 GDestroyNotify destroy_func;
56 struct _GDataset
58 gconstpointer location;
59 GData *datalist;
63 /* --- prototypes --- */
64 static inline GDataset* g_dataset_lookup (gconstpointer dataset_location);
65 static inline void g_datalist_clear_i (GData **datalist);
66 static void g_dataset_destroy_internal (GDataset *dataset);
67 static inline void g_data_set_internal (GData **datalist,
68 GQuark key_id,
69 gpointer data,
70 GDestroyNotify destroy_func,
71 GDataset *dataset);
72 static void g_data_initialize (void);
73 static inline GQuark g_quark_new (gchar *string);
76 /* --- variables --- */
77 G_LOCK_DEFINE_STATIC (g_dataset_global);
78 static GHashTable *g_dataset_location_ht = NULL;
79 static GDataset *g_dataset_cached = NULL; /* should this be
80 threadspecific? */
81 static GMemChunk *g_dataset_mem_chunk = NULL;
82 static GMemChunk *g_data_mem_chunk = NULL;
83 static GData *g_data_cache = NULL;
84 static guint g_data_cache_length = 0;
86 G_LOCK_DEFINE_STATIC (g_quark_global);
87 static GHashTable *g_quark_ht = NULL;
88 static gchar **g_quarks = NULL;
89 static GQuark g_quark_seq_id = 0;
92 /* --- functions --- */
94 /* HOLDS: g_dataset_global_lock */
95 static inline void
96 g_datalist_clear_i (GData **datalist)
98 register GData *list;
100 /* unlink *all* items before walking their destructors
102 list = *datalist;
103 *datalist = NULL;
105 while (list)
107 register GData *prev;
109 prev = list;
110 list = prev->next;
112 if (prev->destroy_func)
114 G_UNLOCK (g_dataset_global);
115 prev->destroy_func (prev->data);
116 G_LOCK (g_dataset_global);
119 if (g_data_cache_length < G_DATA_CACHE_MAX)
121 prev->next = g_data_cache;
122 g_data_cache = prev;
123 g_data_cache_length++;
125 else
126 g_mem_chunk_free (g_data_mem_chunk, prev);
130 void
131 g_datalist_clear (GData **datalist)
133 g_return_if_fail (datalist != NULL);
135 G_LOCK (g_dataset_global);
136 if (!g_dataset_location_ht)
137 g_data_initialize ();
139 while (*datalist)
140 g_datalist_clear_i (datalist);
141 G_UNLOCK (g_dataset_global);
144 /* HOLDS: g_dataset_global_lock */
145 static inline GDataset*
146 g_dataset_lookup (gconstpointer dataset_location)
148 register GDataset *dataset;
150 if (g_dataset_cached && g_dataset_cached->location == dataset_location)
151 return g_dataset_cached;
153 dataset = g_hash_table_lookup (g_dataset_location_ht, dataset_location);
154 if (dataset)
155 g_dataset_cached = dataset;
157 return dataset;
160 /* HOLDS: g_dataset_global_lock */
161 static void
162 g_dataset_destroy_internal (GDataset *dataset)
164 register gconstpointer dataset_location;
166 dataset_location = dataset->location;
167 while (dataset)
169 if (!dataset->datalist)
171 if (dataset == g_dataset_cached)
172 g_dataset_cached = NULL;
173 g_hash_table_remove (g_dataset_location_ht, dataset_location);
174 g_mem_chunk_free (g_dataset_mem_chunk, dataset);
175 break;
178 g_datalist_clear_i (&dataset->datalist);
179 dataset = g_dataset_lookup (dataset_location);
183 void
184 g_dataset_destroy (gconstpointer dataset_location)
186 g_return_if_fail (dataset_location != NULL);
188 G_LOCK (g_dataset_global);
189 if (g_dataset_location_ht)
191 register GDataset *dataset;
193 dataset = g_dataset_lookup (dataset_location);
194 if (dataset)
195 g_dataset_destroy_internal (dataset);
197 G_UNLOCK (g_dataset_global);
200 /* HOLDS: g_dataset_global_lock */
201 static inline void
202 g_data_set_internal (GData **datalist,
203 GQuark key_id,
204 gpointer data,
205 GDestroyNotify destroy_func,
206 GDataset *dataset)
208 register GData *list;
210 list = *datalist;
211 if (!data)
213 register GData *prev;
215 prev = NULL;
216 while (list)
218 if (list->id == key_id)
220 if (prev)
221 prev->next = list->next;
222 else
224 *datalist = list->next;
226 /* the dataset destruction *must* be done
227 * prior to invokation of the data destroy function
229 if (!*datalist && dataset)
230 g_dataset_destroy_internal (dataset);
233 /* the GData struct *must* already be unlinked
234 * when invoking the destroy function.
235 * we use (data==NULL && destroy_func!=NULL) as
236 * a special hint combination to "steal"
237 * data without destroy notification
239 if (list->destroy_func && !destroy_func)
241 G_UNLOCK (g_dataset_global);
242 list->destroy_func (list->data);
243 G_LOCK (g_dataset_global);
246 if (g_data_cache_length < G_DATA_CACHE_MAX)
248 list->next = g_data_cache;
249 g_data_cache = list;
250 g_data_cache_length++;
252 else
253 g_mem_chunk_free (g_data_mem_chunk, list);
255 return;
258 prev = list;
259 list = list->next;
262 else
264 while (list)
266 if (list->id == key_id)
268 if (!list->destroy_func)
270 list->data = data;
271 list->destroy_func = destroy_func;
273 else
275 register GDestroyNotify dfunc;
276 register gpointer ddata;
278 dfunc = list->destroy_func;
279 ddata = list->data;
280 list->data = data;
281 list->destroy_func = destroy_func;
283 /* we need to have updated all structures prior to
284 * invokation of the destroy function
286 G_UNLOCK (g_dataset_global);
287 dfunc (ddata);
288 G_LOCK (g_dataset_global);
291 return;
294 list = list->next;
297 if (g_data_cache)
299 list = g_data_cache;
300 g_data_cache = list->next;
301 g_data_cache_length--;
303 else
304 list = g_chunk_new (GData, g_data_mem_chunk);
305 list->next = *datalist;
306 list->id = key_id;
307 list->data = data;
308 list->destroy_func = destroy_func;
309 *datalist = list;
313 void
314 g_dataset_id_set_data_full (gconstpointer dataset_location,
315 GQuark key_id,
316 gpointer data,
317 GDestroyNotify destroy_func)
319 register GDataset *dataset;
321 g_return_if_fail (dataset_location != NULL);
322 if (!data)
323 g_return_if_fail (destroy_func == NULL);
324 if (!key_id)
326 if (data)
327 g_return_if_fail (key_id > 0);
328 else
329 return;
332 G_LOCK (g_dataset_global);
333 if (!g_dataset_location_ht)
334 g_data_initialize ();
336 dataset = g_dataset_lookup (dataset_location);
337 if (!dataset)
339 dataset = g_chunk_new (GDataset, g_dataset_mem_chunk);
340 dataset->location = dataset_location;
341 g_datalist_init (&dataset->datalist);
342 g_hash_table_insert (g_dataset_location_ht,
343 (gpointer) dataset->location,
344 dataset);
347 g_data_set_internal (&dataset->datalist, key_id, data, destroy_func, dataset);
348 G_UNLOCK (g_dataset_global);
351 void
352 g_datalist_id_set_data_full (GData **datalist,
353 GQuark key_id,
354 gpointer data,
355 GDestroyNotify destroy_func)
357 g_return_if_fail (datalist != NULL);
358 if (!data)
359 g_return_if_fail (destroy_func == NULL);
360 if (!key_id)
362 if (data)
363 g_return_if_fail (key_id > 0);
364 else
365 return;
368 G_LOCK (g_dataset_global);
369 if (!g_dataset_location_ht)
370 g_data_initialize ();
372 g_data_set_internal (datalist, key_id, data, destroy_func, NULL);
373 G_UNLOCK (g_dataset_global);
376 void
377 g_dataset_id_remove_no_notify (gconstpointer dataset_location,
378 GQuark key_id)
380 g_return_if_fail (dataset_location != NULL);
382 G_LOCK (g_dataset_global);
383 if (key_id && g_dataset_location_ht)
385 GDataset *dataset;
387 dataset = g_dataset_lookup (dataset_location);
388 if (dataset)
389 g_data_set_internal (&dataset->datalist, key_id, NULL, (GDestroyNotify) 42, dataset);
391 G_UNLOCK (g_dataset_global);
394 void
395 g_datalist_id_remove_no_notify (GData **datalist,
396 GQuark key_id)
398 g_return_if_fail (datalist != NULL);
400 G_LOCK (g_dataset_global);
401 if (key_id && g_dataset_location_ht)
402 g_data_set_internal (datalist, key_id, NULL, (GDestroyNotify) 42, NULL);
403 G_UNLOCK (g_dataset_global);
406 gpointer
407 g_dataset_id_get_data (gconstpointer dataset_location,
408 GQuark key_id)
410 g_return_val_if_fail (dataset_location != NULL, NULL);
412 G_LOCK (g_dataset_global);
413 if (key_id && g_dataset_location_ht)
415 register GDataset *dataset;
417 dataset = g_dataset_lookup (dataset_location);
418 if (dataset)
420 register GData *list;
422 for (list = dataset->datalist; list; list = list->next)
423 if (list->id == key_id)
425 G_UNLOCK (g_dataset_global);
426 return list->data;
430 G_UNLOCK (g_dataset_global);
432 return NULL;
435 gpointer
436 g_datalist_id_get_data (GData **datalist,
437 GQuark key_id)
439 g_return_val_if_fail (datalist != NULL, NULL);
441 if (key_id)
443 register GData *list;
445 for (list = *datalist; list; list = list->next)
446 if (list->id == key_id)
447 return list->data;
450 return NULL;
453 void
454 g_dataset_foreach (gconstpointer dataset_location,
455 GDataForeachFunc func,
456 gpointer user_data)
458 register GDataset *dataset;
460 g_return_if_fail (dataset_location != NULL);
461 g_return_if_fail (func != NULL);
463 G_LOCK (g_dataset_global);
464 if (g_dataset_location_ht)
466 dataset = g_dataset_lookup (dataset_location);
467 G_UNLOCK (g_dataset_global);
468 if (dataset)
470 register GData *list;
472 for (list = dataset->datalist; list; list = list->next)
473 func (list->id, list->data, user_data);
476 else
478 G_UNLOCK (g_dataset_global);
482 void
483 g_datalist_foreach (GData **datalist,
484 GDataForeachFunc func,
485 gpointer user_data)
487 register GData *list;
489 g_return_if_fail (datalist != NULL);
490 g_return_if_fail (func != NULL);
492 for (list = *datalist; list; list = list->next)
493 func (list->id, list->data, user_data);
496 void
497 g_datalist_init (GData **datalist)
499 g_return_if_fail (datalist != NULL);
501 *datalist = NULL;
504 /* HOLDS: g_dataset_global_lock */
505 static void
506 g_data_initialize (void)
508 g_return_if_fail (g_dataset_location_ht == NULL);
510 g_dataset_location_ht = g_hash_table_new (g_direct_hash, NULL);
511 g_dataset_cached = NULL;
512 g_dataset_mem_chunk =
513 g_mem_chunk_new ("GDataset MemChunk",
514 sizeof (GDataset),
515 sizeof (GDataset) * G_DATASET_MEM_CHUNK_PREALLOC,
516 G_ALLOC_AND_FREE);
517 g_data_mem_chunk =
518 g_mem_chunk_new ("GData MemChunk",
519 sizeof (GData),
520 sizeof (GData) * G_DATA_MEM_CHUNK_PREALLOC,
521 G_ALLOC_AND_FREE);
524 GQuark
525 g_quark_try_string (const gchar *string)
527 GQuark quark = 0;
528 g_return_val_if_fail (string != NULL, 0);
530 G_LOCK (g_quark_global);
531 if (g_quark_ht)
532 quark = GPOINTER_TO_UINT (g_hash_table_lookup (g_quark_ht, string));
533 G_UNLOCK (g_quark_global);
535 return quark;
538 GQuark
539 g_quark_from_string (const gchar *string)
541 GQuark quark;
543 g_return_val_if_fail (string != NULL, 0);
545 G_LOCK (g_quark_global);
546 if (g_quark_ht)
547 quark = (gulong) g_hash_table_lookup (g_quark_ht, string);
548 else
550 g_quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
551 quark = 0;
554 if (!quark)
555 quark = g_quark_new (g_strdup (string));
556 G_UNLOCK (g_quark_global);
558 return quark;
561 GQuark
562 g_quark_from_static_string (const gchar *string)
564 GQuark quark;
566 g_return_val_if_fail (string != NULL, 0);
568 G_LOCK (g_quark_global);
569 if (g_quark_ht)
570 quark = (gulong) g_hash_table_lookup (g_quark_ht, string);
571 else
573 g_quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
574 quark = 0;
577 if (!quark)
578 quark = g_quark_new ((gchar*) string);
579 G_UNLOCK (g_quark_global);
581 return quark;
584 gchar*
585 g_quark_to_string (GQuark quark)
587 gchar* result = NULL;
588 G_LOCK (g_quark_global);
589 if (quark > 0 && quark <= g_quark_seq_id)
590 result = g_quarks[quark - 1];
591 G_UNLOCK (g_quark_global);
593 return result;
596 /* HOLDS: g_quark_global_lock */
597 static inline GQuark
598 g_quark_new (gchar *string)
600 GQuark quark;
602 if (g_quark_seq_id % G_QUARK_BLOCK_SIZE == 0)
603 g_quarks = g_renew (gchar*, g_quarks, g_quark_seq_id + G_QUARK_BLOCK_SIZE);
605 g_quarks[g_quark_seq_id] = string;
606 g_quark_seq_id++;
607 quark = g_quark_seq_id;
608 g_hash_table_insert (g_quark_ht, string, GUINT_TO_POINTER (quark));
610 return quark;