#blocks #allocator #arena #thread-safe #alloc #sized #numbers

nightly block_allocator

Thread-safe, fixed-size allocator that serves re-usable blocks

5 releases

Uses old Rust 2015

0.2.2 Jul 31, 2016
0.2.1 Jul 24, 2016
0.2.0 Jul 22, 2016
0.1.1 May 2, 2015
0.1.0 May 2, 2015

#617 in Memory management

Custom license

361 lines

Block Allocator


Build Status

This is a basic, thread-safe, fixed sized arena which allocates re-usable blocks of your specified sized. Re-usable meaning this isn't a basic arena which is use-once, blocks are free-able and re-allocatable. The intended use case for this allocator are multithreaded services which frequently allocate and deallocate uniform blocks of memory, such as web servers or videogames. It can run forever without any loss of performance due to fragmentation.

Presently it will allocate mutable u8 slices which are bound to the lifetime of the allocator itself, or raw *mut u8 pointers.

Its current limitations are that it can only work on 64 bit architectures, and it can only manage UINT32_MAX - 1 blocks because it uses a pair of 32 bit numbers for offset management. After some refactoring to atomics some time in the future, both limitations will be lifted.

It is currently fairly fast, running concurrently, it can alloc then free in 28ns per iteration, regardless of the size of the buffer or arena.

To use it, simply construct a new allocator, specifying the size of the block and the number of blocks you would like the allocator to manage (note that this number is not growable at runtime, so choose wisely)

    // create blocks of size 256 with a max of 100
    let myalloc = Allocator::new(256, 100).unwrap();

then you can alloc and free to your heart`s content

    let buf : &mut [u8] = myalloc.alloc();


    let ptr : *mut u8 = myalloc.alloc_raw();