|
| 1 | +#![feature(const_fn)] |
| 2 | +#![feature(unique)] |
| 3 | +#![feature(core_intrinsics)] |
| 4 | +#![no_std] |
| 5 | + |
| 6 | +#[cfg(test)] |
| 7 | +#[macro_use] |
| 8 | +extern crate std; |
| 9 | + |
| 10 | +use core::ptr::Unique; |
| 11 | +use core::mem::{self, size_of}; |
| 12 | + |
| 13 | +use hole::Hole; |
| 14 | +use small_hole::SmallHole; |
| 15 | + |
| 16 | +mod hole; |
| 17 | +mod small_hole; |
| 18 | + |
| 19 | +pub struct Heap { |
| 20 | + holes: Hole, // dummy |
| 21 | + small_holes: SmallHole, // dummy |
| 22 | +} |
| 23 | + |
| 24 | +impl Heap { |
| 25 | + pub const fn empty() -> Heap { |
| 26 | + Heap { |
| 27 | + holes: Hole { |
| 28 | + size: 0, |
| 29 | + next: None, |
| 30 | + }, |
| 31 | + small_holes: SmallHole { next: None }, |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + pub fn new(heap_bottom: usize, heap_top: usize) -> Heap { |
| 36 | + assert!(size_of::<SmallHole>() == size_of::<usize>()); |
| 37 | + assert!(size_of::<Hole>() == size_of::<usize>() * 2); |
| 38 | + |
| 39 | + let first_hole = Hole { |
| 40 | + size: heap_top - heap_bottom, |
| 41 | + next: None, |
| 42 | + }; |
| 43 | + |
| 44 | + let mut first_hole_ptr = unsafe { Unique::new(heap_bottom as *mut Hole) }; |
| 45 | + unsafe { mem::forget(mem::replace(first_hole_ptr.get_mut(), first_hole)) }; |
| 46 | + |
| 47 | + let mut heap = Heap::empty(); |
| 48 | + heap.holes.next = Some(first_hole_ptr); |
| 49 | + heap |
| 50 | + } |
| 51 | + |
| 52 | + pub fn allocate_first_fit(&mut self, mut size: usize, align: usize) -> Option<*mut u8> { |
| 53 | + size = align_up(size, size_of::<usize>()); |
| 54 | + let mut ret = None; |
| 55 | + |
| 56 | + if size == size_of::<SmallHole>() { |
| 57 | + ret = ret.or_else(|| { |
| 58 | + self.small_holes.get_first_fit(align).map(|hole| { |
| 59 | + let hole_start_addr = *hole as usize; |
| 60 | + assert!(hole_start_addr % align == 0); |
| 61 | + hole_start_addr as *mut u8 |
| 62 | + }) |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + ret = ret.or_else(|| { |
| 67 | + self.holes.get_first_fit(size, align).map(|hole| { |
| 68 | + let hole_start_addr = *hole as usize; |
| 69 | + let aligned_address = align_up(hole_start_addr, align); |
| 70 | + let padding = aligned_address - hole_start_addr; |
| 71 | + if padding > 0 { |
| 72 | + assert!(unsafe { hole.get().size } - padding >= size); |
| 73 | + self.deallocate(*hole as *mut u8, padding, 1); |
| 74 | + } |
| 75 | + aligned_address as *mut u8 |
| 76 | + }) |
| 77 | + }); |
| 78 | + |
| 79 | + ret |
| 80 | + } |
| 81 | + |
| 82 | + pub fn deallocate(&mut self, ptr: *mut u8, mut size: usize, _align: usize) { |
| 83 | + if size <= size_of::<SmallHole>() { |
| 84 | + let hole = SmallHole { next: None }; |
| 85 | + let mut hole_ptr = unsafe { Unique::new(ptr as *mut SmallHole) }; |
| 86 | + unsafe { mem::forget(mem::replace(hole_ptr.get_mut(), hole)) }; |
| 87 | + |
| 88 | + self.small_holes.add_hole(hole_ptr); |
| 89 | + } else { |
| 90 | + if size < size_of::<Hole>() { |
| 91 | + size = size_of::<Hole>(); |
| 92 | + } |
| 93 | + let hole = Hole { |
| 94 | + size: size, |
| 95 | + next: None, |
| 96 | + }; |
| 97 | + let mut hole_ptr = unsafe { Unique::new(ptr as *mut Hole) }; |
| 98 | + unsafe { mem::forget(mem::replace(hole_ptr.get_mut(), hole)) }; |
| 99 | + |
| 100 | + self.holes.add_hole(hole_ptr); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +fn align_down(value: usize, align: usize) -> usize { |
| 106 | + value / align * align |
| 107 | +} |
| 108 | + |
| 109 | +fn align_up(value: usize, align: usize) -> usize { |
| 110 | + align_down(value + align - 1, align) |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod test { |
| 115 | + use std::prelude::v1::*; |
| 116 | + use std::mem::{size_of, align_of}; |
| 117 | + use super::*; |
| 118 | + |
| 119 | + fn new_heap() -> Heap { |
| 120 | + const HEAP_SIZE: usize = 1000; |
| 121 | + let dummy = Box::into_raw(Box::new([0u8; HEAP_SIZE])); |
| 122 | + |
| 123 | + let heap_bottom = dummy as usize; |
| 124 | + let heap_top = heap_bottom + HEAP_SIZE; |
| 125 | + Heap::new(heap_bottom, heap_top) |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn allocate_double_usize() { |
| 130 | + let mut heap = new_heap(); |
| 131 | + assert!(heap.allocate_first_fit(size_of::<usize>() * 2, align_of::<usize>()).is_some()); |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn allocate_and_free_double_usize() { |
| 136 | + let mut heap = new_heap(); |
| 137 | + |
| 138 | + let x = heap.allocate_first_fit(size_of::<usize>() * 2, align_of::<usize>()).unwrap(); |
| 139 | + unsafe { |
| 140 | + *(x as *mut (usize, usize)) = (0xdeafdeadbeafbabe, 0xdeafdeadbeafbabe); |
| 141 | + } |
| 142 | + heap.deallocate(x, size_of::<usize>() * 2, align_of::<usize>()); |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn reallocate_double_usize() { |
| 147 | + let mut heap = new_heap(); |
| 148 | + |
| 149 | + let x = heap.allocate_first_fit(size_of::<usize>() * 2, align_of::<usize>()).unwrap(); |
| 150 | + heap.deallocate(x, size_of::<usize>() * 2, align_of::<usize>()); |
| 151 | + |
| 152 | + let y = heap.allocate_first_fit(size_of::<usize>() * 2, align_of::<usize>()).unwrap(); |
| 153 | + heap.deallocate(y, size_of::<usize>() * 2, align_of::<usize>()); |
| 154 | + |
| 155 | + assert_eq!(x, y); |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn allocate_multiple_sizes() { |
| 160 | + let mut heap = new_heap(); |
| 161 | + let base_size = size_of::<usize>(); |
| 162 | + let base_align = align_of::<usize>(); |
| 163 | + |
| 164 | + let x = heap.allocate_first_fit(base_size * 2, base_align).unwrap(); |
| 165 | + let y = heap.allocate_first_fit(base_size * 7, base_align).unwrap(); |
| 166 | + assert_eq!(y as usize, x as usize + base_size * 2); |
| 167 | + let z = heap.allocate_first_fit(base_size * 3, base_align * 4).unwrap(); |
| 168 | + assert_eq!(z as usize % (base_size * 4), 0); |
| 169 | + |
| 170 | + heap.deallocate(x, base_size * 2, base_align); |
| 171 | + |
| 172 | + let a = heap.allocate_first_fit(base_size * 4, base_align).unwrap(); |
| 173 | + let b = heap.allocate_first_fit(base_size * 2, base_align).unwrap(); |
| 174 | + assert_eq!(b, x); |
| 175 | + |
| 176 | + heap.deallocate(y, base_size * 7, base_align); |
| 177 | + heap.deallocate(z, base_size * 3, base_align * 4); |
| 178 | + heap.deallocate(a, base_size * 4, base_align); |
| 179 | + heap.deallocate(b, base_size * 2, base_align); |
| 180 | + } |
| 181 | + |
| 182 | + #[test] |
| 183 | + fn allocate_usize() { |
| 184 | + let mut heap = new_heap(); |
| 185 | + |
| 186 | + assert!(heap.allocate_first_fit(size_of::<usize>(), 1).is_some()); |
| 187 | + } |
| 188 | + |
| 189 | + |
| 190 | + #[test] |
| 191 | + fn allocate_usize_in_bigger_block() { |
| 192 | + let mut heap = new_heap(); |
| 193 | + |
| 194 | + let x = heap.allocate_first_fit(size_of::<usize>() * 2, 1).unwrap(); |
| 195 | + let y = heap.allocate_first_fit(size_of::<usize>() * 2, 1).unwrap(); |
| 196 | + heap.deallocate(x, size_of::<usize>() * 2, 1); |
| 197 | + |
| 198 | + let z = heap.allocate_first_fit(size_of::<usize>(), 1); |
| 199 | + assert!(z.is_some()); |
| 200 | + let z = z.unwrap(); |
| 201 | + assert_eq!(x, z); |
| 202 | + |
| 203 | + heap.deallocate(y, size_of::<usize>() * 2, 1); |
| 204 | + heap.deallocate(z, size_of::<usize>(), 1); |
| 205 | + } |
| 206 | +} |
0 commit comments