]> gitweb.factorcode.org Git - factor.git/blob - basis/struct-arrays/struct-arrays.factor
Merge branch 'master' into experimental (untested!)
[factor.git] / basis / struct-arrays / struct-arrays.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types byte-arrays kernel libc
4 math sequences sequences.private ;
5 IN: struct-arrays
6
7 TUPLE: struct-array
8 { underlying c-ptr read-only }
9 { length array-capacity read-only }
10 { element-size array-capacity read-only } ;
11
12 M: struct-array length length>> ;
13
14 M: struct-array nth-unsafe
15     [ element-size>> * ] [ underlying>> ] bi <displaced-alien> ;
16
17 M: struct-array set-nth-unsafe
18     [ nth-unsafe swap ] [ element-size>> ] bi memcpy ;
19
20 M: struct-array new-sequence
21     element-size>> [ * <byte-array> ] 2keep struct-array boa ; inline
22
23 : <struct-array> ( length c-type -- struct-array )
24     heap-size [ * <byte-array> ] 2keep struct-array boa ; inline
25
26 ERROR: bad-byte-array-length byte-array ;
27
28 : byte-array>struct-array ( byte-array c-type -- struct-array )
29     heap-size [
30         [ dup length ] dip /mod 0 =
31         [ drop bad-byte-array-length ] unless
32     ] keep struct-array boa ; inline
33
34 : <direct-struct-array> ( alien length c-type -- struct-array )
35     struct-array boa ; inline
36
37 : malloc-struct-array ( length c-type -- struct-array )
38     heap-size [ calloc ] 2keep <direct-struct-array> ;
39
40 INSTANCE: struct-array sequence