Skip to content

Commit

Permalink
#2063: Update void* to std::byte* in Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
thearusable authored and cwschilly committed Sep 20, 2024
1 parent 238c00d commit d8bf238
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/vt/collective/scatter/scatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void Scatter::scatterIn(ScatterMsg* msg) {
child, num_children, child_bytes_size
);
auto const child_remaining_size =
thePool()->remainingSize(reinterpret_cast<void*>(child_msg.get()));
thePool()->remainingSize(reinterpret_cast<std::byte*>(child_msg.get()));
child_msg->user_han = user_handler;
auto ptr = reinterpret_cast<char*>(child_msg.get()) + sizeof(ScatterMsg);
vt_debug_print(
Expand Down
2 changes: 1 addition & 1 deletion src/vt/collective/scatter/scatter.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void Scatter::scatter(
auto ptr = reinterpret_cast<char*>(scatter_msg.get()) + sizeof(ScatterMsg);
#if vt_check_enabled(memory_pool)
auto remaining_size =
thePool()->remainingSize(reinterpret_cast<void*>(scatter_msg.get()));
thePool()->remainingSize(reinterpret_cast<std::byte*>(scatter_msg.get()));
vtAssertInfo(
remaining_size >= combined_size, "Remaining size must be sufficient",
total_size, combined_size, remaining_size, elm_size
Expand Down
6 changes: 3 additions & 3 deletions src/vt/messaging/active.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ MsgSizeType ActiveMessenger::packMsg(
size, ptr_bytes, print_ptr(ptr)
);

auto const can_grow = thePool()->tryGrowAllocation(msg, ptr_bytes);
auto const can_grow = thePool()->tryGrowAllocation(reinterpret_cast<std::byte*>(msg), ptr_bytes);
// Typically this should be checked by the caller in advance
vtAssert(can_grow, "not enough space to pack message" );

Expand Down Expand Up @@ -236,7 +236,7 @@ EventType ActiveMessenger::sendMsgBytesWithPut(
auto const& put_ptr = envelopeGetPutPtr(msg->env);
auto const& put_size = envelopeGetPutSize(msg->env);
bool const& memory_pool_active = thePool()->active_env();
auto const& rem_size = thePool()->remainingSize(msg);
auto const& rem_size = thePool()->remainingSize(reinterpret_cast<std::byte*>(msg));
/*
* Directly pack if the pool is active (which means it may have
* overallocated and the remaining size of the (envelope) buffer is
Expand Down Expand Up @@ -998,7 +998,7 @@ bool ActiveMessenger::tryProcessIncomingActiveMsg() {
if (flag == 1) {
MPI_Get_count(&stat, MPI_BYTE, &num_probe_bytes);

char* buf = static_cast<char*>(thePool()->alloc(num_probe_bytes));
char* buf = reinterpret_cast<char*>(thePool()->alloc(num_probe_bytes));

NodeType const sender = stat.MPI_SOURCE;

Expand Down
2 changes: 1 addition & 1 deletion src/vt/messaging/message/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ struct ActiveMsg : BaseMsg {
"Message::delete of ptr={}\n", print_ptr(ptr)
);

return thePool()->dealloc(ptr);
return thePool()->dealloc(reinterpret_cast<std::byte*>(ptr));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/vt/messaging/message/smart_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ struct MsgSharedPtr final {

// Obtain the size of the message from the block allocated by the
// memory pool allocator
return thePool()->allocatedSize(ptr_);
return thePool()->allocatedSize(reinterpret_cast<std::byte*>(ptr_));
}

/**
Expand Down
58 changes: 27 additions & 31 deletions src/vt/pool/pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Pool::ePoolSize Pool::getPoolType(
}
}

void* Pool::tryPooledAlloc(size_t const& num_bytes, size_t const& oversize) {
std::byte* Pool::tryPooledAlloc(size_t const& num_bytes, size_t const& oversize) {
ePoolSize const pool_type = getPoolType(num_bytes, oversize);

if (pool_type != ePoolSize::Malloc) {
Expand All @@ -89,10 +89,9 @@ void* Pool::tryPooledAlloc(size_t const& num_bytes, size_t const& oversize) {
}
}

bool Pool::tryPooledDealloc(void* const buf) {
auto buf_byte = reinterpret_cast<std::byte*>(buf);
auto const& actual_alloc_size = HeaderManagerType::getHeaderBytes(buf_byte);
auto const& oversize = HeaderManagerType::getHeaderOversizeBytes(buf_byte);
bool Pool::tryPooledDealloc(std::byte* const buf) {
auto const& actual_alloc_size = HeaderManagerType::getHeaderBytes(buf);
auto const& oversize = HeaderManagerType::getHeaderOversizeBytes(buf);
ePoolSize const pool_type = getPoolType(actual_alloc_size, oversize);

if (pool_type != ePoolSize::Malloc) {
Expand All @@ -103,23 +102,23 @@ bool Pool::tryPooledDealloc(void* const buf) {
}
}

void* Pool::pooledAlloc(
std::byte* Pool::pooledAlloc(
size_t const& num_bytes, size_t const& oversize, ePoolSize const pool_type
) {
void* ret = nullptr;
std::byte* ret = nullptr;

vt_debug_print(
normal, pool,
"Pool::pooled_alloc of size={}, type={}, ret={}\n",
num_bytes, print_pool_type(pool_type), ret
num_bytes, print_pool_type(pool_type), print_ptr(ret)
);

if (pool_type == ePoolSize::Small) {
auto pool = small_msg.get();
ret = pool->alloc(num_bytes, oversize);
ret = reinterpret_cast<std::byte*>(pool->alloc(num_bytes, oversize));
} else if (pool_type == ePoolSize::Medium) {
auto pool = medium_msg.get();
ret = pool->alloc(num_bytes, oversize);
ret = reinterpret_cast<std::byte*>(pool->alloc(num_bytes, oversize));
} else {
vtAssert(0, "Pool must be valid");
ret = nullptr;
Expand All @@ -128,7 +127,7 @@ void* Pool::pooledAlloc(
return ret;
}

void Pool::poolDealloc(void* const buf, ePoolSize const pool_type) {
void Pool::poolDealloc(std::byte* const buf, ePoolSize const pool_type) {
vt_debug_print(
normal, pool,
"Pool::pooled_dealloc of ptr={}, type={}\n",
Expand All @@ -144,19 +143,19 @@ void Pool::poolDealloc(void* const buf, ePoolSize const pool_type) {
}
}

void* Pool::defaultAlloc(size_t const& num_bytes, size_t const& oversize) {
std::byte* Pool::defaultAlloc(size_t const& num_bytes, size_t const& oversize) {
auto alloc_buf = std::malloc(num_bytes + oversize + sizeof(HeaderType));
return HeaderManagerType::setHeader(
num_bytes, oversize, reinterpret_cast<std::byte*>(alloc_buf)
);
}

void Pool::defaultDealloc(void* const ptr) {
std::free(ptr);
void Pool::defaultDealloc(std::byte* const ptr) {
std::free(reinterpret_cast<void*>(ptr));
}

void* Pool::alloc(size_t const& num_bytes, size_t oversize) {
void* ret = nullptr;
std::byte* Pool::alloc(size_t const& num_bytes, size_t oversize) {
std::byte* ret = nullptr;

#if vt_check_enabled(memory_pool)
ret = tryPooledAlloc(num_bytes, oversize);
Expand All @@ -171,21 +170,20 @@ void* Pool::alloc(size_t const& num_bytes, size_t oversize) {
vt_debug_print(
normal, pool,
"Pool::alloc of size={}, ret={}\n",
num_bytes, ret
num_bytes, print_ptr(ret)
);

return ret;
}

void Pool::dealloc(void* const buf) {
auto buf_byte = reinterpret_cast<std::byte*>(buf);
auto const& actual_alloc_size = HeaderManagerType::getHeaderBytes(buf_byte);
auto const& ptr_actual = HeaderManagerType::getHeaderPtr(buf_byte);
void Pool::dealloc(std::byte* const buf) {
auto const& actual_alloc_size = HeaderManagerType::getHeaderBytes(buf);
auto const& ptr_actual = HeaderManagerType::getHeaderPtr(buf);

vt_debug_print(
normal, pool,
"Pool::dealloc of buf={}, alloc_size={}, ptr={}\n",
buf, actual_alloc_size, print_ptr(ptr_actual)
print_ptr(buf), actual_alloc_size, print_ptr(ptr_actual)
);

bool success = false;
Expand All @@ -199,11 +197,10 @@ void Pool::dealloc(void* const buf) {
}
}

Pool::SizeType Pool::remainingSize(void* const buf) const {
Pool::SizeType Pool::remainingSize(std::byte* const buf) const {
#if vt_check_enabled(memory_pool)
auto buf_byte = reinterpret_cast<std::byte*>(buf);
auto const& actual_alloc_size = HeaderManagerType::getHeaderBytes(buf_byte);
auto const& oversize = HeaderManagerType::getHeaderOversizeBytes(buf_byte);
auto const& actual_alloc_size = HeaderManagerType::getHeaderBytes(buf);
auto const& oversize = HeaderManagerType::getHeaderOversizeBytes(buf);

ePoolSize const pool_type = getPoolType(actual_alloc_size, oversize);

Expand All @@ -219,19 +216,18 @@ Pool::SizeType Pool::remainingSize(void* const buf) const {
#endif
}

Pool::SizeType Pool::allocatedSize(void* const buf) const {
auto buf_byte = reinterpret_cast<std::byte*>(buf);
return HeaderManagerType::getHeaderBytes(buf_byte) + HeaderManagerType::getHeaderOversizeBytes(buf_byte);
Pool::SizeType Pool::allocatedSize(std::byte* const buf) const {
return HeaderManagerType::getHeaderBytes(buf) + HeaderManagerType::getHeaderOversizeBytes(buf);
}

bool
Pool::tryGrowAllocation(void* buf, size_t grow_amount) {
Pool::tryGrowAllocation(std::byte* buf, size_t grow_amount) {
// For non-pooled alloc, this condition will always be true
// since remainingSize(buf) would be 0
if ( remainingSize(buf) < grow_amount )
return false;

auto *header = reinterpret_cast<Header*>(HeaderManagerType::getHeaderPtr(reinterpret_cast<std::byte*>(buf)));
auto *header = reinterpret_cast<Header*>(HeaderManagerType::getHeaderPtr(buf));
header->alloc_size += grow_amount;
return true;
}
Expand Down
22 changes: 11 additions & 11 deletions src/vt/pool/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ struct Pool : runtime::component::Component<Pool> {
*
* \return pointer to new allocation
*/
void* alloc(size_t const& num_bytes, size_t oversize = 0);
std::byte* alloc(size_t const& num_bytes, size_t oversize = 0);

/**
* \brief De-allocate a pool-allocated buffer
*
* \param[in] buf the buffer to deallocate
*/
void dealloc(void* const buf);
void dealloc(std::byte* const buf);

/**
* \internal \brief Decided which pool bucket to target based on size
Expand All @@ -130,7 +130,7 @@ struct Pool : runtime::component::Component<Pool> {
*
* \return number of extra bytes
*/
SizeType remainingSize(void* const buf) const;
SizeType remainingSize(std::byte* const buf) const;

/**
* \internal \brief Get total allocated bytes for a pool allocation
Expand All @@ -141,7 +141,7 @@ struct Pool : runtime::component::Component<Pool> {
*
* \return the total number of allocated bytes
*/
SizeType allocatedSize(void* const buf) const;
SizeType allocatedSize(std::byte* const buf) const;

/**
* \internal \brief Attempt to increase the size of an allocation without reallocating
Expand All @@ -157,7 +157,7 @@ struct Pool : runtime::component::Component<Pool> {
* \return false if the grow_amount is too large for the allocated block, true if the operation
* succeeded
*/
bool tryGrowAllocation(void* const buf, size_t grow_amount);
bool tryGrowAllocation(std::byte* const buf, size_t grow_amount);

/**
* \brief Whether the pool is enabled at compile-time
Expand Down Expand Up @@ -190,7 +190,7 @@ struct Pool : runtime::component::Component<Pool> {
*
* \return a pointer to memory if succeeds
*/
void* tryPooledAlloc(size_t const& num_bytes, size_t const& oversize);
std::byte* tryPooledAlloc(size_t const& num_bytes, size_t const& oversize);

/**
* \internal \brief Attempt to de-allocate a buffer
Expand All @@ -199,7 +199,7 @@ struct Pool : runtime::component::Component<Pool> {
*
* \return whether it succeeded or wasn't allocated by the pool
*/
bool tryPooledDealloc(void* const buf);
bool tryPooledDealloc(std::byte* const buf);

/**
* \internal \brief Allocate memory from a specific pool
Expand All @@ -210,7 +210,7 @@ struct Pool : runtime::component::Component<Pool> {
*
* \return the buffer allocated
*/
void* pooledAlloc(
std::byte* pooledAlloc(
size_t const& num_bytes, size_t const& oversize, ePoolSize const pool_type
);

Expand All @@ -220,7 +220,7 @@ struct Pool : runtime::component::Component<Pool> {
* \param[in] buf the buffer
* \param[in] pool_type which pool to target
*/
void poolDealloc(void* const buf, ePoolSize const pool_type);
void poolDealloc(std::byte* const buf, ePoolSize const pool_type);

/**
* \internal \brief Allocate from standard allocator
Expand All @@ -230,14 +230,14 @@ struct Pool : runtime::component::Component<Pool> {
*
* \return the allocated buffer
*/
void* defaultAlloc(size_t const& num_bytes, size_t const& oversize);
std::byte* defaultAlloc(size_t const& num_bytes, size_t const& oversize);

/**
* \internal \brief De-allocate from standard allocator
*
* \param[in] ptr buffer to deallocate
*/
void defaultDealloc(void* const ptr);
void defaultDealloc(std::byte* const ptr);

private:
using MemPoolSType = MemoryPoolPtrType<memory_size_small>;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/pool/test_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ TEST_F(TestPool, pool_alloc) {
std::unique_ptr<pool::Pool> testPool = std::make_unique<pool::Pool>();

for (size_t cur_bytes = 1; cur_bytes < max_bytes; cur_bytes *= 2) {
void* ptr = testPool->alloc(cur_bytes);
std::memset(ptr, init_val, cur_bytes);
std::byte* ptr = testPool->alloc(cur_bytes);
std::memset(reinterpret_cast<void*>(ptr), init_val, cur_bytes);
//fmt::print("alloc {} bytes, ptr={}\n", cur_bytes, ptr);
EXPECT_NE(ptr, nullptr);
for (size_t i = 0; i < cur_bytes; i++) {
EXPECT_EQ(static_cast<CharType*>(ptr)[i], init_val);
EXPECT_EQ(reinterpret_cast<CharType*>(ptr)[i], init_val);
}
testPool->dealloc(ptr);
}
Expand Down

0 comments on commit d8bf238

Please sign in to comment.