rather than deprecate <c-array>, replace it with <c-type-array> . to stay consistent, rename <c-type-direct-array> to <c-direct-array> and require-c-type-arrays to require-c-arrays
							parent
							
								
									be406fa964
								
							
						
					
					
						commit
						361cc04b02
					
				| 
						 | 
				
			
			@ -7,6 +7,6 @@ $nl
 | 
			
		|||
"C type specifiers for array types are documented in " { $link "c-types-specs" } "."
 | 
			
		||||
$nl
 | 
			
		||||
"Specialized sequences are provided for accessing memory as an array of primitive type values. These sequences are implemented in the " { $vocab-link "specialized-arrays" } " and " { $vocab-link "specialized-arrays.direct" } " vocabulary sets. They can also be loaded and constructed through their primitive C types:"
 | 
			
		||||
{ $subsection require-c-type-arrays }
 | 
			
		||||
{ $subsection <c-type-array> }
 | 
			
		||||
{ $subsection <c-type-direct-array> } ;
 | 
			
		||||
{ $subsection require-c-arrays }
 | 
			
		||||
{ $subsection <c-array> }
 | 
			
		||||
{ $subsection <c-direct-array> } ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -49,10 +49,10 @@ HELP: c-setter
 | 
			
		|||
{ $errors "Throws an error if the type does not exist." } ;
 | 
			
		||||
 | 
			
		||||
HELP: <c-array>
 | 
			
		||||
{ $deprecated "New code should use " { $link <c-type-array> } " or the " { $vocab-link "specialized-arrays" } " vocabularies." }
 | 
			
		||||
{ $values { "n" "a non-negative integer" } { "type" "a C type" } { "array" byte-array } }
 | 
			
		||||
{ $description "Creates a byte array large enough to hold " { $snippet "n" } " values of a C type." }
 | 
			
		||||
{ $errors "Throws an error if the type does not exist or the requested size is negative." } ;
 | 
			
		||||
{ $notes "The appropriate specialized array vocabulary must be loaded; otherwise, a " { $link specialized-array-vocab-not-loaded } " error will be thrown. The vocabulary can be loaded with a " { $link POSTPONE: USING: } " form as usual, or with the " { $link require-c-arrays } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary set for details on the underlying sequence type constructed." }
 | 
			
		||||
{ $errors "Throws an error if the type does not exist, the necessary specialized array vocabulary is not loaded, or the requested size is negative." } ;
 | 
			
		||||
 | 
			
		||||
HELP: <c-object>
 | 
			
		||||
{ $values { "type" "a C type" } { "array" byte-array } }
 | 
			
		||||
| 
						 | 
				
			
			@ -72,8 +72,8 @@ HELP: byte-array>memory
 | 
			
		|||
 | 
			
		||||
HELP: malloc-array
 | 
			
		||||
{ $values { "n" "a non-negative integer" } { "type" "a C type" } { "alien" alien } }
 | 
			
		||||
{ $description "Allocates an unmanaged memory block large enough to hold " { $snippet "n" } " values of a C type, then wraps the memory in a sequence object using " { $link <c-type-direct-array> } "." }
 | 
			
		||||
{ $notes "The appropriate direct specialized array vocabulary must be loaded; otherwise, a " { $link specialized-array-vocab-not-loaded } " error will be thrown. The vocabulary can be loaded with a " { $link POSTPONE: USING: } " form as usual, or with the " { $link require-c-type-arrays } " word. See the " { $vocab-link "specialized-arrays.direct" } " vocabulary set for details on the underlying sequence type constructed." }
 | 
			
		||||
{ $description "Allocates an unmanaged memory block large enough to hold " { $snippet "n" } " values of a C type, then wraps the memory in a sequence object using " { $link <c-direct-array> } "." }
 | 
			
		||||
{ $notes "The appropriate direct specialized array vocabulary must be loaded; otherwise, a " { $link specialized-array-vocab-not-loaded } " error will be thrown. The vocabulary can be loaded with a " { $link POSTPONE: USING: } " form as usual, or with the " { $link require-c-arrays } " word. See the " { $vocab-link "specialized-arrays.direct" } " vocabulary set for details on the underlying sequence type constructed." }
 | 
			
		||||
{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
 | 
			
		||||
{ $errors "Throws an error if the type does not exist, if the requested size is negative, if a direct specialized array class appropriate to the type is not loaded, or if memory allocation fails." } ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -89,7 +89,7 @@ HELP: malloc-byte-array
 | 
			
		|||
{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
 | 
			
		||||
{ $errors "Throws an error if memory allocation fails." } ;
 | 
			
		||||
 | 
			
		||||
{ <c-type-array> <c-type-direct-array> malloc-array } related-words
 | 
			
		||||
{ <c-array> <c-direct-array> malloc-array } related-words
 | 
			
		||||
 | 
			
		||||
HELP: box-parameter
 | 
			
		||||
{ $values { "n" integer } { "ctype" string } }
 | 
			
		||||
| 
						 | 
				
			
			@ -130,20 +130,15 @@ HELP: malloc-string
 | 
			
		|||
    }
 | 
			
		||||
} ;
 | 
			
		||||
 | 
			
		||||
HELP: require-c-type-arrays
 | 
			
		||||
HELP: require-c-arrays
 | 
			
		||||
{ $values { "c-type" "a C type" } }
 | 
			
		||||
{ $description { $link require } "s any unloaded vocabularies needed to construct a specialized array or direct array of " { $snippet "c-type" } " using the " { $link <c-type-array> } " or " { $link <c-type-direct-array> } " vocabularies." }
 | 
			
		||||
{ $description { $link require } "s any unloaded vocabularies needed to construct a specialized array or direct array of " { $snippet "c-type" } " using the " { $link <c-array> } " or " { $link <c-direct-array> } " vocabularies." }
 | 
			
		||||
{ $notes "This word must be called inside a compilation unit. See the " { $vocab-link "specialized-arrays" } " and " { $vocab-link "specialized-arrays.direct" } " vocabulary sets for details on the underlying sequence types loaded." } ;
 | 
			
		||||
 | 
			
		||||
HELP: <c-type-array>
 | 
			
		||||
{ $values { "len" integer } { "c-type" "a C type" } { "array" "a specialized array" } }
 | 
			
		||||
{ $description "Constructs a new specialized array of length " { $snippet "len" } " and element type " { $snippet "c-type" } "." }
 | 
			
		||||
{ $notes "The appropriate specialized array vocabulary must be loaded; otherwise, a " { $link specialized-array-vocab-not-loaded } " error will be thrown. The vocabulary can be loaded with a " { $link POSTPONE: USING: } " form as usual, or with the " { $link require-c-type-arrays } " word. See the " { $vocab-link "specialized-arrays" } " vocabulary set for details on the underlying sequence type constructed." } ;
 | 
			
		||||
 | 
			
		||||
HELP: <c-type-direct-array>
 | 
			
		||||
HELP: <c-direct-array>
 | 
			
		||||
{ $values { "alien" c-ptr } { "len" integer } { "c-type" "a C type" } { "array" "a specialized direct array" } }
 | 
			
		||||
{ $description "Constructs a new specialized array of length " { $snippet "len" } " and element type " { $snippet "c-type" } " over the range of memory referenced by " { $snippet "alien" } "." }
 | 
			
		||||
{ $notes "The appropriate direct specialized array vocabulary must be loaded; otherwise, a " { $link specialized-array-vocab-not-loaded } " error will be thrown. The vocabulary can be loaded with a " { $link POSTPONE: USING: } " form as usual, or with the " { $link require-c-type-arrays } " word. See the " { $vocab-link "specialized-arrays.direct" } " vocabulary set for details on the underlying sequence type constructed." } ;
 | 
			
		||||
{ $notes "The appropriate direct specialized array vocabulary must be loaded; otherwise, a " { $link specialized-array-vocab-not-loaded } " error will be thrown. The vocabulary can be loaded with a " { $link POSTPONE: USING: } " form as usual, or with the " { $link require-c-arrays } " word. See the " { $vocab-link "specialized-arrays.direct" } " vocabulary set for details on the underlying sequence type constructed." } ;
 | 
			
		||||
 | 
			
		||||
ARTICLE: "c-strings" "C strings"
 | 
			
		||||
"C string types are arrays with shape " { $snippet "{ \"char*\" encoding }" } ", where " { $snippet "encoding" } " is an encoding descriptor. The type " { $snippet "\"char*\"" } " is an alias for " { $snippet "{ \"char*\" utf8 }" } ". See " { $link "encodings-descriptors" } " for information about encoding descriptors."
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,6 +24,7 @@ size
 | 
			
		|||
align
 | 
			
		||||
array-class
 | 
			
		||||
array-constructor
 | 
			
		||||
(array)-constructor
 | 
			
		||||
direct-array-class
 | 
			
		||||
direct-array-constructor
 | 
			
		||||
sequence-mixin-class ;
 | 
			
		||||
| 
						 | 
				
			
			@ -79,47 +80,65 @@ M: string c-type ( name -- type )
 | 
			
		|||
: ?require-word ( word/pair -- )
 | 
			
		||||
    dup word? [ drop ] [ first require ] ?if ;
 | 
			
		||||
 | 
			
		||||
GENERIC: require-c-type-arrays ( c-type -- )
 | 
			
		||||
GENERIC: require-c-arrays ( c-type -- )
 | 
			
		||||
 | 
			
		||||
M: object require-c-type-arrays
 | 
			
		||||
M: object require-c-arrays
 | 
			
		||||
    drop ;
 | 
			
		||||
 | 
			
		||||
M: c-type require-c-type-arrays
 | 
			
		||||
M: c-type require-c-arrays
 | 
			
		||||
    [ array-class>> ?require-word ]
 | 
			
		||||
    [ sequence-mixin-class>> ?require-word ]
 | 
			
		||||
    [ direct-array-class>> ?require-word ] tri ;
 | 
			
		||||
 | 
			
		||||
M: string require-c-type-arrays
 | 
			
		||||
    c-type require-c-type-arrays ;
 | 
			
		||||
M: string require-c-arrays
 | 
			
		||||
    c-type require-c-arrays ;
 | 
			
		||||
 | 
			
		||||
M: array require-c-type-arrays
 | 
			
		||||
    first c-type require-c-type-arrays ;
 | 
			
		||||
M: array require-c-arrays
 | 
			
		||||
    first c-type require-c-arrays ;
 | 
			
		||||
 | 
			
		||||
ERROR: specialized-array-vocab-not-loaded vocab word ;
 | 
			
		||||
 | 
			
		||||
: c-type-array-constructor ( c-type -- word )
 | 
			
		||||
: c-array-constructor ( c-type -- word )
 | 
			
		||||
    array-constructor>> dup array?
 | 
			
		||||
    [ first2 specialized-array-vocab-not-loaded ] when ; foldable
 | 
			
		||||
 | 
			
		||||
: c-type-direct-array-constructor ( c-type -- word )
 | 
			
		||||
: c-(array)-constructor ( c-type -- word )
 | 
			
		||||
    (array)-constructor>> dup array?
 | 
			
		||||
    [ first2 specialized-array-vocab-not-loaded ] when ; foldable
 | 
			
		||||
 | 
			
		||||
: c-direct-array-constructor ( c-type -- word )
 | 
			
		||||
    direct-array-constructor>> dup array?
 | 
			
		||||
    [ first2 specialized-array-vocab-not-loaded ] when ; foldable
 | 
			
		||||
 | 
			
		||||
GENERIC: <c-type-array> ( len c-type -- array )
 | 
			
		||||
M: object <c-type-array>
 | 
			
		||||
    c-type-array-constructor execute( len -- array ) ; inline
 | 
			
		||||
M: string <c-type-array>
 | 
			
		||||
    c-type <c-type-array> ; inline
 | 
			
		||||
M: array <c-type-array>
 | 
			
		||||
    first c-type <c-type-array> ; inline
 | 
			
		||||
GENERIC: <c-array> ( len c-type -- array )
 | 
			
		||||
M: object <c-array>
 | 
			
		||||
    c-array-constructor execute( len -- array ) ; inline
 | 
			
		||||
M: string <c-array>
 | 
			
		||||
    c-type <c-array> ; inline
 | 
			
		||||
M: array <c-array>
 | 
			
		||||
    first c-type <c-array> ; inline
 | 
			
		||||
 | 
			
		||||
GENERIC: <c-type-direct-array> ( alien len c-type -- array )
 | 
			
		||||
M: object <c-type-direct-array>
 | 
			
		||||
    c-type-direct-array-constructor execute( alien len -- array ) ; inline
 | 
			
		||||
M: string <c-type-direct-array>
 | 
			
		||||
    c-type <c-type-direct-array> ; inline
 | 
			
		||||
M: array <c-type-direct-array>
 | 
			
		||||
    first c-type <c-type-direct-array> ; inline
 | 
			
		||||
GENERIC: (c-array) ( len c-type -- array )
 | 
			
		||||
M: object (c-array)
 | 
			
		||||
    c-(array)-constructor execute( len -- array ) ; inline
 | 
			
		||||
M: string (c-array)
 | 
			
		||||
    c-type (c-array) ; inline
 | 
			
		||||
M: array (c-array)
 | 
			
		||||
    first c-type (c-array) ; inline
 | 
			
		||||
 | 
			
		||||
GENERIC: <c-direct-array> ( alien len c-type -- array )
 | 
			
		||||
M: object <c-direct-array>
 | 
			
		||||
    c-direct-array-constructor execute( alien len -- array ) ; inline
 | 
			
		||||
M: string <c-direct-array>
 | 
			
		||||
    c-type <c-direct-array> ; inline
 | 
			
		||||
M: array <c-direct-array>
 | 
			
		||||
    first c-type <c-direct-array> ; inline
 | 
			
		||||
 | 
			
		||||
: malloc-array ( n type -- alien )
 | 
			
		||||
    [ heap-size calloc ] [ <c-direct-array> ] 2bi ; inline
 | 
			
		||||
 | 
			
		||||
: (malloc-array) ( n type -- alien )
 | 
			
		||||
    [ heap-size * malloc ] [ <c-direct-array> ] 2bi ; inline
 | 
			
		||||
 | 
			
		||||
GENERIC: c-type-class ( name -- class )
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -253,21 +272,12 @@ M: f byte-length drop 0 ; inline
 | 
			
		|||
        [ "Cannot write struct fields with this type" throw ]
 | 
			
		||||
    ] unless* ;
 | 
			
		||||
 | 
			
		||||
: <c-array> ( n type -- array )
 | 
			
		||||
    heap-size * <byte-array> ; inline deprecated
 | 
			
		||||
 | 
			
		||||
: <c-object> ( type -- array )
 | 
			
		||||
    heap-size <byte-array> ; inline
 | 
			
		||||
 | 
			
		||||
: (c-object) ( type -- array )
 | 
			
		||||
    heap-size (byte-array) ; inline
 | 
			
		||||
 | 
			
		||||
: malloc-array ( n type -- alien )
 | 
			
		||||
    [ heap-size calloc ] [ <c-type-direct-array> ] 2bi ; inline
 | 
			
		||||
 | 
			
		||||
: (malloc-array) ( n type -- alien )
 | 
			
		||||
    [ heap-size * malloc ] [ <c-type-direct-array> ] 2bi ; inline
 | 
			
		||||
 | 
			
		||||
: malloc-object ( type -- alien )
 | 
			
		||||
    1 swap heap-size calloc ; inline
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -354,6 +364,10 @@ M: long-long-type box-return ( type -- )
 | 
			
		|||
            [ "specialized-arrays." prepend ]
 | 
			
		||||
            [ "<" "-array>" surround ] bi* ?lookup >>array-constructor
 | 
			
		||||
        ]
 | 
			
		||||
        [
 | 
			
		||||
            [ "specialized-arrays." prepend ]
 | 
			
		||||
            [ "(" "-array)" surround ] bi* ?lookup >>(array)-constructor
 | 
			
		||||
        ]
 | 
			
		||||
        [
 | 
			
		||||
            [ "specialized-arrays." prepend ]
 | 
			
		||||
            [ "-sequence" append ] bi* ?lookup >>sequence-mixin-class
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,10 +10,10 @@ ERROR: bad-byte-array-length byte-array type ;
 | 
			
		|||
M: bad-byte-array-length summary
 | 
			
		||||
    drop "Byte array length doesn't divide type width" ;
 | 
			
		||||
 | 
			
		||||
: (c-array) ( n c-type -- array )
 | 
			
		||||
: (underlying) ( n c-type -- array )
 | 
			
		||||
    heap-size * (byte-array) ; inline
 | 
			
		||||
 | 
			
		||||
: <c-array> ( n type -- array )
 | 
			
		||||
: <underlying> ( n type -- array )
 | 
			
		||||
    heap-size * <byte-array> ; inline
 | 
			
		||||
 | 
			
		||||
FUNCTOR: define-array ( T -- )
 | 
			
		||||
| 
						 | 
				
			
			@ -37,9 +37,9 @@ TUPLE: A
 | 
			
		|||
{ length array-capacity read-only }
 | 
			
		||||
{ underlying byte-array read-only } ;
 | 
			
		||||
 | 
			
		||||
: <A> ( n -- specialized-array ) dup T <c-array> A boa ; inline
 | 
			
		||||
: <A> ( n -- specialized-array ) dup T <underlying> A boa ; inline
 | 
			
		||||
 | 
			
		||||
: (A) ( n -- specialized-array ) dup T (c-array) A boa ; inline
 | 
			
		||||
: (A) ( n -- specialized-array ) dup T (underlying) A boa ; inline
 | 
			
		||||
 | 
			
		||||
: byte-array>A ( byte-array -- specialized-array )
 | 
			
		||||
    dup length T heap-size /mod 0 = [ drop T bad-byte-array-length ] unless
 | 
			
		||||
| 
						 | 
				
			
			@ -86,6 +86,7 @@ A T c-type-boxed-class specialize-vector-words
 | 
			
		|||
T c-type
 | 
			
		||||
    \ A >>array-class
 | 
			
		||||
    \ <A> >>array-constructor
 | 
			
		||||
    \ (A) >>(array)-constructor
 | 
			
		||||
    \ S >>sequence-mixin-class
 | 
			
		||||
    drop
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue