Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a custom allocator?

Tags:

rust

I am looking for a way to implement something like a memory pool in Rust.

I want to allocate a set of related small objects in chunks, and delete the set of objects at once. The objects won't be freed separately. There are several benefits to this approach:

  • It reduces fragmentation.
  • It saves memory.

Is there any way to create a allocator like this in Rust?

like image 829
unnamed_addr Avatar asked May 19 '15 12:05

unnamed_addr


People also ask

What is a custom allocator?

Allocators handle all the requests for allocation and deallocation of memory for a given container. The C++ Standard Library provides general-purpose allocators that are used by default, however, custom allocators may also be supplied by the programmer.

Where does the allocator are implemented?

Where are allocators implemented? Explanation: Allocators are implemented in C++ standard library but it is used for C++ template library.

What does an allocator actually need to do?

They provide an interface to allocate, create, destroy, and deallocate objects. With allocators, containers and algorithms can be parameterized by the way the elements are stored.


2 Answers

It sounds like you want the typed arena crate, which is stable and can be used in Rust 1.0.

extern crate typed_arena;

#[derive(Debug)]
struct Foo {
    a: u8,
    b: u8,
}

fn main() {
    let allocator = typed_arena::Arena::new();
    let f = allocator.alloc(Foo { a: 42, b: 101 });
    println!("{:?}", f)
}

This does have limitations - all the objects must be the same. In my usage, I have a very small set of types that I wish to have, so I have just created a set of Arenas, one for each type.

If that isn't suitable, you can look to arena::Arena, which is unstable and slower than a typed arena.

The basic premise of both allocators is simple - you allow the arena to consume an item and it moves the bits around to its own memory allocation.

Another meaning for the word "allocator" is what is used when you box a value. It is planned that Rust will gain support for "placement new" at some point, and the box syntax is reserved for that.

In unstable versions of Rust, you can do something like box Foo(42), and a (hypothetical) enhancement to that would allow you to say something like box my_arena Foo(42), which would use the specified allocator. This capability is a few versions away from existing it seems.

like image 118
Shepmaster Avatar answered Nov 15 '22 10:11

Shepmaster


Funny thing is, the allocator you want is already available in arena crate. It is unstable, so you have to use nightlies to use this crate. You can look at its sources if you want to know how it is implemented.

like image 43
Vladimir Matveev Avatar answered Nov 15 '22 10:11

Vladimir Matveev