.. highlight:: none


.. index::
   pair: finalization; design

.. _design-finalize:


Finalization
============

.. mps:prefix:: design.mps.finalize


Overview
--------

:mps:tag:`overview` Finalization is implemented internally using the MRG
pool class (design.mps.poolmrg_). Objects can be registered for
finalization by calling :c:func:`mps_finalize()`. Notification of
finalization is given to the client via the messaging interface
(design.mps.message_). The MRG pool class implements a :c:type:`Message`
subclass which implements the finalization messages.

.. _design.mps.poolmrg: poolmrg.html
.. _design.mps.message: message.html


Requirements
------------

:mps:tag:`req` Historically only Dylan had requirements for finalization,
see req.dylan.fun.final_. Now (2003-02-19) Configura have requirements
for finalization. Happily they are very similar.

.. _req.dylan.fun.final: https://info.ravenbrook.com/project/mps/import/2001-09-27/mminfo/doc/req/dylan


Implementation
--------------

:mps:tag:`impl.over` Registering an object for finalization corresponds to
allocating a reference of rank FINAL to that object. This reference is
allocated in a guardian object in a pool belonging to the MRG pool
class (see design.mps.poolmrg_).

.. _design.mps.poolmrg: poolmrg.html

:mps:tag:`impl.arena.struct` A single pool belonging to the MRG pool class
and used for managing final references is kept in the arena and
referred to as the "final pool".

:mps:tag:`impl.arena.lazy` The final pool is lazily created. It is not
created until the first object is registered for finalization.

:mps:tag:`impl.arena.flag` There is a flag in the Arena that indicates
whether the final pool has been created yet or not.

:mps:tag:`impl.scan` An object is determined to be finalizable if it is
fixed at rank FINAL for a trace, and was not fixed at any lower rank
for that trace. See design.mps.poolmrg.scan.wasold_.

.. _design.mps.poolmrg.scan.wasold: poolmrg.html#design.mps.poolmrg.scan.wasold

:mps:tag:`impl.message` When an object is determined to be finalizable, a
message for that object is posted to the arena's message queue.

:mps:tag:`impl.arena-destroy.empty` :c:func:`ArenaDestroy()` empties the message
queue by calling :c:func:`MessageEmpty()`.

:mps:tag:`impl.arena-destroy.final-pool` If the final pool has been created
then :c:func:`ArenaDestroy()` destroys the final pool.

:mps:tag:`impl.access` :c:func:`mps_message_finalization_ref()` needs to access
the finalization message to retrieve the reference and then write it
to where the client asks. This must be done carefully, in order to
avoid invalidating collection invariants such as the segment summary.

:mps:tag:`impl.invariants` We protect the invariants by using
:c:func:`ArenaRead()` and :c:func:`ArenaWrite()` to read and write the reference
via the software barrier.


External interface
------------------

:mps:tag:`if.register` :c:func:`mps_finalize()` registers an object for
finalization.

:mps:tag:`if.deregister` :c:func:`mps_definalize()` deregisters an object for
finalization. It is an error to definalize an object that has not been
registered for finalization.

:mps:tag:`if.get-ref` :c:func:`mps_message_finalization_ref()` returns the reference
to the finalized object stored in the finalization message.

:mps:tag:`if.multiple` The external interface allows an object to be
registered multiple times, but does not specify the number of
finalization messages that will be posted for that object.


Internal interface
------------------

.. c:function:: Res ArenaFinalize(Arena arena, Ref addr)

:mps:tag:`int.finalize.create` Creates the final pool if it has not been
created yet.

:mps:tag:`int.finalize.alloc` Allocates a guardian in the final pool.

:mps:tag:`int.finalize.alloc.multiple` A consequence of this implementation
is that if an object is finalized multiple times, then multiple
guardians are created in the final pool, and so multiple messages will
be posted to the message queue when the object is determined to be
finalizable. But this behaviour is not guaranteed by the
documentation, leaving us free to change the iplementation.

:mps:tag:`int.finalize.write` Writes a reference to the object into the
guardian object.

:mps:tag:`int.finalize.all` That's all.

:mps:tag:`int.finalize.error` If either the creation of the pool or the
allocation of the object fails then the error is returned to the
caller.

:mps:tag:`int.finalize.error.no-unwind` This function does not need to do
any unwinding in the error cases because the creation of the pool is
not something that needs to be undone.

.. c:function:: Res ArenaDefinalize(Arena arena, Ref obj)

:mps:tag:`int.definalize.fail` If the final pool has not been created,
return ``ResFAIL`` immediately.

:mps:tag:`int.definalize.search` Otherwise, search for a guardian in the
final pool that refers to the object and which has not yet been
finalized. If one is found, delete it and return ``ResOK``. Otherwise
no guardians in the final pool refer to the object, so return
``ResFAIL``.