Allocate a storage block with a size given by `sclass`. Returns the first index of an available segment of `self.data` containing `sclass_size(sclass)` elements. The allocated memory is filled with reserved values.
(&mut self, sclass: SizeClass)
| 191 | /// `sclass_size(sclass)` elements. The allocated memory is filled with reserved |
| 192 | /// values. |
| 193 | fn alloc(&mut self, sclass: SizeClass) -> usize { |
| 194 | // First try the free list for this size class. |
| 195 | match self.free.get(sclass as usize).cloned() { |
| 196 | Some(head) if head > 0 => { |
| 197 | // The free list pointers are offset by 1, using 0 to terminate the list. |
| 198 | // A block on the free list has two entries: `[ 0, next ]`. |
| 199 | // The 0 is where the length field would be stored for a block in use. |
| 200 | // The free list heads and the next pointer point at the `next` field. |
| 201 | self.free[sclass as usize] = self.data[head].index(); |
| 202 | head - 1 |
| 203 | } |
| 204 | _ => { |
| 205 | // Nothing on the free list. Allocate more memory. |
| 206 | let offset = self.data.len(); |
| 207 | self.data |
| 208 | .resize(offset + sclass_size(sclass), T::reserved_value()); |
| 209 | offset |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /// Free a storage block with a size given by `sclass`. |
| 215 | /// |