Skip to content

Commit

Permalink
Use memory addresses instead of creating static arrays in Rust template
Browse files Browse the repository at this point in the history
Static arrays will increase the size of the cartridge, because at runtime they will be initialized with the values used in the source (which is then ignored by the allocator).
  • Loading branch information
claudiomattera committed Nov 3, 2021
1 parent eb9e1a6 commit a5f9c23
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cli/assets/templates/rust/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const FAST_HEAP_SIZE: usize = 4 * 1024; // 4 KB
const HEAP_SIZE: usize = 16 * 1024; // 16 KB
const LEAF_SIZE: usize = 16;

static mut FAST_HEAP: [u8; FAST_HEAP_SIZE] = [0u8; FAST_HEAP_SIZE];
static mut HEAP: [u8; HEAP_SIZE] = [0u8; HEAP_SIZE];
static mut FAST_HEAP: *mut [u8; FAST_HEAP_SIZE] = (0x10000 - HEAP_SIZE - FAST_HEAP_SIZE) as *mut [u8; FAST_HEAP_SIZE];
static mut HEAP: *mut [u8; HEAP_SIZE] = (0x10000 - HEAP_SIZE) as *mut [u8; HEAP_SIZE];

#[global_allocator]
static ALLOC: NonThreadsafeAlloc = unsafe {
let fast_param = FastAllocParam::new(FAST_HEAP.as_ptr(), FAST_HEAP_SIZE);
let buddy_param = BuddyAllocParam::new(HEAP.as_ptr(), HEAP_SIZE, LEAF_SIZE);
let fast_param = FastAllocParam::new(FAST_HEAP as *mut u8, FAST_HEAP_SIZE);
let buddy_param = BuddyAllocParam::new(HEAP as *mut u8, HEAP_SIZE, LEAF_SIZE);
NonThreadsafeAlloc::new(fast_param, buddy_param)
};

0 comments on commit a5f9c23

Please # to comment.