3.1 Type Constructors

► |  | 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

▼ | 3 | C Types

3.1 | Type Constructors
3.2 | Numeric Types
3.3 | Other Atomic Types
3.4 | String Types
3.5 | Pointer Types
3.6 | Function Types
3.7 | C Struct Types
3.8 | C Array Types
3.9 | C Union Types
3.10 | Enumerations and Masks

On this page:

make-ctype
ctype?
ctype-sizeof
ctype-alignof
ctype->layout
compiler-sizeof

top contents ← prev up next →

### 3.1 Type Constructors🔗ℹ

> procedure (make-ctype type racket-to-c c-to-racket) → ctype?
> type : ctype?
> racket-to-c : (or/c #f (any/c . -> . any))
> c-to-racket : (or/c #f (any/c . -> . any))

Creates a new C type value whose representation for foreign code is the same as type’s.

The given conversion functions convert to and from the Racket representation of the new type. Either conversion function can be #f, meaning that the conversion for the corresponding direction is the identity function. If both functions are #f, type is returned.

The racket-to-c function takes any value and, if it is a valid representation of the new type, converts it to a representation of type. The c-to-racket function takes a representation of type and produces a representation of the new type.

> procedure (ctype? v) → boolean?
> v : any/c

Returns #t if v is a C type, #f otherwise.

Examples:

> > (ctype? _int)
> #t
> > (ctype? (_fun _int -> _int))
> #t
> > (ctype? #f)
> #f
> > (ctype? "foo")
> #f

> procedure (ctype-sizeof type) → exact-nonnegative-integer?
> type : ctype?
> (ctype-alignof type) → exact-nonnegative-integer?
> type : ctype?

Returns the size or alignment of a given type for the current platform.

Examples:

> > (ctype-sizeof _int)
> 4
> > (ctype-sizeof (_fun _int -> _int))
> 8
> > (ctype-alignof _int)
> 4
> > (ctype-alignof (_fun _int -> _int))
> 8

> procedure (ctype->layout type)
> → (flat-rec-contract rep symbol? (listof rep))
> type : ctype?

Returns a value to describe the eventual C representation of the type. It can be any of the following symbols:

> 'int8 'uint8 'int16 'uint16 'int32 'uint32 'int64 'uint64
> 'float 'double 'bool 'void 'pointer 'fpointer
> 'bytes 'string/ucs-4 'string/utf-16

The result can also be a list, which describes a C struct whose element representations are provided in order within the list. Finally, the result can be a vector of size 2 containing an element representation followed by an exact-integer count.

Examples:

> > (ctype->layout _int)
> 'int32
> > (ctype->layout _void)
> 'void
> > (ctype->layout (_fun _int -> _int))
> 'fpointer

> procedure (compiler-sizeof sym) → exact-nonnegative-integer?
> sym : (or/c symbol? (listof symbol?))

Possible values for sym are 'int, 'char, 'wchar, 'short, 'long, '*, 'void, 'float, 'double, or lists of symbols, such as '(long long). The result is the size of the corresponding type according to the C sizeof operator for the current platform. The compiler-sizeof operation should be used to gather information about the current platform, such as defining alias type like _int to a known type like _int32.

Examples:

> > (compiler-sizeof 'int)
> 4
> > (compiler-sizeof '(long long))
> 8

top contents ← prev up next →