2008-12-03 10:41:48 -05:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: accessors alien alien.c-types byte-arrays kernel libc
|
|
|
|
math sequences sequences.private ;
|
|
|
|
IN: struct-arrays
|
|
|
|
|
|
|
|
TUPLE: struct-array
|
|
|
|
{ underlying c-ptr read-only }
|
|
|
|
{ length array-capacity read-only }
|
|
|
|
{ element-size array-capacity read-only } ;
|
|
|
|
|
|
|
|
M: struct-array length length>> ;
|
|
|
|
|
|
|
|
M: struct-array nth-unsafe
|
|
|
|
[ element-size>> * ] [ underlying>> ] bi <displaced-alien> ;
|
|
|
|
|
|
|
|
M: struct-array set-nth-unsafe
|
|
|
|
[ nth-unsafe swap ] [ element-size>> ] bi memcpy ;
|
|
|
|
|
|
|
|
M: struct-array new-sequence
|
|
|
|
element-size>> [ * <byte-array> ] 2keep struct-array boa ; inline
|
|
|
|
|
|
|
|
: <struct-array> ( length c-type -- struct-array )
|
|
|
|
heap-size [ * <byte-array> ] 2keep struct-array boa ; inline
|
|
|
|
|
|
|
|
ERROR: bad-byte-array-length byte-array ;
|
|
|
|
|
|
|
|
: byte-array>struct-array ( byte-array c-type -- struct-array )
|
|
|
|
heap-size [
|
|
|
|
[ dup length ] dip /mod 0 =
|
|
|
|
[ drop bad-byte-array-length ] unless
|
|
|
|
] keep struct-array boa ; inline
|
|
|
|
|
|
|
|
: <direct-struct-array> ( alien length c-type -- struct-array )
|
2008-12-18 22:17:24 -05:00
|
|
|
heap-size struct-array boa ; inline
|
2008-12-03 10:41:48 -05:00
|
|
|
|
2008-12-03 10:54:02 -05:00
|
|
|
: malloc-struct-array ( length c-type -- struct-array )
|
2009-05-11 19:01:35 -04:00
|
|
|
[ heap-size calloc ] 2keep <direct-struct-array> ; inline
|
2008-12-03 10:54:02 -05:00
|
|
|
|
2008-12-03 10:41:48 -05:00
|
|
|
INSTANCE: struct-array sequence
|