Giter Club home page Giter Club logo

anslabs's Introduction

ANSlab

The purpose of this project is to provide a well tested, easy-to-use Slab allocator for OS kernels.

Usage

First, add the include/ directory to your header search path and include anslabs like this:

#include <anslabs>

All of the functionality of anslabs is under the namespace ANSlabs.

Raw Slabs

Raw slabs allocate a certain number of elements once using a new call. A raw slab will never allocate more elements, so you cannot use them exclusively unless you know the absolute maximum number of elements you will ever need.

While test/test_raw.cpp provides the best example, here I will show you how to use the raw slab allocator. Create an instance like this:

ANSlabs::Raw<X, Y> * allocator = new ANSlabs::Raw<X, Y>();

Here, X is the maximum number of elements the slab allocator may hold, and Y is the size of each element. It is recommended that each element is aligned the way you would like your instances aligned: probably by 16-bytes for the System V ABI.

Now, you can allocate and free buffers from the slab easily:

void * myBuffer = allocator->AllocBuf();
// ... do something here with myBuffer ...
allocator.FreeBuf(myBuffer);

The AllocBuf() method will return NULL when there are no free elements left in the slab.

Cap Slabs

Cap slabs are used pretty much identically to raw slabs, but if they run out of memory they will start returning objects from new and delete. This way any number of elements may be allocated, but the allocation will stop being O(1) after a certain cap (hence the name cap allocator).

You may use a cap allocator the same way you would use a raw allocator, but use the class name ANSlabs::Cap<X, Y>().

Structor Allocator

A structor allocator provides an alternative to new and delete to create and destroy objects quickly. Create a structor allocator on top of a different allocation pool like a cap allocator:

ANSlabs::Structor<MyClass, ANSlabs::Cap<0x10, sizeof(MyClass)> > aStructor();

This will create a new structor on top of a cap allocator that creates instances of MyClass. Now, you may allocate and delete instances like this:

MyClass * anInstance = aStructor.New(some, arguments, here)
/ ... do something here with anInstance ...
aStructor.Delete(anInstance);

anslabs's People

Contributors

unixpickle 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.