diff --git a/src/vt/collective/scatter/scatter.cc b/src/vt/collective/scatter/scatter.cc index 1d9ee9dde0..280d429660 100644 --- a/src/vt/collective/scatter/scatter.cc +++ b/src/vt/collective/scatter/scatter.cc @@ -100,7 +100,7 @@ void Scatter::scatterIn(ScatterMsg* msg) { child, num_children, child_bytes_size ); auto const child_remaining_size = - thePool()->remainingSize(reinterpret_cast(child_msg.get())); + thePool()->remainingSize(reinterpret_cast(child_msg.get())); child_msg->user_han = user_handler; auto ptr = reinterpret_cast(child_msg.get()) + sizeof(ScatterMsg); vt_debug_print( diff --git a/src/vt/collective/scatter/scatter.impl.h b/src/vt/collective/scatter/scatter.impl.h index 8423d08177..7d1fb7d896 100644 --- a/src/vt/collective/scatter/scatter.impl.h +++ b/src/vt/collective/scatter/scatter.impl.h @@ -68,7 +68,7 @@ void Scatter::scatter( auto ptr = reinterpret_cast(scatter_msg.get()) + sizeof(ScatterMsg); #if vt_check_enabled(memory_pool) auto remaining_size = - thePool()->remainingSize(reinterpret_cast(scatter_msg.get())); + thePool()->remainingSize(reinterpret_cast(scatter_msg.get())); vtAssertInfo( remaining_size >= combined_size, "Remaining size must be sufficient", total_size, combined_size, remaining_size, elm_size diff --git a/src/vt/messaging/active.cc b/src/vt/messaging/active.cc index 6fd8fd3f87..4cc9bcd7f3 100644 --- a/src/vt/messaging/active.cc +++ b/src/vt/messaging/active.cc @@ -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(msg), ptr_bytes); // Typically this should be checked by the caller in advance vtAssert(can_grow, "not enough space to pack message" ); @@ -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(msg)); /* * Directly pack if the pool is active (which means it may have * overallocated and the remaining size of the (envelope) buffer is @@ -998,7 +998,7 @@ bool ActiveMessenger::tryProcessIncomingActiveMsg() { if (flag == 1) { MPI_Get_count(&stat, MPI_BYTE, &num_probe_bytes); - char* buf = static_cast(thePool()->alloc(num_probe_bytes)); + char* buf = reinterpret_cast(thePool()->alloc(num_probe_bytes)); NodeType const sender = stat.MPI_SOURCE; diff --git a/src/vt/messaging/message/message.h b/src/vt/messaging/message/message.h index 86597fabc5..63cac620f9 100644 --- a/src/vt/messaging/message/message.h +++ b/src/vt/messaging/message/message.h @@ -154,7 +154,7 @@ struct ActiveMsg : BaseMsg { "Message::delete of ptr={}\n", print_ptr(ptr) ); - return thePool()->dealloc(ptr); + return thePool()->dealloc(reinterpret_cast(ptr)); } /** diff --git a/src/vt/messaging/message/smart_ptr.h b/src/vt/messaging/message/smart_ptr.h index b7d69eabf9..a3f9186244 100644 --- a/src/vt/messaging/message/smart_ptr.h +++ b/src/vt/messaging/message/smart_ptr.h @@ -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(ptr_)); } /** diff --git a/src/vt/pool/pool.cc b/src/vt/pool/pool.cc index 9d5d99277a..3b20a7949d 100644 --- a/src/vt/pool/pool.cc +++ b/src/vt/pool/pool.cc @@ -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) { @@ -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(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) { @@ -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(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(pool->alloc(num_bytes, oversize)); } else { vtAssert(0, "Pool must be valid"); ret = nullptr; @@ -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", @@ -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(alloc_buf) ); } -void Pool::defaultDealloc(void* const ptr) { - std::free(ptr); +void Pool::defaultDealloc(std::byte* const ptr) { + std::free(reinterpret_cast(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); @@ -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(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; @@ -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(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); @@ -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(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(HeaderManagerType::getHeaderPtr(reinterpret_cast(buf))); + auto *header = reinterpret_cast(HeaderManagerType::getHeaderPtr(buf)); header->alloc_size += grow_amount; return true; } diff --git a/src/vt/pool/pool.h b/src/vt/pool/pool.h index 3983bedf43..ab3f8268f1 100644 --- a/src/vt/pool/pool.h +++ b/src/vt/pool/pool.h @@ -99,14 +99,14 @@ struct Pool : runtime::component::Component { * * \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 @@ -130,7 +130,7 @@ struct Pool : runtime::component::Component { * * \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 @@ -141,7 +141,7 @@ struct Pool : runtime::component::Component { * * \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 @@ -157,7 +157,7 @@ struct Pool : runtime::component::Component { * \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 @@ -190,7 +190,7 @@ struct Pool : runtime::component::Component { * * \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 @@ -199,7 +199,7 @@ struct Pool : runtime::component::Component { * * \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 @@ -210,7 +210,7 @@ struct Pool : runtime::component::Component { * * \return the buffer allocated */ - void* pooledAlloc( + std::byte* pooledAlloc( size_t const& num_bytes, size_t const& oversize, ePoolSize const pool_type ); @@ -220,7 +220,7 @@ struct Pool : runtime::component::Component { * \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 @@ -230,14 +230,14 @@ struct Pool : runtime::component::Component { * * \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; diff --git a/tests/unit/pool/test_pool.cc b/tests/unit/pool/test_pool.cc index f15fbadff7..81aaadebbd 100644 --- a/tests/unit/pool/test_pool.cc +++ b/tests/unit/pool/test_pool.cc @@ -107,12 +107,12 @@ TEST_F(TestPool, pool_alloc) { std::unique_ptr testPool = std::make_unique(); 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(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(ptr)[i], init_val); + EXPECT_EQ(reinterpret_cast(ptr)[i], init_val); } testPool->dealloc(ptr); }