new file, gdatasets implement the object data mechanism from GtkObject. a

Mon May 11 01:44:10 1998  Tim Janik  <timj@gtk.org>

        * gdataset.c: new file, gdatasets implement the object data
        mechanism from GtkObject. a generic data pointer is associated with
        a certain location and a key id.

Mon May 11 02:31:19 1998  Tim Janik  <timj@gtk.org>

        * gtk/gtkobject.h:
        * gtk/gtkobject.c:
        (gtk_object_data_try_key):
        (gtk_object_data_force_id): these are now macros substituting the old
        functions. we just use the corresponding g_dataset_* functions for
        key->id associations. (this is to assure unique key<->id associations).
This commit is contained in:
Tim Janik 1998-05-11 00:48:44 +00:00 committed by Tim Janik
parent cb12593d57
commit 01003779e9
14 changed files with 456 additions and 67 deletions

View File

@ -1,3 +1,12 @@
Mon May 11 02:31:19 1998 Tim Janik <timj@gtk.org>
* gtk/gtkobject.h:
* gtk/gtkobject.c:
(gtk_object_data_try_key):
(gtk_object_data_force_id): these are now macros substituting the old
functions. we just use the corresponding g_dataset_* functions for
key->id associations. (this is to assure unique key<->id associations).
1998-05-10 Stefan Jeske <stefan@gtk.org>
* gtk/gtkctree.c (gtk_ctree_is_visible): Changed return value

View File

@ -1,3 +1,12 @@
Mon May 11 02:31:19 1998 Tim Janik <timj@gtk.org>
* gtk/gtkobject.h:
* gtk/gtkobject.c:
(gtk_object_data_try_key):
(gtk_object_data_force_id): these are now macros substituting the old
functions. we just use the corresponding g_dataset_* functions for
key->id associations. (this is to assure unique key<->id associations).
1998-05-10 Stefan Jeske <stefan@gtk.org>
* gtk/gtkctree.c (gtk_ctree_is_visible): Changed return value

View File

@ -1,3 +1,12 @@
Mon May 11 02:31:19 1998 Tim Janik <timj@gtk.org>
* gtk/gtkobject.h:
* gtk/gtkobject.c:
(gtk_object_data_try_key):
(gtk_object_data_force_id): these are now macros substituting the old
functions. we just use the corresponding g_dataset_* functions for
key->id associations. (this is to assure unique key<->id associations).
1998-05-10 Stefan Jeske <stefan@gtk.org>
* gtk/gtkctree.c (gtk_ctree_is_visible): Changed return value

View File

@ -1,3 +1,12 @@
Mon May 11 02:31:19 1998 Tim Janik <timj@gtk.org>
* gtk/gtkobject.h:
* gtk/gtkobject.c:
(gtk_object_data_try_key):
(gtk_object_data_force_id): these are now macros substituting the old
functions. we just use the corresponding g_dataset_* functions for
key->id associations. (this is to assure unique key<->id associations).
1998-05-10 Stefan Jeske <stefan@gtk.org>
* gtk/gtkctree.c (gtk_ctree_is_visible): Changed return value

View File

@ -1,3 +1,12 @@
Mon May 11 02:31:19 1998 Tim Janik <timj@gtk.org>
* gtk/gtkobject.h:
* gtk/gtkobject.c:
(gtk_object_data_try_key):
(gtk_object_data_force_id): these are now macros substituting the old
functions. we just use the corresponding g_dataset_* functions for
key->id associations. (this is to assure unique key<->id associations).
1998-05-10 Stefan Jeske <stefan@gtk.org>
* gtk/gtkctree.c (gtk_ctree_is_visible): Changed return value

View File

@ -1,3 +1,12 @@
Mon May 11 02:31:19 1998 Tim Janik <timj@gtk.org>
* gtk/gtkobject.h:
* gtk/gtkobject.c:
(gtk_object_data_try_key):
(gtk_object_data_force_id): these are now macros substituting the old
functions. we just use the corresponding g_dataset_* functions for
key->id associations. (this is to assure unique key<->id associations).
1998-05-10 Stefan Jeske <stefan@gtk.org>
* gtk/gtkctree.c (gtk_ctree_is_visible): Changed return value

View File

@ -1,3 +1,12 @@
Mon May 11 02:31:19 1998 Tim Janik <timj@gtk.org>
* gtk/gtkobject.h:
* gtk/gtkobject.c:
(gtk_object_data_try_key):
(gtk_object_data_force_id): these are now macros substituting the old
functions. we just use the corresponding g_dataset_* functions for
key->id associations. (this is to assure unique key<->id associations).
1998-05-10 Stefan Jeske <stefan@gtk.org>
* gtk/gtkctree.c (gtk_ctree_is_visible): Changed return value

4
TODO
View File

@ -1,3 +1,7 @@
For 1.1.0 release:
- remove gtk_object_data_force_id and gtk_object_data_try_key from
gtkobject.c
Bugs:
* Scrolled windows (GtkList?) get cought in an endless reallocation loop
under certain (rare) circumstances.

View File

@ -1,3 +1,9 @@
Mon May 11 01:44:10 1998 Tim Janik <timj@gtk.org>
* gdataset.c: new file, gdatasets implement the object data
mechanism from GtkObject. a generic data pointer is associated with
a certain location and a key id.
Sat May 9 20:08:12 1998 Owen Taylor <otaylor@gtk.org>
* glib/gmem.c: Experimentally restore GMemChunk

View File

@ -7,6 +7,7 @@ lib_LTLIBRARIES = libglib.la
libglib_la_SOURCES = \
garray.c \
gcache.c \
gdataset.c \
gerror.c \
ghash.c \
glist.c \

339
glib/gdataset.c Normal file
View File

@ -0,0 +1,339 @@
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* gdataset.c: Generic dataset mechanism, similar to GtkObject data.
* Copyright (C) 1998 Tim Janik
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
#include "glib.h"
/* --- defines --- */
#define G_DATASET_ID_BLOCK_SIZE (1024)
#define G_DATASET_MEM_CHUNK_PREALLOC (512)
#define G_DATASET_DATA_MEM_CHUNK_PREALLOC (1024)
/* --- structures --- */
typedef struct _GDatasetData GDatasetData;
typedef struct _GDataset GDataset;
struct _GDatasetData
{
GDatasetData *next;
guint id;
gpointer data;
GDestroyNotify destroy_func;
};
struct _GDataset
{
gpointer location;
GDatasetData *data_list;
};
/* --- prototypes --- */
static inline GDataset* g_dataset_lookup (const gpointer dataset_location);
static inline void g_dataset_destroy_i (GDataset *dataset);
static void g_dataset_initialize (void);
static guint* g_dataset_id_new (void);
/* --- 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 guint *g_dataset_id_block = NULL;
static guint g_dataset_id_index = G_DATASET_ID_BLOCK_SIZE + 1;
/* --- functions --- */
static inline GDataset*
g_dataset_lookup (const gpointer dataset_location)
{
register GDataset *dataset;
if (g_dataset_cached && g_dataset_cached->location == dataset_location)
return g_dataset_cached;
if (!g_dataset_location_ht)
g_dataset_initialize ();
dataset = g_hash_table_lookup (g_dataset_location_ht, dataset_location);
if (dataset)
g_dataset_cached = dataset;
return dataset;
}
static inline void
g_dataset_destroy_i (GDataset *dataset)
{
register GDatasetData *list;
if (dataset == g_dataset_cached)
g_dataset_cached = NULL;
g_hash_table_remove (g_dataset_location_ht, dataset->location);
list = dataset->data_list;
g_mem_chunk_free (g_dataset_mem_chunk, dataset);
while (list)
{
register GDatasetData *prev;
prev = list;
list = prev->next;
if (prev->destroy_func)
prev->destroy_func (prev->data);
g_mem_chunk_free (g_dataset_data_mem_chunk, prev);
}
}
void
g_dataset_destroy (const gpointer dataset_location)
{
register GDataset *dataset;
g_return_if_fail (dataset_location != NULL);
dataset = g_dataset_lookup (dataset_location);
if (dataset)
g_dataset_destroy_i (dataset);
}
void
g_dataset_id_set_destroy (const gpointer dataset_location,
guint key_id,
GDestroyNotify destroy_func)
{
g_return_if_fail (dataset_location != NULL);
if (key_id)
{
register GDataset *dataset;
dataset = g_dataset_lookup (dataset_location);
if (dataset)
{
register GDatasetData *list;
list = dataset->data_list;
while (list)
{
if (list->id == key_id)
{
list->destroy_func = destroy_func;
return;
}
}
}
}
}
gpointer
g_dataset_id_get_data (const gpointer dataset_location,
guint key_id)
{
g_return_val_if_fail (dataset_location != NULL, NULL);
if (key_id)
{
register GDataset *dataset;
dataset = g_dataset_lookup (dataset_location);
if (dataset)
{
register GDatasetData *list;
for (list = dataset->data_list; list; list = list->next)
if (list->id == key_id)
return list->data;
}
}
return NULL;
}
void
g_dataset_id_set_data_full (const gpointer dataset_location,
guint key_id,
gpointer data,
GDestroyNotify destroy_func)
{
register GDataset *dataset;
register GDatasetData *list;
g_return_if_fail (dataset_location != NULL);
g_return_if_fail (key_id > 0);
dataset = g_dataset_lookup (dataset_location);
if (!dataset)
{
dataset = g_chunk_new (GDataset, g_dataset_mem_chunk);
dataset->location = dataset_location;
dataset->data_list = NULL;
g_hash_table_insert (g_dataset_location_ht, dataset->location, dataset);
}
list = dataset->data_list;
if (!data)
{
register GDatasetData *prev;
prev = NULL;
while (list)
{
if (list->id == key_id)
{
if (prev)
prev->next = list->next;
else
{
dataset->data_list = list->next;
if (!dataset->data_list)
g_dataset_destroy_i (dataset);
}
/* we need to have unlinked before invoking the destroy function
*/
if (list->destroy_func)
list->destroy_func (list->data);
g_mem_chunk_free (g_dataset_data_mem_chunk, list);
break;
}
prev = list;
list = list->next;
}
}
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;
/* we need to have unlinked before invoking the destroy function
*/
if (list->destroy_func)
list->destroy_func (list->data);
break;
}
prev = list;
list = list->next;
}
if (!list)
list = g_chunk_new (GDatasetData, g_dataset_data_mem_chunk);
list->next = dataset->data_list;
list->id = key_id;
list->data = data;
list->destroy_func = destroy_func;
dataset->data_list = list;
}
}
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)
{
register 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)
{
id = g_dataset_id_new ();
g_hash_table_insert (g_dataset_key_ht, g_strdup (key), id);
}
return *id;
}
static void
g_dataset_initialize (void)
{
if (!g_dataset_location_ht)
{
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",
sizeof (GDataset),
sizeof (GDataset) * G_DATASET_MEM_CHUNK_PREALLOC,
G_ALLOC_AND_FREE);
g_dataset_data_mem_chunk =
g_mem_chunk_new ("GDatasetData MemChunk",
sizeof (GDatasetData),
sizeof (GDatasetData) * G_DATASET_DATA_MEM_CHUNK_PREALLOC,
G_ALLOC_AND_FREE);
}
}
static guint*
g_dataset_id_new (void)
{
static guint seq_id = 1;
register guint *id;
if (g_dataset_id_index >= G_DATASET_ID_BLOCK_SIZE)
{
g_dataset_id_block = g_new (guint, G_DATASET_ID_BLOCK_SIZE);
g_dataset_id_index = 0;
}
id = &g_dataset_id_block[g_dataset_id_index++];
*id = seq_id++;
return id;
}

View File

@ -397,7 +397,8 @@ typedef void (*GPrintFunc) (gchar *str);
typedef void (*GScannerMsgFunc) (GScanner *scanner,
gchar *message,
gint error);
typedef void (*GDestroyNotify) (gpointer data);
struct _GList
{
@ -817,6 +818,30 @@ guint g_str_hash (const gpointer v);
guint g_direct_hash (gpointer key);
/* Associated Data
*/
void g_dataset_destroy (const gpointer dataset_location);
guint g_dataset_try_key (const gchar *key);
guint g_dataset_force_id (const gchar *key);
gpointer g_dataset_id_get_data (const gpointer dataset_location,
guint key_id);
void g_dataset_id_set_data_full (const gpointer dataset_location,
guint key_id,
gpointer data,
GDestroyNotify destroy_func);
void g_dataset_id_set_destroy (const gpointer dataset_location,
guint 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_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
/* GScanner: Flexible lexical scanner for general purpose.
*/

View File

@ -74,8 +74,6 @@ static void gtk_object_real_destroy (GtkObject *object);
static void gtk_object_finalize (GtkObject *object);
static void gtk_object_notify_weaks (GtkObject *object);
static guint* gtk_object_data_id_alloc (void);
GtkArg* gtk_object_collect_args (guint *nargs,
va_list args1,
va_list args2);
@ -89,9 +87,6 @@ static guint user_data_key_id = 0;
static const gchar *weakrefs_key = "gtk-weakrefs";
static guint weakrefs_key_id = 0;
static GHashTable *object_data_ht = NULL;
static GSList *object_data_id_list = NULL;
static guint object_data_id_index = 0;
static GtkObjectData *gtk_object_data_free_list = NULL;
#define GTK_OBJECT_DATA_DESTROY( odata ) { \
@ -1094,26 +1089,6 @@ gtk_object_set_data_full (GtkObject *object,
gtk_object_set_data_by_id_full (object, gtk_object_data_force_id (key), data, destroy);
}
guint
gtk_object_data_force_id (const gchar *key)
{
guint *id;
g_return_val_if_fail (key != NULL, 0);
if (!object_data_ht)
object_data_ht = g_hash_table_new (g_str_hash, g_str_equal);
id = g_hash_table_lookup (object_data_ht, (gpointer) key);
if (!id)
{
id = gtk_object_data_id_alloc ();
g_hash_table_insert (object_data_ht, g_strdup (key), id);
}
return *id;
}
gpointer
gtk_object_get_data_by_id (GtkObject *object,
guint data_id)
@ -1152,23 +1127,6 @@ gtk_object_get_data (GtkObject *object,
return NULL;
}
guint
gtk_object_data_try_key (const gchar *key)
{
g_return_val_if_fail (key != NULL, 0);
if (object_data_ht)
{
guint *id;
id = g_hash_table_lookup (object_data_ht, (gpointer) key);
if (id)
return *id;
}
return 0;
}
void
gtk_object_remove_data_by_id (GtkObject *object,
guint data_id)
@ -1190,28 +1148,6 @@ gtk_object_remove_data (GtkObject *object,
gtk_object_set_data_by_id_full (object, id, NULL, NULL);
}
static guint*
gtk_object_data_id_alloc (void)
{
static guint next_id = 1;
guint *ids;
if (!object_data_id_list ||
(object_data_id_index == GTK_OBJECT_DATA_ID_BLOCK_SIZE))
{
ids = g_new (guint, GTK_OBJECT_DATA_ID_BLOCK_SIZE);
object_data_id_index = 0;
object_data_id_list = g_slist_prepend (object_data_id_list, ids);
}
else
{
ids = object_data_id_list->data;
}
ids[object_data_id_index] = next_id++;
return &ids[object_data_id_index++];
}
/*****************************************
* gtk_object_set_user_data:
*
@ -1625,3 +1561,18 @@ gtk_trace_referencing (gpointer *o,
}
#endif /* G_ENABLE_DEBUG */
/* these functions are just in place to preserve binary compatibility,
* they should be removed when releasing 1.1.0
*/
#undef gtk_object_data_force_id
#undef gtk_object_data_try_key
guint
gtk_object_data_force_id (const gchar *key)
{
return g_dataset_force_id (key);
}
guint
gtk_object_data_try_key (const gchar *key)
{
return g_dataset_try_key (key);
}

View File

@ -336,8 +336,8 @@ gpointer gtk_object_get_data_by_id (GtkObject *object,
guint data_id);
void gtk_object_remove_data_by_id (GtkObject *object,
guint data_id);
guint gtk_object_data_try_key (const gchar *key);
guint gtk_object_data_force_id (const gchar *key);
#define gtk_object_data_try_key g_dataset_try_key
#define gtk_object_data_force_id g_dataset_force_id
/* Set the "user_data" object data field of "object". It should
* be noted that this is no different than calling 'gtk_object_set_data'