Skip to content

Commit

Permalink
Add static_asserts about being smaller than signed type
Browse files Browse the repository at this point in the history
  • Loading branch information
chillenzer committed Jul 25, 2024
1 parent 7bf1efe commit 6db04ea
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/mallocMC_example01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ using Acc = alpaka::ExampleDefaultAcc<Dim, Idx>;
struct ScatterHeapConfig
{
static constexpr auto heapsize = 2U * 1024U * 1024U * 1024U;
static constexpr auto accessblocksize = 2U * 1024U * 1024U * 1024U;
static constexpr size_t accessblocksize = 2U * 1024U * 1024U * 1024U;
static constexpr auto pagesize = 4096;
static constexpr auto regionsize = 16;
static constexpr auto wastefactor = 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/mallocMC_example03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ using Acc = alpaka::ExampleDefaultAcc<Dim, Idx>;
struct ScatterConfig
{
static constexpr auto heapsize = 2U * 1024U * 1024U * 1024U;
static constexpr auto accessblocksize = 2U * 1024U * 1024U * 1024U;
static constexpr size_t accessblocksize = 2U * 1024U * 1024U * 1024U;
static constexpr auto pagesize = 4096;
static constexpr auto regionsize = 16;
static constexpr auto wastefactor = 1;
Expand Down
22 changes: 18 additions & 4 deletions src/include/mallocMC/creationPolicies/Scatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <numeric>
#include <sys/types.h>
#include <type_traits>

namespace mallocMC::CreationPolicies::ScatterAlloc
{
Expand All @@ -54,16 +56,28 @@ namespace mallocMC::CreationPolicies::ScatterAlloc
T_HeapConfig::wastefactor,
T_HeapConfig::resetfreedpages>;

static_assert(
T_HeapConfig::accessblocksize
< std::numeric_limits<std::make_signed_t<decltype(T_HeapConfig::accessblocksize)>>::max(),
"Your access block size must be smaller than the maximal value of its signed type because we are using "
"differences in the code occasionally.");

static_assert(
T_HeapConfig::pagesize < std::numeric_limits<std::make_signed_t<decltype(T_HeapConfig::pagesize)>>::max(),
"Your page size must be smaller than the maximal value of its signed type because we are using "
"differences in the code occasionally.");

static_assert(
T_HeapConfig::accessblocksize == sizeof(MyAccessBlock),
"The real access block must have the same size as configured in order to make alignment more easily "
"predictable.");

size_t heapSize{};
MyAccessBlock* accessBlocks{};
volatile uint32_t block = 0U;

ALPAKA_FN_INLINE ALPAKA_FN_ACC auto numBlocks() const -> uint32_t
{
static_assert(
T_HeapConfig::accessblocksize == sizeof(MyAccessBlock),
"accessblock should equal to the use given block size in order to make alignment more easily "
"predictable.");
return heapSize / T_HeapConfig::accessblocksize;
}

Expand Down

0 comments on commit 6db04ea

Please sign in to comment.