6 Miscellaneous Support

▼ |  | The Racket Foreign Interface

1 | Overview
2 | Loading Foreign Libraries
3 | C Types
4 | Pointer Functions
5 | Derived Utilities
6 | Miscellaneous Support
7 | Unexported Primitive Functions
| Bibliography
| Index

On this page:

list->cblock
vector->cblock
vector->cpointer
flvector->cpointer
saved-errno
lookup-errno
cast
cblock->list
cblock->vector

top contents ← prev up next →

## 6 Miscellaneous Support🔗ℹ

> > procedure
> > (list->cblock |  | lst |  |  |  |
> > |  | type |  |  |  |
> > | [ | expect-length |  |  |  |
> > |  | #:malloc-mode malloc-mode]) |  | → |  | cpointer?
> lst : list?
> type : ctype?
> expect-length : (or/c exact-nonnegative-integer? #f) = #f
> malloc-mode : (or/c #f symbol?) = #f

Allocates a memory block of an appropriate size—using malloc with type and (length lst)—and initializes it using values from lst. The lst must hold values that can all be converted to C values according to the given type.

If expect-length is not #f and not the same as (length lst), then an exception is raised instead of allocating memory.

If malloc-mode is not #f, it is provided as an additional argument to malloc.

Changed in version 7.7.0.2 of package base: Added the #:malloc-mode argument.

> > procedure
> > (vector->cblock |  | vec |  |  |  |
> > |  | type |  |  |  |
> > | [ | expect-length |  |  |  |
> > |  | #:malloc-mode malloc-mode]) |  | → |  | cpointer?
> vec : vector?
> type : ctype?
> expect-length : (or/c exact-nonnegative-integer? #f) = #f
> malloc-mode : (or/c #f symbol?) = #f

Like list->cblock, but using values from a vector instead of a list.

Changed in version 7.7.0.2 of package base: Added the #:malloc-mode argument.

> procedure (vector->cpointer vec) → cpointer?
> vec : vector?

Returns a pointer to an array of _scheme values, which is the internal representation of vec.

> procedure (flvector->cpointer flvec) → cpointer?
> flvec : flvector?

Returns a pointer to an array of _double values, which is the internal representation of flvec.

> procedure (saved-errno) → exact-integer?
> (saved-errno new-value) → void?
> new-value : exact-integer?

Returns or sets the error code saved for the current Racket thread. The saved error code is set after a foreign call with a non-#f #:save-errno option (see _fun and _cprocedure), but it can also be set explicitly (for example, to create mock foreign functions for testing).

Changed in version 6.4.0.9 of package base: Added the one-argument variant.

> procedure (lookup-errno sym) → (or/c exact-integer? #f)
> sym : symbol?

Returns a platform-specific positive integer corresponding to a POSIX errno code, or #f if the code is unknown. A code’s value is known if the code is one of the recognized symbols described below and the code was defined by the "errno.h" header used to compile Racket. Note that the contents of "errno.h" vary based on platform and compiler.

The recognized symbols currently consist of the 81 codes defined by IEEE Std 1003.1, 2013 Edition (also known as POSIX.1), including 'EINTR, 'EEXIST, and 'EAGAIN.

Changed in version 6.6.0.5 of package base: Relaxed the contract and added support for more symbols.

> procedure (cast v from-type to-type) → any/c
> v : any/c
> from-type : ctype?
> to-type : ctype?

Converts v from a value matching from-type to a value matching to-type, where (ctype-sizeof from-type) matches (ctype-sizeof to-type).

The conversion is roughly equivalent to

> (let ([p (malloc from-type)])
> (ptr-set! p from-type v)
> (ptr-ref p to-type))

If v is a cpointer, (cpointer-gcable? v) is true, and from-type and to-type are both based on _pointer or _gcpointer, then from-type is implicitly converted with _gcable to ensure that the result cpointer is treated as referring to memory that is managed by the garbage collector.

If v is a pointer with an offset component (e.g., from ptr-add), (cpointer-gcable? v) is true, and the result is a cpointer, then the result pointer has the same offset component as v. If (cpointer-gcable? v) is false, then any offset is folded into the pointer base for the result.

> procedure (cblock->list cblock type length) → list?
> cblock : any/c
> type : ctype?
> length : exact-nonnegative-integer?

Converts C cblock, which is a vector of types, to a Racket list. The arguments are the same as in the list->cblock. The length must be specified because there is no way to know where the block ends.

> procedure (cblock->vector cblock type length) → vector?
> cblock : any/c
> type : ctype?
> length : exact-nonnegative-integer?

Like cblock->list, but for Racket vectors.
top contents ← prev up next →