summaryrefslogtreecommitdiff
path: root/gdataset.c
blob: 85f0b7f1e4fd3d870986e82f46334e11ae1861e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* 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        <string.h>
#include	"glib.h"



/* --- defines --- */
#define	G_DATASET_BLOCK_SIZE			(512)
#define	G_DATASET_MEM_CHUNK_PREALLOC		(64)
#define	G_DATASET_DATA_MEM_CHUNK_PREALLOC	(128)


/* --- structures --- */
typedef struct _GDatasetData GDatasetData;
typedef struct _GDataset GDataset;
struct _GDatasetData
{
  GDatasetData *next;
  guint id;
  gpointer data;
  GDestroyNotify destroy_func;
};

struct _GDataset
{
  gconstpointer location;
  GDatasetData *data_list;
};


/* --- prototypes --- */
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 inline GQuark	g_quark_new		(const gchar  *string);


/* --- variables --- */
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;



/* --- functions --- */
static inline GDataset*
g_dataset_lookup (gconstpointer	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 (gconstpointer  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 (gconstpointer  dataset_location,
			  GQuark         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 (gconstpointer  dataset_location,
		       GQuark         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 (gconstpointer  dataset_location,
			    GQuark         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, 
			   (gpointer) dataset->location, /* Yuck */
			   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);

	      return;
	    }
	  
	  prev = list;
	  list = list->next;
	}
    }
  else
    {
      while (list)
	{
	  if (list->id == key_id)
	    {
	      register GDestroyNotify dfunc;
	      register gpointer ddata;
	      
	      dfunc = list->destroy_func;
	      ddata = list->data;
	      list->data = data;
	      list->destroy_func = destroy_func;
	      
	      /* we need to have updated all structures prior to
	       * invokation of the destroy function
	       */
	      if (dfunc)
		dfunc (ddata);

	      return;
	    }
	  
	  list = list->next;
	}
      
      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;
    }
}

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_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);
    }
}

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_quark_to_string (GQuark quark)
{
  if (quark > 0 && quark <= g_quark_seq_id)
    return g_quarks[quark - 1];
  else
    return NULL;
}

static inline GQuark
g_quark_new (const gchar *string)
{
  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));
  
  return quark;
}