unnamed_addr
unnamed_addr

Reputation: 1307

How to implement a custom allocator?

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:

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

Upvotes: 6

Views: 6400

Answers (3)

Shepmaster
Shepmaster

Reputation: 430841

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.

Upvotes: 8

DK.
DK.

Reputation: 59035

You may want to look at arena::TypedArena in the standard library (Note: this is not stable and, as a result, is only available in nightly builds).

If this doesn't fit your needs, you can always examine the source code (you can click the [src] link in the top right of the documentation) to see how it's done.

Upvotes: 2

Vladimir Matveev
Vladimir Matveev

Reputation: 127801

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.

Upvotes: 2

Related Questions