Skip to content

Commit

Permalink
gh-610 Rename Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
nolan-veed authored and gavv committed Nov 16, 2023
1 parent 2a1fd2d commit 62a5016
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 154 deletions.
2 changes: 1 addition & 1 deletion src/internal_modules/roc_core/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#include "roc_core/align_ops.h"
#include "roc_core/macro_helpers.h"
#include "roc_core/pool.h"
#include "roc_core/ref_counted.h"
#include "roc_core/slab_pool.h"
#include "roc_core/stddefs.h"

namespace roc {
Expand Down
4 changes: 2 additions & 2 deletions src/internal_modules/roc_core/buffer_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include "roc_core/allocation_policy.h"
#include "roc_core/buffer.h"
#include "roc_core/noncopyable.h"
#include "roc_core/pool.h"
#include "roc_core/shared_ptr.h"
#include "roc_core/slab_pool.h"

namespace roc {
namespace core {
Expand Down Expand Up @@ -44,7 +44,7 @@ template <class T> class BufferFactory : public core::NonCopyable<> {
}

private:
Pool<Buffer<T> > buffer_pool_;
SlabPool<Buffer<T> > buffer_pool_;
const size_t buffer_size_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

//! @file roc_core/pool.h
//! @file roc_core/slab_pool.h
//! @brief Memory pool.

#ifndef ROC_CORE_POOL_H_
#define ROC_CORE_POOL_H_
#ifndef ROC_CORE_SLAB_POOL_H_
#define ROC_CORE_SLAB_POOL_H_

#include "roc_core/aligned_storage.h"
#include "roc_core/attributes.h"
#include "roc_core/iarena.h"
#include "roc_core/ipool.h"
#include "roc_core/noncopyable.h"
#include "roc_core/pool_impl.h"
#include "roc_core/slab_pool_impl.h"
#include "roc_core/stddefs.h"

namespace roc {
namespace core {

//! Memory pool flags.
enum PoolFlags {
enum SlabPoolFlags {
//! Enable guards for buffer overflow, invalid ownership, etc.
PoolFlag_EnableGuards = (1 << 0),
SlabPoolFlag_EnableGuards = (1 << 0),
};

//! Default memory pool flags.
enum { DefaultPoolFlags = (PoolFlag_EnableGuards) };
enum { DefaultSlabPoolFlags = (SlabPoolFlag_EnableGuards) };

//! Memory pool.
//!
Expand All @@ -56,13 +56,13 @@ enum { DefaultPoolFlags = (PoolFlag_EnableGuards) };
//! @tparam T defines pool object type. It is used to determine allocation size. If
//! runtime size is different from static size of T, it can be provided via constructor.
//!
//! @tparam EmbeddedCapacity defines number of slots embedded directly into Pool
//! @tparam EmbeddedCapacity defines number of slots embedded directly into SlabPool
//! instance. If non-zero, this memory will be used for first allocations, before
//! using memory arena.
//!
//! Thread-safe.
template <class T, size_t EmbeddedCapacity = 0>
class Pool : public IPool, public NonCopyable<> {
class SlabPool : public IPool, public NonCopyable<> {
public:
//! Initialize.
//!
Expand All @@ -72,13 +72,13 @@ class Pool : public IPool, public NonCopyable<> {
//! - @p object_size defines size of single object in bytes
//! - @p min_alloc_bytes defines minimum size in bytes per request to arena
//! - @p max_alloc_bytes defines maximum size in bytes per request to arena
//! - @p flags defines options to modify behaviour as indicated in PoolFlags
explicit Pool(const char* name,
IArena& arena,
size_t object_size = sizeof(T),
size_t min_alloc_bytes = 0,
size_t max_alloc_bytes = 0,
size_t flags = DefaultPoolFlags)
//! - @p flags defines options to modify behaviour as indicated in SlabPoolFlags
explicit SlabPool(const char* name,
IArena& arena,
size_t object_size = sizeof(T),
size_t min_alloc_bytes = 0,
size_t max_alloc_bytes = 0,
size_t flags = DefaultSlabPoolFlags)
: impl_(name,
arena,
object_size,
Expand Down Expand Up @@ -116,15 +116,15 @@ class Pool : public IPool, public NonCopyable<> {

private:
enum {
SlotSize = (sizeof(PoolImpl::SlotHeader) + sizeof(PoolImpl::SlotCanary)
+ sizeof(T) + sizeof(PoolImpl::SlotCanary) + sizeof(AlignMax) - 1)
SlotSize = (sizeof(SlabPoolImpl::SlotHeader) + sizeof(SlabPoolImpl::SlotCanary)
+ sizeof(T) + sizeof(SlabPoolImpl::SlotCanary) + sizeof(AlignMax) - 1)
/ sizeof(AlignMax) * sizeof(AlignMax)
};
AlignedStorage<EmbeddedCapacity * SlotSize> embedded_data_;
PoolImpl impl_;
SlabPoolImpl impl_;
};

} // namespace core
} // namespace roc

#endif // ROC_CORE_POOL_H_
#endif // ROC_CORE_SLAB_POOL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include "roc_core/pool_impl.h"
#include "roc_core/slab_pool_impl.h"
#include "roc_core/align_ops.h"
#include "roc_core/log.h"
#include "roc_core/memory_ops.h"
#include "roc_core/panic.h"
#include "roc_core/pool.h"
#include "roc_core/slab_pool.h"

namespace roc {
namespace core {
Expand All @@ -30,14 +30,14 @@ size_t clamp(size_t value, size_t lower_limit, size_t upper_limit) {

} // namespace

PoolImpl::PoolImpl(const char* name,
IArena& arena,
size_t object_size,
size_t min_alloc_bytes,
size_t max_alloc_bytes,
void* preallocated_data,
size_t preallocated_size,
size_t flags)
SlabPoolImpl::SlabPoolImpl(const char* name,
IArena& arena,
size_t object_size,
size_t min_alloc_bytes,
size_t max_alloc_bytes,
void* preallocated_data,
size_t preallocated_size,
size_t flags)
: name_(name)
, arena_(arena)
, n_used_slots_(0)
Expand Down Expand Up @@ -68,21 +68,21 @@ PoolImpl::PoolImpl(const char* name,
}
}

PoolImpl::~PoolImpl() {
SlabPoolImpl::~SlabPoolImpl() {
deallocate_everything_();
}

size_t PoolImpl::object_size() const {
size_t SlabPoolImpl::object_size() const {
return object_size_;
}

bool PoolImpl::reserve(size_t n_objects) {
bool SlabPoolImpl::reserve(size_t n_objects) {
Mutex::Lock lock(mutex_);

return reserve_slots_(n_objects);
}

void* PoolImpl::allocate() {
void* SlabPoolImpl::allocate() {
Slot* slot;

{
Expand All @@ -98,7 +98,7 @@ void* PoolImpl::allocate() {
return give_slot_to_user_(slot);
}

void PoolImpl::deallocate(void* memory) {
void SlabPoolImpl::deallocate(void* memory) {
if (memory == NULL) {
roc_panic("pool: deallocating null pointer: name=%s", name_);
}
Expand All @@ -116,13 +116,13 @@ void PoolImpl::deallocate(void* memory) {
}
}

size_t PoolImpl::num_guard_failures() const {
size_t SlabPoolImpl::num_guard_failures() const {
Mutex::Lock lock(mutex_);

return num_guard_failures_;
}

void* PoolImpl::give_slot_to_user_(Slot* slot) {
void* SlabPoolImpl::give_slot_to_user_(Slot* slot) {
slot->~Slot();

SlotHeader* slot_hdr = (SlotHeader*)slot;
Expand All @@ -140,15 +140,15 @@ void* PoolImpl::give_slot_to_user_(Slot* slot) {
return memory;
}

PoolImpl::Slot* PoolImpl::take_slot_from_user_(void* memory) {
SlabPoolImpl::Slot* SlabPoolImpl::take_slot_from_user_(void* memory) {
SlotHeader* slot_hdr =
ROC_CONTAINER_OF((char*)memory - sizeof(SlotCanary), SlotHeader, data);

const bool is_owner = slot_hdr->owner == this;

if (!is_owner) {
num_guard_failures_++;
if (flags_ & PoolFlag_EnableGuards) {
if (flags_ & SlabPoolFlag_EnableGuards) {
roc_panic("pool: attempt to deallocate slot not belonging to this pool:"
" name=%s this_pool=%p slot_pool=%p",
name_, (const void*)this, (const void*)slot_hdr->owner);
Expand All @@ -166,7 +166,7 @@ PoolImpl::Slot* PoolImpl::take_slot_from_user_(void* memory) {

if (!canary_before_ok || !canary_after_ok) {
num_guard_failures_++;
if (flags_ & PoolFlag_EnableGuards) {
if (flags_ & SlabPoolFlag_EnableGuards) {
roc_panic("pool: detected memory violation: name=%s ok_before=%d ok_after=%d",
name_, (int)canary_before_ok, (int)canary_after_ok);
}
Expand All @@ -177,7 +177,7 @@ PoolImpl::Slot* PoolImpl::take_slot_from_user_(void* memory) {
return new (slot_hdr) Slot;
}

PoolImpl::Slot* PoolImpl::acquire_slot_() {
SlabPoolImpl::Slot* SlabPoolImpl::acquire_slot_() {
if (free_slots_.is_empty()) {
allocate_new_slab_();
}
Expand All @@ -191,7 +191,7 @@ PoolImpl::Slot* PoolImpl::acquire_slot_() {
return slot;
}

void PoolImpl::release_slot_(Slot* slot) {
void SlabPoolImpl::release_slot_(Slot* slot) {
if (n_used_slots_ == 0) {
roc_panic("pool: unpaired deallocation: name=%s", name_);
}
Expand All @@ -200,7 +200,7 @@ void PoolImpl::release_slot_(Slot* slot) {
free_slots_.push_front(*slot);
}

bool PoolImpl::reserve_slots_(size_t desired_slots) {
bool SlabPoolImpl::reserve_slots_(size_t desired_slots) {
if (desired_slots > free_slots_.size()) {
increase_slab_size_(desired_slots - free_slots_.size());

Expand All @@ -214,7 +214,7 @@ bool PoolImpl::reserve_slots_(size_t desired_slots) {
return true;
}

void PoolImpl::increase_slab_size_(size_t desired_slots) {
void SlabPoolImpl::increase_slab_size_(size_t desired_slots) {
if (desired_slots > slab_max_slots_ && slab_max_slots_ != 0) {
desired_slots = slab_max_slots_;
}
Expand All @@ -229,7 +229,7 @@ void PoolImpl::increase_slab_size_(size_t desired_slots) {
}
}

bool PoolImpl::allocate_new_slab_() {
bool SlabPoolImpl::allocate_new_slab_() {
const size_t slab_size_bytes = slot_offset_(slab_cur_slots_);

void* memory = arena_.allocate(slab_size_bytes);
Expand All @@ -249,7 +249,7 @@ bool PoolImpl::allocate_new_slab_() {
return true;
}

void PoolImpl::deallocate_everything_() {
void SlabPoolImpl::deallocate_everything_() {
if (n_used_slots_ != 0) {
roc_panic("pool: detected memory leak: name=%s n_used=%lu n_free=%lu", name_,
(unsigned long)n_used_slots_, (unsigned long)free_slots_.size());
Expand All @@ -265,7 +265,7 @@ void PoolImpl::deallocate_everything_() {
}
}

void PoolImpl::add_preallocated_memory_(void* memory, size_t memory_size) {
void SlabPoolImpl::add_preallocated_memory_(void* memory, size_t memory_size) {
if (memory == NULL) {
roc_panic("pool: preallocated memory is null: name=%s", name_);
}
Expand All @@ -278,7 +278,7 @@ void PoolImpl::add_preallocated_memory_(void* memory, size_t memory_size) {
}
}

size_t PoolImpl::slots_per_slab_(size_t slab_size, bool round_up) const {
size_t SlabPoolImpl::slots_per_slab_(size_t slab_size, bool round_up) const {
roc_panic_if(slot_size_ == 0);

if (slab_size < slab_hdr_size_) {
Expand All @@ -293,7 +293,7 @@ size_t PoolImpl::slots_per_slab_(size_t slab_size, bool round_up) const {
/ slot_size_;
}

size_t PoolImpl::slot_offset_(size_t slot_index) const {
size_t SlabPoolImpl::slot_offset_(size_t slot_index) const {
return slab_hdr_size_ + slot_index * slot_size_;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

//! @file roc_core/pool_impl.h
//! @file roc_core/slab_pool_impl.h
//! @brief Memory pool implementation class.

#ifndef ROC_CORE_POOL_IMPL_H_
#define ROC_CORE_POOL_IMPL_H_
#ifndef ROC_CORE_SLAB_POOL_IMPL_H_
#define ROC_CORE_SLAB_POOL_IMPL_H_

#include "roc_core/align_ops.h"
#include "roc_core/attributes.h"
Expand Down Expand Up @@ -42,13 +42,13 @@ namespace core {
//! If user data requires padding to be maximum-aligned, this padding
//! also becomes part of the trailing canary guard.
//!
//! @see Pool.
class PoolImpl : public NonCopyable<> {
//! @see SlabPool.
class SlabPoolImpl : public NonCopyable<> {
public:
//! Slot header.
struct SlotHeader {
//! The pool that the slot belongs to.
PoolImpl* owner;
SlabPoolImpl* owner;
//! Variable-length data surrounded by canary guard.
AlignMax data[];
};
Expand All @@ -57,17 +57,17 @@ class PoolImpl : public NonCopyable<> {
typedef AlignMax SlotCanary;

//! Initialize.
PoolImpl(const char* name,
IArena& arena,
size_t object_size,
size_t min_alloc_bytes,
size_t max_alloc_bytes,
void* preallocated_data,
size_t preallocated_size,
size_t flags);
SlabPoolImpl(const char* name,
IArena& arena,
size_t object_size,
size_t min_alloc_bytes,
size_t max_alloc_bytes,
void* preallocated_data,
size_t preallocated_size,
size_t flags);

//! Deinitialize.
~PoolImpl();
~SlabPoolImpl();

//! Get size of objects in pool.
size_t object_size() const;
Expand Down Expand Up @@ -133,4 +133,4 @@ class PoolImpl : public NonCopyable<> {
} // namespace core
} // namespace roc

#endif // ROC_CORE_POOL_IMPL_H_
#endif // ROC_CORE_SLAB_POOL_IMPL_H_
Loading

0 comments on commit 62a5016

Please sign in to comment.