From 38b65e00f4fa7896c737a0fd9929cc73d7dfe718 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 6 Sep 2008 20:34:02 -0500 Subject: [PATCH] Fix instances to not allocate memory while scanning heap --- core/memory/memory-tests.factor | 2 ++ core/memory/memory.factor | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/memory/memory-tests.factor b/core/memory/memory-tests.factor index 3fe1387582..9fded3eb3a 100755 --- a/core/memory/memory-tests.factor +++ b/core/memory/memory-tests.factor @@ -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 1quotation call drop ; diff --git a/core/memory/memory.factor b/core/memory/memory.factor index cb5c5bf7e4..42527371f2 100644 --- a/core/memory/memory.factor +++ b/core/memory/memory.factor @@ -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 + ] keep swap + [ [ push-if ] 2curry each-object ] keep >array ; inline : save ( -- ) image save-image ;