blob: 29a77a442d7fe821222f9f62b207f0599b9da185 [file] [log] [blame]
Brian Silverman8751d482022-05-18 23:28:44 -07001#include <stdint.h>
2
3// This file has some exciting magic to get Rust code linking in a cc_binary.
4// The Rust compiler generates some similar symbol aliases when it links, so we
Brian Silvermana8ad1af2022-07-23 16:05:12 -07005// have to do it manually. We mark all our symbols as weak so that linking this
6// via Rust tooling to produce a binary with a Rust main works.
Brian Silverman8751d482022-05-18 23:28:44 -07007//
8// It is intended to be used in rust_toolchain.allocator_library.
9//
10// https://github.com/rust-lang/rust/blob/master/library/alloc/src/alloc.rs
11// and https://github.com/rust-lang/rust/blob/master/library/std/src/alloc.rs
12// are the best source of docs I've found on these functions and variables.
13// https://doc.rust-lang.org/std/alloc/index.html talks about how this is
14// intended to be used.
15//
16// Also note
17// https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html for
18// the sizes of the various integer types.
19//
20// This file strongly assumes that the default allocator is used. It will
21// not work with any other allocated switched in via `#[global_allocator]`.
22
23// New feature as of https://github.com/rust-lang/rust/pull/88098.
Brian Silvermana8ad1af2022-07-23 16:05:12 -070024__attribute__((weak)) uint8_t __rust_alloc_error_handler_should_panic = 0;
Brian Silverman8751d482022-05-18 23:28:44 -070025
26uint8_t *__rdl_alloc(uintptr_t size, uintptr_t align);
Philipp Schrader790cb542023-07-05 21:06:52 -070027__attribute__((weak)) uint8_t *__rust_alloc(uintptr_t size, uintptr_t align) {
Brian Silverman8751d482022-05-18 23:28:44 -070028 return __rdl_alloc(size, align);
29}
30void __rdl_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align);
Philipp Schrader790cb542023-07-05 21:06:52 -070031__attribute__((weak)) void __rust_dealloc(uint8_t *ptr, uintptr_t size,
32 uintptr_t align) {
Brian Silverman8751d482022-05-18 23:28:44 -070033 __rdl_dealloc(ptr, size, align);
34}
35uint8_t *__rdl_realloc(uint8_t *ptr, uintptr_t old_size, uintptr_t align,
36 uintptr_t new_size);
Philipp Schrader790cb542023-07-05 21:06:52 -070037__attribute__((weak)) uint8_t *__rust_realloc(uint8_t *ptr, uintptr_t old_size,
38 uintptr_t align,
39 uintptr_t new_size) {
Brian Silverman8751d482022-05-18 23:28:44 -070040 return __rdl_realloc(ptr, old_size, align, new_size);
41}
42uint8_t *__rdl_alloc_zeroed(uintptr_t size, uintptr_t align);
Philipp Schrader790cb542023-07-05 21:06:52 -070043__attribute__((weak)) uint8_t *__rust_alloc_zeroed(uintptr_t size,
44 uintptr_t align) {
Brian Silverman8751d482022-05-18 23:28:44 -070045 return __rdl_alloc_zeroed(size, align);
46}
47void __rdl_oom(uintptr_t size, uintptr_t align);
Philipp Schrader790cb542023-07-05 21:06:52 -070048__attribute__((weak)) void __rust_alloc_error_handler(uintptr_t size,
49 uintptr_t align) {
Brian Silverman8751d482022-05-18 23:28:44 -070050 __rdl_oom(size, align);
51}