Skip to content

Commit

Permalink
Add MemReq struct, and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ManifoldFR committed Oct 16, 2024
1 parent 45bbf2d commit 5f75d5e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
45 changes: 45 additions & 0 deletions gar/include/aligator/gar/mem-req.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <cstdlib>
#include <vector>
#include <cassert>

namespace aligator::gar {
typedef unsigned int uint;

/// \brief Utility class for asking for sets of bytes aligned to a given value.
/// It allows preallocation of a block of memory. It is not like a memory pool.
struct MemReq {
MemReq(uint alignment) noexcept;

MemReq &addBytes(uint size) noexcept;

/// \brief Request for bytes from an array.
template <typename T, typename... Args>
MemReq &addArray(uint n1, Args... args) noexcept {
uint size = n1;
std::initializer_list<uint> dims{static_cast<uint>(args)...};
for (auto s : dims) {
size *= s;
}
addBytes(size * sizeof(T));
return *this;
}

void *allocate() const;

template <typename T> void advance(T *&memory) {
assert(_cursor != _chunkSizes.size() && "Reached end of the chunks.");
memory += _chunkSizes[_cursor++] / sizeof(T);
}

uint totalBytes() const { return _totalBytes; }

private:
uint _align;
uint _totalBytes{0};
std::vector<uint> _chunkSizes;
uint _cursor{0};
};

} // namespace aligator::gar
17 changes: 17 additions & 0 deletions gar/src/mem-req.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "aligator/gar/mem-req.hpp"
#include <stdlib.h>

namespace aligator::gar {
MemReq::MemReq(uint alignment) noexcept : _align(alignment) {}

MemReq &MemReq::addBytes(uint size) noexcept {
uint memsize = (size + _align - 1) / _align * _align;
_totalBytes += memsize;
_chunkSizes.push_back(memsize);
return *this;
}

void *MemReq::allocate() const {
return std::aligned_alloc(_align, _totalBytes);
}
} // namespace aligator::gar
2 changes: 2 additions & 0 deletions tests/gar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ add_gar_test(block-matrix)
if(BUILD_WITH_OPENMP_SUPPORT)
add_gar_test(parallel aligator)
endif()

add_gar_test(mem-req)
26 changes: 26 additions & 0 deletions tests/gar/mem-req.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "aligator/gar/mem-req.hpp"

#include <boost/test/unit_test.hpp>

using namespace aligator::gar;

BOOST_AUTO_TEST_CASE(mem_req) {
MemReq req = MemReq(32).addBytes(sizeof(double) * 3 * 3);
BOOST_CHECK_EQUAL(req.totalBytes(), 96);

req = MemReq(64).addBytes(sizeof(double) * 3 * 3);
BOOST_CHECK_EQUAL(req.totalBytes(), 128);
}

BOOST_AUTO_TEST_CASE(array) {
MemReq req{32};

req.addArray<double>(3, 4);
BOOST_CHECK_EQUAL(req.totalBytes(), 96);

req = MemReq{32}.addArray<double>(3, 1, 2);
BOOST_CHECK_EQUAL(req.totalBytes(), 64);

req = MemReq{64}.addArray<double>(3, 1, 2);
BOOST_CHECK_EQUAL(req.totalBytes(), 64);
}

0 comments on commit 5f75d5e

Please sign in to comment.