# MemoryPool.hpp

This File On Github
Ask A Question

API: latest
module: oatpp
#include "oatpp/core/base/memory/MemoryPool.hpp"

# MemoryPool

Memory Pool allocates memory chunks. Each chunk consists of specified number of fixed-size entries. Entries can be obtained and freed by user. When memory pool runs out of free entries, new chunk is allocated.

namespace oatpp { namespace base { namespace memory { 
  class MemoryPool {}
}}}

# Methods

Return Type Name Summary
[none] MemoryPool Multiple implementations:
  1. Constructor.
  2. Deleted copy-constructor.
[none] ~MemoryPool Virtual destructor.
void* obtain Obtain pointer to memory entry.
void free Free obtained earlier memory entry.
std::string getName Get name of the memory pool.
v_buff_size getEntrySize Get size of the memory entry in bytes which can be obtained by call to MemoryPool::obtain().
v_buff_size getSize Get size of the memory allocated by memory pool.
v_int64 getObjectsCount Get number of entries currently in use.

# MemoryPool::MemoryPool

  1. Constructor.
    • @param name - name of the pool.
    • @param entrySize - size of the entry in bytes returned in call to MemoryPool::obtain().
    • @param chunkSize - number of entries in one chunk.
    MemoryPool(const std::string& name, v_buff_size entrySize, v_buff_size chunkSize)
    
  2. Deleted copy-constructor.
    MemoryPool(const MemoryPool&) = delete
    

# MemoryPool::~MemoryPool

Virtual destructor.

virtual ~MemoryPool()

# MemoryPool::obtain

Obtain pointer to memory entry. When entry is no more needed, user must call oatpp::base::memory::MemoryPool::free() and pass obtained entry pointer as a parameter.

  • @return - pointer to memory entry.

void* obtain()

# MemoryPool::free

Free obtained earlier memory entry. This method is static, because entry "knows" to what pool it belongs.

static void free(void* entry)

# MemoryPool::getName

Get name of the memory pool.

  • @return - memory pool name as std::string.

std::string getName()

# MemoryPool::getEntrySize

Get size of the memory entry in bytes which can be obtained by call to MemoryPool::obtain().

  • @return - size of the enrty in bytes.

v_buff_size getEntrySize()

# MemoryPool::getSize

Get size of the memory allocated by memory pool.

  • @return - size of the memory allocated by memory pool.

v_buff_size getSize()

# MemoryPool::getObjectsCount

Get number of entries currently in use.

  • @return - number of entries currently in use.

v_int64 getObjectsCount()

# ThreadDistributedMemoryPool

Creates multiple MemoryPools (MemoryPool) to reduce concurrency blocking in call to ThreadDistributedMemoryPool::obtain()

namespace oatpp { namespace base { namespace memory { 
  class ThreadDistributedMemoryPool {}
}}}

# Fields

Type Name Summary
const v_int64 SHARDS_COUNT_DEFAULT Default number of MemoryPools (MemoryPool) "shards" to create.

# Methods

Return Type Name Summary
[none] ThreadDistributedMemoryPool Multiple implementations:
  1. Constructor.
  2. Deleted copy-constructor.
void* obtain Obtain pointer to memory entry.

# ThreadDistributedMemoryPool::SHARDS_COUNT_DEFAULT

Default number of MemoryPools (MemoryPool) "shards" to create.

static const v_int64 SHARDS_COUNT_DEFAULT

# ThreadDistributedMemoryPool::ThreadDistributedMemoryPool

  1. Constructor.
    • @param name - name of the memory pool.
    • @param entrySize - size of memory pool entry.
    • @param chunkSize - number of entries in chunk.
    • @param shardsCount - number of MemoryPools (MemoryPool) "shards" to create.
    ThreadDistributedMemoryPool(const std::string& name, v_buff_size entrySize, v_buff_size chunkSize,
                                v_int64 shardsCount = SHARDS_COUNT_DEFAULT)
    
  2. Deleted copy-constructor.
    ThreadDistributedMemoryPool(const ThreadDistributedMemoryPool&) = delete
    

# ThreadDistributedMemoryPool::obtain

Obtain pointer to memory entry. When entry is no more needed, user must call oatpp::base::memory::MemoryPool::free() and pass obtained entry pointer as a parameter.

  • @return - pointer to memory entry.

void* obtain()

# Bench

Not thread-safe pool of objects of specified type.

  • @tparam T - object type.

namespace oatpp { namespace base { namespace memory { 
  template<typename T>
  class Bench {}
}}}

# Methods

Return Type Name Summary
[none] Bench Constructor.
[none] ~Bench Non virtual destructor.
T* obtain Construct object and get pointer to constructed object.
void free Call object destructor and put it on "bench".

# Bench::Bench

Constructor.

  • @param growSize - number of objects to allocate when no free objects left.

Bench(v_buff_size growSize)
  : m_growSize(growSize)
  , m_size(0)
  , m_indexPosition(0)
  , m_blocks(nullptr)
  , m_index(nullptr)

# Bench::~Bench

Non virtual destructor.

~Bench()

# Bench::obtain

Construct object and get pointer to constructed object.

  • @tparam Args - arguments to be passed to object constructor.
  • @param args - actual arguments to pass to object constructor.
  • @return - pointer to constructed object.

template<typename ... Args>
T* obtain(Args... args)

# Bench::free

Call object destructor and put it on "bench".

  • @param entry - object to be freed.

void free(T* entry)