PreviousUpNext

15.4.1141  src/lib/std/src/nj/weak-reference.pkg

## weak-reference.pkg
#
# Weak references provide access to a value while
# still allowing it to be garbage collected.
#
# A  typical application is to keep an index of all
# existing values of a particular sort (say, open
# X windows connections), while still allowing old
# values to be garbe-collected normally.
#
# The penalty for using a weak reference is that
# any access to its value may return NULL due to
# the underlying value having been garbage-collected.

# Compiled by:
#     src/lib/std/src/standard-core.sublib

package   weak_reference
:         Weak_Reference                                        # Weak_Reference        is from   src/lib/std/src/nj/weak-reference.api
{
    weak_pointer_ctag = 2;
        #
        # The above definition must track the
        #     src/lib/compiler/back/low/main/main/heap-tags.pkg
        # definition
        #    weak_pointer_ctag = 2;
        # and the
        #   src/c/h/heap-tags.h
        # definition
        #   #define WEAK_POINTER_CTAG 2
        #
        #       Eventually, we might make weak and strong into primops,
        #       so that we don't need to keep things synchronized.              XXX BUGGO FIXME

    Weak_Reference(X) = X;

    fun make_weak_reference (x:  X) : Weak_Reference(X)
        =
        inline_t::make_special (weak_pointer_ctag, x);

    fun get_normal_reference_from_weak_reference (x:  Weak_Reference(X)) : Null_Or(X)
        =
        if (inline_t::getspecial x == weak_pointer_ctag)
            #
            THE (inline_t::record_get (inline_t::cast x, 0));
        else
            NULL;
        fi;

    Weak_Reference' =  runtime::Chunk;

    fun make_weak_reference'                      x =   inline_t::make_special (weak_pointer_ctag, x);
    fun get_normal_reference_from_weak_reference' x =   inline_t::getspecial   x == weak_pointer_ctag;
};



## COPYRIGHT (c) 1995 AT&T Bell Laboratories.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext