-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45bbf2d
commit 5f75d5e
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |