From afdf8b93a308562faa2bd76c2bd64c6870fc0c66 Mon Sep 17 00:00:00 2001 From: Tim Janik Date: Fri, 19 Jun 1998 02:00:23 +0000 Subject: [PATCH] removed g_dataset_try_key, g_dataset_force_id and g_dataset_retrive_key in Fri Jun 19 03:11:02 1998 Tim Janik * gdataset.c: removed g_dataset_try_key, g_dataset_force_id and g_dataset_retrive_key in favour of GQuarks. a GQuark is an numeric id wich is associated with a certain string. (g_quark_try_string): try to get the quark associated with this string, if the lookup failed return 0. (g_quark_from_string): get the associated quark for a string, if there isn't currently a GQuark associated with this string, then allocate a new quark and return that. (g_quark_from_static_string): like the above function, but the string isn't strdup()ed to save memory. (g_quark_to_string): get the string that is associated with a certain GQuark. * gdataset.c (g_dataset_id_set_data_full): invoke the destroy function _after_ the new data has been setup. --- ChangeLog | 18 ++++++ ChangeLog.pre-2-0 | 18 ++++++ ChangeLog.pre-2-10 | 18 ++++++ ChangeLog.pre-2-12 | 18 ++++++ ChangeLog.pre-2-2 | 18 ++++++ ChangeLog.pre-2-4 | 18 ++++++ ChangeLog.pre-2-6 | 18 ++++++ ChangeLog.pre-2-8 | 18 ++++++ gdataset.c | 181 +++++++++++++++++++++++++++-------------------------- glib.h | 23 ++++--- glib/gdataset.c | 181 +++++++++++++++++++++++++++-------------------------- glib/glib.h | 23 ++++--- 12 files changed, 356 insertions(+), 196 deletions(-) diff --git a/ChangeLog b/ChangeLog index 97a10c3..4d3558c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +Fri Jun 19 03:11:02 1998 Tim Janik + + * gdataset.c: removed g_dataset_try_key, g_dataset_force_id and + g_dataset_retrive_key in favour of GQuarks. + a GQuark is an numeric id wich is associated with a certain string. + (g_quark_try_string): try to get the quark associated with this string, + if the lookup failed return 0. + (g_quark_from_string): get the associated quark for a string, if there + isn't currently a GQuark associated with this string, then allocate a + new quark and return that. + (g_quark_from_static_string): like the above function, but the string + isn't strdup()ed to save memory. + (g_quark_to_string): get the string that is associated with a certain + GQuark. + + * gdataset.c (g_dataset_id_set_data_full): invoke the destroy function + _after_ the new data has been setup. + Thu Jun 18 02:35:21 1998 Owen Taylor * glib.h: Changed messages for g_return_[val]_if_fail to diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 97a10c3..4d3558c 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,21 @@ +Fri Jun 19 03:11:02 1998 Tim Janik + + * gdataset.c: removed g_dataset_try_key, g_dataset_force_id and + g_dataset_retrive_key in favour of GQuarks. + a GQuark is an numeric id wich is associated with a certain string. + (g_quark_try_string): try to get the quark associated with this string, + if the lookup failed return 0. + (g_quark_from_string): get the associated quark for a string, if there + isn't currently a GQuark associated with this string, then allocate a + new quark and return that. + (g_quark_from_static_string): like the above function, but the string + isn't strdup()ed to save memory. + (g_quark_to_string): get the string that is associated with a certain + GQuark. + + * gdataset.c (g_dataset_id_set_data_full): invoke the destroy function + _after_ the new data has been setup. + Thu Jun 18 02:35:21 1998 Owen Taylor * glib.h: Changed messages for g_return_[val]_if_fail to diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 97a10c3..4d3558c 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,21 @@ +Fri Jun 19 03:11:02 1998 Tim Janik + + * gdataset.c: removed g_dataset_try_key, g_dataset_force_id and + g_dataset_retrive_key in favour of GQuarks. + a GQuark is an numeric id wich is associated with a certain string. + (g_quark_try_string): try to get the quark associated with this string, + if the lookup failed return 0. + (g_quark_from_string): get the associated quark for a string, if there + isn't currently a GQuark associated with this string, then allocate a + new quark and return that. + (g_quark_from_static_string): like the above function, but the string + isn't strdup()ed to save memory. + (g_quark_to_string): get the string that is associated with a certain + GQuark. + + * gdataset.c (g_dataset_id_set_data_full): invoke the destroy function + _after_ the new data has been setup. + Thu Jun 18 02:35:21 1998 Owen Taylor * glib.h: Changed messages for g_return_[val]_if_fail to diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 97a10c3..4d3558c 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,21 @@ +Fri Jun 19 03:11:02 1998 Tim Janik + + * gdataset.c: removed g_dataset_try_key, g_dataset_force_id and + g_dataset_retrive_key in favour of GQuarks. + a GQuark is an numeric id wich is associated with a certain string. + (g_quark_try_string): try to get the quark associated with this string, + if the lookup failed return 0. + (g_quark_from_string): get the associated quark for a string, if there + isn't currently a GQuark associated with this string, then allocate a + new quark and return that. + (g_quark_from_static_string): like the above function, but the string + isn't strdup()ed to save memory. + (g_quark_to_string): get the string that is associated with a certain + GQuark. + + * gdataset.c (g_dataset_id_set_data_full): invoke the destroy function + _after_ the new data has been setup. + Thu Jun 18 02:35:21 1998 Owen Taylor * glib.h: Changed messages for g_return_[val]_if_fail to diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 97a10c3..4d3558c 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,21 @@ +Fri Jun 19 03:11:02 1998 Tim Janik + + * gdataset.c: removed g_dataset_try_key, g_dataset_force_id and + g_dataset_retrive_key in favour of GQuarks. + a GQuark is an numeric id wich is associated with a certain string. + (g_quark_try_string): try to get the quark associated with this string, + if the lookup failed return 0. + (g_quark_from_string): get the associated quark for a string, if there + isn't currently a GQuark associated with this string, then allocate a + new quark and return that. + (g_quark_from_static_string): like the above function, but the string + isn't strdup()ed to save memory. + (g_quark_to_string): get the string that is associated with a certain + GQuark. + + * gdataset.c (g_dataset_id_set_data_full): invoke the destroy function + _after_ the new data has been setup. + Thu Jun 18 02:35:21 1998 Owen Taylor * glib.h: Changed messages for g_return_[val]_if_fail to diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 97a10c3..4d3558c 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,21 @@ +Fri Jun 19 03:11:02 1998 Tim Janik + + * gdataset.c: removed g_dataset_try_key, g_dataset_force_id and + g_dataset_retrive_key in favour of GQuarks. + a GQuark is an numeric id wich is associated with a certain string. + (g_quark_try_string): try to get the quark associated with this string, + if the lookup failed return 0. + (g_quark_from_string): get the associated quark for a string, if there + isn't currently a GQuark associated with this string, then allocate a + new quark and return that. + (g_quark_from_static_string): like the above function, but the string + isn't strdup()ed to save memory. + (g_quark_to_string): get the string that is associated with a certain + GQuark. + + * gdataset.c (g_dataset_id_set_data_full): invoke the destroy function + _after_ the new data has been setup. + Thu Jun 18 02:35:21 1998 Owen Taylor * glib.h: Changed messages for g_return_[val]_if_fail to diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 97a10c3..4d3558c 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,21 @@ +Fri Jun 19 03:11:02 1998 Tim Janik + + * gdataset.c: removed g_dataset_try_key, g_dataset_force_id and + g_dataset_retrive_key in favour of GQuarks. + a GQuark is an numeric id wich is associated with a certain string. + (g_quark_try_string): try to get the quark associated with this string, + if the lookup failed return 0. + (g_quark_from_string): get the associated quark for a string, if there + isn't currently a GQuark associated with this string, then allocate a + new quark and return that. + (g_quark_from_static_string): like the above function, but the string + isn't strdup()ed to save memory. + (g_quark_to_string): get the string that is associated with a certain + GQuark. + + * gdataset.c (g_dataset_id_set_data_full): invoke the destroy function + _after_ the new data has been setup. + Thu Jun 18 02:35:21 1998 Owen Taylor * glib.h: Changed messages for g_return_[val]_if_fail to diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 97a10c3..4d3558c 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,21 @@ +Fri Jun 19 03:11:02 1998 Tim Janik + + * gdataset.c: removed g_dataset_try_key, g_dataset_force_id and + g_dataset_retrive_key in favour of GQuarks. + a GQuark is an numeric id wich is associated with a certain string. + (g_quark_try_string): try to get the quark associated with this string, + if the lookup failed return 0. + (g_quark_from_string): get the associated quark for a string, if there + isn't currently a GQuark associated with this string, then allocate a + new quark and return that. + (g_quark_from_static_string): like the above function, but the string + isn't strdup()ed to save memory. + (g_quark_to_string): get the string that is associated with a certain + GQuark. + + * gdataset.c (g_dataset_id_set_data_full): invoke the destroy function + _after_ the new data has been setup. + Thu Jun 18 02:35:21 1998 Owen Taylor * glib.h: Changed messages for g_return_[val]_if_fail to diff --git a/gdataset.c b/gdataset.c index 7392270..85f0b7f 100644 --- a/gdataset.c +++ b/gdataset.c @@ -51,19 +51,17 @@ struct _GDataset static inline GDataset* g_dataset_lookup (gconstpointer dataset_location); static inline void g_dataset_destroy_i (GDataset *dataset); static void g_dataset_initialize (void); -static void g_dataset_alloc_key (const gchar *string, - guint **id, - gchar **key); - +static inline GQuark g_quark_new (const gchar *string); + /* --- variables --- */ -static GHashTable *g_dataset_location_ht = NULL; -static GHashTable *g_dataset_key_ht = NULL; -static GDataset *g_dataset_cached = NULL; -static GMemChunk *g_dataset_mem_chunk = NULL; -static GMemChunk *g_dataset_data_mem_chunk = NULL; -static gchar **g_dataset_key_array = NULL; -static guint g_dataset_seq_id = 0; +static GHashTable *g_quark_ht = NULL; +static gchar **g_quarks = NULL; +static GQuark g_quark_seq_id = 0; +static GHashTable *g_dataset_location_ht = NULL; +static GDataset *g_dataset_cached = NULL; +static GMemChunk *g_dataset_mem_chunk = NULL; +static GMemChunk *g_dataset_data_mem_chunk = NULL; @@ -126,7 +124,7 @@ g_dataset_destroy (gconstpointer dataset_location) void g_dataset_id_set_destroy (gconstpointer dataset_location, - guint key_id, + GQuark key_id, GDestroyNotify destroy_func) { g_return_if_fail (dataset_location != NULL); @@ -155,7 +153,7 @@ g_dataset_id_set_destroy (gconstpointer dataset_location, gpointer g_dataset_id_get_data (gconstpointer dataset_location, - guint key_id) + GQuark key_id) { g_return_val_if_fail (dataset_location != NULL, NULL); @@ -179,7 +177,7 @@ g_dataset_id_get_data (gconstpointer dataset_location, void g_dataset_id_set_data_full (gconstpointer dataset_location, - guint key_id, + GQuark key_id, gpointer data, GDestroyNotify destroy_func) { @@ -226,7 +224,8 @@ g_dataset_id_set_data_full (gconstpointer dataset_location, list->destroy_func (list->data); g_mem_chunk_free (g_dataset_data_mem_chunk, list); - break; + + return; } prev = list; @@ -235,32 +234,31 @@ g_dataset_id_set_data_full (gconstpointer dataset_location, } else { - register GDatasetData *prev; - - prev = NULL; while (list) { if (list->id == key_id) { - if (prev) - prev->next = list->next; - else - dataset->data_list = list->next; + register GDestroyNotify dfunc; + register gpointer ddata; - /* we need to have unlinked before invoking the destroy function - */ - if (list->destroy_func) - list->destroy_func (list->data); + dfunc = list->destroy_func; + ddata = list->data; + list->data = data; + list->destroy_func = destroy_func; - break; + /* we need to have updated all structures prior to + * invokation of the destroy function + */ + if (dfunc) + dfunc (ddata); + + return; } - prev = list; list = list->next; } - if (!list) - list = g_chunk_new (GDatasetData, g_dataset_data_mem_chunk); + list = g_chunk_new (GDatasetData, g_dataset_data_mem_chunk); list->next = dataset->data_list; list->id = key_id; list->data = data; @@ -269,52 +267,13 @@ g_dataset_id_set_data_full (gconstpointer dataset_location, } } -guint -g_dataset_try_key (const gchar *key) -{ - register guint *id; - g_return_val_if_fail (key != NULL, 0); - - if (g_dataset_key_ht) - { - id = g_hash_table_lookup (g_dataset_key_ht, (gpointer) key); - - if (id) - return *id; - } - - return 0; -} - -guint -g_dataset_force_id (const gchar *key) -{ - guint *id; - - g_return_val_if_fail (key != NULL, 0); - - if (!g_dataset_key_ht) - g_dataset_initialize (); - - id = g_hash_table_lookup (g_dataset_key_ht, (gpointer) key); - if (!id) - { - gchar *new_key; - - g_dataset_alloc_key (key, &id, &new_key); - g_hash_table_insert (g_dataset_key_ht, new_key, id); - } - - return *id; -} - static void g_dataset_initialize (void) { if (!g_dataset_location_ht) { + g_quark_ht = g_hash_table_new (g_str_hash, g_str_equal); g_dataset_location_ht = g_hash_table_new (g_direct_hash, NULL); - g_dataset_key_ht = g_hash_table_new (g_str_hash, g_str_equal); g_dataset_cached = NULL; g_dataset_mem_chunk = g_mem_chunk_new ("GDataset MemChunk", @@ -329,28 +288,74 @@ g_dataset_initialize (void) } } +GQuark +g_quark_try_string (const gchar *string) +{ + g_return_val_if_fail (string != NULL, 0); + + if (g_quark_ht) + return (gulong) g_hash_table_lookup (g_quark_ht, string); + else + return 0; +} + +GQuark +g_quark_from_string (const gchar *string) +{ + GQuark quark; + + g_return_val_if_fail (string != NULL, 0); + + if (!g_quark_ht) + g_dataset_initialize (); + + quark = (gulong) g_hash_table_lookup (g_quark_ht, string); + if (!quark) + quark = g_quark_new (g_strdup (string)); + + return quark; +} + +GQuark +g_quark_from_static_string (const gchar *string) +{ + GQuark quark; + + g_return_val_if_fail (string != NULL, 0); + + if (!g_quark_ht) + g_dataset_initialize (); + + quark = (gulong) g_hash_table_lookup (g_quark_ht, string); + if (!quark) + quark = g_quark_new (string); + + return quark; +} + gchar* -g_dataset_retrive_key (guint id) +g_quark_to_string (GQuark quark) { - if (id > 0 && id <= g_dataset_seq_id) - return g_dataset_key_array[id - 1]; - return NULL; + if (quark > 0 && quark <= g_quark_seq_id) + return g_quarks[quark - 1]; + else + return NULL; } -static void -g_dataset_alloc_key (const gchar *string, - guint **id, - gchar **key) +static inline GQuark +g_quark_new (const gchar *string) { - if (g_dataset_seq_id % G_DATASET_BLOCK_SIZE == 0) - g_dataset_key_array = g_realloc (g_dataset_key_array, - (g_dataset_seq_id + G_DATASET_BLOCK_SIZE) * sizeof (gchar*)); + GQuark quark; + + if (g_quark_seq_id % G_DATASET_BLOCK_SIZE == 0) + g_quarks = g_realloc (g_quarks, + (g_quark_seq_id + G_DATASET_BLOCK_SIZE) * sizeof (gchar*)); + + + g_quarks[g_quark_seq_id] = (gchar*) string; + g_quark_seq_id++; + quark = g_quark_seq_id; + g_hash_table_insert (g_quark_ht, (gchar*) string, GUINT_TO_POINTER (quark)); - *key = g_new (gchar, sizeof (guint) + strlen (string) + 1); - *id = (guint*) *key; - *key += sizeof (guint); - strcpy (*key, string); - g_dataset_key_array[g_dataset_seq_id] = *key; - g_dataset_seq_id++; - **id = g_dataset_seq_id; + return quark; } diff --git a/glib.h b/glib.h index ba44691..0d0f1da 100644 --- a/glib.h +++ b/glib.h @@ -436,7 +436,7 @@ typedef unsigned long guint32; typedef gint32 gssize; typedef guint32 gsize; typedef gint32 gtime; - +typedef guint32 GQuark; typedef struct _GList GList; typedef struct _GSList GSList; @@ -975,28 +975,31 @@ gint g_direct_equal (gconstpointer v, gconstpointer v2); +/* Quarks (string<->id association) + */ +GQuark g_quark_try_string (const gchar *string); +GQuark g_quark_from_static_string (const gchar *string); +GQuark g_quark_from_string (const gchar *string); +gchar* g_quark_to_string (GQuark quark); /* Location Associated Data */ void g_dataset_destroy (gconstpointer dataset_location); -guint g_dataset_try_key (const gchar *key); -guint g_dataset_force_id (const gchar *key); -gchar* g_dataset_retrive_key (guint key_id); gpointer g_dataset_id_get_data (gconstpointer dataset_location, - guint key_id); + GQuark key_id); void g_dataset_id_set_data_full (gconstpointer dataset_location, - guint key_id, + GQuark key_id, gpointer data, GDestroyNotify destroy_func); void g_dataset_id_set_destroy (gconstpointer dataset_location, - guint key_id, + GQuark key_id, GDestroyNotify destroy_func); #define g_dataset_id_set_data(l,k,d) G_STMT_START{g_dataset_id_set_data_full((l),(k),(d),NULL);}G_STMT_END #define g_dataset_id_remove_data(l,k) G_STMT_START{g_dataset_id_set_data((l),(k),NULL);}G_STMT_END -#define g_dataset_get_data(l,k) (g_dataset_id_get_data((l),g_dataset_try_key(k))) -#define g_dataset_set_data_full(l,k,d,f) G_STMT_START{g_dataset_id_set_data_full((l),g_dataset_force_id(k),(d),(f));}G_STMT_END -#define g_dataset_set_destroy(l,k,f) G_STMT_START{g_dataset_id_set_destroy((l),g_dataset_force_id(k),(f));}G_STMT_END +#define g_dataset_get_data(l,k) (g_dataset_id_get_data((l),g_quark_try_string(k))) +#define g_dataset_set_data_full(l,k,d,f) G_STMT_START{g_dataset_id_set_data_full((l),g_quark_from_string(k),(d),(f));}G_STMT_END +#define g_dataset_set_destroy(l,k,f) G_STMT_START{g_dataset_id_set_destroy((l),g_quark_from_string(k),(f));}G_STMT_END #define g_dataset_set_data(l,k,d) G_STMT_START{g_dataset_set_data_full((l),(k),(d),NULL);}G_STMT_END #define g_dataset_remove_data(l,k) G_STMT_START{g_dataset_set_data((l),(k),NULL);}G_STMT_END diff --git a/glib/gdataset.c b/glib/gdataset.c index 7392270..85f0b7f 100644 --- a/glib/gdataset.c +++ b/glib/gdataset.c @@ -51,19 +51,17 @@ struct _GDataset static inline GDataset* g_dataset_lookup (gconstpointer dataset_location); static inline void g_dataset_destroy_i (GDataset *dataset); static void g_dataset_initialize (void); -static void g_dataset_alloc_key (const gchar *string, - guint **id, - gchar **key); - +static inline GQuark g_quark_new (const gchar *string); + /* --- variables --- */ -static GHashTable *g_dataset_location_ht = NULL; -static GHashTable *g_dataset_key_ht = NULL; -static GDataset *g_dataset_cached = NULL; -static GMemChunk *g_dataset_mem_chunk = NULL; -static GMemChunk *g_dataset_data_mem_chunk = NULL; -static gchar **g_dataset_key_array = NULL; -static guint g_dataset_seq_id = 0; +static GHashTable *g_quark_ht = NULL; +static gchar **g_quarks = NULL; +static GQuark g_quark_seq_id = 0; +static GHashTable *g_dataset_location_ht = NULL; +static GDataset *g_dataset_cached = NULL; +static GMemChunk *g_dataset_mem_chunk = NULL; +static GMemChunk *g_dataset_data_mem_chunk = NULL; @@ -126,7 +124,7 @@ g_dataset_destroy (gconstpointer dataset_location) void g_dataset_id_set_destroy (gconstpointer dataset_location, - guint key_id, + GQuark key_id, GDestroyNotify destroy_func) { g_return_if_fail (dataset_location != NULL); @@ -155,7 +153,7 @@ g_dataset_id_set_destroy (gconstpointer dataset_location, gpointer g_dataset_id_get_data (gconstpointer dataset_location, - guint key_id) + GQuark key_id) { g_return_val_if_fail (dataset_location != NULL, NULL); @@ -179,7 +177,7 @@ g_dataset_id_get_data (gconstpointer dataset_location, void g_dataset_id_set_data_full (gconstpointer dataset_location, - guint key_id, + GQuark key_id, gpointer data, GDestroyNotify destroy_func) { @@ -226,7 +224,8 @@ g_dataset_id_set_data_full (gconstpointer dataset_location, list->destroy_func (list->data); g_mem_chunk_free (g_dataset_data_mem_chunk, list); - break; + + return; } prev = list; @@ -235,32 +234,31 @@ g_dataset_id_set_data_full (gconstpointer dataset_location, } else { - register GDatasetData *prev; - - prev = NULL; while (list) { if (list->id == key_id) { - if (prev) - prev->next = list->next; - else - dataset->data_list = list->next; + register GDestroyNotify dfunc; + register gpointer ddata; - /* we need to have unlinked before invoking the destroy function - */ - if (list->destroy_func) - list->destroy_func (list->data); + dfunc = list->destroy_func; + ddata = list->data; + list->data = data; + list->destroy_func = destroy_func; - break; + /* we need to have updated all structures prior to + * invokation of the destroy function + */ + if (dfunc) + dfunc (ddata); + + return; } - prev = list; list = list->next; } - if (!list) - list = g_chunk_new (GDatasetData, g_dataset_data_mem_chunk); + list = g_chunk_new (GDatasetData, g_dataset_data_mem_chunk); list->next = dataset->data_list; list->id = key_id; list->data = data; @@ -269,52 +267,13 @@ g_dataset_id_set_data_full (gconstpointer dataset_location, } } -guint -g_dataset_try_key (const gchar *key) -{ - register guint *id; - g_return_val_if_fail (key != NULL, 0); - - if (g_dataset_key_ht) - { - id = g_hash_table_lookup (g_dataset_key_ht, (gpointer) key); - - if (id) - return *id; - } - - return 0; -} - -guint -g_dataset_force_id (const gchar *key) -{ - guint *id; - - g_return_val_if_fail (key != NULL, 0); - - if (!g_dataset_key_ht) - g_dataset_initialize (); - - id = g_hash_table_lookup (g_dataset_key_ht, (gpointer) key); - if (!id) - { - gchar *new_key; - - g_dataset_alloc_key (key, &id, &new_key); - g_hash_table_insert (g_dataset_key_ht, new_key, id); - } - - return *id; -} - static void g_dataset_initialize (void) { if (!g_dataset_location_ht) { + g_quark_ht = g_hash_table_new (g_str_hash, g_str_equal); g_dataset_location_ht = g_hash_table_new (g_direct_hash, NULL); - g_dataset_key_ht = g_hash_table_new (g_str_hash, g_str_equal); g_dataset_cached = NULL; g_dataset_mem_chunk = g_mem_chunk_new ("GDataset MemChunk", @@ -329,28 +288,74 @@ g_dataset_initialize (void) } } +GQuark +g_quark_try_string (const gchar *string) +{ + g_return_val_if_fail (string != NULL, 0); + + if (g_quark_ht) + return (gulong) g_hash_table_lookup (g_quark_ht, string); + else + return 0; +} + +GQuark +g_quark_from_string (const gchar *string) +{ + GQuark quark; + + g_return_val_if_fail (string != NULL, 0); + + if (!g_quark_ht) + g_dataset_initialize (); + + quark = (gulong) g_hash_table_lookup (g_quark_ht, string); + if (!quark) + quark = g_quark_new (g_strdup (string)); + + return quark; +} + +GQuark +g_quark_from_static_string (const gchar *string) +{ + GQuark quark; + + g_return_val_if_fail (string != NULL, 0); + + if (!g_quark_ht) + g_dataset_initialize (); + + quark = (gulong) g_hash_table_lookup (g_quark_ht, string); + if (!quark) + quark = g_quark_new (string); + + return quark; +} + gchar* -g_dataset_retrive_key (guint id) +g_quark_to_string (GQuark quark) { - if (id > 0 && id <= g_dataset_seq_id) - return g_dataset_key_array[id - 1]; - return NULL; + if (quark > 0 && quark <= g_quark_seq_id) + return g_quarks[quark - 1]; + else + return NULL; } -static void -g_dataset_alloc_key (const gchar *string, - guint **id, - gchar **key) +static inline GQuark +g_quark_new (const gchar *string) { - if (g_dataset_seq_id % G_DATASET_BLOCK_SIZE == 0) - g_dataset_key_array = g_realloc (g_dataset_key_array, - (g_dataset_seq_id + G_DATASET_BLOCK_SIZE) * sizeof (gchar*)); + GQuark quark; + + if (g_quark_seq_id % G_DATASET_BLOCK_SIZE == 0) + g_quarks = g_realloc (g_quarks, + (g_quark_seq_id + G_DATASET_BLOCK_SIZE) * sizeof (gchar*)); + + + g_quarks[g_quark_seq_id] = (gchar*) string; + g_quark_seq_id++; + quark = g_quark_seq_id; + g_hash_table_insert (g_quark_ht, (gchar*) string, GUINT_TO_POINTER (quark)); - *key = g_new (gchar, sizeof (guint) + strlen (string) + 1); - *id = (guint*) *key; - *key += sizeof (guint); - strcpy (*key, string); - g_dataset_key_array[g_dataset_seq_id] = *key; - g_dataset_seq_id++; - **id = g_dataset_seq_id; + return quark; } diff --git a/glib/glib.h b/glib/glib.h index ba44691..0d0f1da 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -436,7 +436,7 @@ typedef unsigned long guint32; typedef gint32 gssize; typedef guint32 gsize; typedef gint32 gtime; - +typedef guint32 GQuark; typedef struct _GList GList; typedef struct _GSList GSList; @@ -975,28 +975,31 @@ gint g_direct_equal (gconstpointer v, gconstpointer v2); +/* Quarks (string<->id association) + */ +GQuark g_quark_try_string (const gchar *string); +GQuark g_quark_from_static_string (const gchar *string); +GQuark g_quark_from_string (const gchar *string); +gchar* g_quark_to_string (GQuark quark); /* Location Associated Data */ void g_dataset_destroy (gconstpointer dataset_location); -guint g_dataset_try_key (const gchar *key); -guint g_dataset_force_id (const gchar *key); -gchar* g_dataset_retrive_key (guint key_id); gpointer g_dataset_id_get_data (gconstpointer dataset_location, - guint key_id); + GQuark key_id); void g_dataset_id_set_data_full (gconstpointer dataset_location, - guint key_id, + GQuark key_id, gpointer data, GDestroyNotify destroy_func); void g_dataset_id_set_destroy (gconstpointer dataset_location, - guint key_id, + GQuark key_id, GDestroyNotify destroy_func); #define g_dataset_id_set_data(l,k,d) G_STMT_START{g_dataset_id_set_data_full((l),(k),(d),NULL);}G_STMT_END #define g_dataset_id_remove_data(l,k) G_STMT_START{g_dataset_id_set_data((l),(k),NULL);}G_STMT_END -#define g_dataset_get_data(l,k) (g_dataset_id_get_data((l),g_dataset_try_key(k))) -#define g_dataset_set_data_full(l,k,d,f) G_STMT_START{g_dataset_id_set_data_full((l),g_dataset_force_id(k),(d),(f));}G_STMT_END -#define g_dataset_set_destroy(l,k,f) G_STMT_START{g_dataset_id_set_destroy((l),g_dataset_force_id(k),(f));}G_STMT_END +#define g_dataset_get_data(l,k) (g_dataset_id_get_data((l),g_quark_try_string(k))) +#define g_dataset_set_data_full(l,k,d,f) G_STMT_START{g_dataset_id_set_data_full((l),g_quark_from_string(k),(d),(f));}G_STMT_END +#define g_dataset_set_destroy(l,k,f) G_STMT_START{g_dataset_id_set_destroy((l),g_quark_from_string(k),(f));}G_STMT_END #define g_dataset_set_data(l,k,d) G_STMT_START{g_dataset_set_data_full((l),(k),(d),NULL);}G_STMT_END #define g_dataset_remove_data(l,k) G_STMT_START{g_dataset_set_data((l),(k),NULL);}G_STMT_END -- 2.7.4