Skip to content

Commit

Permalink
Fix build when std::align is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
daboehme committed Mar 12, 2024
1 parent 65df898 commit 0206a20
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/caliper/MemoryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,26 @@ struct MemoryPool::MemoryPoolImpl
m_total_reserved += len;
}

void* allocate(size_t bytes, bool can_expand) {
size_t n = bytes + ((bytes+7)%8);

void* allocate(size_t bytes, size_t alignment, bool can_expand) {
std::lock_guard<util::spinlock>
g(m_lock);

if (m_chunks.empty() || m_chunks.back().wmark + n > m_chunks.back().size) {
if (m_chunks.empty() || m_chunks.back().wmark + bytes + alignment > m_chunks.back().size) {
if (can_expand)
expand(bytes);
else
return nullptr;
}

void *ptr = static_cast<void*>(m_chunks.back().ptr + m_chunks.back().wmark);
m_chunks.back().wmark += n;
unsigned char *ptr = m_chunks.back().ptr + m_chunks.back().wmark;
std::uintptr_t pn = reinterpret_cast<std::uintptr_t>(ptr);
std::uintptr_t aligned = (pn + alignment-1) & - alignment;
std::size_t n = bytes + (aligned - pn);

m_chunks.back().wmark += n;
m_total_used += n;
return ptr;

return reinterpret_cast<void*>(aligned);
}

void merge(MemoryPoolImpl& other) {
Expand Down Expand Up @@ -157,7 +159,12 @@ MemoryPool::~MemoryPool()

void* MemoryPool::allocate(size_t bytes)
{
return mP->allocate(bytes, mP->m_can_expand);
return mP->allocate(bytes, 1, mP->m_can_expand);
}

void* MemoryPool::allocate(size_t bytes, size_t alignment)
{
return mP->allocate(bytes, alignment, mP->m_can_expand);
}

void MemoryPool::merge(MemoryPool& other)
Expand Down
5 changes: 5 additions & 0 deletions src/caliper/MemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ class MemoryPool
// --- allocate

void* allocate(std::size_t bytes);
void* allocate(std::size_t bytes, std::size_t alignment);

template<typename T>
T* aligned_alloc(std::size_t n = 1, std::size_t a = alignof(T)) {
/*
// ancient gcc 4.9-based STL versions don't have std::align :-((
std::size_t size = n * sizeof(T);
std::size_t space = n * sizeof(T) + a;
void* p = allocate(space);
return reinterpret_cast<T*>(std::align(a, size, p, space));
*/
return reinterpret_cast<T*>(allocate(n*sizeof(T), a));
}

/// \brief Move \a other's data into this mempool.
Expand Down

0 comments on commit 0206a20

Please sign in to comment.