Skip to content
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
2 changes: 1 addition & 1 deletion clients/shmem/villas-shmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Shmem : public Tool {
outsmps[i]->sequence = insmps[i]->sequence;
outsmps[i]->ts = insmps[i]->ts;

int len = MIN(insmps[i]->length, outsmps[i]->capacity);
int len = std::min(insmps[i]->length, outsmps[i]->capacity);
memcpy(outsmps[i]->data, insmps[i]->data, SAMPLE_DATA_LENGTH(len));

outsmps[i]->length = len;
Expand Down
11 changes: 0 additions & 11 deletions common/include/villas/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@

#define LIST_CHUNKSIZE 16

// Static list initialization
#define LIST_INIT_STATIC(l) \
__attribute__((constructor(105))) static void UNIQUE(__ctor)() { \
int ret __attribute__((unused)); \
ret = list_init(l); \
} \
__attribute__((destructor(105))) static void UNIQUE(__dtor)() { \
int ret __attribute__((unused)); \
ret = list_destroy(l, nullptr, false); \
}

#define list_length(list) ((list)->length)
#define list_at_safe(list, index) \
((list)->length > index ? (list)->array[index] : nullptr)
Expand Down
92 changes: 4 additions & 88 deletions common/include/villas/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#pragma once

#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <list>
#include <span>
#include <string>
#include <string_view>
#include <vector>

#include <openssl/sha.h>
Expand All @@ -23,93 +24,16 @@
#include <villas/config.hpp>
#include <villas/fs.hpp>

#ifdef __GNUC__
#define LIKELY(x) __builtin_expect((x), 1)
#define UNLIKELY(x) __builtin_expect((x), 0)
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif

// Check assertion and exit if failed.
#ifndef assert
#define assert(exp) \
do { \
if (!EXPECT(exp, 0)) \
error("Assertion failed: '%s' in %s(), %s:%d", XSTR(exp), __FUNCTION__, \
__BASE_FILE__, __LINE__); \
} while (0)
#endif

// CPP stringification
#define XSTR(x) STR(x)
#define STR(x) #x

#define CONCAT_DETAIL(x, y) x##y
#define CONCAT(x, y) CONCAT_DETAIL(x, y)
#define UNIQUE(x) CONCAT(x, __COUNTER__)

#ifdef ALIGN
#undef ALIGN
#endif
#define ALIGN(x, a) ALIGN_MASK(x, (uintptr_t)(a)-1)
#define ALIGN_MASK(x, m) (((uintptr_t)(x) + (m)) & ~(m))
#define IS_ALIGNED(x, a) (ALIGN(x, a) == (uintptr_t)x)

#define SWAP(x, y) \
do { \
auto t = x; \
x = y; \
y = t; \
} while (0)

// Round-up integer division
#define CEIL(x, y) (((x) + (y)-1) / (y))

// Get nearest up-rounded power of 2
#define LOG2_CEIL(x) (1 << (villas::utils::log2i((x)-1) + 1))

// Check if the number is a power of 2
#define IS_POW2(x) (((x) != 0) && !((x) & ((x)-1)))

// Calculate the number of elements in an array.
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a)[0])

// Return the bigger value
#ifdef MAX
#undef MAX
#endif
#define MAX(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})

// Return the smaller value
#ifdef MIN
#undef MIN
#endif
#define MIN(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a < _b ? _a : _b; \
})
#define MIN3(a, b, c) MIN(MIN((a), (b)), (c))

#ifndef offsetof
#define offsetof(type, member) __builtin_offsetof(type, member)
#endif

#ifndef container_of
#define container_of(ptr, type, member) \
({ \
const typeof(((type *)0)->member) *__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); \
})
#endif

#define BITS_PER_LONGLONG (sizeof(long long) * 8)

// Some helper macros
Expand All @@ -123,11 +47,6 @@ namespace utils {
std::vector<std::string> tokenize(const std::string &s,
const std::string &delimiter);

template <typename T> void assertExcept(bool condition, const T &exception) {
if (not condition)
throw exception;
}

// Register a exit callback for program termination: SIGINT, SIGKILL & SIGALRM.
int signalsInit(void (*cb)(int signal, siginfo_t *sinfo, void *ctx),
std::list<int> cbSignals = {},
Expand Down Expand Up @@ -213,14 +132,11 @@ template <class... Ts> struct overloaded : Ts... {
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

void write_to_file(std::string data, const fs::path file);
std::vector<std::string> read_names_in_directory(const fs::path &directory);

namespace base64 {

using byte = std::uint8_t;

std::string encode(const std::vector<byte> &input);
std::vector<byte> decode(const std::string &input);
std::string encode(std::span<const std::byte> input);
std::vector<std::byte> decode(std::string_view input);

} // namespace base64
} // namespace utils
Expand Down
40 changes: 20 additions & 20 deletions common/lib/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <string>
Expand All @@ -20,45 +21,44 @@ static const char kEncodeLookup[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char kPadCharacter = '=';

std::string encode(const std::vector<byte> &input) {
std::string encode(std::span<const std::byte> input) {
std::string encoded;
encoded.reserve(((input.size() / 3) + (input.size() % 3 > 0)) * 4);

std::uint32_t temp{};
auto it = input.begin();

for (std::size_t i = 0; i < input.size() / 3; ++i) {
temp = (*it++) << 16;
temp += (*it++) << 8;
temp += (*it++);
auto temp = std::to_integer<std::uint32_t>(*it++) << 16;
temp += std::to_integer<std::uint32_t>(*it++) << 8;
temp += std::to_integer<std::uint32_t>(*it++);
encoded.append(1, kEncodeLookup[(temp & 0x00FC0000) >> 18]);
encoded.append(1, kEncodeLookup[(temp & 0x0003F000) >> 12]);
encoded.append(1, kEncodeLookup[(temp & 0x00000FC0) >> 6]);
encoded.append(1, kEncodeLookup[(temp & 0x0000003F)]);
}

switch (input.size() % 3) {
case 1:
temp = (*it++) << 16;
case 1: {
auto temp = std::to_integer<std::uint32_t>(*it++) << 16;
encoded.append(1, kEncodeLookup[(temp & 0x00FC0000) >> 18]);
encoded.append(1, kEncodeLookup[(temp & 0x0003F000) >> 12]);
encoded.append(2, kPadCharacter);
break;
} break;

case 2:
temp = (*it++) << 16;
temp += (*it++) << 8;
case 2: {
auto temp = std::to_integer<std::uint32_t>(*it++) << 16;
temp += std::to_integer<std::uint32_t>(*it++) << 8;
encoded.append(1, kEncodeLookup[(temp & 0x00FC0000) >> 18]);
encoded.append(1, kEncodeLookup[(temp & 0x0003F000) >> 12]);
encoded.append(1, kEncodeLookup[(temp & 0x00000FC0) >> 6]);
encoded.append(1, kPadCharacter);
break;
} break;
}

return encoded;
}

std::vector<byte> decode(const std::string &input) {
std::vector<std::byte> decode(std::string_view input) {
if (input.length() % 4)
throw std::runtime_error("Invalid base64 length!");

Expand All @@ -71,7 +71,7 @@ std::vector<byte> decode(const std::string &input) {
padding++;
}

std::vector<byte> decoded;
std::vector<std::byte> decoded;
decoded.reserve(((input.length() / 4) * 3) - padding);

std::uint32_t temp{};
Expand All @@ -93,11 +93,11 @@ std::vector<byte> decode(const std::string &input) {
else if (*it == kPadCharacter) {
switch (input.end() - it) {
case 1:
decoded.push_back((temp >> 16) & 0x000000FF);
decoded.push_back((temp >> 8) & 0x000000FF);
decoded.push_back(static_cast<std::byte>((temp >> 16) & 0x000000FF));
decoded.push_back(static_cast<std::byte>((temp >> 8) & 0x000000FF));
return decoded;
case 2:
decoded.push_back((temp >> 10) & 0x000000FF);
decoded.push_back(static_cast<std::byte>((temp >> 10) & 0x000000FF));
return decoded;
default:
throw std::runtime_error("Invalid padding in base64!");
Expand All @@ -108,9 +108,9 @@ std::vector<byte> decode(const std::string &input) {
++it;
}

decoded.push_back((temp >> 16) & 0x000000FF);
decoded.push_back((temp >> 8) & 0x000000FF);
decoded.push_back((temp) & 0x000000FF);
decoded.push_back(static_cast<std::byte>((temp >> 16) & 0x000000FF));
decoded.push_back(static_cast<std::byte>((temp >> 8) & 0x000000FF));
decoded.push_back(static_cast<std::byte>((temp) & 0x000000FF));
}

return decoded;
Expand Down
8 changes: 2 additions & 6 deletions common/lib/kernel/devices/ip_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,9 @@ std::vector<villas::kernel::devices::IpDevice>
IpDevice::from_directory(fs::path devices_directory) {
std::vector<villas::kernel::devices::IpDevice> devices;

const std::vector<std::string> devicetree_names =
villas::utils::read_names_in_directory(devices_directory);

for (auto devicetree_name : devicetree_names) {
auto path_to_device = devices_directory / fs::path(devicetree_name);
for (auto devicetree : fs::directory_iterator{devices_directory}) {
try {
auto device = villas::kernel::devices::IpDevice::from(path_to_device);
auto device = villas::kernel::devices::IpDevice::from(devicetree.path());
devices.push_back(device);
} catch (std::runtime_error &e) {
}
Expand Down
4 changes: 2 additions & 2 deletions common/lib/memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ MemoryTranslation::operator+=(const MemoryTranslation &other) {
logger->debug("other_src_high: {:#x}", other_src_high);

// Make sure there is a common memory area
assertExcept(other.src < this_dst_high, MemoryManager::InvalidTranslation());
assertExcept(this->dst < other_src_high, MemoryManager::InvalidTranslation());
if (other.src < this_dst_high or this->dst < other_src_high)
throw MemoryManager::InvalidTranslation();

const uintptr_t hi = std::max(this_dst_high, other_src_high);
const uintptr_t lo = std::min(this->dst, other.src);
Expand Down
8 changes: 0 additions & 8 deletions common/lib/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,5 @@ void write_to_file(std::string data, const fs::path file) {
}
}

std::vector<std::string> read_names_in_directory(const fs::path &directory) {
std::vector<std::string> names;
for (auto const &dir_entry : fs::directory_iterator{directory}) {
names.push_back(dir_entry.path().filename());
}
return names;
}

} // namespace utils
} // namespace villas
5 changes: 3 additions & 2 deletions common/tests/unit/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ using namespace villas::utils::base64;
// cppcheck-suppress unknownMacro
TestSuite(base64, .description = "Base64 En/decoder");

static std::vector<byte> vec(const char *str) {
return std::vector<byte>((byte *)str, (byte *)str + strlen(str));
static std::vector<std::byte> vec(const char *str) {
auto bytes = std::as_bytes(std::span{str, strlen(str)});
return std::vector<std::byte>(bytes.begin(), bytes.end());
}

Test(base64, encoding) {
Expand Down
4 changes: 2 additions & 2 deletions common/tests/unit/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ Test(list, list_search) {
cr_assert_eq(ret, 0);

// Fill list
for (unsigned i = 0; i < ARRAY_LEN(nouns); i++)
for (unsigned i = 0; i < std::size(nouns); i++)
list_push(&l, (void *)nouns[i]);

cr_assert_eq(list_length(&l), ARRAY_LEN(nouns));
cr_assert_eq(list_length(&l), std::size(nouns));

// Declare on stack!
char positive[] = "woman";
Expand Down
13 changes: 0 additions & 13 deletions common/tests/unit/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,6 @@ Test(utils, ceil) {
cr_assert_eq(CEIL(4, 3), 2);
}

Test(utils, is_pow2) {
// Positive
cr_assert(IS_POW2(1));
cr_assert(IS_POW2(2));
cr_assert(IS_POW2(64));

// Negative
cr_assert(!IS_POW2(0));
cr_assert(!IS_POW2(3));
cr_assert(!IS_POW2(11111));
cr_assert(!IS_POW2(-1));
}

Test(utils, strf) {
char *buf = nullptr;

Expand Down
2 changes: 1 addition & 1 deletion include/villas/nodes/iec61850_goose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class GooseNode : public Node {
int id;
RSecurityAlgorithm security;
RSignatureAlgorithm signature;
std::vector<uint8_t> data;
std::vector<std::byte> data;
};

struct InputMapping {
Expand Down
1 change: 0 additions & 1 deletion include/villas/nodes/opal_orchestra/ddf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#pragma once

#include <filesystem>
#include <optional>
#include <string>
#include <unordered_map>
Expand Down
8 changes: 4 additions & 4 deletions lib/api/requests/universal/channels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class SignalsRequest : public UniversalRequest {

auto *json_sigs = json_array();

for (size_t i = 0; i < MIN(api_node->getOutputSignals()->size(),
api_node->write.channels.size());
for (size_t i = 0; i < std::min(api_node->getOutputSignals()->size(),
api_node->write.channels.size());
i++) {
auto sig = api_node->getOutputSignals()->at(i);
auto ch = api_node->write.channels.at(i);
Expand All @@ -38,8 +38,8 @@ class SignalsRequest : public UniversalRequest {
json_array_append(json_sigs, json_sig);
}

for (size_t i = 0; i < MIN(api_node->getInputSignals()->size(),
api_node->read.channels.size());
for (size_t i = 0; i < std::min(api_node->getInputSignals()->size(),
api_node->read.channels.size());
i++) {
auto sig = api_node->getInputSignals()->at(i);
auto ch = api_node->read.channels.at(i);
Expand Down
Loading