GithubHelp home page GithubHelp logo

slaballoc's Introduction

slaballoc: A no-std compatible, thread-safe fixed-size memory allocator

Do you need to allocate objects of a single type on a bare-metal system using #[no_std]? Do you have the requirement that an allocator be concurrently accessed from multiple threads or CPU cores?

Look no further!

This crate provides a lockless no_std compatible slab allocator, for fixed-size allocations.

This slab allocator takes a fixed-size block of memory, as well as a [Sized] type for allocations. The slab allocator will partition the block of memory into two parts: an area for allocated objects, and an area for an allocation bitmap.

The bitmap will have a bit size of mem_size / size_of::<T>(), such that there is a bit for every possible slot that an object can take. So, for a 4096 byte block of memory and objects that are 16 bytes each, the bitmap will take 256 bits, or 32 bytes.

| objects........................................ - bitmap |

Note that the bitmap will be a little larger than necessary because it accounts for the entire block of memory (including the part that it takes up). This can be accounted for, but it's an annoying self-dependency so it'll take some iterations.

The allocation bitmap can then be accessed locklessly using atomic intrinsics. When allocating, we'll simply set a bit in the bitmap, and if successful, return the memory corresponding to that bit. When deallocating, we can simply unset the bit and the memory is freed.

Alternatives

If you're using Rust on a hosted environment with std available, you might be interested in using sharded-slab instead. That crate gets you the same semantics as this one, without having to used a fixed-size memory block.

slaballoc's People

Contributors

drchat avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.