/** * _cairo_hash_table_foreach: * @hash_table: a hash table * @hash_callback: function to be called for each live entry * @closure: additional argument to be passed to @hash_callback * * Call @hash_callback for each live entry in the hash table, in a * non-specified order. * * Entries in @hash_table may be removed by code executed from @hash_callback. * * Entries may not be inserted to @hash_table, nor may @hash_table * be destroyed by code executed from @hash_callback. The relevant * functions will halt in these cases. **/ void _cairo_hash_table_foreach (cairo_hash_table_t *hash_table, cairo_hash_callback_func_t hash_callback, void *closure) { unsigned long i; cairo_hash_entry_t *entry; if (hash_table == NULL) return; /* Mark the table for iteration */ ++hash_table->iterating; for (i = 0; i < hash_table->arrangement->size; i++) { entry = hash_table->entries[i]; if (ENTRY_IS_LIVE(entry)) hash_callback (entry, closure); } /* If some elements were deleted during the iteration, * the table may need resizing. Just do this every time * as the check is inexpensive. */ if (--hash_table->iterating == 0) { /* Should we fail to shrink the hash table, it is left unaltered, * and we don't need to propagate the error status. */ _cairo_hash_table_resize (hash_table); } }
/** * _cairo_hash_table_insert: * @hash_table: a hash table * @key_and_value: an entry to be inserted * * Insert the entry #key_and_value into the hash table. * * WARNING: It is a fatal error if an entry exists in the hash table * with a matching key, (this function will halt). * * WARNING: It is a fatal error to insert an element while * an iterator is running * * Instead of using insert to replace an entry, consider just editing * the entry obtained with _cairo_hash_table_lookup. Or if absolutely * necessary, use _cairo_hash_table_remove first. * * Return value: CAIRO_STATUS_SUCCESS if successful or * CAIRO_STATUS_NO_MEMORY if insufficient memory is available. **/ cairo_status_t _cairo_hash_table_insert (cairo_hash_table_t *hash_table, cairo_hash_entry_t *key_and_value) { cairo_status_t status; cairo_hash_entry_t **entry; /* Insert is illegal while an iterator is running. */ assert (hash_table->iterating == 0); entry = _cairo_hash_table_lookup_internal (hash_table, key_and_value, FALSE); if (ENTRY_IS_LIVE(*entry)) { /* User is being bad, let's crash. */ ASSERT_NOT_REACHED; } *entry = key_and_value; hash_table->live_entries++; status = _cairo_hash_table_resize (hash_table); if (status) { /* abort the insert... */ *entry = DEAD_ENTRY; hash_table->live_entries--; return status; } return CAIRO_STATUS_SUCCESS; }
/** * _cairo_hash_table_remove: * @hash_table: a hash table * @key: key of entry to be removed * * Remove an entry from the hash table which has a key that matches * @key, if any (as determined by the keys_equal() function passed to * _cairo_hash_table_create). * * Return value: CAIRO_STATUS_SUCCESS if successful or * CAIRO_STATUS_NO_MEMORY if out of memory. **/ void _cairo_hash_table_remove (cairo_hash_table_t *hash_table, cairo_hash_entry_t *key) { cairo_hash_entry_t **entry; entry = _cairo_hash_table_lookup_internal (hash_table, key, FALSE); if (! ENTRY_IS_LIVE(*entry)) return; *entry = DEAD_ENTRY; hash_table->live_entries--; /* Check for table resize. Don't do this when iterating as this will * reorder elements of the table and cause the iteration to potentially * skip some elements. */ if (hash_table->iterating == 0) { /* This call _can_ fail, but only in failing to allocate new * memory to shrink the hash table. It does leave the table in a * consistent state, and we've already succeeded in removing the * entry, so we don't examine the failure status of this call. */ _cairo_hash_table_resize (hash_table); } }