PreviousUpNext

15.4.889  src/lib/src/finalize-g.pkg

## finalize-g.pkg
## AUTHOR:  John Reppy
##          AT&T Bell Laboratories
##          Murray Hill, NJ 07974
##          jhr@research.att.com

# Compiled by:
#     src/lib/std/standard.lib


stipulate
    package wkr =  weak_reference;                                              # weak_reference        is from   src/lib/std/src/nj/weak-reference.pkg
herein

    generic package   finalize_g   (chunk:  Finalized_Chunk)
    :                 Finalize                                                  # Finalize              is from   src/lib/src/finalize.api
    {
        package chunk =  chunk;

        chunk_list
            =
            REF ([] :  List( (wkr::Weak_Reference( chunk::Chunk ), chunk::Chunk_Info)) );


        fun register_chunk (chunk, info)
            =
            chunk_list
                :=
                (wkr::make_weak_reference chunk, info) ! *chunk_list;


        fun prune ([], live, dead)
                =>
                (live, dead);

            prune ((x as (weakref, info)) ! r, live, dead)
                =>
                case (wkr::get_normal_reference_from_weak_reference  weakref)
                    #
                    THE _  =>  prune (r, x ! live,        dead);
                    NULL   =>  prune (r,     live, info ! dead);
                esac;
        end;

        fun get_dead ()
            =
            {   my (live, dead)
                    =
                    prune (*chunk_list, [], []);

                chunk_list := live;
                dead;
            };

        fun finalize ()
            =
            {   fun reap ([], live)
                        =>
                        live;

                    reap ((x as (weakref, info)) ! r, live)
                        =>
                        case (wkr::get_normal_reference_from_weak_reference  weakref)
                            #
                            THE _ =>    reap (r, x ! live);
                            #
                            NULL  =>    {   chunk::finalize info;
                                            reap (r, live);
                                        };
                        esac;
                end;

                chunk_list
                    :=
                    reap (*chunk_list, []);
            };

    };                  # finalize_g
end;

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


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext