| 45 | unsafe impl GlobalAlloc for Locked<BumpAlloc> |
| 46 | { |
| 47 | unsafe fn alloc(&self, layout: Layout) -> *mut u8 |
| 48 | { |
| 49 | // Get a mutable reference. |
| 50 | let mut bump = self.lock(); |
| 51 | |
| 52 | let alloc_init = alignup(bump.next, layout.align()); |
| 53 | let alloc_end = match alloc_init.checked_add(layout.size()) |
| 54 | { |
| 55 | Some(end) => end, |
| 56 | None => return ptr::null_mut(), |
| 57 | }; |
| 58 | |
| 59 | if alloc_end > bump.endheap |
| 60 | { |
| 61 | // No memory remaining. |
| 62 | ptr::null_mut() |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | bump.next = alloc_end; |
| 67 | bump.allocations += 1; |
| 68 | alloc_init as *mut u8 |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) |
| 73 | { |