]> gitweb.factorcode.org Git - factor.git/blob - core/memory/memory.factor
Merge branch 'master' into experimental
[factor.git] / core / memory / memory.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel continuations sequences vectors arrays system math ;
4 IN: memory
5
6 : (each-object) ( quot: ( obj -- ) -- )
7     next-object dup [
8         swap [ call ] keep (each-object)
9     ] [ 2drop ] if ; inline recursive
10
11 : each-object ( quot -- )
12     gc begin-scan [ (each-object) ] [ end-scan ] [ ] cleanup ; inline
13
14 : count-instances ( quot -- n )
15     0 swap [ 1 0 ? + ] compose each-object ; inline
16
17 : instances ( quot -- seq )
18     #! To ensure we don't need to grow the vector while scanning
19     #! the heap, we do two scans, the first one just counts the
20     #! number of objects that satisfy the predicate.
21     [ count-instances 100 + <vector> ] keep swap
22     [ [ push-if ] 2curry each-object ] keep >array ; inline
23
24 : save ( -- ) image save-image ;