@@ -57,24 +57,26 @@ struct MemoryPool::MemoryPoolImpl
57
57
m_total_reserved += len;
58
58
}
59
59
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) {
63
61
std::lock_guard<util::spinlock>
64
62
g (m_lock);
65
63
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 ) {
67
65
if (can_expand)
68
66
expand (bytes);
69
67
else
70
68
return nullptr ;
71
69
}
72
70
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);
75
75
76
+ m_chunks.back ().wmark += n;
76
77
m_total_used += n;
77
- return ptr;
78
+
79
+ return reinterpret_cast <void *>(aligned);
78
80
}
79
81
80
82
void merge (MemoryPoolImpl& other) {
@@ -157,7 +159,12 @@ MemoryPool::~MemoryPool()
157
159
158
160
void * MemoryPool::allocate (size_t bytes)
159
161
{
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 );
161
168
}
162
169
163
170
void MemoryPool::merge (MemoryPool& other)
0 commit comments