Grok 20.3.2
grk::SlabPool Class Reference

A thread-safe slab-based buffer pool with configurable slot size and number of slots. More...

#include <SlabPool.h>

Public Member Functions

 SlabPool (size_t numSlots, size_t slotSize)
 Constructs a slab pool with a specified number of slots and slot size.
 SlabPool (const SlabPool &)=delete
 Deleted copy constructor to prevent copying.
SlabPooloperator= (const SlabPool &)=delete
 Deleted assignment operator to prevent copying.
 SlabPool (SlabPool &&other) noexcept
 Move constructor for transferring ownership.
SlabPooloperator= (SlabPool &&other) noexcept
 Move assignment operator for transferring ownership.
 ~SlabPool ()=default
 Destructor, frees the slab memory.
uint8_t * allocate ()
 Allocates a buffer from the pool.
void recycle (uint8_t *buffer)
 Returns a buffer to the pool for reuse.
size_t freeCount () const
 Returns the number of free buffers currently available.
size_t totalSlots () const
 Returns the total number of slots in the pool.
size_t slotSize () const
 Returns the size of each slot in bytes.
size_t slabSize () const
 Returns the total size of the slab in bytes.

Private Attributes

size_t numSlots_
size_t slotSize_
size_t slabSize_
std::unique_ptr< uint8_t[]> slab_
std::mutex mutex_
std::queue< uint8_t * > freeBuffers_

Detailed Description

A thread-safe slab-based buffer pool with configurable slot size and number of slots.

Manages a single contiguous slab of memory, divided into slots of a specified size, with a specified number of slots. Provides allocation and recycling of buffers for tile part fetching and decompression workflows, reducing memory allocation overhead.

Constructor & Destructor Documentation

◆ SlabPool() [1/3]

grk::SlabPool::SlabPool ( size_t numSlots,
size_t slotSize )
inline

Constructs a slab pool with a specified number of slots and slot size.

Parameters
numSlotsNumber of slots in the slab.
slotSizeSize of each slot in bytes.
Exceptions
std::invalid_argumentif numSlots or slotSize is 0.
std::bad_allocif slab allocation fails.

References freeBuffers_, numSlots_, slab_, slabSize_, slotSize(), and slotSize_.

Referenced by operator=(), operator=(), SlabPool(), and SlabPool().

◆ SlabPool() [2/3]

grk::SlabPool::SlabPool ( const SlabPool & )
delete

Deleted copy constructor to prevent copying.

References SlabPool().

◆ SlabPool() [3/3]

grk::SlabPool::SlabPool ( SlabPool && other)
inlinenoexcept

Move constructor for transferring ownership.

References freeBuffers_, and SlabPool().

◆ ~SlabPool()

grk::SlabPool::~SlabPool ( )
default

Destructor, frees the slab memory.

Member Function Documentation

◆ allocate()

uint8_t * grk::SlabPool::allocate ( )
inline

Allocates a buffer from the pool.

Returns
Pointer to a buffer of slotSize_ bytes.
Exceptions
std::runtime_errorif no free buffers are available.

References freeBuffers_, and mutex_.

◆ freeCount()

size_t grk::SlabPool::freeCount ( ) const
inline

Returns the number of free buffers currently available.

Returns
Number of free buffers.

References freeBuffers_, and mutex_.

◆ operator=() [1/2]

SlabPool & grk::SlabPool::operator= ( const SlabPool & )
delete

Deleted assignment operator to prevent copying.

References SlabPool().

◆ operator=() [2/2]

SlabPool & grk::SlabPool::operator= ( SlabPool && other)
inlinenoexcept

Move assignment operator for transferring ownership.

References freeBuffers_, mutex_, numSlots_, slab_, SlabPool(), slabSize_, and slotSize_.

◆ recycle()

void grk::SlabPool::recycle ( uint8_t * buffer)
inline

Returns a buffer to the pool for reuse.

Parameters
bufferPointer to the buffer to recycle (must be from this pool).

References freeBuffers_, mutex_, slab_, slabSize_, and slotSize_.

◆ slabSize()

size_t grk::SlabPool::slabSize ( ) const
inline

Returns the total size of the slab in bytes.

Returns
Total slab size (numSlots_ * slotSize_).

References slabSize_.

◆ slotSize()

size_t grk::SlabPool::slotSize ( ) const
inline

Returns the size of each slot in bytes.

Returns
Slot size in bytes.

References slotSize_.

Referenced by SlabPool().

◆ totalSlots()

size_t grk::SlabPool::totalSlots ( ) const
inline

Returns the total number of slots in the pool.

Returns
Total number of slots.

References numSlots_.

Member Data Documentation

◆ freeBuffers_

std::queue<uint8_t*> grk::SlabPool::freeBuffers_
private

◆ mutex_

std::mutex grk::SlabPool::mutex_
mutableprivate

◆ numSlots_

size_t grk::SlabPool::numSlots_
private

Referenced by operator=(), SlabPool(), and totalSlots().

◆ slab_

std::unique_ptr<uint8_t[]> grk::SlabPool::slab_
private

Referenced by operator=(), recycle(), and SlabPool().

◆ slabSize_

size_t grk::SlabPool::slabSize_
private

Referenced by operator=(), recycle(), SlabPool(), and slabSize().

◆ slotSize_

size_t grk::SlabPool::slotSize_
private

Referenced by operator=(), recycle(), SlabPool(), and slotSize().


The documentation for this class was generated from the following file: