1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2004 Red Hat, Inc.
4 * Copyright © 2005 Red Hat, Inc.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
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/
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.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Red Hat, Inc.
34 * Keith Packard <keithp@keithp.com>
35 * Graydon Hoare <graydon@redhat.com>
36 * Carl Worth <cworth@cworth.org>
42 * An entry can be in one of three states:
44 * FREE: Entry has never been used, terminates all searches.
45 * Appears in the table as a %NULL pointer.
47 * DEAD: Entry had been live in the past. A dead entry can be reused
48 * but does not terminate a search for an exact entry.
49 * Appears in the table as a pointer to DEAD_ENTRY.
51 * LIVE: Entry is currently being used.
52 * Appears in the table as any non-%NULL, non-DEAD_ENTRY pointer.
55 static cairo_hash_entry_t dead_entry
= { 0 };
56 #define DEAD_ENTRY (&dead_entry)
58 #define ENTRY_IS_FREE(entry) ((entry) == NULL)
59 #define ENTRY_IS_DEAD(entry) ((entry) == DEAD_ENTRY)
60 #define ENTRY_IS_LIVE(entry) ((entry) && ! ENTRY_IS_DEAD(entry))
62 /* We expect keys will not be destroyed frequently, so our table does not
63 * contain any explicit shrinking code nor any chain-coalescing code for
64 * entries randomly deleted by memory pressure (except during rehashing, of
65 * course). These assumptions are potentially bad, but they make the
66 * implementation straightforward.
68 * Revisit later if evidence appears that we're using excessive memory from
69 * a mostly-dead table.
71 * This table is open-addressed with double hashing. Each table size is a
72 * prime chosen to be a little more than double the high water mark for a
73 * given arrangement, so the tables should remain < 50% full. The table
74 * size makes for the "first" hash modulus; a second prime (2 less than the
75 * first prime) serves as the "second" hash modulus, which is co-prime and
76 * thus guarantees a complete permutation of table indices.
78 * This structure, and accompanying table, is borrowed/modified from the
79 * file xserver/render/glyph.c in the freedesktop.org x server, with
80 * permission (and suggested modification of doubling sizes) by Keith
84 typedef struct _cairo_hash_table_arrangement
{
85 unsigned long high_water_mark
;
88 } cairo_hash_table_arrangement_t
;
90 static const cairo_hash_table_arrangement_t hash_table_arrangements
[] = {
100 { 8192, 18043, 18041 },
101 { 16384, 36109, 36107 },
102 { 32768, 72091, 72089 },
103 { 65536, 144409, 144407 },
104 { 131072, 288361, 288359 },
105 { 262144, 576883, 576881 },
106 { 524288, 1153459, 1153457 },
107 { 1048576, 2307163, 2307161 },
108 { 2097152, 4613893, 4613891 },
109 { 4194304, 9227641, 9227639 },
110 { 8388608, 18455029, 18455027 },
111 { 16777216, 36911011, 36911009 },
112 { 33554432, 73819861, 73819859 },
113 { 67108864, 147639589, 147639587 },
114 { 134217728, 295279081, 295279079 },
115 { 268435456, 590559793, 590559791 }
118 #define NUM_HASH_TABLE_ARRANGEMENTS ARRAY_LENGTH (hash_table_arrangements)
120 struct _cairo_hash_table
{
121 cairo_hash_keys_equal_func_t keys_equal
;
123 const cairo_hash_table_arrangement_t
*arrangement
;
124 cairo_hash_entry_t
**entries
;
126 unsigned long live_entries
;
127 unsigned long iterating
; /* Iterating, no insert, no resize */
131 * _cairo_hash_table_create:
132 * @keys_equal: a function to return %TRUE if two keys are equal
134 * Creates a new hash table which will use the keys_equal() function
135 * to compare hash keys. Data is provided to the hash table in the
136 * form of user-derived versions of #cairo_hash_entry_t. A hash entry
137 * must be able to hold both a key (including a hash code) and a
138 * value. Sometimes only the key will be necessary, (as in
139 * _cairo_hash_table_remove), and other times both a key and a value
140 * will be necessary, (as in _cairo_hash_table_insert).
142 * See #cairo_hash_entry_t for more details.
144 * Return value: the new hash table or %NULL if out of memory.
147 _cairo_hash_table_create (cairo_hash_keys_equal_func_t keys_equal
)
149 cairo_hash_table_t
*hash_table
;
151 hash_table
= malloc (sizeof (cairo_hash_table_t
));
152 if (hash_table
== NULL
) {
153 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY
);
157 hash_table
->keys_equal
= keys_equal
;
159 hash_table
->arrangement
= &hash_table_arrangements
[0];
161 hash_table
->entries
= calloc (hash_table
->arrangement
->size
,
162 sizeof(cairo_hash_entry_t
*));
163 if (hash_table
->entries
== NULL
) {
164 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY
);
169 hash_table
->live_entries
= 0;
170 hash_table
->iterating
= 0;
176 * _cairo_hash_table_destroy:
177 * @hash_table: an empty hash table to destroy
179 * Immediately destroys the given hash table, freeing all resources
180 * associated with it.
182 * WARNING: The hash_table must have no live entries in it before
183 * _cairo_hash_table_destroy is called. It is a fatal error otherwise,
184 * and this function will halt. The rationale for this behavior is to
185 * avoid memory leaks and to avoid needless complication of the API
186 * with destroy notifiy callbacks.
188 * WARNING: The hash_table must have no running iterators in it when
189 * _cairo_hash_table_destroy is called. It is a fatal error otherwise,
190 * and this function will halt.
193 _cairo_hash_table_destroy (cairo_hash_table_t
*hash_table
)
195 if (hash_table
== NULL
)
198 /* The hash table must be empty. Otherwise, halt. */
199 assert (hash_table
->live_entries
== 0);
200 /* No iterators can be running. Otherwise, halt. */
201 assert (hash_table
->iterating
== 0);
203 free (hash_table
->entries
);
204 hash_table
->entries
= NULL
;
210 * _cairo_hash_table_lookup_internal:
212 * @hash_table: a #cairo_hash_table_t to search
213 * @key: the key to search on
214 * @hash_code: the hash_code for @key
215 * @key_unique: If %TRUE, then caller asserts that no key already
216 * exists that will compare equal to #key, so search can be
217 * optimized. If unsure, set to %FALSE and the code will always work.
219 * Search the hashtable for a live entry for which
220 * hash_table->keys_equal returns true. If no such entry exists then
221 * return the first available (free or dead entry).
223 * If the key_unique flag is set, then the search will never call
224 * hash_table->keys_equal and will act as if it always returned
225 * false. This is useful as a performance optimization in special
226 * circumstances where the caller knows that there is no existing
227 * entry in the hash table with a matching key.
229 * Return value: The matching entry in the hash table (if
230 * any). Otherwise, the first available entry. The caller should check
231 * entry->state to check whether a match was found or not.
233 static cairo_hash_entry_t
**
234 _cairo_hash_table_lookup_internal (cairo_hash_table_t
*hash_table
,
235 cairo_hash_entry_t
*key
,
236 cairo_bool_t key_is_unique
)
238 cairo_hash_entry_t
**entry
, **first_available
= NULL
;
239 unsigned long table_size
, i
, idx
, step
;
241 table_size
= hash_table
->arrangement
->size
;
243 idx
= key
->hash
% table_size
;
246 for (i
= 0; i
< table_size
; ++i
)
248 entry
= &hash_table
->entries
[idx
];
250 if (ENTRY_IS_FREE(*entry
))
254 else if (ENTRY_IS_DEAD(*entry
))
259 if (! first_available
)
260 first_available
= entry
;
263 else /* ENTRY_IS_LIVE(*entry) */
266 if (hash_table
->keys_equal (key
, *entry
))
271 step
= key
->hash
% hash_table
->arrangement
->rehash
;
277 if (idx
>= table_size
)
282 * The table should not have permitted you to get here if you were just
283 * looking for a free slot: there should have been room.
285 assert (key_is_unique
== 0);
287 return first_available
;
291 * _cairo_hash_table_resize:
292 * @hash_table: a hash table
294 * Resize the hash table if the number of entries has gotten much
295 * bigger or smaller than the ideal number of entries for the current
298 * Return value: %CAIRO_STATUS_SUCCESS if successful or
299 * %CAIRO_STATUS_NO_MEMORY if out of memory.
301 static cairo_status_t
302 _cairo_hash_table_resize (cairo_hash_table_t
*hash_table
)
304 cairo_hash_table_t tmp
;
305 cairo_hash_entry_t
**entry
;
306 unsigned long new_size
, i
;
308 /* This keeps the hash table between 25% and 50% full. */
309 unsigned long high
= hash_table
->arrangement
->high_water_mark
;
310 unsigned long low
= high
>> 2;
312 if (hash_table
->live_entries
>= low
&& hash_table
->live_entries
<= high
)
313 return CAIRO_STATUS_SUCCESS
;
317 if (hash_table
->live_entries
> high
)
319 tmp
.arrangement
= hash_table
->arrangement
+ 1;
320 /* This code is being abused if we can't make a table big enough. */
321 assert (tmp
.arrangement
- hash_table_arrangements
<
322 NUM_HASH_TABLE_ARRANGEMENTS
);
324 else /* hash_table->live_entries < low */
326 /* Can't shrink if we're at the smallest size */
327 if (hash_table
->arrangement
== &hash_table_arrangements
[0])
328 return CAIRO_STATUS_SUCCESS
;
329 tmp
.arrangement
= hash_table
->arrangement
- 1;
332 new_size
= tmp
.arrangement
->size
;
333 tmp
.entries
= calloc (new_size
, sizeof (cairo_hash_entry_t
*));
334 if (tmp
.entries
== NULL
)
335 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
337 for (i
= 0; i
< hash_table
->arrangement
->size
; ++i
) {
338 if (ENTRY_IS_LIVE (hash_table
->entries
[i
])) {
339 entry
= _cairo_hash_table_lookup_internal (&tmp
,
340 hash_table
->entries
[i
],
342 assert (ENTRY_IS_FREE(*entry
));
343 *entry
= hash_table
->entries
[i
];
347 free (hash_table
->entries
);
348 hash_table
->entries
= tmp
.entries
;
349 hash_table
->arrangement
= tmp
.arrangement
;
351 return CAIRO_STATUS_SUCCESS
;
355 * _cairo_hash_table_lookup:
356 * @hash_table: a hash table
357 * @key: the key of interest
358 * @entry_return: pointer for return value.
360 * Performs a lookup in @hash_table looking for an entry which has a
361 * key that matches @key, (as determined by the keys_equal() function
362 * passed to _cairo_hash_table_create).
364 * Return value: %TRUE if there is an entry in the hash table that
365 * matches the given key, (which will now be in *entry_return). %FALSE
366 * otherwise, (in which case *entry_return will be %NULL).
369 _cairo_hash_table_lookup (cairo_hash_table_t
*hash_table
,
370 cairo_hash_entry_t
*key
,
371 cairo_hash_entry_t
**entry_return
)
373 cairo_hash_entry_t
**entry
;
375 /* See if we have an entry in the table already. */
376 entry
= _cairo_hash_table_lookup_internal (hash_table
, key
, FALSE
);
377 if (ENTRY_IS_LIVE(*entry
)) {
378 *entry_return
= *entry
;
382 *entry_return
= NULL
;
387 * _cairo_hash_table_random_entry:
388 * @hash_table: a hash table
389 * @predicate: a predicate function, or %NULL for any entry.
391 * Find a random entry in the hash table satisfying the given
392 * @predicate. A %NULL @predicate is taken as equivalent to a function
393 * which always returns %TRUE, (eg. any entry in the table will do).
395 * We use the same algorithm as the lookup algorithm to walk over the
396 * entries in the hash table in a pseudo-random order. Walking
397 * linearly would favor entries following gaps in the hash table. We
398 * could also call rand() repeatedly, which works well for almost-full
399 * tables, but degrades when the table is almost empty, or predicate
400 * returns %TRUE for most entries.
402 * Return value: a random live entry or %NULL if there are no entries
403 * that match the given predicate. In particular, if predicate is
404 * %NULL, a %NULL return value indicates that the table is empty.
407 _cairo_hash_table_random_entry (cairo_hash_table_t
*hash_table
,
408 cairo_hash_predicate_func_t predicate
)
410 cairo_hash_entry_t
**entry
;
412 unsigned long table_size
, i
, idx
, step
;
414 table_size
= hash_table
->arrangement
->size
;
417 idx
= hash
% table_size
;
420 for (i
= 0; i
< table_size
; ++i
)
422 entry
= &hash_table
->entries
[idx
];
424 if (ENTRY_IS_LIVE (*entry
) &&
425 (predicate
== NULL
|| predicate (*entry
)))
431 step
= hash
% hash_table
->arrangement
->rehash
;
437 if (idx
>= table_size
)
445 * _cairo_hash_table_insert:
446 * @hash_table: a hash table
447 * @key_and_value: an entry to be inserted
449 * Insert the entry #key_and_value into the hash table.
451 * WARNING: It is a fatal error if an entry exists in the hash table
452 * with a matching key, (this function will halt).
454 * WARNING: It is a fatal error to insert an element while
455 * an iterator is running
457 * Instead of using insert to replace an entry, consider just editing
458 * the entry obtained with _cairo_hash_table_lookup. Or if absolutely
459 * necessary, use _cairo_hash_table_remove first.
461 * Return value: %CAIRO_STATUS_SUCCESS if successful or
462 * %CAIRO_STATUS_NO_MEMORY if insufficient memory is available.
465 _cairo_hash_table_insert (cairo_hash_table_t
*hash_table
,
466 cairo_hash_entry_t
*key_and_value
)
468 cairo_status_t status
;
469 cairo_hash_entry_t
**entry
;
471 /* Insert is illegal while an iterator is running. */
472 assert (hash_table
->iterating
== 0);
474 entry
= _cairo_hash_table_lookup_internal (hash_table
,
475 key_and_value
, FALSE
);
477 if (ENTRY_IS_LIVE(*entry
))
479 /* User is being bad, let's crash. */
483 *entry
= key_and_value
;
484 hash_table
->live_entries
++;
486 status
= _cairo_hash_table_resize (hash_table
);
488 /* abort the insert... */
490 hash_table
->live_entries
--;
494 return CAIRO_STATUS_SUCCESS
;
498 * _cairo_hash_table_remove:
499 * @hash_table: a hash table
500 * @key: key of entry to be removed
502 * Remove an entry from the hash table which has a key that matches
503 * @key, if any (as determined by the keys_equal() function passed to
504 * _cairo_hash_table_create).
506 * Return value: %CAIRO_STATUS_SUCCESS if successful or
507 * %CAIRO_STATUS_NO_MEMORY if out of memory.
510 _cairo_hash_table_remove (cairo_hash_table_t
*hash_table
,
511 cairo_hash_entry_t
*key
)
513 cairo_hash_entry_t
**entry
;
515 entry
= _cairo_hash_table_lookup_internal (hash_table
, key
, FALSE
);
516 if (! ENTRY_IS_LIVE(*entry
))
520 hash_table
->live_entries
--;
522 /* Check for table resize. Don't do this when iterating as this will
523 * reorder elements of the table and cause the iteration to potentially
524 * skip some elements. */
525 if (hash_table
->iterating
== 0) {
526 /* This call _can_ fail, but only in failing to allocate new
527 * memory to shrink the hash table. It does leave the table in a
528 * consistent state, and we've already succeeded in removing the
529 * entry, so we don't examine the failure status of this call. */
530 _cairo_hash_table_resize (hash_table
);
535 * _cairo_hash_table_foreach:
536 * @hash_table: a hash table
537 * @hash_callback: function to be called for each live entry
538 * @closure: additional argument to be passed to @hash_callback
540 * Call @hash_callback for each live entry in the hash table, in a
541 * non-specified order.
543 * Entries in @hash_table may be removed by code executed from @hash_callback.
545 * Entries may not be inserted to @hash_table, nor may @hash_table
546 * be destroyed by code executed from @hash_callback. The relevant
547 * functions will halt in these cases.
550 _cairo_hash_table_foreach (cairo_hash_table_t
*hash_table
,
551 cairo_hash_callback_func_t hash_callback
,
555 cairo_hash_entry_t
*entry
;
557 if (hash_table
== NULL
)
560 /* Mark the table for iteration */
561 ++hash_table
->iterating
;
562 for (i
= 0; i
< hash_table
->arrangement
->size
; i
++) {
563 entry
= hash_table
->entries
[i
];
564 if (ENTRY_IS_LIVE(entry
))
565 hash_callback (entry
, closure
);
567 /* If some elements were deleted during the iteration,
568 * the table may need resizing. Just do this every time
569 * as the check is inexpensive.
571 if (--hash_table
->iterating
== 0) {
572 /* Should we fail to shrink the hash table, it is left unaltered,
573 * and we don't need to propagate the error status. */
574 _cairo_hash_table_resize (hash_table
);