1const ALLOC_STEP: usize = 16;
12const ALLOC_MIN_CLASS: usize = 32;
13const ALLOC_MAX_CLASS: usize = 32768;
14const ALLOC_CLASSES: usize = ALLOC_MAX_CLASS / ALLOC_STEP + 1;
15const ALLOC_HDR: usize = core::mem::size_of::<AllocBlock>();
16const ALLOC_LARGE: usize = 1usize << 63;
17const ALLOC_ALIGNED: usize = 1usize << 62;
18const ALLOC_PAGES_MASK: usize = ALLOC_ALIGNED - 1;
21
22#[repr(C)]
23struct AllocBlock {
24 next: *mut AllocBlock,
25 tag: usize,
26}
27
28static ALLOC_LOCK: AtomicUsize = AtomicUsize::new(0);
29static mut ALLOC_FREE: [*mut AllocBlock; ALLOC_CLASSES] =
30 [null_mut(); ALLOC_CLASSES];
31
32#[inline]
33unsafe fn alloc_lock() {
34 while ALLOC_LOCK.swap(1, Ordering::Acquire) != 0 {
35 core::hint::spin_loop();
36 }
37}
38
39#[inline]
40unsafe fn alloc_unlock() {
41 ALLOC_LOCK.store(0, Ordering::Release);
42}
43
44pub(crate) unsafe fn allocator_after_fork() {
45 ALLOC_LOCK.store(0, Ordering::Release);
46}
47
48#[inline]
49fn alloc_class_for(size: usize) -> Option<usize> {
50 let total = size.checked_add(ALLOC_HDR)?;
51 let class = if total <= ALLOC_MIN_CLASS {
52 ALLOC_MIN_CLASS
53 } else {
54 match total.checked_add(ALLOC_STEP - 1) {
55 Some(v) => v & !(ALLOC_STEP - 1),
56 None => return None,
57 }
58 };
59 if class <= ALLOC_MAX_CLASS { Some(class) } else { None }
60}
61
62unsafe fn alloc_mmap(size: usize) -> *mut u8 {
63 let pages = (size + PAGE - 1) & !(PAGE - 1);
64 let p = sys_mmap(
65 null_mut(),
66 pages,
67 PROT_READ | PROT_WRITE,
68 MAP_PRIVATE | MAP_ANONYMOUS,
69 -1,
70 0,
71 );
72 if p == MMAP_FAILED { null_mut() } else { p }
73}
74
75unsafe fn alloc_large(size: usize) -> *mut u8 {
76 let total = match size.checked_add(ALLOC_HDR) {
77 Some(v) => v,
78 None => {
79 ERRNO!() = ENOMEM;
80 return null_mut();
81 }
82 };
83 if total > usize::MAX - (PAGE - 1) {
84 ERRNO!() = ENOMEM;
85 return null_mut();
86 }
87 let pages = (total + PAGE - 1) / PAGE;
88 let map_size = pages * PAGE;
89 let base = alloc_mmap(map_size);
90 if base.is_null() {
91 return null_mut();
92 }
93 let block = base as *mut AllocBlock;
94 (*block).next = block;
95 (*block).tag = ALLOC_LARGE | pages;
96 base.add(ALLOC_HDR)
97}
98
99unsafe fn alloc_aligned_impl(alignment: usize, size: usize) -> *mut u8 {
100 if alignment == 0 || !alignment.is_power_of_two() {
101 ERRNO!() = EINVAL;
102 return null_mut();
103 }
104 if alignment <= ALLOC_STEP {
105 return malloc(size) as *mut u8;
106 }
107 let total = match size.checked_add(alignment).and_then(|v| v.checked_add(ALLOC_HDR)) {
108 Some(v) => v,
109 None => {
110 ERRNO!() = ENOMEM;
111 return null_mut();
112 }
113 };
114 if total > usize::MAX - (PAGE - 1) {
115 ERRNO!() = ENOMEM;
116 return null_mut();
117 }
118 let pages = (total + PAGE - 1) / PAGE;
119 let map_size = pages * PAGE;
120 let base = alloc_mmap(map_size);
121 if base.is_null() {
122 return null_mut();
123 }
124 let data_addr = (base as usize + ALLOC_HDR + alignment - 1) & !(alignment - 1);
125 let block = (data_addr - ALLOC_HDR) as *mut AllocBlock;
126 (*block).next = base as *mut AllocBlock;
127 (*block).tag = ALLOC_ALIGNED | pages;
128 data_addr as *mut u8
129}
130
131pub(crate) unsafe fn alloc_usable_size(ptr: *mut u8) -> usize {
132 if ptr.is_null() {
133 return 0;
134 }
135 let block = ptr.sub(ALLOC_HDR) as *mut AllocBlock;
136 let tag = (*block).tag;
137 if tag & ALLOC_ALIGNED != 0 {
138 let base = (*block).next as usize;
139 let pages = tag & ALLOC_PAGES_MASK;
140 pages * PAGE - (ptr as usize - base)
141 } else if tag & ALLOC_LARGE != 0 {
142 let pages = tag & ALLOC_PAGES_MASK;
143 pages * PAGE - ALLOC_HDR
144 } else if tag >= ALLOC_MIN_CLASS && tag <= ALLOC_MAX_CLASS {
145 tag - ALLOC_HDR
146 } else {
147 0
148 }
149}
150
151#[no_mangle]
152pub unsafe extern "C" fn malloc(size: SizeT) -> *mut c_void {
153 let class = match alloc_class_for(size) {
154 Some(c) => c,
155 None => return alloc_large(size) as *mut c_void,
156 };
157 let idx = class / ALLOC_STEP;
158
159 alloc_lock();
160 let head = ALLOC_FREE[idx];
161 if !head.is_null() {
162 ALLOC_FREE[idx] = (*head).next;
163 alloc_unlock();
164 return (head as *mut u8).add(ALLOC_HDR) as *mut c_void;
165 }
166
167 let chunk_size = if class < 4096 { 65536 } else { class * 2 };
169 let map_size = (chunk_size + PAGE - 1) & !(PAGE - 1);
170 let base = alloc_mmap(map_size);
171 if base.is_null() {
172 alloc_unlock();
173 return null_mut();
174 }
175 let count = map_size / class;
176 let mut i = count;
177 while i > 1 {
178 i -= 1;
179 let block = base.add(i * class) as *mut AllocBlock;
180 (*block).tag = class;
181 (*block).next = ALLOC_FREE[idx];
182 ALLOC_FREE[idx] = block;
183 }
184 let first = base as *mut AllocBlock;
185 (*first).tag = class;
186 alloc_unlock();
187 base.add(ALLOC_HDR) as *mut c_void
188}
189
190#[no_mangle]
191pub unsafe extern "C" fn free(ptr: *mut c_void) {
192 if ptr.is_null() {
193 return;
194 }
195 let block = (ptr as *mut u8).sub(ALLOC_HDR) as *mut AllocBlock;
196 let tag = (*block).tag;
197 if tag & ALLOC_ALIGNED != 0 {
198 let base = (*block).next as *mut u8;
199 let pages = tag & ALLOC_PAGES_MASK;
200 sys_munmap(base, pages * PAGE);
201 return;
202 }
203 if tag & ALLOC_LARGE != 0 {
204 let pages = tag & ALLOC_PAGES_MASK;
205 sys_munmap(block as *mut u8, pages * PAGE);
206 return;
207 }
208 if tag < ALLOC_MIN_CLASS || tag > ALLOC_MAX_CLASS {
209 return;
210 }
211 let idx = tag / ALLOC_STEP;
212 alloc_lock();
213 (*block).next = ALLOC_FREE[idx];
214 ALLOC_FREE[idx] = block;
215 alloc_unlock();
216}
217
218#[no_mangle]
219pub unsafe extern "C" fn calloc(count: SizeT, size: SizeT) -> *mut c_void {
220 let total = match count.checked_mul(size) {
221 Some(v) => v,
222 None => {
223 ERRNO!() = ENOMEM;
224 return null_mut();
225 }
226 };
227 let ptr = malloc(total);
228 if !ptr.is_null() {
229 core::ptr::write_bytes(ptr as *mut u8, 0, total);
230 }
231 ptr
232}
233
234#[no_mangle]
235pub unsafe extern "C" fn realloc(ptr: *mut c_void, new_size: SizeT) -> *mut c_void {
236 if ptr.is_null() {
237 return malloc(new_size);
238 }
239 if new_size == 0 {
240 free(ptr);
241 return null_mut();
242 }
243 let old_size = alloc_usable_size(ptr as *mut u8);
244 if new_size <= old_size {
245 return ptr;
246 }
247 let new_ptr = malloc(new_size);
248 if new_ptr.is_null() {
249 return null_mut();
250 }
251 core::ptr::copy_nonoverlapping(ptr as *const u8, new_ptr as *mut u8, old_size);
252 free(ptr);
253 new_ptr
254}