3 Values and Types (CS)
A Racket value is represented by a pointer-sized value. The low bits
of the value indicate the encoding that it uses. For example, two (on
32-bit platform) or three (on 64-bit platforms) low bits indicates a
fixnum encoding, while a one low bit and zero second-lowest bit
indicates a pair whose address in memory is specified by the other
bits.
The C type for a Racket value is ptr. For most Racket types, a
constructor is provided for creating values of the type. For example,
Scons takes two ptr values and returns the cons
of the values as a new ptr value. In addition to providing
constructors, Racket defines several global constant Racket values,
such as Strue for #t.
3.1 Global Constants
There are six global constants:
3.2 Value Functions
Many of these functions are actually macros.
Predicates to recognize different kinds of Racket values, such as
fixnums, characters, the empty list, etc. The
Srecordp predicate
recognizes structures, but some built-in Racket datatypes are also
implemented as records.
Returns a Racket integer value, where i must fit in a fixnum.
Returns an integer value for different conversions from C, where the
result is allocated as a bignum if necessary to hold the value.
Converts a Racket fixnum to a C integer.
Converts a Racket integer (possibly a bignum) to a C integer, assuming
that the integer fits in the return type.
Returns a Racket flonum value.
Converts a Racket flonum value to a C floating-point number.
Returns a Racket character value. The ch value must be a legal
Unicode code point (and not a surrogate, for example). All characters
are represented by constant values.
Returns the Unicode code point for the Racket character ch.
ptr | | Scons | ( | ptr car, | | | | | ptr cdr) |
|
Extracts the
car or
cdr of a pair.
Returns the interned symbol whose name matches str.
Returns the Racket immutable string value for the Racket symbol
sym.
Allocates a fresh Racket mutable string with len characters. The
content of the string is either all chs when ch is
provided or unspecified otherwise.
Allocates a fresh Racket mutable string with the content of
str.
If
len is not provided,
str must be nul-terminated.
In the case of
Sstring_utf8,
str is decoded as
UTF-8, otherwise it is decided as Latin-1.
Returns the length of the string str.
Returns the ith Racket character of the string str.
Installs ch as the ith Racket character of the string str.
Allocates a fresh mutable
vector of length
len and with
v initially in every slot.
Returns the length of the vector vec.
Returns the ith element of the vector vec.
Installs v as the ith element of the vector vec.
Allocates a fresh mutable
fxvector of
length
len and with
v initially in every slot.
Returns the length of the fxvector vec.
Returns the ith fixnum of the fxvector vec.
Installs the fixnum v as the ith element of the fxvector
vec.
Allocates a fresh mutable
byte string of
length
len and with
byte initially in every slot.
Returns the length of the byte string bstr.
Returns the ith byte of the byte string bstr.
Installs byte as the ith byte of the byte string bstr.
Returns a pointer to the start of the bytes for the byte string bstr.
Allocates a fresh mutable
box containing
v.
Extract the content of the box bx.
Installs v as the content of the box bx.
Accesses record information, where Racket structures are implemented
as records. The
Srecord_type returns a value representing a
record’s type (so, a structure type). Given a record type,
Srecord_type_parent returns its supertype or
Sfalse,
Srecord_type_size returns the allocation size of a record in
bytes, and
Srecord_type_uniformp indicates whether all of the
record fields are Scheme values —
which is always true for a Racket
structure. When a record has all Scheme-valued fields, the allocation
size is the number of fields plus one times the size of a pointer in
bytes.
When a record has all Scheme fields (which is the case for all Racket
structures), Srecord_uniform_ref accesses a field value in the
same way as unsafe-struct*-ref.
Extracts an address and offset from a C-pointer object in the sense of
cpointer?, but only for values using the predefined representation
that is not a byte string,
#f, or implemented by a new
structure type with
prop:cpointer.
The result of racket_cpointer_address is the same as
racket_cpointer_base_address plus racket_cpointer_offset,
where racket_cpointer_offset is non-zero for C-pointer values
created by ptr-add.
“Locks” or “unlocks” n object, which prevents it from being
garbage collected or moved to a different address.
Lock objects sparingly, because the garbage collector is not designed
to deal with a large number of locked objects. To retain multiple
values from use from C, a good approach may be to allocate and lock a
vector that has a slot for each other (unlocked) object to retain.