2024-09-12 02:03:05 +00:00
|
|
|
* Guile Weak References
|
2024-09-12 01:59:02 +00:00
|
|
|
|
|
|
|
This is a simple little library that makes use of the weak hash table functionality in core Guile
|
|
|
|
to hackily implement weak references, objects which weakly point to other objects.
|
|
|
|
|
|
|
|
The interface is very simple:
|
|
|
|
|
|
|
|
(make-weakref obj)
|
|
|
|
- Create a weak reference, which will not count as a reference, so the object can be garbage collected
|
|
|
|
|
|
|
|
(weakref-object obj)
|
|
|
|
- Get the object, or #f if it has been garbage collected.
|
|
|
|
|
2024-09-12 02:03:05 +00:00
|
|
|
** Implementation
|
2024-09-12 01:59:02 +00:00
|
|
|
This is a very simple implementation. There's a globally incremented ID associated with each object, and
|
|
|
|
the object is stored in two global weak hash tables, one id-to-object and another object-to-id. The weak
|
|
|
|
reference is a record type that stores the ID and also a tombstone property to cache whether the object
|
|
|
|
has been collected to avoid the need to look it up after that point.
|
|
|
|
|
|
|
|
I created this because I needed an interface outside of the hash tables to refer to single objects instead
|
|
|
|
of creating a bunch of weak dictionaries. Also, Guile Hoot does not yet support iteration through weak key
|
|
|
|
hash tables, so this will allow me to create a list of weak refs to iterate over instead.
|