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