Name: simpool
Owner: Lawrence Livermore National Laboratory
Description: Simpool is a set of simple pooled memory allocators
Created: 2018-04-03 17:06:33.0
Updated: 2018-05-16 22:28:37.0
Pushed: 2018-04-16 23:01:55.0
Size: 24
Language: C++
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Simpool is a very simple pooled memory allocator that offers recipes
for use in C++ by overloading ::operator new(std::size_t)
and
fulfilling an STL allocator concept.
The concept behind a pooled memory allocator is to reduce the number of system calls to allocate memory, and instead takes memory from an already allocated segment of memory. It can be more efficient if there are many small allocations or if the allocator function incurs significant overhead.
Why do we need another memory pool?
While there are many other existing pool implementations, all the implementations that I can find have a flaw. This code addresses the following weaknesses:
While others create a usable pool for the memory, they do not for
keeping track of the blocks, meaning these allocations still incur
the overhead of the system malloc
for each block. This seems
like an oversight.
Other implementations do not offer the ability to select the memory spaces used for both the memory pool internally and for the memory pointers provided by the class.
This code uses a series of pools to represent the internal and allocated memory. These pools can be in any memory space reachable from the thread allocation and deallocation function.
The FixedPoolAllocator<T, MA, NP>
class stores “pools” each of
NP*sizeof(unsigned int)*8
objects of type T
in the memory space
with allocator struct MA
. An example of an allocator struct for the
system malloc()/free()
methods:
ct CPUAllocator
atic inline void *allocate(std::size_t size) { return std::malloc(size); }
atic inline void deallocate(void *ptr) { std::free(ptr); }
The class keeps track of which locations in the pool are unused by flipping single bits. It can do this because the objects are all the same size.
This algorithm calls the allocator function once per pool creation, so
is guaranteed to call it no more than every NP*sizeof(unsigned
int)*8
allocations.
The public non-constructor/destructor methods are:
T* allocate()
: returns a pointer to memory for an object T
void deallocate(T* ptr)
: Tells the pool that ptr
will no longer
be used. The behavior is undefined if ptr
was not returned from
allocate()
above.std::size_t allocatedSize() const
: Return the allocated size,
without internal overhead.std::size_t totalSize() const
: Return the total size of all
allocations within.std::size_t numPools() const
: Return the number of fixed size
pools.The DynamicPoolAllocator<MA, IA, MINSIZE>
class allocates objects
with the MA
allocator struct, and internally keeps track of the
blocks using a FixedMemoryPool
using the IA
allocator struct. Each
of the blocks allocated with MA
are at least size MINSIZE
- smaller
allocations are carved out.
This class largely follows the algorithm used in
cnmem. This involves splitting
blocks if allocations are smaller than MINSIZE
, and merging blocks
if possible when memory is marked as no longer used. It is therefore
difficult to determine an upper bound on the number of allocations
made with IA
and MA
.
The public non-constructor/destructor methods are:
void* allocate(std::size_t size)
: returns a pointer to size
bytes of memory.void deallocate(T* ptr)
: Tells the pool that ptr
will no longer
be used. The behavior is undefined if ptr
was not returned from
allocate(std::size_t)
above.std::size_t allocatedSize() const
: Return the allocated size,
without internal overhead.std::size_t totalSize() const
: Return the total size of the class
and all allocations within.std::size_t numFreeBlocks() const
: Return the number of free blocks.std::size_t numUsedBlocks() const
: Return the number of used blocks.© Copyright 2017 IBM Corporation. MIT License.
obstack
: info pagecnmem
: Github pagetcmalloc
: websitejemalloc
: website