! Copyright (C) 2007 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: continuations io.backend libc kernel namespaces sequences system vectors ; IN: destructors SYMBOL: error-destructors SYMBOL: always-destructors TUPLE: destructor object destroyed? ; M: destructor dispose dup destructor-destroyed? [ drop ] [ dup destructor-object dispose t swap set-destructor-destroyed? ] if ; : ( obj -- newobj ) f destructor boa ; : add-error-destructor ( obj -- ) error-destructors get push ; : add-always-destructor ( obj -- ) always-destructors get push ; : dispose-each ( seq -- ) [ dispose ] each ; : do-always-destructors ( -- ) always-destructors get dispose-each ; : do-error-destructors ( -- ) error-destructors get dispose-each ; : with-destructors ( quot -- ) [ V{ } clone always-destructors set V{ } clone error-destructors set [ do-always-destructors ] [ do-error-destructors ] cleanup ] with-scope ; inline ! Memory allocations TUPLE: memory-destructor alien ; C: memory-destructor M: memory-destructor dispose ( obj -- ) memory-destructor-alien free ; : free-always ( alien -- ) add-always-destructor ; : free-later ( alien -- ) add-error-destructor ; ! Handles TUPLE: handle-destructor alien ; C: handle-destructor HOOK: destruct-handle io-backend ( obj -- ) M: handle-destructor dispose ( obj -- ) handle-destructor-alien destruct-handle ; : close-always ( handle -- ) add-always-destructor ; : close-later ( handle -- ) add-error-destructor ; ! Sockets TUPLE: socket-destructor alien ; C: socket-destructor HOOK: destruct-socket io-backend ( obj -- ) M: socket-destructor dispose ( obj -- ) socket-destructor-alien destruct-socket ; : close-socket-always ( handle -- ) add-always-destructor ; : close-socket-later ( handle -- ) add-error-destructor ;