]> gitweb.factorcode.org Git - factor.git/commitdiff
alien: a test and docs for free-callback
authorBjörn Lindqvist <bjourne@gmail.com>
Tue, 16 Sep 2014 15:14:39 +0000 (17:14 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 29 Sep 2014 14:30:21 +0000 (07:30 -0700)
core/alien/alien-docs.factor
core/alien/alien-tests.factor

index 4230d14631363cf8d5962d730b6b4670f70ae7d9..b4c61e6ce33200c7f8d5e199f59907623648987d 100644 (file)
@@ -60,6 +60,13 @@ $nl
 
 { <alien> <displaced-alien> alien-address } related-words
 
+HELP: free-callback
+{ $values { "alien" alien } }
+{ $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." } ;
+
+{ <callback> free-callback } related-words
+
 HELP: alien-address
 { $values { "c-ptr" c-ptr } { "addr" "a non-negative integer" } }
 { $description "Outputs the address of an alien." }
index 075a672bbde57f33f4ba185d3144ff7eeb1ca5ee..f6de02b621bc994e2440c436de8b406e94ee7cc2 100644 (file)
@@ -1,6 +1,6 @@
-USING: accessors alien alien.accessors alien.syntax byte-arrays arrays
-kernel kernel.private namespaces tools.test sequences libc math
-system prettyprint layouts alien.libraries sets ;
+USING: accessors alien alien.accessors alien.c-types alien.libraries
+alien.syntax arrays byte-arrays continuations fry kernel kernel.private layouts
+libc math namespaces prettyprint sequences sets system tools.test ;
 FROM: namespaces => set ;
 IN: alien.tests
 
@@ -48,7 +48,7 @@ cell 8 = [
       0x123412341234 over 0 set-alien-signed-8
       0 alien-signed-8
     ] unit-test
-    
+
     [ 0x123412341234 ] [
       8 <byte-array>
       0x123412341234 over 0 set-alien-signed-cell
@@ -85,3 +85,17 @@ f initialize-test set-global
 [ 7575 ] [ initialize-test [ 7575 ] initialize-alien ] unit-test
 
 [ { BAD-ALIEN } ] [ { BAD-ALIEN BAD-ALIEN BAD-ALIEN } members ] unit-test
+
+! Generate callbacks until the whole callback-heap is full, then free
+! them. Do it ten times in a row for good measure.
+: produce-until-error ( quot -- error seq )
+    '[ [ @ t ] [ f ] recover ] [ ] produce ; inline
+
+SYMBOL: foo
+
+: fill-and-free-callback-heap ( -- )
+    [ \ foo 33 <callback> ] produce-until-error nip [ free-callback ] each ;
+
+[ ] [
+    10 [ fill-and-free-callback-heap ] times
+] unit-test