#bump

bump_alloc

global_allocator implementation of a bump allocator

1 unstable release

0.1.0 Jan 31, 2019

#22 in #bump

Download history 9/week @ 2024-07-22 14/week @ 2024-07-29 21/week @ 2024-08-05 10/week @ 2024-08-12 13/week @ 2024-08-19 8/week @ 2024-08-26 10/week @ 2024-09-02 4/week @ 2024-09-09 7/week @ 2024-09-16 17/week @ 2024-09-23 25/week @ 2024-09-30 9/week @ 2024-10-07 22/week @ 2024-10-14 8/week @ 2024-10-21 27/week @ 2024-11-04

58 downloads per month
Used in rust_hero

CC0 license

8KB
83 lines

Rust Bump Allocator

Build status

This Rust crate adds a global_allocator implementation for a bump allocator. A bump allocator is one in which a single underlying memory region is used to deal with all allocation requests, and any new request for memory is sourced from a 'bump' of the previously allocated pointers address. A crucial observation of a bump allocator is that any attempts to free (or in Rust parlance dealloc) the memory is a no-operation - EG. all memory allocations effectively leak.

We service this bump allocator underneath by using a single mmap or VirtualAlloc call that requests a sufficiently sized region to handle all applications of a bump allocator.

How to Use

Just include the crate like:

extern crate bump_alloc;

#[global_allocator]
static A : BumpAlloc = BumpAlloc::new();

And it'll remap all Rust allocations using the bump allocator.

By default we reserve one gigabyte of memory using mmap on Unix systems, or VirtualAlloc on Windows systems. If you have a need for more memory in your application, you can use the with_size method, specifying the number of bytes you want the bump allocator to reserve:

extern crate bump_alloc;

#[global_allocator]
static A : BumpAlloc = BumpAlloc::with_size(1024 * 1024 * 4);

This example would allocate four megabytes of memory for use in the bump allocator.

A note for the uninitiatied, both mmap and VirtualAlloc do not just allocate up front the amount of memory that is requested - they just lay the ground work in the operating system so that we can allocate that amount of memory. So an application that reserves one gigabyte of memory but ony uses a single megabyte will only use the single megabyte of RAM.

Where to Use

If you have short running applications, or applications that do not overly abuse allocations, a bump allocator can be a useful trade-off for using more memory but achieving a faster executable. The implementation of a bump allocator is such that performing allocations is an incredibly simple operation, which leaves more CPU cycles for actual useful work.

If you have an application that performs a huge number of allocations and is a long running applicaton, this bump allocator is not the allocator you are looking for. Memory exhaustion is likely in these scenarios, beware!

License

This code is licensed under the CC0 1.0 Universal license, which is a permissible public domain license.

Dependencies

~1–270KB