Crate chunked_range_alloc

Crate chunked_range_alloc 

Source
Expand description

A simple range allocator for chunked external memory

ChunkedRangeAlloc was created for 2 use cases:

  1. packing game assets into archive files
  2. basis of specialized vulkan memory allocator

§Features:

§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
ChunkedRangeAlloc allocation
ChunkedRangeAlloc
A simple range allocator for chunked external memory, see Self::alloc