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/.
30 * MT safe ; FIXME: might still freeze, watch out, not thoroughly
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
;
53 GDestroyNotify destroy_func
;
58 gconstpointer location
;
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
,
70 GDestroyNotify destroy_func
,
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
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 */
96 g_datalist_clear_i (GData
**datalist
)
100 /* unlink *all* items before walking their destructors
107 register GData
*prev
;
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
;
123 g_data_cache_length
++;
126 g_mem_chunk_free (g_data_mem_chunk
, prev
);
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 ();
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
);
155 g_dataset_cached
= dataset
;
160 /* HOLDS: g_dataset_global_lock */
162 g_dataset_destroy_internal (GDataset
*dataset
)
164 register gconstpointer dataset_location
;
166 dataset_location
= dataset
->location
;
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
);
178 g_datalist_clear_i (&dataset
->datalist
);
179 dataset
= g_dataset_lookup (dataset_location
);
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
);
195 g_dataset_destroy_internal (dataset
);
197 G_UNLOCK (g_dataset_global
);
200 /* HOLDS: g_dataset_global_lock */
202 g_data_set_internal (GData
**datalist
,
205 GDestroyNotify destroy_func
,
208 register GData
*list
;
213 register GData
*prev
;
218 if (list
->id
== key_id
)
221 prev
->next
= list
->next
;
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
;
250 g_data_cache_length
++;
253 g_mem_chunk_free (g_data_mem_chunk
, list
);
266 if (list
->id
== key_id
)
268 if (!list
->destroy_func
)
271 list
->destroy_func
= destroy_func
;
275 register GDestroyNotify dfunc
;
276 register gpointer ddata
;
278 dfunc
= list
->destroy_func
;
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
);
288 G_LOCK (g_dataset_global
);
300 g_data_cache
= list
->next
;
301 g_data_cache_length
--;
304 list
= g_chunk_new (GData
, g_data_mem_chunk
);
305 list
->next
= *datalist
;
308 list
->destroy_func
= destroy_func
;
314 g_dataset_id_set_data_full (gconstpointer dataset_location
,
317 GDestroyNotify destroy_func
)
319 register GDataset
*dataset
;
321 g_return_if_fail (dataset_location
!= NULL
);
323 g_return_if_fail (destroy_func
== NULL
);
327 g_return_if_fail (key_id
> 0);
332 G_LOCK (g_dataset_global
);
333 if (!g_dataset_location_ht
)
334 g_data_initialize ();
336 dataset
= g_dataset_lookup (dataset_location
);
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
,
347 g_data_set_internal (&dataset
->datalist
, key_id
, data
, destroy_func
, dataset
);
348 G_UNLOCK (g_dataset_global
);
352 g_datalist_id_set_data_full (GData
**datalist
,
355 GDestroyNotify destroy_func
)
357 g_return_if_fail (datalist
!= NULL
);
359 g_return_if_fail (destroy_func
== NULL
);
363 g_return_if_fail (key_id
> 0);
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
);
377 g_dataset_id_remove_no_notify (gconstpointer dataset_location
,
380 g_return_if_fail (dataset_location
!= NULL
);
382 G_LOCK (g_dataset_global
);
383 if (key_id
&& g_dataset_location_ht
)
387 dataset
= g_dataset_lookup (dataset_location
);
389 g_data_set_internal (&dataset
->datalist
, key_id
, NULL
, (GDestroyNotify
) 42, dataset
);
391 G_UNLOCK (g_dataset_global
);
395 g_datalist_id_remove_no_notify (GData
**datalist
,
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
);
407 g_dataset_id_get_data (gconstpointer dataset_location
,
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
);
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
);
430 G_UNLOCK (g_dataset_global
);
436 g_datalist_id_get_data (GData
**datalist
,
439 g_return_val_if_fail (datalist
!= NULL
, NULL
);
443 register GData
*list
;
445 for (list
= *datalist
; list
; list
= list
->next
)
446 if (list
->id
== key_id
)
454 g_dataset_foreach (gconstpointer dataset_location
,
455 GDataForeachFunc func
,
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
);
470 register GData
*list
;
472 for (list
= dataset
->datalist
; list
; list
= list
->next
)
473 func (list
->id
, list
->data
, user_data
);
478 G_UNLOCK (g_dataset_global
);
483 g_datalist_foreach (GData
**datalist
,
484 GDataForeachFunc func
,
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
);
497 g_datalist_init (GData
**datalist
)
499 g_return_if_fail (datalist
!= NULL
);
504 /* HOLDS: g_dataset_global_lock */
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",
515 sizeof (GDataset
) * G_DATASET_MEM_CHUNK_PREALLOC
,
518 g_mem_chunk_new ("GData MemChunk",
520 sizeof (GData
) * G_DATA_MEM_CHUNK_PREALLOC
,
525 g_quark_try_string (const gchar
*string
)
528 g_return_val_if_fail (string
!= NULL
, 0);
530 G_LOCK (g_quark_global
);
532 quark
= GPOINTER_TO_UINT (g_hash_table_lookup (g_quark_ht
, string
));
533 G_UNLOCK (g_quark_global
);
539 g_quark_from_string (const gchar
*string
)
543 g_return_val_if_fail (string
!= NULL
, 0);
545 G_LOCK (g_quark_global
);
547 quark
= (gulong
) g_hash_table_lookup (g_quark_ht
, string
);
550 g_quark_ht
= g_hash_table_new (g_str_hash
, g_str_equal
);
555 quark
= g_quark_new (g_strdup (string
));
556 G_UNLOCK (g_quark_global
);
562 g_quark_from_static_string (const gchar
*string
)
566 g_return_val_if_fail (string
!= NULL
, 0);
568 G_LOCK (g_quark_global
);
570 quark
= (gulong
) g_hash_table_lookup (g_quark_ht
, string
);
573 g_quark_ht
= g_hash_table_new (g_str_hash
, g_str_equal
);
578 quark
= g_quark_new ((gchar
*) string
);
579 G_UNLOCK (g_quark_global
);
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
);
596 /* HOLDS: g_quark_global_lock */
598 g_quark_new (gchar
*string
)
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
;
607 quark
= g_quark_seq_id
;
608 g_hash_table_insert (g_quark_ht
, string
, GUINT_TO_POINTER (quark
));