Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alignment support to contiguous resource #181

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions core/src/memory/contiguous_memory_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "vecmem/utils/debug.hpp"

// System include(s).
#include <memory>
#include <stdexcept>

namespace vecmem {
Expand All @@ -38,33 +39,36 @@ contiguous_memory_resource::~contiguous_memory_resource() {
m_size, m_begin);
}

void *contiguous_memory_resource::do_allocate(std::size_t size, std::size_t) {
void *contiguous_memory_resource::do_allocate(std::size_t size,
std::size_t alignment) {
/*
* Calculate where the end of this current allocation would be.
* Compute the remaining space, which needs to be an lvalue for standard
* library-related reasons.
*/
void *next = static_cast<void *>(static_cast<char *>(m_next) + size);
std::size_t rem =
m_size - (static_cast<char *>(m_next) - static_cast<char *>(m_begin));

/*
* The start of this allocation, save it in a temporary variable as we
* will override it later before returning.
* Employ std::align to find the next properly aligned address.
*/
void *curr = m_next;
if (std::align(alignment, size, m_next, rem)) {
/*
* Store the return pointer, update the stored next pointer, then
* return.
*/
void *res = m_next;
m_next = static_cast<char *>(m_next) + size;

/*
* If the end of this allocation is past the end of our memory space,
* we can't allocate, and should throw an error.
*/
if (next >= (static_cast<char *>(m_begin) + m_size)) {
VECMEM_DEBUG_MSG(4, "Allocated %lu bytes at %p", size, res);

return res;
} else {
/*
* If std::align returns a false-like value, the allocation has failed
* and we throw an exception.
*/
throw std::bad_alloc();
}

/*
* Update the start of the next allocation and return.
*/
m_next = next;

VECMEM_DEBUG_MSG(4, "Allocated %lu bytes at %p", size, curr);
return curr;
}

void contiguous_memory_resource::do_deallocate(void *, std::size_t,
Expand Down
8 changes: 4 additions & 4 deletions tests/core/test_core_contiguous_memory_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ TEST_F(core_contiguous_memory_resource_test, allocations) {
vecmem::vector<int> vec1(VECTOR_SIZE, &m_resource);

vecmem::vector<char> vec2(VECTOR_SIZE, &m_resource);
EXPECT_EQ(static_cast<void*>(&*(vec2.begin())),
EXPECT_GE(static_cast<void*>(&*(vec2.begin())),
static_cast<void*>(&*(vec1.end())));

vecmem::vector<double> vec3(VECTOR_SIZE, &m_resource);
EXPECT_EQ(static_cast<void*>(&*(vec3.begin())),
EXPECT_GE(static_cast<void*>(&*(vec3.begin())),
static_cast<void*>(&*(vec2.end())));

vecmem::vector<float> vec4(VECTOR_SIZE, &m_resource);
EXPECT_EQ(static_cast<void*>(&*(vec4.begin())),
EXPECT_GE(static_cast<void*>(&*(vec4.begin())),
static_cast<void*>(&*(vec3.end())));

vecmem::vector<int> vec5(VECTOR_SIZE, &m_resource);
EXPECT_EQ(static_cast<void*>(&*(vec5.begin())),
EXPECT_GE(static_cast<void*>(&*(vec5.begin())),
static_cast<void*>(&*(vec4.end())));

#endif // MSVC debug build...
Expand Down