API Reference


Portable finalizers, weak hash-tables and weak pointers.


    provides a portable API to finalizers, weak hash-tables and weak
    pointers on all major implementations of the Common Lisp
    programming language. For a good introduction to these
    data-structures, have a look at
    References: Data Types and Implementation} by Bruno Haible.

    Source code is available at
    which you are welcome to use for submitting patches and/or
    reports}. Discussion takes place on

    releases} are available, but the easiest way to install this
    library is via @a[]{Quicklisp}:
    @code{(ql:quickload :trivial-garbage)}.

    @begin[Weak Pointers]{section}
    A @em{weak pointer} holds an object in a way that does not prevent
    it from being reclaimed by the garbage collector.  An object
    referenced only by weak pointers is considered unreachable (or
    "weakly reachable") and so may be collected at any time. When
    that happens, the weak pointer's value becomes @code{nil}.


    @begin[Weak Hash-Tables]{section}
    A @em{weak hash-table} is one that weakly references its keys
    and/or values. When both key and value are unreachable (or weakly
    reachable) that pair is reclaimed by the garbage collector.


    A @em{finalizer} is a hook that is executed after a given object
    has been reclaimed by the garbage collector.

  • Function GC (&key full verbose)
    Initiates a garbage collection. @code{full} forces the collection of all generations, when applicable. When @code{verbose} is @em{true}, diagnostic information about the collection is printed if possible.
  • Function MAKE-WEAK-POINTER (object)
    Creates a new weak pointer which points to @code{object}. For portability reasons, @code{object} must not be @code{nil}.
  • Function WEAK-POINTER-P (object)
    Returns @em{true} if @code{object} is a weak pointer and @code{nil} otherwise.
  • Function WEAK-POINTER-VALUE (weak-pointer)
    If @code{weak-pointer} is valid, returns its value. Otherwise, returns @code{nil}.
  • Function MAKE-WEAK-HASH-TABLE (&rest args &key weakness (weakness-matters t) &allow-other-keys)
    Returns a new weak hash table. In addition to the standard arguments accepted by @code{cl:make-hash-table}, this function adds extra keywords: @code{:weakness} being the kind of weak table it should create, and @code{:weakness-matters} being whether an error should be signalled when that weakness isn't available (the default is to signal an error). @code{weakness} can be one of @code{:key}, @code{:value}, @code{:key-or-value}, @code{:key-and-value}. If @code{weakness} is @code{:key} or @code{:value}, an entry is kept as long as its key or value is reachable, respectively. If @code{weakness} is @code{:key-or-value} or @code{:key-and-value}, an entry is kept if either or both of its key and value are reachable, respectively. @code{tg::make-hash-table} is available as an alias for this function should you wish to import it into your package and shadow @code{cl:make-hash-table}.
  • Function HASH-TABLE-WEAKNESS (ht)
    Returns one of @code{nil}, @code{:key}, @code{:value}, @code{:key-or-value} or @code{:key-and-value}.
  • Function FINALIZE (object function)
    Pushes a new @code{function} to the @code{object}'s list of finalizers. @code{function} should take no arguments. Returns @code{object}. @b{Note:} @code{function} should not attempt to look at @code{object} by closing over it because that will prevent it from being garbage collected.
  • Function CANCEL-FINALIZATION (object)
    Cancels all of @code{object}'s finalizers, if any.