Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Using the memory pool

Jan Stephan edited this page Dec 9, 2016 · 2 revisions

Philosophy

In some cases it is not desirable to frequently allocate and free memory, especially when the allocated objects are of the same size. In order to prevent the malloc/free overhead for these objects GLADOS offers memory pools for 1D, 2D and 3D cases.

Usage

To use GLADOS' memory pool the user needs to specify three types via template parameters:

  1. the type of the allocated memory
  2. the memory layout (1D, 2D or 3D)
  3. an allocator conforming to the abovementioned points

Example for GLADOS' CUDA device allocator:

using device_allocator = glados::cuda::device_allocator<float, memory_layout::pointer_2D>;
using pool_allocator = glados::pool_allocator<float, memory_layout::pointer_2D, device_allocator>;

auto alloc = pool_allocator{};

// optionally specify a limit
auto limit = std::size_t{100};
auto alloc_limit = pool_allocator{limit};

To allocate objects of the same size either call allocate (for a raw pointer) or allocate_smart (for an unique_ptr-like object):

// allocate raw 2D pointer
auto raw = alloc.allocate(800, 600);
// allocate smart 2D pointer
auto smart = alloc.allocate_smart(800, 600);

// return raw 2D pointer to pool
alloc.deallocate(raw);
// no need to deallocate smart 2D pointer -- let RAII handle this

To release the managed memory, call release:

// calls the actual allocators memory free functions
alloc.release();

Note that the managed memory is NOT automatically freed upon memory_pool's destruction - the user MUST call release once the pool is no longer needed.

Important notes

GLADOS' pool_allocator uses fixed-size pointers. This means that the first call to allocate or allocate_smart will set the dimensions for all succeeding calls as well; the size parameters will therefore be ignored. If you want to reuse the pool for other dimensions later you need to release the managed memory first by calling release; this will automatically reset the size limit.

Clone this wiki locally