]> gitweb.factorcode.org Git - factor.git/blob - extra/memory/piles/piles.factor
690845dfdc5d9df3b1d0ecfad7155e85f3397e2b
[factor.git] / extra / memory / piles / piles.factor
1 ! Copyright (C) 2009 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data destructors kernel libc math ;
4 IN: memory.piles
5
6 TUPLE: pile
7     { underlying c-ptr }
8     { size integer }
9     { offset integer } ;
10
11 ERROR: not-enough-pile-space pile ;
12
13 M: pile dispose
14     [ [ free ] when* f ] change-underlying drop ;
15
16 : <pile> ( size -- pile )
17     [ malloc ] keep 0 pile boa ;
18
19 : pile-empty ( pile -- )
20     0 >>offset drop ;
21
22 : pile-alloc ( pile size -- alien )
23     [
24         [ [ ] [ size>> ] [ offset>> ] tri ] dip +
25         < [ not-enough-pile-space ] [ drop ] if
26     ] [
27         drop [ offset>> ] [ underlying>> ] bi <displaced-alien>
28     ] [
29         [ + ] curry change-offset drop
30     ] 2tri ;
31
32 : <pile-c-object> ( pile c-type -- alien )
33     heap-size pile-alloc ; inline
34
35 : <pile-c-array> ( pile n c-type -- alien )
36     heap-size * pile-alloc ; inline
37
38 : pile-align ( pile align -- pile )
39     [ align ] curry change-offset ;