5.2 Safe C Vectors
The
ffi/unsafe/cvector library exports the bindings of
this section. The ffi/cvector library exports the same
bindings, except for the unsafe make-cvector* operation.
The cvector form can be used as a type C vectors (i.e., a pointer to a memory block).
Like _bytes, _cvector can be used as a simple type
that corresponds to a pointer that is managed as a safe C vector on
the Racket side. The longer form behaves similarly to the
_list and _vector custom types, except that
_cvector is more efficient; no Racket list or vector is
needed.
procedure
(make-cvector type length) → cvector?
type : ctype? length : exact-nonnegative-integer?
Allocates a C vector using the given type and
length. The resulting vector is not guaranteed to
contain any particular values.
Creates a C vector of the given type, initialized to the
given list of vals.
Returns #t if v is a C vector, #f otherwise.
procedure
(cvector-length cvec) → exact-nonnegative-integer?
cvec : cvector?
Returns the length of a C vector.
procedure
(cvector-type cvec) → ctype?
cvec : cvector?
Returns the C type object of a C vector.
procedure
(cvector-ptr cvec) → cpointer?
cvec : cvector?
Returns the pointer that points at the beginning block of the given C vector.
procedure
(cvector-ref cvec k) → any
cvec : cvector? k : exact-nonnegative-integer?
References the kth element of the cvec C vector.
The result has the type that the C vector uses.
procedure
(cvector-set! cvec k val) → void?
cvec : cvector? k : exact-nonnegative-integer? val : any
Sets the kth element of the cvec C vector to
val. The val argument should be a value that can be
used with the type that the C vector uses.
procedure
(cvector->list cvec) → list?
cvec : cvector?
Converts the cvec C vector object to a list of values.
procedure
(list->cvector lst type) → cvector?
lst : list? type : ctype?
Converts the list lst to a C vector of the given
type.
procedure
(make-cvector* cptr type length) → cvector?
cptr : any/c type : ctype? length : exact-nonnegative-integer?
Constructs a C vector using an existing pointer object. This
operation is not safe, so it is intended to be used in specific
situations where the type and length are known.