]> gitweb.factorcode.org Git - factor.git/commitdiff
Fix instances to not allocate memory while scanning heap
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 7 Sep 2008 01:34:02 +0000 (20:34 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 7 Sep 2008 01:34:02 +0000 (20:34 -0500)
core/memory/memory-tests.factor
core/memory/memory.factor

index 3fe1387582b17d8dbafe60ee974197671f51ff64..9fded3eb3a6bafc45a81a5008a0c3cf4a50b4c6c 100755 (executable)
@@ -3,6 +3,8 @@ sequences tools.test words namespaces layouts classes
 classes.builtin arrays quotations ;
 IN: memory.tests
 
+[ [ ] instances ] must-infer
+
 ! Code GC wasn't kicking in when needed
 : leak-step 800000 f <array> 1quotation call drop ;
 
index cb5c5bf7e4d4cfa9dfb4054d285648f0a5edf448..42527371f2ffa9957d2e71415e71a0126df851a0 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2005, 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel continuations sequences arrays system ;
+USING: kernel continuations sequences vectors arrays system math ;
 IN: memory
 
 : (each-object) ( quot: ( obj -- ) -- )
@@ -9,7 +9,14 @@ IN: memory
 : each-object ( quot -- )
     begin-scan [ (each-object) ] [ end-scan ] [ ] cleanup ; inline
 
+: count-instances ( quot -- n )
+    0 swap [ 1 0 ? + ] compose each-object ; inline
+
 : instances ( quot -- seq )
-    pusher [ each-object ] dip >array ; inline
+    #! To ensure we don't need to grow the vector while scanning
+    #! the heap, we do two scans, the first one just counts the
+    #! number of objects that satisfy the predicate.
+    [ count-instances 100 + <vector> ] keep swap
+    [ [ push-if ] 2curry each-object ] keep >array ; inline
 
 : save ( -- ) image save-image ;