1#![feature(allocator_api)]
2
3use std::{
4 alloc::{Allocator, Layout},
5 ptr::NonNull,
6};
7
8use rulloc::Rulloc;
9
10fn print_alloc(addr: NonNull<u8>, layout: Layout) {
11 println!("Alloc of {} bytes at {addr:?}", layout.size());
12}
13
14fn main() {
15 let allocator = Rulloc::<3>::with_bucket_sizes([8, 16, 24]);
16
17 println!("Allocator configured with bucket sizes 8, 16 and 24.");
18 println!("Notice how addresses are located in different regions.");
19 println!("If page size is 4096 bytes there should be 4KB of difference between them:");
20
21 unsafe {
22 let layout1 = Layout::array::<u8>(8).unwrap();
23 let addr1 = allocator.allocate(layout1).unwrap().cast();
24 print_alloc(addr1, layout1);
25
26 let layout2 = Layout::array::<u8>(16).unwrap();
27 let addr2 = allocator.allocate(layout2).unwrap().cast();
28 print_alloc(addr2, layout2);
29
30 let layout3 = Layout::array::<u8>(24).unwrap();
31 let addr3 = allocator.allocate(layout3).unwrap().cast();
32 print_alloc(addr3, layout3);
33
34 allocator.deallocate(addr1, layout1);
35 allocator.deallocate(addr2, layout2);
36 allocator.deallocate(addr3, layout3);
37 }
38}