alien,stack-checker.alien: new word with-callback
Since callbacks aren't automatically deallocated this word intends to make sure "inline" callbacks are.db4
parent
8206adc2bb
commit
aad8d4b8d6
|
@ -105,10 +105,7 @@ TUPLE: alien-callback-params < alien-node-params xt ;
|
||||||
infer-return ;
|
infer-return ;
|
||||||
|
|
||||||
: callback-xt ( word -- alien )
|
: callback-xt ( word -- alien )
|
||||||
callbacks get [
|
callbacks get [ dup "stack-cleanup" word-prop <callback> ] cache ;
|
||||||
dup "stack-cleanup" word-prop <callback>
|
|
||||||
[ callback-destructor new-disposable callback<< ] keep
|
|
||||||
] cache ;
|
|
||||||
|
|
||||||
: callback-bottom ( params -- )
|
: callback-bottom ( params -- )
|
||||||
"( callback )" <uninterned-word> >>xt
|
"( callback )" <uninterned-word> >>xt
|
||||||
|
|
|
@ -65,7 +65,11 @@ HELP: free-callback
|
||||||
{ $description "Releases the callback heap memory allocated for an alien callback. " }
|
{ $description "Releases the callback heap memory allocated for an alien callback. " }
|
||||||
{ $warning "If the callback is invoked (either from C or Factor) after it has been freed, then Factor may crash." } ;
|
{ $warning "If the callback is invoked (either from C or Factor) after it has been freed, then Factor may crash." } ;
|
||||||
|
|
||||||
{ <callback> free-callback } related-words
|
HELP: with-callback
|
||||||
|
{ $values { "alien" alien } { "quot" quotation } }
|
||||||
|
{ $description "Calls the quotation with an alien value on the stack which is supposed to be a callback. Resources for the callback is guaranteed to be released afterwards." } ;
|
||||||
|
|
||||||
|
{ <callback> free-callback unregister-and-free-callback with-callback } related-words
|
||||||
|
|
||||||
HELP: alien-address
|
HELP: alien-address
|
||||||
{ $values { "c-ptr" c-ptr } { "addr" "a non-negative integer" } }
|
{ $values { "c-ptr" c-ptr } { "addr" "a non-negative integer" } }
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
! Copyright (C) 2004, 2010 Slava Pestov.
|
! Copyright (C) 2004, 2010 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors assocs byte-arrays byte-vectors continuations.private
|
USING: accessors assocs byte-arrays byte-vectors continuations
|
||||||
destructors init kernel kernel.private math namespaces sequences ;
|
continuations.private destructors init kernel kernel.private math namespaces
|
||||||
|
sequences ;
|
||||||
IN: alien
|
IN: alien
|
||||||
|
|
||||||
BUILTIN: alien { underlying c-ptr read-only initial: f } expired ;
|
BUILTIN: alien { underlying c-ptr read-only initial: f } expired ;
|
||||||
|
@ -111,15 +112,6 @@ SYMBOL: callbacks
|
||||||
current-callback
|
current-callback
|
||||||
[ 2drop call ] [ swap call( callback -- ) drop ] 3bi ; inline
|
[ 2drop call ] [ swap call( callback -- ) drop ] 3bi ; inline
|
||||||
|
|
||||||
! Used by stack-checker.alien to register destructors for callbacks.
|
|
||||||
TUPLE: callback-destructor callback ;
|
|
||||||
|
|
||||||
: delete-values ( value assoc -- )
|
|
||||||
[ rot drop = not ] with assoc-filter! drop ;
|
|
||||||
|
|
||||||
M: callback-destructor dispose ( disposable -- )
|
|
||||||
callback>> [ callbacks get delete-values ] [ free-callback ] bi ;
|
|
||||||
|
|
||||||
! A utility for defining global variables that are recompiled in
|
! A utility for defining global variables that are recompiled in
|
||||||
! every session
|
! every session
|
||||||
TUPLE: expiry-check object alien ;
|
TUPLE: expiry-check object alien ;
|
||||||
|
@ -127,8 +119,17 @@ TUPLE: expiry-check object alien ;
|
||||||
: recompute-value? ( check -- ? )
|
: recompute-value? ( check -- ? )
|
||||||
dup [ alien>> expired? ] [ drop t ] if ;
|
dup [ alien>> expired? ] [ drop t ] if ;
|
||||||
|
|
||||||
|
: delete-values ( value assoc -- )
|
||||||
|
[ rot drop = not ] with assoc-filter! drop ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
: unregister-and-free-callback ( alien -- )
|
||||||
|
[ callbacks get delete-values ] [ free-callback ] bi ;
|
||||||
|
|
||||||
|
: with-callback ( alien quot -- )
|
||||||
|
over [ unregister-and-free-callback ] curry [ ] cleanup ; inline
|
||||||
|
|
||||||
: initialize-alien ( symbol quot -- )
|
: initialize-alien ( symbol quot -- )
|
||||||
swap dup get-global dup recompute-value?
|
swap dup get-global dup recompute-value?
|
||||||
[ drop [ call dup 31337 <alien> expiry-check boa ] dip set-global ]
|
[ drop [ call dup 31337 <alien> expiry-check boa ] dip set-global ]
|
||||||
|
|
Loading…
Reference in New Issue