Giter Site home page Giter Site logo

blockallocator's Introduction

BlockAllocator

This is a single-header block allocator that I wrote as an excercise. There are definitively better options out there, but it might be good for educational purposes.

Usage

This allocator is a standard block/pool allocator that, when created, allocates a block of memory from the heap. You can then store objects in this block using the member method alloc(). If you run out of memory in the current block, another block will be allocated. How many elements can be stored in one block, and the maximum total amount of blocks that can be allocated is defined by the user. A too small block size will result in a lot of dynamic allocation, so it's recommended to set these values accordingly.

Creation

This example creates an allocator that stores objects of the type Foo, where one block can hold 8 elements, and a total of 2 blocks can be allocated.

// Like this for custom values
BlockAllocator<Foo, 8, 2> allocator;
// Or like this for a default of 32 and 4
BlockAllocator<Foo> allocator;

Allocation

This stores the object on the heap and returns the address. The object is sent as a reference, and is then copied onto the heap.

Foo* objPtr = allocator.insert(Foo(1, 2, 3));

This constructs the object directly onto the heap, taking the object's constructor arguments.

Foo* objPtr = allocator.alloc(1, 2, 3);

Both these examples assume that Foo has a constructor that takes 3 integers.

Deallocation

This removes the element from the block, and frees up its spot.

allocator.dealloc(objPtr);

If invoking this method empties a block, this block will not be deallocated. If a block has been created it will stay until the allocator itself is destroyed. This makes it so that we don't have to allocate a new block if we want to add that element again.

Fragmentation Prevention

This allocator does not defragment, since this would require the use of smart pointers. It does however prevent fragmentation to some extent.
In this example, c will simply fill in a's old position. And when we allocate d it skips to the next free slot.

BlockAllocator<Foo, 3, 1> allocator;  // [-, -, -]
Foo* a = allocator.alloc(Foo());      // [A, -, -]
Foo* b = allocator.alloc(Foo());      // [A, B, -]
allocator.dealloc(a);                 // [-, B, -]
Foo* c = allocator.alloc(Foo());      // [C, B, -]
Foo* d = allocator.alloc(Foo());      // [C, B, D]

blockallocator's People

Contributors

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