Next: iklib records printer, Previous: iklib records fields, Up: iklib records [Index]
Vicare allows records to be finalised either explicitly or automatically by the garbage collector, by applying a destruction function to them; here is how automatic finalisation works:
#!r6rs (import (vicare)) (define-record-type <alpha> (fields a b c)) (define (<alpha>-destructor rec) (fprintf (current-error-port) "finalising ~s\n" rec)) (record-type-destructor-set! (record-type-descriptor <alpha>) <alpha>-destructor) (pretty-print (make-<alpha> 1 2 3) (current-error-port)) -| (record <alpha> (a 1) (b 2) (c 3)) (collect) -| finalising #[record <alpha> a=1 b=2 c=3]
A destructor function is associated to a record–type by registering it in the record–type descriptor. After a destructor is set in the descriptor: new instances of the record–type are registered, upon creation, into an internal guardian, iklib guardians for details; whenever such records are garbage collected: the guardian applies the destructor to them.
When the destructor is called by the garbage collector: exceptions
raised by it are catched with guard
and discarded; destructor
functions should take care of exceptions by themselves.
It is possible for a destructor function to be applied multiple times to the same record: once a destructor is set in the descriptor, it can be explicitly applied to records and later applied again by the garbage collector. Destructor functions must be written in such a way that multiple applications are not a problem. For example, it is usually possible, upon destruction, to reset some record fields to the void object: when the destructor detects a field set to void, it knows that the record has already been finalised.
Select the procedure destructor as destructor for R6RS records of type rtd; return unspecified values. The destructor accepts a single argument being the record to finalise; the destructor can return unspecified values.
Return the function set in rtd as instance destructor; return
#f
if no destructor is set in rtd.
Return the current destructor for the record record: #f
or a
function.
Select a record destruction logging mode for debugging purposes. When a record is finalised by the garbage collector, using the destructor registered in the RTD:
#f
: no additional actions are
performed.
#t
: the function
record-guardian-log
is used to log the operations to the textual
output port returned by current-error-port
.
See the documentation of record-guardian-log
for the calling
protocol of the logger functions.
Built in logger function to be used to log record finalisation
operations by the garbage collector. record is the record to be
finalised; exception is #f
or an object raised by the record
destructor; action is one of the symbols:
before-destruction
, after-destruction
, exception
.
When this function is used as value for the parameter
record-guardian-logger
:
registration
and
exception set to #f
.
before-destruction
and
exception set to #f
.
after-destruction
and
exception set to #f
.
exception
and exception set to the raised object.
The current implementation is the following:
(define (record-guardian-log S E action) (case action ((registration) (fprintf (current-error-port) "*** Vicare debug: record guardian: registered record:\n\ ***\t~s\n" S)) ((before-destruction) (fprintf (current-error-port) "*** Vicare debug: record guardian: before destruction:\n\ ***\t~s\n" S)) ((after-destruction) (fprintf (current-error-port) "*** Vicare debug: record guardian: after destruction:\n\ ***\t~s\n" S)) ((exception) (fprintf (current-error-port) "*** Vicare debug: record guardian: exception:\n\ ***\t~s\n\ ***\t~s\n" S E)) (else (assertion-violation 'record-guardian-log "invalid action in record destruction process" S action))))
Next: iklib records printer, Previous: iklib records fields, Up: iklib records [Index]