1 ! (c)2009, 2010 Slava Pestov, Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.arrays alien.strings
3 arrays byte-arrays cpu.architecture fry io io.encodings.binary
4 io.files io.streams.memory kernel libc math math.functions
5 sequences words macros combinators generalizations
6 stack-checker.dependencies combinators.short-circuit ;
10 GENERIC: require-c-array ( c-type -- )
12 M: array require-c-array first require-c-array ;
14 GENERIC: c-array-constructor ( c-type -- word ) foldable
16 GENERIC: c-(array)-constructor ( c-type -- word ) foldable
18 GENERIC: c-direct-array-constructor ( c-type -- word ) foldable
20 GENERIC: <c-array> ( len c-type -- array )
23 c-array-constructor execute( len -- array ) ; inline
25 GENERIC: (c-array) ( len c-type -- array )
28 c-(array)-constructor execute( len -- array ) ; inline
30 GENERIC: <c-direct-array> ( alien len c-type -- array )
32 M: word <c-direct-array>
33 c-direct-array-constructor execute( alien len -- array ) ; inline
35 : malloc-array ( n type -- array )
36 [ heap-size calloc ] [ <c-direct-array> ] 2bi ; inline
38 : (malloc-array) ( n type -- alien )
39 [ heap-size * malloc ] [ <c-direct-array> ] 2bi ; inline
41 : <c-object> ( type -- array )
42 heap-size <byte-array> ; inline
44 : (c-object) ( type -- array )
45 heap-size (byte-array) ; inline
47 : malloc-object ( type -- alien )
48 1 swap heap-size calloc ; inline
50 : (malloc-object) ( type -- alien )
51 heap-size malloc ; inline
53 : malloc-byte-array ( byte-array -- alien )
54 binary-object [ nip malloc dup ] 2keep memcpy ;
56 : memory>byte-array ( alien len -- byte-array )
57 [ nip (byte-array) dup ] 2keep memcpy ;
59 : malloc-string ( string encoding -- alien )
60 string>alien malloc-byte-array ;
62 M: memory-stream stream-read
64 [ index>> ] [ alien>> ] bi <displaced-alien>
65 swap memory>byte-array
66 ] [ [ + ] change-index drop ] 2bi ;
68 M: value-type c-type-rep drop int-rep ;
70 M: value-type c-type-getter
71 drop [ swap <displaced-alien> ] ;
73 M: value-type c-type-setter ( type -- quot )
74 [ c-type-getter ] [ heap-size ] bi '[ @ swap _ memcpy ] ;
76 M: array c-type-boxer-quot
77 unclip [ array-length ] dip [ <c-direct-array> ] 2curry ;
79 M: array c-type-unboxer-quot drop [ >c-ptr ] ;
81 ERROR: local-allocation-error ;
85 : (local-allot) ( size align -- alien ) local-allocation-error ;
87 : (cleanup-allot) ( -- )
88 ! Inhibit TCO in order for the last word in the quotation
89 ! to still be abl to access scope-allocated data.
92 MACRO: (simple-local-allot) ( c-type -- quot )
94 [ dup '[ _ heap-size _ c-type-align (local-allot) ] ] bi ;
96 : [hairy-local-allot] ( c-type initial -- quot )
97 over '[ _ (simple-local-allot) _ over 0 _ set-alien-value ] ;
99 : hairy-local-allot? ( obj -- ? )
103 [ second initial: eq? ]
106 MACRO: (hairy-local-allot) ( obj -- quot )
107 dup hairy-local-allot?
108 [ first3 nip [hairy-local-allot] ]
109 [ '[ _ (simple-local-allot) ] ]
112 MACRO: (local-allots) ( c-types -- quot )
113 [ '[ _ (hairy-local-allot) ] ] map [ ] join ;
115 MACRO: box-values ( c-types -- quot )
116 [ c-type-boxer-quot ] map '[ _ spread ] ;
118 MACRO: out-parameters ( c-types -- quot )
119 [ dup hairy-local-allot? [ first ] when ] map
120 [ length ] [ [ '[ 0 _ alien-value ] ] map ] bi
121 '[ _ nkeep _ spread ] ;
125 : with-scoped-allocation ( c-types quot -- )
126 [ [ (local-allots) ] [ box-values ] bi ] dip call
127 (cleanup-allot) ; inline
129 : with-out-parameters ( c-types quot finish -- values... )
130 [ [ drop (local-allots) ] [ swap out-parameters ] 2bi ] dip call
131 (cleanup-allot) ; inline
133 GENERIC: binary-zero? ( value -- ? )
135 M: object binary-zero? drop f ; inline
136 M: f binary-zero? drop t ; inline
137 M: integer binary-zero? zero? ; inline
138 M: math:float binary-zero? double>bits zero? ; inline
139 M: complex binary-zero? >rect [ binary-zero? ] both? ; inline