Expand description
A simple range allocator for chunked external memory
ChunkedRangeAlloc was created for 2 use cases:
- packing game assets into archive files
- basis of specialized vulkan memory allocator
§Features:
AllocationincludesAllocation::chunk_indexin addition toAllocation::offsetandAllocation::lenChunkedRangeAlloc::from_allocationsconstructor for loading existing allocations (example: game assets index)- optional
bincodeandserdesupport, seeAllocation - simple, safe code
- good enough performance: allocator uses
BTreeinternally, best-fit search strategy, immediately coalesces on free
§Non-goals:
- blazingly fast constant O(🚀) time complexity
§Example
use std::num::NonZeroU32;
use chunked_range_alloc::ChunkedRangeAlloc;
// create allocator with chunk size = 4
let mut alloc = ChunkedRangeAlloc::new(NonZeroU32::new(4).unwrap());
// allocate size 1 with align = 1
let one = alloc.alloc(NonZeroU32::MIN, NonZeroU32::MIN);
assert_eq!(one.chunk_index, 0);
assert_eq!(one.offset, 0);
assert_eq!(one.len.get(), 1);
// allocate size 1 with align 2
let two = alloc.alloc(NonZeroU32::MIN, NonZeroU32::new(2).unwrap());
assert_eq!(two.chunk_index, 0);
assert_eq!(two.offset, 2); // offset is not 1 because of alignment
assert_eq!(two.len.get(), 1);
let free_chunk = alloc.free(one);
assert!(!free_chunk);
// free returns true if chunk becomes completely free
// you can eagerly free external memory in that case
let free_chunk = alloc.free(two);
assert!(free_chunk);
Structs§
- Allocation
ChunkedRangeAllocallocation- Chunked
Range Alloc - A simple range allocator for chunked external memory, see
Self::alloc