#os #malloc #slab #alloc #memory

slabmalloc

Simple slab based malloc implementation in rust. Can be used stand-alone or in order to provide the necessary interface to rusts liballoc library. slabmalloc only relies on libcore.

10 releases

0.3.4 Sep 16, 2019
0.3.3 Sep 16, 2019
0.3.1 Jul 5, 2019
0.3.0 Jun 28, 2018
0.0.1 Jul 12, 2015

#20 in Memory management

Download history 6/week @ 2019-06-02 30/week @ 2019-06-09 8/week @ 2019-06-16 10/week @ 2019-06-23 40/week @ 2019-06-30 25/week @ 2019-07-07 3/week @ 2019-07-14 7/week @ 2019-07-21 5/week @ 2019-07-28 8/week @ 2019-08-04 30/week @ 2019-08-11 2/week @ 2019-08-18 40/week @ 2019-08-25 21/week @ 2019-09-01 22/week @ 2019-09-08

77 downloads per month

MIT license

36KB
725 lines

slabmalloc Build Status Crates.io

Simple slab based malloc implementation in rust, in order to provide the necessary interface to rusts liballoc library. slabmalloc only relies on libcore and is designed to be used in kernel level code as the only interface a client needs to provide is the necessary mechanism to allocate and free 4KiB frames (or any other default page-size on non-x86 hardware).

Usage

  • The slabmalloc API is designed to satisfy the rust liballoc low-level memory allocation interface:
use slabmalloc::{SafeZoneAllocator};
#[global_allocator]
static MEM_PROVIDER: SafeZoneAllocator = SafeZoneAllocator::new(&PAGER);
  • Use the ZoneAllocator to allocate arbitrary sized objects:
let object_size = 12;
let alignment = 4;
let mmap = Mutex::new(MmapPageProvider::new());
let mut zone = ZoneAllocator::new(&mmap);

unsafe {
  let layout = Layout::from_size_align(object_size, alignment).unwrap();
  let allocated = zone.allocate(layout);
  assert!(!allocated.is_null());
  zone.deallocate(allocated, layout);
}
  • Use the SCAllocator to allocate fixed sized objects:
let object_size = 10;
let alignment = 8;
let layout = Layout::from_size_align(object_size, alignment).unwrap();
let mut mmap = Mutex::new(MmapPageProvider::new());
let mut sa: SCAllocator = SCAllocator::new(object_size, &mut mmap);
sa.allocate(layout);

Using on stable

By default this packages requires a nightly version of the Rust compiler. To be able to use this package with a stable version of the Rust compiler, default features have to be disabled, e.g. with

slabmalloc = { version = ..., default_features = false }

Documentation

TODO

  • No focus on performance yet

Dependencies

~130KB