1
/* cairo - a vector graphics library with display and print output
2
 *
3
 * Copyright © 2004 Red Hat, Inc.
4
 * Copyright © 2005 Red Hat, Inc.
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is Red Hat, Inc.
32
 *
33
 * Contributor(s):
34
 *      Keith Packard <keithp@keithp.com>
35
 *	Graydon Hoare <graydon@redhat.com>
36
 *	Carl Worth <cworth@cworth.org>
37
 */
38

            
39
#include "cairoint.h"
40
#include "cairo-error-private.h"
41

            
42
/*
43
 * An entry can be in one of three states:
44
 *
45
 * FREE: Entry has never been used, terminates all searches.
46
 *       Appears in the table as a %NULL pointer.
47
 *
48
 * DEAD: Entry had been live in the past. A dead entry can be reused
49
 *       but does not terminate a search for an exact entry.
50
 *       Appears in the table as a pointer to DEAD_ENTRY.
51
 *
52
 * LIVE: Entry is currently being used.
53
 *       Appears in the table as any non-%NULL, non-DEAD_ENTRY pointer.
54
 */
55

            
56
#define DEAD_ENTRY ((cairo_hash_entry_t *) 0x1)
57

            
58
#define ENTRY_IS_FREE(entry) ((entry) == NULL)
59
#define ENTRY_IS_DEAD(entry) ((entry) == DEAD_ENTRY)
60
#define ENTRY_IS_LIVE(entry) ((entry) >  DEAD_ENTRY)
61

            
62
/*
63
 * This table is open-addressed with double hashing. Each table size
64
 * is a prime and it makes for the "first" hash modulus; a second
65
 * prime (2 less than the first prime) serves as the "second" hash
66
 * modulus, which is smaller and thus guarantees a complete
67
 * permutation of table indices.
68
 *
69
 * Hash tables are rehashed in order to keep between 12.5% and 50%
70
 * entries in the hash table alive and at least 25% free. When table
71
 * size is changed, the new table has about 25% live elements.
72
 *
73
 * The free entries guarantee an expected constant-time lookup.
74
 * Doubling/halving the table in the described fashion guarantees
75
 * amortized O(1) insertion/removal.
76
 *
77
 * This structure, and accompanying table, is borrowed/modified from the
78
 * file xserver/render/glyph.c in the freedesktop.org x server, with
79
 * permission (and suggested modification of doubling sizes) by Keith
80
 * Packard.
81
 */
82

            
83
static const unsigned long hash_table_sizes[] = {
84
    43,
85
    73,
86
    151,
87
    283,
88
    571,
89
    1153,
90
    2269,
91
    4519,
92
    9013,
93
    18043,
94
    36109,
95
    72091,
96
    144409,
97
    288361,
98
    576883,
99
    1153459,
100
    2307163,
101
    4613893,
102
    9227641,
103
    18455029,
104
    36911011,
105
    73819861,
106
    147639589,
107
    295279081,
108
    590559793
109
};
110

            
111
struct _cairo_hash_table {
112
    cairo_hash_keys_equal_func_t keys_equal;
113

            
114
    cairo_hash_entry_t *cache[32];
115

            
116
    const unsigned long *table_size;
117
    cairo_hash_entry_t **entries;
118

            
119
    unsigned long live_entries;
120
    unsigned long free_entries;
121
    unsigned long iterating;   /* Iterating, no insert, no resize */
122
};
123

            
124
/**
125
 * _cairo_hash_table_uid_keys_equal:
126
 * @key_a: the first key to be compared
127
 * @key_b: the second key to be compared
128
 *
129
 * Provides a #cairo_hash_keys_equal_func_t which always returns
130
 * %TRUE. This is useful to create hash tables using keys whose hash
131
 * completely describes the key, because in this special case
132
 * comparing the hashes is sufficient to guarantee that the keys are
133
 * equal.
134
 *
135
 * Return value: %TRUE.
136
 **/
137
static cairo_bool_t
138
816001
_cairo_hash_table_uid_keys_equal (const void *key_a, const void *key_b)
139
{
140
816001
    return TRUE;
141
}
142

            
143
/**
144
 * _cairo_hash_table_create:
145
 * @keys_equal: a function to return %TRUE if two keys are equal
146
 *
147
 * Creates a new hash table which will use the keys_equal() function
148
 * to compare hash keys. Data is provided to the hash table in the
149
 * form of user-derived versions of #cairo_hash_entry_t. A hash entry
150
 * must be able to hold both a key (including a hash code) and a
151
 * value. Sometimes only the key will be necessary, (as in
152
 * _cairo_hash_table_remove), and other times both a key and a value
153
 * will be necessary, (as in _cairo_hash_table_insert).
154
 *
155
 * If @keys_equal is %NULL, two keys will be considered equal if and
156
 * only if their hashes are equal.
157
 *
158
 * See #cairo_hash_entry_t for more details.
159
 *
160
 * Return value: the new hash table or %NULL if out of memory.
161
 **/
162
cairo_hash_table_t *
163
5439
_cairo_hash_table_create (cairo_hash_keys_equal_func_t keys_equal)
164
{
165
    cairo_hash_table_t *hash_table;
166

            
167
5439
    hash_table = _cairo_calloc (sizeof (cairo_hash_table_t));
168
5439
    if (unlikely (hash_table == NULL)) {
169
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
170
	return NULL;
171
    }
172

            
173
5439
    if (keys_equal == NULL)
174
1411
	hash_table->keys_equal = _cairo_hash_table_uid_keys_equal;
175
    else
176
4028
	hash_table->keys_equal = keys_equal;
177

            
178
5439
    memset (&hash_table->cache, 0, sizeof (hash_table->cache));
179
5439
    hash_table->table_size = &hash_table_sizes[0];
180

            
181
5439
    hash_table->entries = _cairo_calloc_ab (*hash_table->table_size,
182
				  sizeof (cairo_hash_entry_t *));
183
5439
    if (unlikely (hash_table->entries == NULL)) {
184
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
185
	free (hash_table);
186
	return NULL;
187
    }
188

            
189
5439
    hash_table->live_entries = 0;
190
5439
    hash_table->free_entries = *hash_table->table_size;
191
5439
    hash_table->iterating = 0;
192

            
193
5439
    return hash_table;
194
}
195

            
196
/**
197
 * _cairo_hash_table_destroy:
198
 * @hash_table: an empty hash table to destroy
199
 *
200
 * Immediately destroys the given hash table, freeing all resources
201
 * associated with it.
202
 *
203
 * WARNING: The hash_table must have no live entries in it before
204
 * _cairo_hash_table_destroy is called. It is a fatal error otherwise,
205
 * and this function will halt. The rationale for this behavior is to
206
 * avoid memory leaks and to avoid needless complication of the API
207
 * with destroy notify callbacks.
208
 *
209
 * WARNING: The hash_table must have no running iterators in it when
210
 * _cairo_hash_table_destroy is called. It is a fatal error otherwise,
211
 * and this function will halt.
212
 **/
213
void
214
443
_cairo_hash_table_destroy (cairo_hash_table_t *hash_table)
215
{
216
    /* The hash table must be empty. Otherwise, halt. */
217
443
    assert (hash_table->live_entries == 0);
218
    /* No iterators can be running. Otherwise, halt. */
219
443
    assert (hash_table->iterating == 0);
220

            
221
443
    free (hash_table->entries);
222
443
    free (hash_table);
223
443
}
224

            
225
static cairo_hash_entry_t **
226
13336
_cairo_hash_table_lookup_unique_key (cairo_hash_table_t *hash_table,
227
				     cairo_hash_entry_t *key)
228
{
229
    unsigned long table_size, i, idx, step;
230
    cairo_hash_entry_t **entry;
231

            
232
13336
    table_size = *hash_table->table_size;
233
13336
    idx = key->hash % table_size;
234

            
235
13336
    entry = &hash_table->entries[idx];
236
13336
    if (! ENTRY_IS_LIVE (*entry))
237
12705
	return entry;
238

            
239
631
    i = 1;
240
631
    step = 1 + key->hash % (table_size - 2);
241
    do {
242
819
	idx += step;
243
819
	if (idx >= table_size)
244
408
	    idx -= table_size;
245

            
246
819
	entry = &hash_table->entries[idx];
247
819
	if (! ENTRY_IS_LIVE (*entry))
248
631
	    return entry;
249
188
    } while (++i < table_size);
250

            
251
    ASSERT_NOT_REACHED;
252
    return NULL;
253
}
254

            
255
/**
256
 * _cairo_hash_table_manage:
257
 * @hash_table: a hash table
258
 *
259
 * Resize the hash table if the number of entries has gotten much
260
 * bigger or smaller than the ideal number of entries for the current
261
 * size and guarantee some free entries to be used as lookup
262
 * termination points.
263
 *
264
 * Return value: %CAIRO_STATUS_SUCCESS if successful or
265
 * %CAIRO_STATUS_NO_MEMORY if out of memory.
266
 **/
267
static cairo_status_t
268
13859
_cairo_hash_table_manage (cairo_hash_table_t *hash_table)
269
{
270
    cairo_hash_table_t tmp;
271
    unsigned long new_size, i;
272

            
273
    /* Keep between 12.5% and 50% entries in the hash table alive and
274
     * at least 25% free. */
275
13859
    unsigned long live_high = *hash_table->table_size >> 1;
276
13859
    unsigned long live_low = live_high >> 2;
277
13859
    unsigned long free_low = live_high >> 1;
278

            
279
13859
    tmp = *hash_table;
280

            
281
13859
    if (hash_table->live_entries > live_high)
282
    {
283
54
	tmp.table_size = hash_table->table_size + 1;
284
	/* This code is being abused if we can't make a table big enough. */
285
54
	assert (tmp.table_size - hash_table_sizes <
286
		ARRAY_LENGTH (hash_table_sizes));
287
    }
288
13805
    else if (hash_table->live_entries < live_low)
289
    {
290
	/* Can't shrink if we're at the smallest size */
291
10103
	if (hash_table->table_size == &hash_table_sizes[0])
292
10100
	    tmp.table_size = hash_table->table_size;
293
	else
294
3
	    tmp.table_size = hash_table->table_size - 1;
295
    }
296

            
297
13859
    if (tmp.table_size == hash_table->table_size &&
298
13802
	hash_table->free_entries > free_low)
299
    {
300
	/* The number of live entries is within the desired bounds
301
	 * (we're not going to resize the table) and we have enough
302
	 * free entries. Do nothing. */
303
13802
	return CAIRO_STATUS_SUCCESS;
304
    }
305

            
306
57
    new_size = *tmp.table_size;
307
57
    tmp.entries = _cairo_calloc_ab (new_size, sizeof (cairo_hash_entry_t*));
308
57
    if (unlikely (tmp.entries == NULL))
309
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
310

            
311
3048
    for (i = 0; i < *hash_table->table_size; ++i) {
312
2991
	if (ENTRY_IS_LIVE (hash_table->entries[i])) {
313
1437
	    *_cairo_hash_table_lookup_unique_key (&tmp, hash_table->entries[i])
314
1437
		= hash_table->entries[i];
315
	}
316
    }
317

            
318
57
    free (hash_table->entries);
319
57
    hash_table->entries = tmp.entries;
320
57
    hash_table->table_size = tmp.table_size;
321
57
    hash_table->free_entries = new_size - hash_table->live_entries;
322

            
323
57
    return CAIRO_STATUS_SUCCESS;
324
}
325

            
326
/**
327
 * _cairo_hash_table_lookup:
328
 * @hash_table: a hash table
329
 * @key: the key of interest
330
 *
331
 * Performs a lookup in @hash_table looking for an entry which has a
332
 * key that matches @key, (as determined by the keys_equal() function
333
 * passed to _cairo_hash_table_create).
334
 *
335
 * Return value: the matching entry, of %NULL if no match was found.
336
 **/
337
void *
338
1019046
_cairo_hash_table_lookup (cairo_hash_table_t *hash_table,
339
			  cairo_hash_entry_t *key)
340
{
341
    cairo_hash_entry_t *entry;
342
    unsigned long table_size, i, idx, step;
343
1019046
    uintptr_t hash = key->hash;
344

            
345
1019046
    entry = hash_table->cache[hash & 31];
346
1019046
    if (entry && entry->hash == hash && hash_table->keys_equal (key, entry))
347
975712
	return entry;
348

            
349
43334
    table_size = *hash_table->table_size;
350
43334
    idx = hash % table_size;
351

            
352
43334
    entry = hash_table->entries[idx];
353
43334
    if (ENTRY_IS_LIVE (entry)) {
354
33041
	if (entry->hash == hash && hash_table->keys_equal (key, entry))
355
32593
		goto insert_cache;
356
10293
    } else if (ENTRY_IS_FREE (entry))
357
10266
	return NULL;
358

            
359
475
    i = 1;
360
475
    step = 1 + hash % (table_size - 2);
361
    do {
362
618
	idx += step;
363
618
	if (idx >= table_size)
364
267
	    idx -= table_size;
365

            
366
618
	entry = hash_table->entries[idx];
367
618
	if (ENTRY_IS_LIVE (entry)) {
368
233
	    if (entry->hash == hash && hash_table->keys_equal (key, entry))
369
91
		    goto insert_cache;
370
385
	} else if (ENTRY_IS_FREE (entry))
371
384
	    return NULL;
372
143
    } while (++i < table_size);
373

            
374
    ASSERT_NOT_REACHED;
375
    return NULL;
376

            
377
32684
insert_cache:
378
32684
    hash_table->cache[hash & 31] = entry;
379
32684
    return entry;
380
}
381

            
382
/**
383
 * _cairo_hash_table_random_entry:
384
 * @hash_table: a hash table
385
 * @predicate: a predicate function.
386
 *
387
 * Find a random entry in the hash table satisfying the given
388
 * @predicate.
389
 *
390
 * We use the same algorithm as the lookup algorithm to walk over the
391
 * entries in the hash table in a pseudo-random order. Walking
392
 * linearly would favor entries following gaps in the hash table. We
393
 * could also call rand() repeatedly, which works well for almost-full
394
 * tables, but degrades when the table is almost empty, or predicate
395
 * returns %TRUE for most entries.
396
 *
397
 * Return value: a random live entry or %NULL if there are no entries
398
 * that match the given predicate. In particular, if predicate is
399
 * %NULL, a %NULL return value indicates that the table is empty.
400
 **/
401
void *
402
_cairo_hash_table_random_entry (cairo_hash_table_t	   *hash_table,
403
				cairo_hash_predicate_func_t predicate)
404
{
405
    cairo_hash_entry_t *entry;
406
    unsigned long hash;
407
    unsigned long table_size, i, idx, step;
408

            
409
    assert (predicate != NULL);
410

            
411
    table_size = *hash_table->table_size;
412
    hash = rand ();
413
    idx = hash % table_size;
414

            
415
    entry = hash_table->entries[idx];
416
    if (ENTRY_IS_LIVE (entry) && predicate (entry))
417
	return entry;
418

            
419
    i = 1;
420
    step = 1 + hash % (table_size - 2);
421
    do {
422
	idx += step;
423
	if (idx >= table_size)
424
	    idx -= table_size;
425

            
426
	entry = hash_table->entries[idx];
427
	if (ENTRY_IS_LIVE (entry) && predicate (entry))
428
	    return entry;
429
    } while (++i < table_size);
430

            
431
    return NULL;
432
}
433

            
434
/**
435
 * _cairo_hash_table_insert:
436
 * @hash_table: a hash table
437
 * @key_and_value: an entry to be inserted
438
 *
439
 * Insert the entry #key_and_value into the hash table.
440
 *
441
 * WARNING: There must not be an existing entry in the hash table
442
 * with a matching key.
443
 *
444
 * WARNING: It is a fatal error to insert an element while
445
 * an iterator is running
446
 *
447
 * Instead of using insert to replace an entry, consider just editing
448
 * the entry obtained with _cairo_hash_table_lookup. Or if absolutely
449
 * necessary, use _cairo_hash_table_remove first.
450
 *
451
 * Return value: %CAIRO_STATUS_SUCCESS if successful or
452
 * %CAIRO_STATUS_NO_MEMORY if insufficient memory is available.
453
 **/
454
cairo_status_t
455
11899
_cairo_hash_table_insert (cairo_hash_table_t *hash_table,
456
			  cairo_hash_entry_t *key_and_value)
457
{
458
    cairo_hash_entry_t **entry;
459
    cairo_status_t status;
460

            
461
    /* Insert is illegal while an iterator is running. */
462
11899
    assert (hash_table->iterating == 0);
463

            
464
11899
    status = _cairo_hash_table_manage (hash_table);
465
11899
    if (unlikely (status))
466
	return status;
467

            
468
11899
    entry = _cairo_hash_table_lookup_unique_key (hash_table, key_and_value);
469

            
470
11899
    if (ENTRY_IS_FREE (*entry))
471
11852
	hash_table->free_entries--;
472

            
473
11899
    *entry = key_and_value;
474
11899
    hash_table->cache[key_and_value->hash & 31] = key_and_value;
475
11899
    hash_table->live_entries++;
476

            
477
11899
    return CAIRO_STATUS_SUCCESS;
478
}
479

            
480
static cairo_hash_entry_t **
481
1987
_cairo_hash_table_lookup_exact_key (cairo_hash_table_t *hash_table,
482
				    cairo_hash_entry_t *key)
483
{
484
    unsigned long table_size, i, idx, step;
485
    cairo_hash_entry_t **entry;
486

            
487
1987
    table_size = *hash_table->table_size;
488
1987
    idx = key->hash % table_size;
489

            
490
1987
    entry = &hash_table->entries[idx];
491
1987
    if (*entry == key)
492
1957
	return entry;
493

            
494
30
    i = 1;
495
30
    step = 1 + key->hash % (table_size - 2);
496
    do {
497
37
	idx += step;
498
37
	if (idx >= table_size)
499
16
	    idx -= table_size;
500

            
501
37
	entry = &hash_table->entries[idx];
502
37
	if (*entry == key)
503
30
	    return entry;
504
7
    } while (++i < table_size);
505

            
506
    ASSERT_NOT_REACHED;
507
    return NULL;
508
}
509
/**
510
 * _cairo_hash_table_remove:
511
 * @hash_table: a hash table
512
 * @key: key of entry to be removed
513
 *
514
 * Remove an entry from the hash table which points to @key.
515
 *
516
 * Return value: %CAIRO_STATUS_SUCCESS if successful or
517
 * %CAIRO_STATUS_NO_MEMORY if out of memory.
518
 **/
519
void
520
1987
_cairo_hash_table_remove (cairo_hash_table_t *hash_table,
521
			  cairo_hash_entry_t *key)
522
{
523
1987
    *_cairo_hash_table_lookup_exact_key (hash_table, key) = DEAD_ENTRY;
524
1987
    hash_table->live_entries--;
525
1987
    hash_table->cache[key->hash & 31] = NULL;
526

            
527
    /* Check for table resize. Don't do this when iterating as this will
528
     * reorder elements of the table and cause the iteration to potentially
529
     * skip some elements. */
530
1987
    if (hash_table->iterating == 0) {
531
	/* This call _can_ fail, but only in failing to allocate new
532
	 * memory to shrink the hash table. It does leave the table in a
533
	 * consistent state, and we've already succeeded in removing the
534
	 * entry, so we don't examine the failure status of this call. */
535
1855
	_cairo_hash_table_manage (hash_table);
536
    }
537
1987
}
538

            
539
/**
540
 * _cairo_hash_table_foreach:
541
 * @hash_table: a hash table
542
 * @hash_callback: function to be called for each live entry
543
 * @closure: additional argument to be passed to @hash_callback
544
 *
545
 * Call @hash_callback for each live entry in the hash table, in a
546
 * non-specified order.
547
 *
548
 * Entries in @hash_table may be removed by code executed from @hash_callback.
549
 *
550
 * Entries may not be inserted to @hash_table, nor may @hash_table
551
 * be destroyed by code executed from @hash_callback. The relevant
552
 * functions will halt in these cases.
553
 **/
554
void
555
105
_cairo_hash_table_foreach (cairo_hash_table_t	      *hash_table,
556
			   cairo_hash_callback_func_t  hash_callback,
557
			   void			      *closure)
558
{
559
    unsigned long i;
560
    cairo_hash_entry_t *entry;
561

            
562
    /* Mark the table for iteration */
563
105
    ++hash_table->iterating;
564
4620
    for (i = 0; i < *hash_table->table_size; i++) {
565
4515
	entry = hash_table->entries[i];
566
4515
	if (ENTRY_IS_LIVE(entry))
567
142
	    hash_callback (entry, closure);
568
    }
569
    /* If some elements were deleted during the iteration,
570
     * the table may need resizing. Just do this every time
571
     * as the check is inexpensive.
572
     */
573
105
    if (--hash_table->iterating == 0) {
574
	/* Should we fail to shrink the hash table, it is left unaltered,
575
	 * and we don't need to propagate the error status. */
576
105
	_cairo_hash_table_manage (hash_table);
577
    }
578
105
}