* libguile/weak-table.c (scm_t_weak_table); Add last_gc_no member.
* libguile/weak-table.c (vacuum_weak_table): Only vacuum if we haven't
done so since the last GC.
(scm_c_weak_table_ref, scm_c_weak_table_put_x, scm_c_weak_table_remove_x)
(scm_c_weak_table_fold): Vacuum the weak table if needed.
(scm_weak_table_clear_x): Update last_gc_no flag, as no more vacuuming
will be needed.
This change should make weak tables work better with libgc, as the weak
components that need mark functions are smaller, so they don't overflow
the mark queue. Also this prevents the need to move disappearing
links.
* libguile/weak-table.c (scm_t_weak_entry): Change to be a hash table
chain entry.
(struct weak_entry_data, do_read_weak_entry, read_weak_entry): Read
out the key and value directly.
(GC_move_disappearing_link, move_disappearing_links, move_weak_entry):
Remove.
(scm_t_weak_table): Rename "entries" member to "buckets", and "size" to
"n_buckets".
(hash_to_index, entry_distance, rob_from_rich, give_to_poor): Remove.
(mark_weak_key_entry, mark_weak_value_entry): Mark a single link, and
the next link.
(mark_doubly_weak_entry): New kind.
(allocate_entry): Allocate a single entry.
(add_entry): New helper.
(resize_table): Reimplement more like normal hash tables.
(vacuum_weak_table): Adapt to new implementation.
(weak_table_ref, weak_table_put_x, weak_table_remove_x): Adapt.
(make_weak_table): Adapt.
(scm_weak_table_clear_x): Actually unregister the links to prevent a
memory leak.
(scm_c_weak_table_fold): Collect items in an alist, then fold outside
the lock.
(scm_weak_table_prehistory): Initialize doubly_weak_gc_kind.
* libguile/finalizers.c (async_gc_finalizer):
(scm_i_register_async_gc_callback): Replace "weak gc callback"
mechanism with "async gc callback" mechanism. Very similar but the
new API is designed to be called a bounded number of times, to avoid
running afoul of libgc heuristics.
* libguile/weak-list.h: New internal header.
* libguile/Makefile.am (noinst_HEADERS): Add weak-list.h.
* libguile/weak-set.c (vacuum_all_weak_sets):
(scm_c_make_weak_set, scm_init_weak_set):
* libguile/weak-table.c (vacuum_all_weak_tables):
(scm_c_make_weak_table, scm_init_weak_table): Arrange to vacuum all
weak sets from a single async GC callback, and likewise for weak
tables.
Thanks to Ludovic Courtès for tracking this bug down!
* libguile/weak-table.c (GC_move_disappearing_link)
(move_disappearing_links): Factor out stub, as in previous weak-set
commit.
(resize_table): As in weak-set commit, finalizers do not run within
allocators.
(do_vacuum_weak_table): Add comment about need for trylock.
(scm_c_make_weak_table): Use the new scm_i_register_weak_gc_callback.
Previously this was the case because of a mistyped check for a
"HAVE_GC_TABLE_START_CALLBACK" symbol, whereas the symbol was actually
HAVE_GC_SET_START_CALLBACK. Anyway, it's better to call this after GC
anyway, as vacuuming might need to allocate weak links (on pre-7.3
libgc without GC_move_disappearing_link) or reallocate the backing
vector.
* libguile/weak-set.c (resize_set): When releasing a lock temporarily to do
an allocation, re-lock it afterwards, instead of unlocking again.
* libguile/weak-table.c (resize_table): Likewise.
* libguile/weak-table.h:
* libguile/weak-table.c (scm_weak_table_putq_x)
(scm_weak_table_remq_x, scm_weak_table_clear_x)
(scm_weak_table_for_each): Declare these as returning void instead of
SCM.
* libguile/hashtab.c (scm_hashq_set_x, scm_hashq_remove_x)
(scm_hashv_set_x, scm_hashv_remove_x)
(scm_hash_set_x, scm_hash_remove_x)
(scm_hashx_set_x, scm_hashx_remove_x):
(scm_hash_for_each): For weak tables, have the set! functions return
the values, as they used to do. Have remove! functions return #f,
indicating the lack of a handle. Shim around for-each to return
unspecified, even though that wasn't yet a problem.
* test-suite/tests/weaks.test: Add a test.
* libguile/weak-set.c:
* libguile/weak-table.c: Attempt to avoid bouncing between sizes when
growing the vector causes elements to be removed from the set/table.
* libguile/weak-set.c (make_weak_set):
* libguile/weak-table.c (make_weak_table): If we have a threaded Guile,
* keep a weak set (table) of weak sets (tables). Use this and the
pthread_atfork mechanism to lock and unlock weak sets and weak tables
during a fork().
* libguile/weak-set.h (scm_weak_set_prehistory): New internal API.
* libguile/init.c: Add call to scm_weak_set_prehistory().
* libguile/weak-set.c (resize_set):
* libguile/weak-table.c (resize_table): Drop the set/table lock while
allocating the new vector. Fixes a bug in which a finalizer could
recursively try to grab the port table lock.
* libguile/ports.c (scm_putc, scm_puts):
* libguile/ports.h (scm_putc_unlocked, scm_puts_unlocked): Separate into
_unlocked and locked variants. Change all callers to use the
_unlocked versions.
* libguile/tags.h (SCM_HEAP_OBJECT_BASE): New macro. Given a SCM,
returns a pointer to the start of its memory area on the heap.
* libguile/bytevectors.c:
* libguile/fluids.c:
* libguile/foreign.c:
* libguile/gc.h:
* libguile/guardians.c:
* libguile/numbers.h:
* libguile/ports.c:
* libguile/smob.c:
* libguile/struct.c:
* libguile/weak-set.c:
* libguile/weak-table.c:
* libguile/weak-vector.c: Use it.
* libguile/tags.h (SCM_UNPACK_POINTER, SCM_PACK_POINTER): New macros.
The old SCM2PTR and PTR2SCM were defined in such a way that
round-tripping through a pointer could lose precision, even in the
case in which you weren't interested in actually dereferencing the
pointer, it was simply that you needed to plumb a SCM through APIs
that take pointers. These new macros are more like SCM_PACK and
SCM_UNPACK, but for pointer types. The bit representation of the
pointer should be the same as the scm_t_bits representation.
* libguile/gc.h (PTR2SCM, SCM2PTR): Remove support for (old) UNICOS
pointers. We are going to try tagging the SCM object itself in the
future, and I don't think that keeping this support is worth its
cost. It probably doesn't work anyway.
* libguile/backtrace.c:
* libguile/bytevectors.c:
* libguile/continuations.c:
* libguile/fluids.c:
* libguile/foreign.c:
* libguile/gc.h:
* libguile/guardians.c:
* libguile/hashtab.c:
* libguile/load.c:
* libguile/numbers.c:
* libguile/ports.c:
* libguile/smob.c:
* libguile/strings.c:
* libguile/symbols.c:
* libguile/vm.c:
* libguile/weak-set.c:
* libguile/weak-table.c:
* libguile/weak-vector.c: Update many sites to use the new macros.
* libguile/hashtab.c:
* libguile/hashtab.h: Reimplement the weak hash table implementation in
terms of weak tables. All is well except for the horrific hack for
hashx tables.
* libguile/weak-table.h:
* libguile/weak-table.c (scm_make_weak_key_hash_table)
(scm_make_weak_value_hash_table, scm_make_doubly_weak_hash_table)
(scm_weak_key_hash_table_p, scm_weak_value_hash_table_p)
(scm_doubly_weak_hash_table_p): Move these definitions here.
* libguile/weak-table.c:
* libguile/weak-table.h: New files, implementing open-addressed weak
hash tables, similar to the implementation of weak sets. This will
let us remove weak pairs.
* libguile.h:
* libguile/Makefile.am:
* libguile/evalext.c:
* libguile/gc.c:
* libguile/init.c:
* libguile/print.c:
* libguile/tags.h: Update all the pieces for the new files and tc7.