blob: a321af38dcfc96d90183ed8f907b9024c4878512 [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);
Brian Silvermana8ad1af2022-07-23 16:05:12 -070027__attribute__((weak))
Brian Silverman8751d482022-05-18 23:28:44 -070028uint8_t *__rust_alloc(uintptr_t size, uintptr_t align) {
29 return __rdl_alloc(size, align);
30}
31void __rdl_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align);
Brian Silvermana8ad1af2022-07-23 16:05:12 -070032__attribute__((weak))
Brian Silverman8751d482022-05-18 23:28:44 -070033void __rust_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align) {
34 __rdl_dealloc(ptr, size, align);
35}
36uint8_t *__rdl_realloc(uint8_t *ptr, uintptr_t old_size, uintptr_t align,
37 uintptr_t new_size);
Brian Silvermana8ad1af2022-07-23 16:05:12 -070038__attribute__((weak))
Brian Silverman8751d482022-05-18 23:28:44 -070039uint8_t *__rust_realloc(uint8_t *ptr, uintptr_t old_size, uintptr_t align,
40 uintptr_t new_size) {
41 return __rdl_realloc(ptr, old_size, align, new_size);
42}
43uint8_t *__rdl_alloc_zeroed(uintptr_t size, uintptr_t align);
Brian Silvermana8ad1af2022-07-23 16:05:12 -070044__attribute__((weak))
Brian Silverman8751d482022-05-18 23:28:44 -070045uint8_t *__rust_alloc_zeroed(uintptr_t size, uintptr_t align) {
46 return __rdl_alloc_zeroed(size, align);
47}
48void __rdl_oom(uintptr_t size, uintptr_t align);
Brian Silvermana8ad1af2022-07-23 16:05:12 -070049__attribute__((weak))
Brian Silverman8751d482022-05-18 23:28:44 -070050void __rust_alloc_error_handler(uintptr_t size, uintptr_t align) {
51 __rdl_oom(size, align);
52}