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 ;
8 { underlying c-ptr read-only }
9 { length array-capacity read-only }
10 { element-size array-capacity read-only } ;
12 M: struct-array length length>> ;
14 M: struct-array nth-unsafe
15 [ element-size>> * ] [ underlying>> ] bi <displaced-alien> ;
17 M: struct-array set-nth-unsafe
18 [ nth-unsafe swap ] [ element-size>> ] bi memcpy ;
20 M: struct-array new-sequence
21 element-size>> [ * <byte-array> ] 2keep struct-array boa ; inline
23 : <struct-array> ( length c-type -- struct-array )
24 heap-size [ * <byte-array> ] 2keep struct-array boa ; inline
26 ERROR: bad-byte-array-length byte-array ;
28 : byte-array>struct-array ( byte-array c-type -- struct-array )
30 [ dup length ] dip /mod 0 =
31 [ drop bad-byte-array-length ] unless
32 ] keep struct-array boa ; inline
34 : <direct-struct-array> ( alien length c-type -- struct-array )
35 heap-size struct-array boa ; inline
37 : malloc-struct-array ( length c-type -- struct-array )
38 [ heap-size calloc ] 2keep <direct-struct-array> ;
40 INSTANCE: struct-array sequence