1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#![no_std]

extern crate alloc;

use core::alloc::{GlobalAlloc, Layout};

use core::cell::UnsafeCell;
use core::ptr::null_mut;
use core::sync::atomic::{AtomicUsize, Ordering::SeqCst};
const ARENA_SIZE: usize = 8192 * 8192;
const MAX_SUPPORTED_ALIGN: usize = 4096;
#[repr(C, align(4096))] // 4096 == MAX_SUPPORTED_ALIGN
struct FAlloc {
	arena: UnsafeCell<[u8; ARENA_SIZE]>,
	remaining: AtomicUsize, // we allocate from the top, counting down
}

#[global_allocator]
static ALLOCATOR: FAlloc = FAlloc {
	arena: UnsafeCell::new([0x55; ARENA_SIZE]),
	remaining: AtomicUsize::new(ARENA_SIZE),
};
unsafe impl Sync for FAlloc {}
unsafe impl GlobalAlloc for FAlloc {
	unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
		let size = layout.size();
		let align = layout.align();
		// `Layout` contract forbids making a `Layout` with align=0, or align not power of 2.
		// So we can safely use a mask to ensure alignment without worrying about UB.
		let align_mask_to_round_down = !(align - 1);
		if align > MAX_SUPPORTED_ALIGN {
			return null_mut();
		}
		let mut allocated = 0;
		if self
			.remaining
			.fetch_update(SeqCst, SeqCst, |mut remaining| {
				if size > remaining {
					return None;
				}
				remaining -= size;
				remaining &= align_mask_to_round_down;
				allocated = remaining;
				Some(remaining)
			})
			.is_err()
		{
			return null_mut();
		};
		self.arena.get().cast::<u8>().add(allocated)
	}
	unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}
#[cfg(test)]
mod tests {
	use super::*;
	use alloc::vec;

	#[test]
	fn it_works() {
		let _ = unsafe { ALLOCATOR.alloc(Layout::from_size_align(1024, 4).unwrap()) };
		assert!(true);
	}

	#[test]
	fn vec_test() {
		let _ = vec![1, 2, 3];
		assert!(true);
	}
	#[test]
	fn vec_test_2() {
		let mut v = vec![1, 2, 3];
		for _ in 0..10000 {
			v.push(1)
		}
	}
}