Skip to content

Commit 0206a20

Browse files
committed
Fix build when std::align is missing
1 parent 65df898 commit 0206a20

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/caliper/MemoryPool.cpp

+15-8
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,26 @@ struct MemoryPool::MemoryPoolImpl
5757
m_total_reserved += len;
5858
}
5959

60-
void* allocate(size_t bytes, bool can_expand) {
61-
size_t n = bytes + ((bytes+7)%8);
62-
60+
void* allocate(size_t bytes, size_t alignment, bool can_expand) {
6361
std::lock_guard<util::spinlock>
6462
g(m_lock);
6563

66-
if (m_chunks.empty() || m_chunks.back().wmark + n > m_chunks.back().size) {
64+
if (m_chunks.empty() || m_chunks.back().wmark + bytes + alignment > m_chunks.back().size) {
6765
if (can_expand)
6866
expand(bytes);
6967
else
7068
return nullptr;
7169
}
7270

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

76+
m_chunks.back().wmark += n;
7677
m_total_used += n;
77-
return ptr;
78+
79+
return reinterpret_cast<void*>(aligned);
7880
}
7981

8082
void merge(MemoryPoolImpl& other) {
@@ -157,7 +159,12 @@ MemoryPool::~MemoryPool()
157159

158160
void* MemoryPool::allocate(size_t bytes)
159161
{
160-
return mP->allocate(bytes, mP->m_can_expand);
162+
return mP->allocate(bytes, 1, mP->m_can_expand);
163+
}
164+
165+
void* MemoryPool::allocate(size_t bytes, size_t alignment)
166+
{
167+
return mP->allocate(bytes, alignment, mP->m_can_expand);
161168
}
162169

163170
void MemoryPool::merge(MemoryPool& other)

src/caliper/MemoryPool.h

+5
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@ class MemoryPool
3838
// --- allocate
3939

4040
void* allocate(std::size_t bytes);
41+
void* allocate(std::size_t bytes, std::size_t alignment);
4142

4243
template<typename T>
4344
T* aligned_alloc(std::size_t n = 1, std::size_t a = alignof(T)) {
45+
/*
46+
// ancient gcc 4.9-based STL versions don't have std::align :-((
4447
std::size_t size = n * sizeof(T);
4548
std::size_t space = n * sizeof(T) + a;
4649
void* p = allocate(space);
4750
return reinterpret_cast<T*>(std::align(a, size, p, space));
51+
*/
52+
return reinterpret_cast<T*>(allocate(n*sizeof(T), a));
4853
}
4954

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

0 commit comments

Comments
 (0)