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., 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
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>
37 * Karl Tomlinson <karlt+@karlt.net>, Mozilla Corporation
40 #include "cairo-script-private.h"
45 * An entry can be in one of three states:
47 * FREE: Entry has never been used, terminates all searches.
48 * Appears in the table as a %NULL pointer.
50 * DEAD: Entry had been live in the past. A dead entry can be reused
51 * but does not terminate a search for an exact entry.
52 * Appears in the table as a pointer to DEAD_ENTRY.
54 * LIVE: Entry is currently being used.
55 * Appears in the table as any non-%NULL, non-DEAD_ENTRY pointer.
58 #define DEAD_ENTRY ((csi_hash_entry_t *) 0x1)
60 #define ENTRY_IS_FREE(entry) ((entry) == NULL)
61 #define ENTRY_IS_DEAD(entry) ((entry) == DEAD_ENTRY)
62 #define ENTRY_IS_LIVE(entry) ((entry) > DEAD_ENTRY)
65 /* This table is open-addressed with double hashing. Each table size is a
66 * prime chosen to be a little more than double the high water mark for a
67 * given arrangement, so the tables should remain < 50% full. The table
68 * size makes for the "first" hash modulus; a second prime (2 less than the
69 * first prime) serves as the "second" hash modulus, which is co-prime and
70 * thus guarantees a complete permutation of table indices.
72 * This structure, and accompanying table, is borrowed/modified from the
73 * file xserver/render/glyph.c in the freedesktop.org x server, with
74 * permission (and suggested modification of doubling sizes) by Keith
78 static const csi_hash_table_arrangement_t hash_table_arrangements [] = {
88 { 8192, 18043, 18041 },
89 { 16384, 36109, 36107 },
90 { 32768, 72091, 72089 },
91 { 65536, 144409, 144407 },
92 { 131072, 288361, 288359 },
93 { 262144, 576883, 576881 },
94 { 524288, 1153459, 1153457 },
95 { 1048576, 2307163, 2307161 },
96 { 2097152, 4613893, 4613891 },
97 { 4194304, 9227641, 9227639 },
98 { 8388608, 18455029, 18455027 },
99 { 16777216, 36911011, 36911009 },
100 { 33554432, 73819861, 73819859 },
101 { 67108864, 147639589, 147639587 },
102 { 134217728, 295279081, 295279079 },
103 { 268435456, 590559793, 590559791 }
106 #define NUM_HASH_TABLE_ARRANGEMENTS ARRAY_LENGTH (hash_table_arrangements)
109 * _csi_hash_table_create:
110 * @keys_equal: a function to return %TRUE if two keys are equal
112 * Creates a new hash table which will use the keys_equal() function
113 * to compare hash keys. Data is provided to the hash table in the
114 * form of user-derived versions of #csi_hash_entry_t. A hash entry
115 * must be able to hold both a key (including a hash code) and a
116 * value. Sometimes only the key will be necessary, (as in
117 * _csi_hash_table_remove), and other times both a key and a value
118 * will be necessary, (as in _csi_hash_table_insert).
120 * See #csi_hash_entry_t for more details.
122 * Return value: the new hash table or %NULL if out of memory.
125 _csi_hash_table_init (csi_hash_table_t *hash_table,
126 csi_hash_keys_equal_func_t keys_equal)
128 hash_table->keys_equal = keys_equal;
130 hash_table->arrangement = &hash_table_arrangements[0];
132 hash_table->entries = calloc (hash_table->arrangement->size,
133 sizeof(csi_hash_entry_t *));
134 if (hash_table->entries == NULL)
135 return _csi_error (CAIRO_STATUS_NO_MEMORY);
137 hash_table->live_entries = 0;
138 hash_table->used_entries = 0;
139 hash_table->iterating = 0;
141 return CSI_STATUS_SUCCESS;
145 * _csi_hash_table_destroy:
146 * @hash_table: an empty hash table to destroy
148 * Immediately destroys the given hash table, freeing all resources
149 * associated with it.
151 * WARNING: The hash_table must have no live entries in it before
152 * _csi_hash_table_destroy is called. It is a fatal error otherwise,
153 * and this function will halt. The rationale for this behavior is to
154 * avoid memory leaks and to avoid needless complication of the API
155 * with destroy notifiy callbacks.
157 * WARNING: The hash_table must have no running iterators in it when
158 * _csi_hash_table_destroy is called. It is a fatal error otherwise,
159 * and this function will halt.
162 _csi_hash_table_fini (csi_hash_table_t *hash_table)
164 free (hash_table->entries);
167 static csi_hash_entry_t **
168 _csi_hash_table_lookup_unique_key (csi_hash_table_t *hash_table,
169 csi_hash_entry_t *key)
171 unsigned long table_size, i, idx, step;
172 csi_hash_entry_t **entry;
174 table_size = hash_table->arrangement->size;
175 idx = key->hash % table_size;
177 entry = &hash_table->entries[idx];
178 if (! ENTRY_IS_LIVE (*entry))
182 step = key->hash % hash_table->arrangement->rehash;
187 if (idx >= table_size)
190 entry = &hash_table->entries[idx];
191 if (! ENTRY_IS_LIVE (*entry))
193 } while (++i < table_size);
199 * _csi_hash_table_manage:
200 * @hash_table: a hash table
202 * Resize the hash table if the number of entries has gotten much
203 * bigger or smaller than the ideal number of entries for the current
204 * size, or control the number of dead entries by moving the entries
207 * Return value: %CAIRO_STATUS_SUCCESS if successful or
208 * %CAIRO_STATUS_NO_MEMORY if out of memory.
211 _csi_hash_table_manage (csi_hash_table_t *hash_table)
213 csi_hash_table_t tmp;
214 csi_boolean_t realloc = TRUE;
217 /* This keeps the size of the hash table between 2 and approximately 8
218 * times the number of live entries and keeps the proportion of free
219 * entries (search-terminations) > 25%.
221 unsigned long high = hash_table->arrangement->high_water_mark;
222 unsigned long low = high >> 2;
223 unsigned long max_used = high + high / 2;
227 if (hash_table->live_entries > high) {
228 tmp.arrangement = hash_table->arrangement + 1;
229 /* This code is being abused if we can't make a table big enough. */
230 } else if (hash_table->live_entries < low &&
231 /* Can't shrink if we're at the smallest size */
232 hash_table->arrangement != &hash_table_arrangements[0])
234 tmp.arrangement = hash_table->arrangement - 1;
236 else if (hash_table->used_entries > max_used)
238 /* Clean out dead entries to prevent lookups from becoming too slow. */
239 for (i = 0; i < hash_table->arrangement->size; ++i) {
240 if (ENTRY_IS_DEAD (hash_table->entries[i]))
241 hash_table->entries[i] = NULL;
243 hash_table->used_entries = hash_table->live_entries;
245 /* There is no need to reallocate but some entries may need to be
246 * moved. Typically the proportion of entries needing to be moved is
247 * small, but, if the moving should leave a large number of dead
248 * entries, they will be cleaned out next time this code is
254 return CAIRO_STATUS_SUCCESS;
258 tmp.entries = calloc (tmp.arrangement->size,
259 sizeof (csi_hash_entry_t*));
260 if (tmp.entries == NULL)
261 return _csi_error (CAIRO_STATUS_NO_MEMORY);
263 hash_table->used_entries = 0;
266 for (i = 0; i < hash_table->arrangement->size; ++i) {
267 csi_hash_entry_t *entry, **pos;
269 entry = hash_table->entries[i];
270 if (ENTRY_IS_LIVE (entry)) {
271 hash_table->entries[i] = DEAD_ENTRY;
273 pos = _csi_hash_table_lookup_unique_key (&tmp, entry);
274 if (ENTRY_IS_FREE (*pos))
275 hash_table->used_entries++;
282 free (hash_table->entries);
283 hash_table->entries = tmp.entries;
284 hash_table->arrangement = tmp.arrangement;
287 return CAIRO_STATUS_SUCCESS;
291 * _csi_hash_table_lookup:
292 * @hash_table: a hash table
293 * @key: the key of interest
295 * Performs a lookup in @hash_table looking for an entry which has a
296 * key that matches @key, (as determined by the keys_equal() function
297 * passed to _csi_hash_table_create).
299 * Return value: the matching entry, of %NULL if no match was found.
302 _csi_hash_table_lookup (csi_hash_table_t *hash_table,
303 csi_hash_entry_t *key)
305 csi_hash_entry_t **entry;
306 unsigned long table_size, i, idx, step;
308 table_size = hash_table->arrangement->size;
309 idx = key->hash % table_size;
310 entry = &hash_table->entries[idx];
312 if (ENTRY_IS_LIVE (*entry)) {
313 if ((*entry)->hash == key->hash && hash_table->keys_equal (key, *entry))
315 } else if (ENTRY_IS_FREE (*entry))
319 step = key->hash % hash_table->arrangement->rehash;
324 if (idx >= table_size)
327 entry = &hash_table->entries[idx];
328 if (ENTRY_IS_LIVE (*entry)) {
329 if ((*entry)->hash == key->hash &&
330 hash_table->keys_equal (key, *entry))
334 } else if (ENTRY_IS_FREE (*entry))
336 } while (++i < table_size);
342 * _csi_hash_table_insert:
343 * @hash_table: a hash table
344 * @key_and_value: an entry to be inserted
346 * Insert the entry #key_and_value into the hash table.
348 * WARNING: There must not be an existing entry in the hash table
349 * with a matching key.
351 * WARNING: It is a fatal error to insert an element while
352 * an iterator is running
354 * Instead of using insert to replace an entry, consider just editing
355 * the entry obtained with _csi_hash_table_lookup. Or if absolutely
356 * necessary, use _csi_hash_table_remove first.
358 * Return value: %CAIRO_STATUS_SUCCESS if successful or
359 * %CAIRO_STATUS_NO_MEMORY if insufficient memory is available.
362 _csi_hash_table_insert (csi_hash_table_t *hash_table,
363 csi_hash_entry_t *key_and_value)
366 csi_hash_entry_t **entry;
368 hash_table->live_entries++;
369 status = _csi_hash_table_manage (hash_table);
370 if (_csi_unlikely (status)) {
371 /* abort the insert... */
372 hash_table->live_entries--;
376 entry = _csi_hash_table_lookup_unique_key (hash_table,
378 if (ENTRY_IS_FREE (*entry))
379 hash_table->used_entries++;
381 *entry = key_and_value;
382 return CAIRO_STATUS_SUCCESS;
385 static csi_hash_entry_t **
386 _csi_hash_table_lookup_exact_key (csi_hash_table_t *hash_table,
387 csi_hash_entry_t *key)
389 unsigned long table_size, i, idx, step;
390 csi_hash_entry_t **entry;
392 table_size = hash_table->arrangement->size;
393 idx = key->hash % table_size;
395 entry = &hash_table->entries[idx];
400 step = key->hash % hash_table->arrangement->rehash;
405 if (idx >= table_size)
408 entry = &hash_table->entries[idx];
411 } while (++i < table_size);
416 * _csi_hash_table_remove:
417 * @hash_table: a hash table
418 * @key: key of entry to be removed
420 * Remove an entry from the hash table which points to @key.
422 * Return value: %CAIRO_STATUS_SUCCESS if successful or
423 * %CAIRO_STATUS_NO_MEMORY if out of memory.
426 _csi_hash_table_remove (csi_hash_table_t *hash_table,
427 csi_hash_entry_t *key)
429 *_csi_hash_table_lookup_exact_key (hash_table, key) = DEAD_ENTRY;
430 hash_table->live_entries--;
432 /* Check for table resize. Don't do this when iterating as this will
433 * reorder elements of the table and cause the iteration to potentially
434 * skip some elements. */
435 if (hash_table->iterating == 0) {
436 /* This call _can_ fail, but only in failing to allocate new
437 * memory to shrink the hash table. It does leave the table in a
438 * consistent state, and we've already succeeded in removing the
439 * entry, so we don't examine the failure status of this call. */
440 _csi_hash_table_manage (hash_table);
445 * _csi_hash_table_foreach:
446 * @hash_table: a hash table
447 * @hash_callback: function to be called for each live entry
448 * @closure: additional argument to be passed to @hash_callback
450 * Call @hash_callback for each live entry in the hash table, in a
451 * non-specified order.
453 * Entries in @hash_table may be removed by code executed from @hash_callback.
455 * Entries may not be inserted to @hash_table, nor may @hash_table
456 * be destroyed by code executed from @hash_callback. The relevant
457 * functions will halt in these cases.
460 _csi_hash_table_foreach (csi_hash_table_t *hash_table,
461 csi_hash_callback_func_t hash_callback,
465 csi_hash_entry_t *entry;
467 /* Mark the table for iteration */
468 ++hash_table->iterating;
469 for (i = 0; i < hash_table->arrangement->size; i++) {
470 entry = hash_table->entries[i];
471 if (ENTRY_IS_LIVE(entry))
472 hash_callback (entry, closure);
474 /* If some elements were deleted during the iteration,
475 * the table may need resizing. Just do this every time
476 * as the check is inexpensive.
478 if (--hash_table->iterating == 0) {
479 /* Should we fail to shrink the hash table, it is left unaltered,
480 * and we don't need to propagate the error status. */
481 _csi_hash_table_manage (hash_table);