Skip to content

Commit

Permalink
#1913: make use_header a template parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
stmcgovern committed Sep 13, 2022
1 parent 30cd063 commit 0e111f5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
39 changes: 19 additions & 20 deletions src/vt/pool/static_sized/memory_pool_equal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@

namespace vt { namespace pool {

template <int64_t num_bytes_t>
MemoryPoolEqual<num_bytes_t>::MemoryPoolEqual(SlotType const in_pool_size, bool in_use_header)
: pool_size_(in_pool_size),
use_header_(in_use_header)
template <int64_t num_bytes_t, bool use_header>
MemoryPoolEqual<num_bytes_t, use_header>::MemoryPoolEqual(SlotType const in_pool_size)
: pool_size_(in_pool_size)
{
resizePool();
}

template <int64_t num_bytes_t>
/*virtual*/ MemoryPoolEqual<num_bytes_t>::~MemoryPoolEqual() {
template <int64_t num_bytes_t, bool use_header>
/*virtual*/ MemoryPoolEqual<num_bytes_t, use_header>::~MemoryPoolEqual() {
vt_debug_print(
normal, pool,
"cur_slot_={}\n", cur_slot_
Expand All @@ -78,8 +77,8 @@ template <int64_t num_bytes_t>
}
}

template <int64_t num_bytes_t>
void* MemoryPoolEqual<num_bytes_t>::alloc(
template <int64_t num_bytes_t, bool use_header>
void* MemoryPoolEqual<num_bytes_t, use_header>::alloc(
size_t const& sz, size_t const& oversize
) {
if (static_cast<size_t>(cur_slot_ + 1) >= holder_.size()) {
Expand All @@ -94,7 +93,7 @@ void* MemoryPoolEqual<num_bytes_t>::alloc(
auto const& slot = cur_slot_;
void* const ptr = holder_[slot];
void* ptr_ret = ptr;
if (use_header_) {
if (use_header) {
ptr_ret = HeaderManagerType::setHeader(
sz, oversize, static_cast<char*>(ptr)
);
Expand All @@ -111,8 +110,8 @@ void* MemoryPoolEqual<num_bytes_t>::alloc(
return ptr_ret;
}

template <int64_t num_bytes_t>
void MemoryPoolEqual<num_bytes_t>::dealloc(void* const t) {
template <int64_t num_bytes_t, bool use_header>
void MemoryPoolEqual<num_bytes_t, use_header>::dealloc(void* const t) {
vt_debug_print(
normal, pool,
"dealloc t={}, cur_slot={}\n", t, cur_slot_
Expand All @@ -124,32 +123,32 @@ void MemoryPoolEqual<num_bytes_t>::dealloc(void* const t) {

auto t_char = static_cast<char*>(t);
void* ptr_actual = t;
if (use_header_) {
if (use_header) {
ptr_actual = HeaderManagerType::getHeaderPtr(t_char);
}

holder_[--cur_slot_] = ptr_actual;
}

template <int64_t num_bytes_t>
void MemoryPoolEqual<num_bytes_t>::resizePool() {
template <int64_t num_bytes_t, bool use_header>
void MemoryPoolEqual<num_bytes_t, use_header>::resizePool() {
SlotType const cur_size = holder_.size();
SlotType const new_size = cur_size == 0 ? pool_size_ : cur_size * 2;

holder_.resize(new_size);

for (auto i = cur_size; i < new_size; i++) {
holder_[i] = static_cast<void*>(malloc(use_header_ ? num_full_bytes_ : num_bytes_t));
holder_[i] = static_cast<void*>(malloc(use_header ? num_full_bytes_ : num_bytes_t));
}
}

template <int64_t num_bytes_t>
typename MemoryPoolEqual<num_bytes_t>::SlotType
MemoryPoolEqual<num_bytes_t>::getNumBytes() {
template <int64_t num_bytes_t, bool use_header>
typename MemoryPoolEqual<num_bytes_t, use_header>::SlotType
MemoryPoolEqual<num_bytes_t, use_header>::getNumBytes() {
return num_bytes_;
}

template struct MemoryPoolEqual<memory_size_small>;
template struct MemoryPoolEqual<memory_size_medium>;
template struct MemoryPoolEqual<memory_size_small, true>;
template struct MemoryPoolEqual<memory_size_medium, true>;

}} //end namespace vt::pool
5 changes: 2 additions & 3 deletions src/vt/pool/static_sized/memory_pool_equal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static constexpr size_t const medium_msg_size_buf =
static constexpr size_t const memory_size_medium =
sizeof(EpochTagEnvelope) + medium_msg_size_buf;

template <int64_t num_bytes_t>
template <int64_t num_bytes_t, bool use_header = true>
struct MemoryPoolEqual {
using ContainerType = std::vector<void*>;
using SlotType = int64_t;
Expand All @@ -74,7 +74,7 @@ struct MemoryPoolEqual {
static constexpr SlotType const fst_pool_slot = 0;
static constexpr SlotType const default_pool_size = 1024;

MemoryPoolEqual(SlotType const in_pool_size = default_pool_size, bool in_use_header = true);
MemoryPoolEqual(SlotType const in_pool_size = default_pool_size);

virtual ~MemoryPoolEqual();

Expand All @@ -100,7 +100,6 @@ struct MemoryPoolEqual {
SlotType cur_slot_ = fst_pool_slot;

ContainerType holder_;
bool use_header_ = true;
};

}} //end namespace vt::pool
Expand Down

0 comments on commit 0e111f5

Please sign in to comment.