Skip to content
Snippets Groups Projects
Commit 0de95a3c authored by Florian Schmaus's avatar Florian Schmaus
Browse files

Merge branch 'avoid-c-arrays' into 'master'

Towards fixing modernize-avoid-c-arrays, add template_util::getSize()

See merge request !84
parents c1596716 04f2883e
No related branches found
No related tags found
1 merge request!84Towards fixing modernize-avoid-c-arrays, add template_util::getSize()
Pipeline #57616 passed
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020 Florian Schmaus
#include <cstddef>
namespace emper::lib::template_util {
// https://stackoverflow.com/a/66043076/194894
template <typename T>
auto getSize(T& t) -> size_t {
typename T::size_type size = t.size();
size_t value_type_size = sizeof(typename T::value_type);
return size * value_type_size;
}
} // namespace emper::lib::template_util
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020-2021 Florian Schmaus
#include <array>
#include <atomic> // for atomic, __atomic_base
#include <cstdint> // for uint64_t, UINT64_MAX
#include <cstdlib> // for free, exit, EXIT_FAILURE
......@@ -12,8 +13,11 @@
#include "Fiber.hpp" // for Fiber, Fiber::NOT_AFFINE
#include "Runtime.hpp" // for Runtime
#include "emper-common.h" // for UNUSED_ARG, workeraffini...
#include "lib/template_util.hpp"
#include "strategies/laws/LawsStrategyFactory.hpp"
namespace tu = emper::lib::template_util;
class RuntimeStrategyFactory;
static const unsigned int ROUND_COUNT = 10;
......@@ -22,8 +26,7 @@ static const unsigned int PAYLOAD_COUNT = 4096;
using FiberData = struct ALIGN_TO_CACHE_LINE {
// 4096 * 8 byte (64 bit) = 32 KiB = L1 cache size of most systems
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
uint64_t payload[PAYLOAD_COUNT];
std::array<uint64_t, PAYLOAD_COUNT> payload;
CPS* cps;
unsigned int fiberNum;
};
......@@ -56,7 +59,9 @@ static void alphaFun() {
for (unsigned int i = 0; i < FIBER_COUNT; ++i) {
FiberData& currentFiberData = fiberData[i];
memset(currentFiberData.payload, 0, sizeof(uint64_t) * PAYLOAD_COUNT);
auto& payload = currentFiberData.payload;
memset(payload.data(), 0, tu::getSize(payload));
currentFiberData.fiberNum = i;
currentFiberData.cps = nullptr;
......
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2021 Florian Schmaus
#include <gtest/gtest.h>
#include <array>
#include <cstddef>
#include <cstdint>
#include <memory>
#include "lib/template_util.hpp"
namespace tu = emper::lib::template_util;
template <typename T>
void test_getSize(T& t, size_t expected) {
size_t actual = tu::getSize(t);
ASSERT_EQ(actual, expected);
}
// NOLINTNEXTLINE(modernize-use-trailing-return-type)
TEST(getSize, simple_uint8_t) {
const size_t bytes = 42;
std::array<uint8_t, bytes> array;
test_getSize(array, bytes);
}
// NOLINTNEXTLINE(modernize-use-trailing-return-type)
TEST(getSize, simple_uint32_t) {
const size_t elements = 42;
std::array<uint32_t, elements> array;
test_getSize(array, elements * 4);
}
......@@ -144,6 +144,11 @@ tests = {
'test_suite': 'io',
'args': ['10', '10000'],
},
'TemplateUtilTest.cpp':
{
'gtest': true,
'is_parallel': true,
},
}
undef_ndebug = '-UNDEBUG'
......@@ -163,6 +168,10 @@ io_uring_enabled = get_option('io')
parallel_ok = (not io_uring_enabled)
# Meson integration for GTest and GMock
# See https://mesonbuild.com/Dependencies.html#gtest-and-gmock
gtest_dep = dependency('gtest', main: true)
foreach source, test_dict : tests
# TODO: Use meson fs (filesystem) module once meson >= 0.53 is in
# buster-backports, instead of split('.')[0]
......@@ -171,6 +180,11 @@ foreach source, test_dict : tests
test_name = source.split('.')[0]
test_deps = [thread_dep, test_fixtures]
if test_dict.get('gtest', false)
test_deps += gtest_dep
endif
if test_dict.has_key('dependencies')
test_deps += test_dict['dependencies']
endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment