#define HASHTABLE_SIZE_N (sizeof(hashtable_size)/sizeof(unsigned long))
-static void
-resize_table (scm_t_weak_table *table)
+static int
+compute_size_index (scm_t_weak_table *table)
{
- scm_t_weak_entry *old_entries, *new_entries;
- int i;
- unsigned long old_size, new_size, old_k;
+ int i = table->size_index;
- old_entries = table->entries;
- old_size = table->size;
-
if (table->n_items < table->lower)
{
/* rehashing is not triggered when i <= min_size */
- i = table->size_index;
do
--i;
while (i > table->min_size_index
- && table->n_items < hashtable_size[i] / 4);
+ && table->n_items < hashtable_size[i] / 5);
}
- else
+ else if (table->n_items > table->upper)
{
- i = table->size_index + 1;
+ ++i;
if (i >= HASHTABLE_SIZE_N)
/* The biggest size currently is 230096423, which for a 32-bit
machine will occupy 2.3GB of memory at a load of 80%. There
abort ();
}
- new_size = hashtable_size[i];
- new_entries = allocate_entries (new_size, table->kind);
+ return i;
+}
- table->size_index = i;
+static int
+is_acceptable_size_index (scm_t_weak_table *table, int size_index)
+{
+ int computed = compute_size_index (table);
+
+ if (size_index == computed)
+ /* We were going to grow or shrink, and allocating the new vector
+ didn't change the target size. */
+ return 1;
+
+ if (size_index == computed + 1)
+ {
+ /* We were going to enlarge the table, but allocating the new
+ vector finalized some objects, making an enlargement
+ unnecessary. It might still be a good idea to use the larger
+ table, though. (This branch also gets hit if, while allocating
+ the vector, some other thread was actively removing items from
+ the table. That is less likely, though.) */
+ unsigned long new_lower = hashtable_size[size_index] / 5;
+
+ return table->size > new_lower;
+ }
+
+ if (size_index == computed - 1)
+ {
+ /* We were going to shrink the table, but when we dropped the lock
+ to allocate the new vector, some other thread added elements to
+ the table. */
+ return 0;
+ }
+
+ /* The computed size differs from our newly allocated size by more
+ than one size index -- recalculate. */
+ return 0;
+}
+
+static void
+resize_table (scm_t_weak_table *table)
+{
+ scm_t_weak_entry *old_entries, *new_entries;
+ int new_size_index;
+ unsigned long old_size, new_size, old_k;
+
+ do
+ {
+ new_size_index = compute_size_index (table);
+ if (new_size_index == table->size_index)
+ return;
+ new_size = hashtable_size[new_size_index];
+ scm_i_pthread_mutex_unlock (&table->lock);
+ /* Allocating memory might cause finalizers to run, which could
+ run anything, so drop our lock to avoid deadlocks. */
+ new_entries = allocate_entries (new_size, table->kind);
+ scm_i_pthread_mutex_lock (&table->lock);
+ }
+ while (!is_acceptable_size_index (table, new_size_index));
+
+ old_entries = table->entries;
+ old_size = table->size;
+
+ table->size_index = new_size_index;
table->size = new_size;
- if (i <= table->min_size_index)
+ if (new_size_index <= table->min_size_index)
table->lower = 0;
else
table->lower = new_size / 5;
weak_gc_finalizer (void *ptr, void *data)
{
if (weak_gc_callback (ptr))
- GC_REGISTER_FINALIZER_NO_ORDER (ptr, weak_gc_finalizer, data, NULL, NULL);
+ scm_i_set_finalizer (ptr, weak_gc_finalizer, data);
}
#endif
#ifdef HAVE_GC_TABLE_START_CALLBACK
scm_c_hook_add (&scm_after_gc_c_hook, weak_gc_hook, weak, 0);
#else
- GC_REGISTER_FINALIZER_NO_ORDER (weak, weak_gc_finalizer, NULL, NULL, NULL);
+ scm_i_set_finalizer (weak, weak_gc_finalizer, NULL);
#endif
}
dflt);
}
-SCM
+void
scm_weak_table_putq_x (SCM table, SCM key, SCM value)
{
scm_c_weak_table_put_x (table, scm_ihashq (key, -1),
assq_predicate, SCM_UNPACK_POINTER (key),
key, value);
- return SCM_UNSPECIFIED;
}
-SCM
+void
scm_weak_table_remq_x (SCM table, SCM key)
{
scm_c_weak_table_remove_x (table, scm_ihashq (key, -1),
assq_predicate, SCM_UNPACK_POINTER (key));
- return SCM_UNSPECIFIED;
}
-SCM
+void
scm_weak_table_clear_x (SCM table)
#define FUNC_NAME "weak-table-clear!"
{
t->n_items = 0;
scm_i_pthread_mutex_unlock (&t->lock);
-
- return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
return seed;
}
-SCM
+void
scm_weak_table_for_each (SCM proc, SCM table)
#define FUNC_NAME "weak-table-for-each"
{
SCM_VALIDATE_PROC (1, proc);
scm_c_weak_table_fold (for_each_trampoline, SCM_UNPACK_POINTER (proc), SCM_BOOL_F, table);
-
- return SCM_UNSPECIFIED;
}
#undef FUNC_NAME