diff --git a/basis/struct-arrays/struct-arrays-docs.factor b/basis/struct-arrays/struct-arrays-docs.factor index 7b49d6ef42..175b2e2783 100644 --- a/basis/struct-arrays/struct-arrays-docs.factor +++ b/basis/struct-arrays/struct-arrays-docs.factor @@ -7,11 +7,11 @@ $nl "The " { $slot "underlying" } " slot holds a " { $link c-ptr } " with the raw data. This pointer can be passed to C functions." } ; HELP: -{ $values { "length" integer } { "c-type" string } { "struct-array" struct-array } } -{ $description "Creates a new array for holding values of the specified C type." } ; +{ $values { "length" integer } { "struct-class" struct-class } { "struct-array" struct-array } } +{ $description "Creates a new array for holding values of the specified struct type." } ; HELP: -{ $values { "alien" c-ptr } { "length" integer } { "c-type" string } { "struct-array" struct-array } } +{ $values { "alien" c-ptr } { "length" integer } { "struct-class" struct-class } { "struct-array" struct-array } } { $description "Creates a new array for holding values of the specified C type, backed by the memory at " { $snippet "alien" } "." } ; HELP: struct-array-on diff --git a/basis/struct-arrays/struct-arrays.factor b/basis/struct-arrays/struct-arrays.factor index cc34072d2c..3adc4496ee 100755 --- a/basis/struct-arrays/struct-arrays.factor +++ b/basis/struct-arrays/struct-arrays.factor @@ -5,9 +5,6 @@ classes classes.struct kernel libc math parser sequences sequences.private words fry memoize compiler.units ; IN: struct-arrays -: c-type-struct-class ( c-type -- class ) - c-type boxed-class>> ; foldable - TUPLE: struct-array { underlying c-ptr read-only } { length array-capacity read-only } @@ -15,35 +12,36 @@ TUPLE: struct-array { class read-only } { ctor read-only } ; -M: struct-array length length>> ; inline -M: struct-array byte-length [ length>> ] [ element-size>> ] bi * ; inline +> * >fixnum ] [ underlying>> ] bi ; inline +: (struct-element-constructor) ( struct-class -- word ) + [ + "struct-array-ctor" f + [ swap '[ _ memory>struct ] (( alien -- object )) define-inline ] keep + ] with-compilation-unit ; + +! Foldable memo word. This is an optimization; by precompiling a +! constructor for array elements, we avoid memory>struct's slow path. +MEMO: struct-element-constructor ( struct-class -- word ) + (struct-element-constructor) ; foldable + +PRIVATE> + +M: struct-array length length>> ; inline + +M: struct-array byte-length [ length>> ] [ element-size>> ] bi * ; inline + M: struct-array nth-unsafe [ (nth-ptr) ] [ ctor>> ] bi execute( alien -- object ) ; inline M: struct-array set-nth-unsafe [ (nth-ptr) swap ] [ element-size>> ] bi memcpy ; inline -: (struct-element-constructor) ( c-type -- word ) - [ - "struct-array-ctor" f - [ - swap dup struct-class? - [ '[ _ memory>struct ] [ ] like ] [ drop [ ] ] if - (( alien -- object )) define-inline - ] keep - ] with-compilation-unit ; - -! Foldable memo word. This is an optimization; by precompiling a -! constructor for array elements, we avoid memory>struct's slow path. -MEMO: struct-element-constructor ( c-type -- word ) - (struct-element-constructor) ; foldable - -: ( alien length c-type -- struct-array ) - [ heap-size ] [ c-type-struct-class ] [ struct-element-constructor ] +: ( alien length struct-class -- struct-array ) + [ heap-size ] [ ] [ struct-element-constructor ] tri struct-array boa ; inline M: struct-array new-sequence