Download View/Open

Transcript
50
4.1
CHAPTER 4. RUST EMBEDDED LIBRARY
The Core Library
As described in Section 2.1.2, the RCL defines the core functionality of the Rust
language. The RCL does not have any library dependencies, but in order to use
the library without RSL, a few definitions are needed. These definitions are given
in Table 4.1.
Functions
memcpy, memcmp, memset
rust begin unwind
Description
Basic memory management
Handles panicking
Table 4.1: External dependencies of RCL
The memory management functions given in Table 4.1 are provided by newlib and
are exposed through the startup library described in Section 3.1.3.
Panicking is Rust’s way of unwinding the currently executing thread, ultimately
resulting in the thread being terminated. A panic in Rust can happen, e.g. when
an array is indexed out of bounds, which causes the rust begin unwind function
to be called. The rust begin unwind is also defined in startup, but the implementation is only an infinite loop to aid debugging. In contrast, the definition
of rust begin unwind given in RSL will abort the program and print an error
message.
4.2
The Allocation Library
Heap allocation is introduced in a library called alloc. The library defines the
managed pointer, Box, which is Rust’s main means of allocating memory on the
heap. Also, the allocation library defines the types Rc and Arc, which are Rust’s
reference counted and atomically reference counted heap pointers.
1
2
3
fn rust_allocate(usize, usize) -> *mut u8;
fn rust_deallocate(*mut u8, usize, usize);
fn rust_reallocate(*mut u8, usize, usize, usize) -> *mut u8;
Listing 4.1: External dependencies of the alloc library
The allocation library is by default dependent on libc, but this dependency can
be broken by supplying the --cfg feature="external funcs" flag to the compilation process. When breaking this dependency, the allocation library requires
the functions in Listing 4.1 to be defined elsewhere. Note that these functions
map directly to the alloc, dealloc, and realloc functions, which all are part of