diff --git a/clients/shmem/villas-shmem.cpp b/clients/shmem/villas-shmem.cpp index 2668b5bfe..a39d975fa 100644 --- a/clients/shmem/villas-shmem.cpp +++ b/clients/shmem/villas-shmem.cpp @@ -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; diff --git a/common/include/villas/list.hpp b/common/include/villas/list.hpp index 59b6941c1..d09447b00 100644 --- a/common/include/villas/list.hpp +++ b/common/include/villas/list.hpp @@ -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) diff --git a/common/include/villas/utils.hpp b/common/include/villas/utils.hpp index 1ebb1cf08..318aeceac 100644 --- a/common/include/villas/utils.hpp +++ b/common/include/villas/utils.hpp @@ -9,10 +9,11 @@ #pragma once #include -#include #include #include +#include #include +#include #include #include @@ -23,32 +24,6 @@ #include #include -#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 @@ -56,60 +31,9 @@ #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 @@ -123,11 +47,6 @@ namespace utils { std::vector tokenize(const std::string &s, const std::string &delimiter); -template 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 cbSignals = {}, @@ -213,14 +132,11 @@ template struct overloaded : Ts... { template overloaded(Ts...) -> overloaded; void write_to_file(std::string data, const fs::path file); -std::vector read_names_in_directory(const fs::path &directory); namespace base64 { -using byte = std::uint8_t; - -std::string encode(const std::vector &input); -std::vector decode(const std::string &input); +std::string encode(std::span input); +std::vector decode(std::string_view input); } // namespace base64 } // namespace utils diff --git a/common/lib/base64.cpp b/common/lib/base64.cpp index ac3c8536f..795e21a51 100644 --- a/common/lib/base64.cpp +++ b/common/lib/base64.cpp @@ -5,6 +5,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include #include #include @@ -20,17 +21,16 @@ static const char kEncodeLookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char kPadCharacter = '='; -std::string encode(const std::vector &input) { +std::string encode(std::span 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(*it++) << 16; + temp += std::to_integer(*it++) << 8; + temp += std::to_integer(*it++); encoded.append(1, kEncodeLookup[(temp & 0x00FC0000) >> 18]); encoded.append(1, kEncodeLookup[(temp & 0x0003F000) >> 12]); encoded.append(1, kEncodeLookup[(temp & 0x00000FC0) >> 6]); @@ -38,27 +38,27 @@ std::string encode(const std::vector &input) { } switch (input.size() % 3) { - case 1: - temp = (*it++) << 16; + case 1: { + auto temp = std::to_integer(*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(*it++) << 16; + temp += std::to_integer(*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 decode(const std::string &input) { +std::vector decode(std::string_view input) { if (input.length() % 4) throw std::runtime_error("Invalid base64 length!"); @@ -71,7 +71,7 @@ std::vector decode(const std::string &input) { padding++; } - std::vector decoded; + std::vector decoded; decoded.reserve(((input.length() / 4) * 3) - padding); std::uint32_t temp{}; @@ -93,11 +93,11 @@ std::vector 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((temp >> 16) & 0x000000FF)); + decoded.push_back(static_cast((temp >> 8) & 0x000000FF)); return decoded; case 2: - decoded.push_back((temp >> 10) & 0x000000FF); + decoded.push_back(static_cast((temp >> 10) & 0x000000FF)); return decoded; default: throw std::runtime_error("Invalid padding in base64!"); @@ -108,9 +108,9 @@ std::vector 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((temp >> 16) & 0x000000FF)); + decoded.push_back(static_cast((temp >> 8) & 0x000000FF)); + decoded.push_back(static_cast((temp) & 0x000000FF)); } return decoded; diff --git a/common/lib/kernel/devices/ip_device.cpp b/common/lib/kernel/devices/ip_device.cpp index d5d454978..af6612625 100644 --- a/common/lib/kernel/devices/ip_device.cpp +++ b/common/lib/kernel/devices/ip_device.cpp @@ -58,13 +58,9 @@ std::vector IpDevice::from_directory(fs::path devices_directory) { std::vector devices; - const std::vector 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) { } diff --git a/common/lib/memory_manager.cpp b/common/lib/memory_manager.cpp index 1c5f7cc53..904b228b3 100644 --- a/common/lib/memory_manager.cpp +++ b/common/lib/memory_manager.cpp @@ -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); diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index 1431533eb..2bbeda096 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -364,13 +364,5 @@ void write_to_file(std::string data, const fs::path file) { } } -std::vector read_names_in_directory(const fs::path &directory) { - std::vector names; - for (auto const &dir_entry : fs::directory_iterator{directory}) { - names.push_back(dir_entry.path().filename()); - } - return names; -} - } // namespace utils } // namespace villas diff --git a/common/tests/unit/base64.cpp b/common/tests/unit/base64.cpp index f8a68e811..80fd5534f 100644 --- a/common/tests/unit/base64.cpp +++ b/common/tests/unit/base64.cpp @@ -16,8 +16,9 @@ using namespace villas::utils::base64; // cppcheck-suppress unknownMacro TestSuite(base64, .description = "Base64 En/decoder"); -static std::vector vec(const char *str) { - return std::vector((byte *)str, (byte *)str + strlen(str)); +static std::vector vec(const char *str) { + auto bytes = std::as_bytes(std::span{str, strlen(str)}); + return std::vector(bytes.begin(), bytes.end()); } Test(base64, encoding) { diff --git a/common/tests/unit/list.cpp b/common/tests/unit/list.cpp index ae39da14e..4e3d42511 100644 --- a/common/tests/unit/list.cpp +++ b/common/tests/unit/list.cpp @@ -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"; diff --git a/common/tests/unit/utils.cpp b/common/tests/unit/utils.cpp index d49e793dd..4de4dc6d9 100644 --- a/common/tests/unit/utils.cpp +++ b/common/tests/unit/utils.cpp @@ -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; diff --git a/include/villas/nodes/iec61850_goose.hpp b/include/villas/nodes/iec61850_goose.hpp index 47a7d3c77..5ab47216f 100644 --- a/include/villas/nodes/iec61850_goose.hpp +++ b/include/villas/nodes/iec61850_goose.hpp @@ -134,7 +134,7 @@ class GooseNode : public Node { int id; RSecurityAlgorithm security; RSignatureAlgorithm signature; - std::vector data; + std::vector data; }; struct InputMapping { diff --git a/include/villas/nodes/opal_orchestra/ddf.hpp b/include/villas/nodes/opal_orchestra/ddf.hpp index df16bb85f..482a14215 100644 --- a/include/villas/nodes/opal_orchestra/ddf.hpp +++ b/include/villas/nodes/opal_orchestra/ddf.hpp @@ -8,7 +8,6 @@ #pragma once -#include #include #include #include diff --git a/lib/api/requests/universal/channels.cpp b/lib/api/requests/universal/channels.cpp index 62ff6fbc9..89ccfba8a 100644 --- a/lib/api/requests/universal/channels.cpp +++ b/lib/api/requests/universal/channels.cpp @@ -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); @@ -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); diff --git a/lib/formats/json_kafka.cpp b/lib/formats/json_kafka.cpp index c4452ce76..b627f3c00 100644 --- a/lib/formats/json_kafka.cpp +++ b/lib/formats/json_kafka.cpp @@ -58,7 +58,8 @@ int JsonKafkaFormat::packSample(json_t **json_smp, const struct Sample *smp) { } // Include sample data - for (size_t i = 0; i < MIN(smp->length, smp->signals->size()); i++) { + for (size_t i = 0; + i < std::min(std::size_t{smp->length}, smp->signals->size()); i++) { const auto sig = smp->signals->getByIndex(i); const auto *data = &smp->data[i]; diff --git a/lib/formats/msg.cpp b/lib/formats/msg.cpp index 31818b8f2..f52eb1c3a 100644 --- a/lib/formats/msg.cpp +++ b/lib/formats/msg.cpp @@ -66,8 +66,9 @@ int villas::node::msg_to_sample(const struct Message *msg, struct Sample *smp, if (ret) return ret; - unsigned len = MIN(msg->length, smp->capacity); - for (i = 0; i < MIN(len, sigs->size()); i++) { + unsigned len = std::min({static_cast(msg->length), + static_cast(sigs->size()), smp->capacity}); + for (i = 0; i < len; i++) { auto sig = sigs->getByIndex(i); if (!sig) return -1; diff --git a/lib/formats/opal_asyncip.cpp b/lib/formats/opal_asyncip.cpp index aa05b4f19..7e42d612b 100644 --- a/lib/formats/opal_asyncip.cpp +++ b/lib/formats/opal_asyncip.cpp @@ -38,7 +38,7 @@ int OpalAsyncIPFormat::sprint(char *buf, size_t len, size_t *wbytes, "We only send the first {}..", MAXSIZE, MAXSIZE); - for (unsigned j = 0; j < MIN(MAXSIZE, smp->length); j++) { + for (unsigned j = 0; j < std::min(MAXSIZE, smp->length); j++) { auto sig = smp->signals->getByIndex(j); auto d = smp->data[j]; @@ -78,7 +78,7 @@ int OpalAsyncIPFormat::sscan(const char *buf, size_t len, size_t *rbytes, smp->flags = (int)SampleFlags::HAS_SEQUENCE | (int)SampleFlags::HAS_DATA; smp->signals = signals; - for (unsigned j = 0; j < MIN(smp->length, smp->capacity); j++) { + for (unsigned j = 0; j < std::min(smp->length, smp->capacity); j++) { auto sig = signals->getByIndex(j); SignalData d; diff --git a/lib/formats/protobuf.cpp b/lib/formats/protobuf.cpp index c34913bcc..9b960b151 100644 --- a/lib/formats/protobuf.cpp +++ b/lib/formats/protobuf.cpp @@ -159,7 +159,7 @@ int ProtobufFormat::sscan(const char *buf, size_t len, size_t *rbytes, if (!pb_msg) return -1; - for (i = 0; i < MIN(pb_msg->n_samples, cnt); i++) { + for (i = 0; i < std::min(pb_msg->n_samples, std::size_t{cnt}); i++) { struct Sample *smp = smps[i]; Villas__Node__Sample *pb_smp = pb_msg->samples[i]; @@ -184,7 +184,8 @@ int ProtobufFormat::sscan(const char *buf, size_t len, size_t *rbytes, smp->ts.origin.tv_nsec = pb_smp->ts_origin->nsec; } - for (j = 0; j < MIN(pb_smp->n_values, smp->capacity); j++) { + for (j = 0; j < std::min(pb_smp->n_values, std::size_t{smp->capacity}); + j++) { Villas__Node__Value *pb_val = pb_smp->values[j]; enum SignalType fmt = detect(pb_val); diff --git a/lib/hook_list.cpp b/lib/hook_list.cpp index 4fcdceca7..ed5db006d 100644 --- a/lib/hook_list.cpp +++ b/lib/hook_list.cpp @@ -132,7 +132,7 @@ int HookList::process(struct Sample *smps[], unsigned cnt) { } stop: - SWAP(smps[processed], smps[current]); + std::swap(smps[processed], smps[current]); processed++; skip: {} } diff --git a/lib/kernel/tc.cpp b/lib/kernel/tc.cpp index 924a9f1e5..525eff64c 100644 --- a/lib/kernel/tc.cpp +++ b/lib/kernel/tc.cpp @@ -34,7 +34,7 @@ int villas::kernel::tc::prio(Interface *i, struct rtnl_qdisc **qd, /* This is the default priomap used by the tc-prio qdisc * We will use the first 'bands' bands internally */ uint8_t map[] = QDISC_PRIO_DEFAULT_PRIOMAP; - for (unsigned i = 0; i < ARRAY_LEN(map); i++) + for (unsigned i = 0; i < std::size(map); i++) map[i] += bands; rtnl_tc_set_link(TC_CAST(q), i->nl_link); diff --git a/lib/mapping.cpp b/lib/mapping.cpp index 965b943e2..62db59cf9 100644 --- a/lib/mapping.cpp +++ b/lib/mapping.cpp @@ -149,7 +149,8 @@ int MappingEntry::update(struct Sample *remapped, case Type::DATA: for (unsigned j = data.offset, i = offset; - j < MIN(original->length, (unsigned)(data.offset + length)); + j < std::min(original->length, + static_cast(data.offset + length)); j++, i++) { if (j >= original->length) remapped->data[i].f = -1; @@ -157,7 +158,8 @@ int MappingEntry::update(struct Sample *remapped, remapped->data[i] = original->data[j]; } - len = MIN((unsigned)length, original->length - data.offset); + len = + std::min(static_cast(length), original->length - data.offset); break; case Type::UNKNOWN: diff --git a/lib/node.cpp b/lib/node.cpp index e343ed614..59718db6b 100644 --- a/lib/node.cpp +++ b/lib/node.cpp @@ -145,7 +145,7 @@ int Node::parse(json_t *json) { const char *fields[] = {"signals", "builtin", "vectorize", "hooks"}; - for (unsigned j = 0; j < ARRAY_LEN(dirs); j++) { + for (unsigned j = 0; j < std::size(dirs); j++) { json_t *json_dir = json_object_get(json, dirs[j].str); // Skip if direction is unused @@ -154,7 +154,7 @@ int Node::parse(json_t *json) { } // Copy missing fields from main node config to direction config - for (unsigned i = 0; i < ARRAY_LEN(fields); i++) { + for (unsigned i = 0; i < std::size(fields); i++) { json_t *json_field_dir = json_object_get(json_dir, fields[i]); json_t *json_field_node = json_object_get(json, fields[i]); @@ -271,7 +271,7 @@ int Node::read(struct Sample *smps[], unsigned cnt) { vect = cnt; while (cnt - nread > 0) { - toread = MIN(cnt - nread, vect); + toread = std::min(cnt - nread, vect); readd = _read(&smps[nread], toread); if (readd < 0) return readd; @@ -325,7 +325,7 @@ int Node::write(struct Sample *smps[], unsigned cnt) { vect = cnt; while (cnt > static_cast(nsent)) { - tosend = MIN(cnt - nsent, vect); + tosend = std::min(cnt - nsent, vect); sent = _write(&smps[nsent], tosend); if (sent < 0) return sent; @@ -432,7 +432,7 @@ json_t *Node::toJson() const { return json_node; } -void Node::swapSignals() { SWAP(in.signals, out.signals); } +void Node::swapSignals() { std::swap(in.signals, out.signals); } Node *NodeFactory::make(json_t *json, const uuid_t &id, const std::string &name) { diff --git a/lib/node_direction.cpp b/lib/node_direction.cpp index 39088b014..c766eaae1 100644 --- a/lib/node_direction.cpp +++ b/lib/node_direction.cpp @@ -163,7 +163,8 @@ SignalList::Ptr NodeDirection::getSignals(int after_hooks) const { unsigned NodeDirection::getSignalsMaxCount() const { #ifdef WITH_HOOKS if (hooks.size() > 0) - return MAX(signals->size(), hooks.getSignalsMaxCount()); + return std::max(static_cast(signals->size()), + hooks.getSignalsMaxCount()); #endif // WITH_HOOKS return signals->size(); diff --git a/lib/nodes/comedi.cpp b/lib/nodes/comedi.cpp index 4a8ebe1e0..f8c017b38 100644 --- a/lib/nodes/comedi.cpp +++ b/lib/nodes/comedi.cpp @@ -532,7 +532,7 @@ int villas::node::comedi_read(NodeCompat *n, struct Sample *const smps[], const size_t bytes_requested = cnt * villas_sample_size; ret = read(comedi_fileno(c->dev), c->bufptr, - MIN(bytes_requested, buffer_bytes_free)); + std::min(bytes_requested, buffer_bytes_free)); if (ret < 0) { if (errno == EAGAIN) throw RuntimeError("Failed read()"); diff --git a/lib/nodes/ethercat.cpp b/lib/nodes/ethercat.cpp index 424ddef23..acd0c120d 100644 --- a/lib/nodes/ethercat.cpp +++ b/lib/nodes/ethercat.cpp @@ -61,7 +61,7 @@ static void ethercat_cyclic_task(NodeCompat *n) { continue; } - smp->length = MIN(w->in.num_channels, smp->capacity); + smp->length = std::min(w->in.num_channels, smp->capacity); smp->flags = (int)SampleFlags::HAS_DATA; smp->signals = n->getInputSignals(false); diff --git a/lib/nodes/fpga.cpp b/lib/nodes/fpga.cpp index 45f1e3f24..4d8ccc3f3 100644 --- a/lib/nodes/fpga.cpp +++ b/lib/nodes/fpga.cpp @@ -221,10 +221,10 @@ int FpgaNode::start() { // If fastWrite receives less signals than expected, the previous data // will be reused for the remaining signals int FpgaNode::fastWrite(Sample *smps[], unsigned cnt) { - Sample *smp = smps[0]; - assert(cnt == 1 && smps != nullptr && smps[0] != nullptr); + Sample *smp = smps[0]; + for (unsigned i = 0; i < smp->length; i++) { if (smp->signals->getByIndex(i)->type == SignalType::FLOAT) { (*accessorTxFloat)[i] = static_cast(smp->data[i].f); @@ -273,7 +273,10 @@ int FpgaNode::fastRead(Sample *smps[], unsigned cnt) { // We assume a lot without checking at this point. All for the latency! smp->length = 0; - for (unsigned i = 0; i < MIN(read / sizeof(uint32_t), smp->capacity); i++) { + for (unsigned i = 0; + i < + std::min(static_cast(read / sizeof(uint32_t)), smp->capacity); + i++) { if (i >= in.signals->size()) { logger->warn( "Received more data than expected. Maybe the descriptor cache needs " @@ -317,7 +320,7 @@ int FpgaNode::slowRead(Sample *smps[], unsigned cnt) { auto mem = MemoryAccessor(*blockRx); smp->length = 0; - for (unsigned i = 0; i < MIN(read, smp->capacity); i++) { + for (unsigned i = 0; i < std::min(read, smp->capacity); i++) { smp->data[i].f = static_cast(mem[i]); smp->length++; } @@ -337,11 +340,10 @@ int FpgaNode::_write(Sample *smps[], unsigned cnt) { } int FpgaNode::slowWrite(Sample *smps[], unsigned cnt) { - // unsigned int written; - Sample *smp = smps[0]; - assert(cnt == 1 && smps != nullptr && smps[0] != nullptr); + Sample *smp = smps[0]; + auto mem = MemoryAccessor(*blockTx); for (unsigned i = 0; i < smps[0]->length; i++) { diff --git a/lib/nodes/iec60870.cpp b/lib/nodes/iec60870.cpp index 55f62f180..fa8850c63 100644 --- a/lib/nodes/iec60870.cpp +++ b/lib/nodes/iec60870.cpp @@ -586,7 +586,8 @@ void SlaveNode::sendPeriodicASDUsForSample(Sample const *sample) const for (auto const &type : output.asdu_types) { // Search all occurrences of this ASDU type for (unsigned signal = 0; - signal < MIN(sample->length, output.mapping.size());) { + signal < std::min(sample->length, + static_cast(output.mapping.size()));) { // Create an ASDU for periodic transmission CS101_ASDU asdu = CS101_ASDU_create(server.asdu_app_layer_parameters, 0, CS101_COT_PERIODIC, 0, @@ -618,7 +619,8 @@ void SlaveNode::sendPeriodicASDUsForSample(Sample const *sample) const timestamp}) == false) // ASDU is full -> dispatch -> create a new one break; - } while (++signal < MIN(sample->length, output.mapping.size())); + } while (++signal < std::min(sample->length, static_cast( + output.mapping.size()))); if (CS101_ASDU_getNumberOfElements(asdu) != 0) CS104_Slave_enqueueASDU(server.slave, asdu); @@ -637,7 +639,9 @@ int SlaveNode::_write(Sample *samples[], unsigned sample_count) { // Update last_values output.last_values_mutex.lock(); - for (unsigned i = 0; i < MIN(sample->length, output.last_values.size()); + for (unsigned i = 0; + i < std::min(sample->length, + static_cast(output.last_values.size())); i++) output.last_values[i] = sample->data[i]; diff --git a/lib/nodes/iec61850.cpp b/lib/nodes/iec61850.cpp index 70989a29c..0e26af8c8 100644 --- a/lib/nodes/iec61850.cpp +++ b/lib/nodes/iec61850.cpp @@ -62,7 +62,7 @@ static int users = 0; const struct iec61850_type_descriptor * villas::node::iec61850_lookup_type(const char *name) { - for (unsigned i = 0; i < ARRAY_LEN(type_descriptors); i++) { + for (unsigned i = 0; i < std::size(type_descriptors); i++) { if (!strcmp(name, type_descriptors[i].name)) return &type_descriptors[i]; } diff --git a/lib/nodes/iec61850_goose.cpp b/lib/nodes/iec61850_goose.cpp index 805618ef7..508ced936 100644 --- a/lib/nodes/iec61850_goose.cpp +++ b/lib/nodes/iec61850_goose.cpp @@ -353,7 +353,8 @@ void GooseNode::createReceiver() noexcept(false) { throw RuntimeError("failed to set local address for R-GOOSE session"); for (auto &key : keys) { - err = RSession_addKey(input.session, key.id, key.data.data(), + err = RSession_addKey(input.session, key.id, + reinterpret_cast(key.data.data()), key.data.size(), key.security, key.signature); if (err != R_SESSION_ERROR_OK) throw RuntimeError("failed to add key with id {} to R-GOOSE session", @@ -423,7 +424,8 @@ void GooseNode::createPublishers() { output.remote_address); for (auto &key : keys) { - err = RSession_addKey(output.session, key.id, key.data.data(), + err = RSession_addKey(output.session, key.id, + reinterpret_cast(key.data.data()), key.data.size(), key.security, key.signature); if (err != R_SESSION_ERROR_OK) throw RuntimeError("failed to add key with id {} to R-GOOSE session", @@ -725,14 +727,16 @@ void GooseNode::parseSessionKey(json_t *json) { char const *security_str; char const *signature_str; char const *data_str = nullptr; + std::size_t data_str_len; char const *data_base64 = nullptr; - ret = json_unpack_ex(json, &err, 0, // - "{ s:i, s:s, s:s, s:?s, s:?s }", // - "id", &id, // - "security", &security_str, // - "signature", &signature_str, // - "string", &data_str, // - "base64", &data_base64); + std::size_t data_base64_len; + ret = json_unpack_ex(json, &err, 0, // + "{ s:i, s:s, s:s, s:?s%, s:?s% }", // + "id", &id, // + "security", &security_str, // + "signature", &signature_str, // + "string", &data_str, &data_str_len, // + "base64", &data_base64, &data_base64_len); if (ret) throw ConfigError(json, err, "node-config-node-iec61850-8-1"); @@ -768,13 +772,13 @@ void GooseNode::parseSessionKey(json_t *json) { else throw RuntimeError("unknown signature algorithm {}", signature_str); - std::vector data; + std::vector data; if (data_str && data_base64) throw RuntimeError( "can't use both 'base64' and 'string' for R-GOOSE key with id {}", id); else if (data_str) { - auto data_sv = std::string_view(data_str); - data = std::vector(begin(data_sv), end(data_sv)); + auto data_bytes = std::as_bytes(std::span{data_str, data_str_len}); + data = std::vector(data_bytes.begin(), data_bytes.end()); } else if (data_base64) { data = base64::decode(data_base64); } else { diff --git a/lib/nodes/iec61850_sv.cpp b/lib/nodes/iec61850_sv.cpp index 4cd5486d8..b5096956f 100644 --- a/lib/nodes/iec61850_sv.cpp +++ b/lib/nodes/iec61850_sv.cpp @@ -33,7 +33,8 @@ using namespace villas::node; static unsigned iec61850_sv_setup_asdu(NodeCompat *n, struct Sample *smp) { auto *i = n->getData(); - unsigned new_length = MIN(list_length(&i->out.signals), smp->length); + unsigned new_length = std::min( + static_cast(list_length(&i->out.signals)), smp->length); SVPublisher_ASDU_resetBuffer(i->out.asdu); SVPublisher_ASDU_enableRefrTm(i->out.asdu); @@ -78,7 +79,7 @@ static void iec61850_sv_listener(SVSubscriber subscriber, void *ctx, const char *sv_id = SVSubscriber_ASDU_getSvId(asdu); int smp_cnt = SVSubscriber_ASDU_getSmpCnt(asdu); - size_t data_size = (size_t)SVSubscriber_ASDU_getDataSize(asdu); + unsigned data_size = SVSubscriber_ASDU_getDataSize(asdu); n->logger->debug("Received sample: sv_id={}, smp_cnt={}", sv_id, smp_cnt); @@ -101,9 +102,10 @@ static void iec61850_sv_listener(SVSubscriber subscriber, void *ctx, smp->flags |= (int)SampleFlags::HAS_TS_ORIGIN; } - for (size_t j = 0, off = 0; - j < MIN(list_length(&i->in.signals), smp->capacity) && - off < MIN(i->in.total_size, data_size); + for (unsigned j = 0, off = 0; + j < std::min(static_cast(list_length(&i->in.signals)), + smp->capacity) && + off < std::min(i->in.total_size, data_size); j++) { struct iec61850_type_descriptor *td = (struct iec61850_type_descriptor *)list_at(&i->in.signals, j); @@ -453,7 +455,8 @@ int villas::node::iec61850_sv_write(NodeCompat *n, struct Sample *const smps[], for (unsigned j = 0; j < cnt; j++) { auto *smp = smps[j]; - unsigned asdu_length = MIN(smp->length, list_length(&i->out.signals)); + unsigned asdu_length = std::min( + smp->length, static_cast(list_length(&i->out.signals))); if (i->out.asdu_length != asdu_length) i->out.asdu_length = iec61850_sv_setup_asdu(n, smp); diff --git a/lib/nodes/infiniband.cpp b/lib/nodes/infiniband.cpp index 457300260..65f973a4f 100644 --- a/lib/nodes/infiniband.cpp +++ b/lib/nodes/infiniband.cpp @@ -25,7 +25,7 @@ using namespace villas::utils; static int ib_disconnect(NodeCompat *n) { auto *ib = n->getData(); - struct ibv_wc wc[MAX(ib->recv_cq_size, ib->send_cq_size)]; + struct ibv_wc wc[std::max(ib->recv_cq_size, ib->send_cq_size)]; int wcs; n->logger->debug("Starting to clean up"); @@ -153,7 +153,7 @@ static int ib_connect_request(NodeCompat *n, struct rdma_cm_id *id) { int villas::node::ib_reverse(NodeCompat *n) { auto *ib = n->getData(); - SWAP(ib->conn.src_addr, ib->conn.dst_addr); + std::swap(ib->conn.src_addr, ib->conn.dst_addr); return 0; } diff --git a/lib/nodes/kafka.cpp b/lib/nodes/kafka.cpp index 1c56e1e5f..035fd6e74 100644 --- a/lib/nodes/kafka.cpp +++ b/lib/nodes/kafka.cpp @@ -117,7 +117,7 @@ static void *kafka_loop_thread(void *ctx) { int villas::node::kafka_reverse(NodeCompat *n) { auto *k = n->getData(); - SWAP(k->produce, k->consume); + std::swap(k->produce, k->consume); return 0; } diff --git a/lib/nodes/mqtt.cpp b/lib/nodes/mqtt.cpp index d76704ff0..3fba0a7b3 100644 --- a/lib/nodes/mqtt.cpp +++ b/lib/nodes/mqtt.cpp @@ -113,7 +113,7 @@ static void mqtt_subscribe_cb(struct mosquitto *mosq, void *ctx, int mid, int villas::node::mqtt_reverse(NodeCompat *n) { auto *m = n->getData(); - SWAP(m->publish, m->subscribe); + std::swap(m->publish, m->subscribe); return 0; } diff --git a/lib/nodes/ngsi.cpp b/lib/nodes/ngsi.cpp index e80f0091e..ba2a0b4a3 100644 --- a/lib/nodes/ngsi.cpp +++ b/lib/nodes/ngsi.cpp @@ -651,7 +651,7 @@ int villas::node::ngsi_start(NodeCompat *n) { CURL *handles[] = {i->in.curl, i->out.curl}; - for (unsigned p = 0; p < ARRAY_LEN(handles); p++) { + for (unsigned p = 0; p < std::size(handles); p++) { curl_easy_setopt(handles[p], CURLOPT_SSL_VERIFYPEER, i->ssl_verify); curl_easy_setopt(handles[p], CURLOPT_TIMEOUT_MS, i->timeout * 1e3); curl_easy_setopt(handles[p], CURLOPT_HTTPHEADER, i->headers); @@ -804,7 +804,7 @@ int villas::node::ngsi_reverse(NodeCompat *n) { auto *i = n->getData(); n->swapSignals(); - SWAP(i->in.signals, i->out.signals); + std::swap(i->in.signals, i->out.signals); return 0; } diff --git a/lib/nodes/opal_async.cpp b/lib/nodes/opal_async.cpp index f3a5dcee6..e25d0f07e 100644 --- a/lib/nodes/opal_async.cpp +++ b/lib/nodes/opal_async.cpp @@ -322,7 +322,7 @@ int OpalAsyncNode::_write(struct Sample *smps[], unsigned cnt) { } else realLen = smp->length; - for (unsigned i = 0; i < MIN(realLen, smp->signals->size()); i++) { + for (unsigned i = 0; i < std::min(realLen, smp->signals->size()); i++) { auto sig = smp->signals->getByIndex(i); data[i] = smp->data[i].cast(sig->type, SignalType::FLOAT).f; } diff --git a/lib/nodes/redis.cpp b/lib/nodes/redis.cpp index 1a69c8a40..939decf97 100644 --- a/lib/nodes/redis.cpp +++ b/lib/nodes/redis.cpp @@ -598,7 +598,8 @@ int villas::node::redis_write(NodeCompat *n, struct Sample *const smps[], std::unordered_map kvs; - unsigned len = MIN(smp->signals->size(), smp->length); + unsigned len = + std::min(static_cast(smp->signals->size()), smp->length); for (unsigned j = 0; j < len; j++) { const auto sig = smp->signals->getByIndex(j); const auto *data = &smp->data[j]; diff --git a/lib/nodes/rtp.cpp b/lib/nodes/rtp.cpp index 964e6f1c0..8ee172895 100644 --- a/lib/nodes/rtp.cpp +++ b/lib/nodes/rtp.cpp @@ -104,8 +104,8 @@ int villas::node::rtp_init(NodeCompat *n) { int villas::node::rtp_reverse(NodeCompat *n) { auto *r = n->getData(); - SWAP(r->in.saddr_rtp, r->out.saddr_rtp); - SWAP(r->in.saddr_rtcp, r->out.saddr_rtcp); + std::swap(r->in.saddr_rtp, r->out.saddr_rtp); + std::swap(r->in.saddr_rtcp, r->out.saddr_rtcp); return 0; } diff --git a/lib/nodes/shmem.cpp b/lib/nodes/shmem.cpp index e3b818a3d..601fec8ee 100644 --- a/lib/nodes/shmem.cpp +++ b/lib/nodes/shmem.cpp @@ -90,16 +90,16 @@ int villas::node::shmem_prepare(NodeCompat *n) { auto *shm = n->getData(); if (shm->conf.queuelen < 0) - shm->conf.queuelen = MAX(DEFAULT_SHMEM_QUEUELEN, n->in.vectorize); + shm->conf.queuelen = std::max(DEFAULT_SHMEM_QUEUELEN, n->in.vectorize); if (shm->conf.samplelen < 0) { - auto input_sigs = n->getInputSignals(false)->size(); + auto input_sigs = static_cast(n->getInputSignals(false)->size()); auto output_sigs = 0U; if (n->getOutputSignals(true)) output_sigs = n->getOutputSignals(true)->size(); - shm->conf.samplelen = MAX(input_sigs, output_sigs); + shm->conf.samplelen = std::max(input_sigs, output_sigs); } return 0; diff --git a/lib/nodes/signal.cpp b/lib/nodes/signal.cpp index 11f9e4391..31576d3b3 100644 --- a/lib/nodes/signal.cpp +++ b/lib/nodes/signal.cpp @@ -294,7 +294,7 @@ int SignalNode::_read(struct Sample *smps[], unsigned cnt) { (int)SampleFlags::HAS_SEQUENCE; t->ts.origin = ts; t->sequence = sequence; - t->length = MIN(signals.size(), t->capacity); + t->length = std::min(signals.size(), std::size_t{t->capacity}); t->signals = in.signals; for (unsigned i = 0; i < t->length; i++) { diff --git a/lib/nodes/signal_v1.cpp b/lib/nodes/signal_v1.cpp index 0d9b993c2..2cf016ff2 100644 --- a/lib/nodes/signal_v1.cpp +++ b/lib/nodes/signal_v1.cpp @@ -341,7 +341,7 @@ int villas::node::signal_node_read(NodeCompat *n, struct Sample *const smps[], (int)SampleFlags::HAS_SEQUENCE; t->ts.origin = ts; t->sequence = s->counter; - t->length = MIN(s->values, t->capacity); + t->length = std::min(s->values, t->capacity); t->signals = n->getInputSignals(false); for (unsigned i = 0; i < t->length; i++) { diff --git a/lib/nodes/stats.cpp b/lib/nodes/stats.cpp index a5a60a8aa..74d2facd2 100644 --- a/lib/nodes/stats.cpp +++ b/lib/nodes/stats.cpp @@ -205,7 +205,8 @@ int villas::node::stats_node_read(NodeCompat *n, struct Sample *const smps[], s->task.wait(); - unsigned len = MIN(list_length(&s->signals), smps[0]->capacity); + unsigned len = std::min(static_cast(list_length(&s->signals)), + smps[0]->capacity); for (size_t i = 0; i < len; i++) { struct stats_node_signal *sig = diff --git a/lib/nodes/test_rtt.cpp b/lib/nodes/test_rtt.cpp index 6cb95ef28..efa884330 100644 --- a/lib/nodes/test_rtt.cpp +++ b/lib/nodes/test_rtt.cpp @@ -274,11 +274,11 @@ int TestRTT::parse(json_t *json) { switch (mode) { case Mode::MIN: - count_effective = MIN(count, count_duration); + count_effective = std::min(count, count_duration); break; case Mode::MAX: - count_effective = MAX(count, count_duration); + count_effective = std::max(count, count_duration); break; case Mode::STOP_COUNT: diff --git a/lib/nodes/uldaq.cpp b/lib/nodes/uldaq.cpp index d82ca79dd..9964fb953 100644 --- a/lib/nodes/uldaq.cpp +++ b/lib/nodes/uldaq.cpp @@ -92,7 +92,7 @@ static const struct { {"external", SO_EXTCLOCK}}; static AiInputMode uldaq_parse_input_mode(const char *str) { - for (unsigned i = 0; i < ARRAY_LEN(input_modes); i++) { + for (unsigned i = 0; i < std::size(input_modes); i++) { if (!strcmp(input_modes[i].name, str)) return input_modes[i].mode; } @@ -101,7 +101,7 @@ static AiInputMode uldaq_parse_input_mode(const char *str) { } static DaqDeviceInterface uldaq_parse_interface_type(const char *str) { - for (unsigned i = 0; i < ARRAY_LEN(interface_types); i++) { + for (unsigned i = 0; i < std::size(interface_types); i++) { if (!strcmp(interface_types[i].name, str)) return interface_types[i].interface; } @@ -110,7 +110,7 @@ static DaqDeviceInterface uldaq_parse_interface_type(const char *str) { } static const char *uldaq_print_interface_type(DaqDeviceInterface iftype) { - for (unsigned i = 0; i < ARRAY_LEN(interface_types); i++) { + for (unsigned i = 0; i < std::size(interface_types); i++) { if (interface_types[i].interface == iftype) return interface_types[i].name; } @@ -119,7 +119,7 @@ static const char *uldaq_print_interface_type(DaqDeviceInterface iftype) { } static Range uldaq_parse_range(const char *str) { - for (unsigned i = 0; i < ARRAY_LEN(ranges); i++) { + for (unsigned i = 0; i < std::size(ranges); i++) { if (!strcmp(ranges[i].name, str)) return ranges[i].range; } @@ -128,7 +128,7 @@ static Range uldaq_parse_range(const char *str) { } static ScanOption uldaq_parse_clock_source(const char *str) { - for (unsigned i = 0; i < ARRAY_LEN(clock_sources); i++) { + for (unsigned i = 0; i < std::size(clock_sources); i++) { if (!strcmp(clock_sources[i].name, str)) return clock_sources[i].clock_source; } diff --git a/lib/nodes/websocket.cpp b/lib/nodes/websocket.cpp index aa0f21d8f..0a8aa6b53 100644 --- a/lib/nodes/websocket.cpp +++ b/lib/nodes/websocket.cpp @@ -642,7 +642,7 @@ int villas::node::websocket_poll_fds(NodeCompat *n, int fds[]) { return 1; } -__attribute__((constructor(110))) static void UNIQUE(__ctor)() { +__attribute__((constructor(110))) static void register_plugin() { p.name = "websocket"; p.description = "Send and receive samples of a WebSocket connection (libwebsockets)"; diff --git a/lib/path.cpp b/lib/path.cpp index c15234f6d..a23e63ede 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -297,7 +297,7 @@ void Path::prepare(NodeList &nodes) { // Prepare pool auto osigs = getOutputSignals(); - unsigned pool_size = MAX(1UL, destinations.size()) * queuelen; + unsigned pool_size = std::max(1UL, destinations.size()) * queuelen; ret = pool_init(&pool, pool_size, SAMPLE_LENGTH(osigs->size()), pool_mt); if (ret) @@ -443,8 +443,8 @@ void Path::check() { throw RuntimeError("Setting 'rate' of path {} must be a positive number.", this->toString()); - if (!IS_POW2(queuelen)) { - queuelen = LOG2_CEIL(queuelen); + if (!std::has_single_bit(queuelen)) { + queuelen = std::bit_ceil(queuelen); logger->warn("Queue length should always be a power of 2. Adjusting to {}", queuelen); } @@ -647,7 +647,8 @@ SignalList::Ptr Path::getOutputSignals(bool after_hooks) { unsigned Path::getOutputSignalsMaxCount() { #ifdef WITH_HOOKS if (hooks.size() > 0) - return MAX(signals->size(), hooks.getSignalsMaxCount()); + return std::max(static_cast(signals->size()), + hooks.getSignalsMaxCount()); #endif // WITH_HOOKS return signals->size(); diff --git a/lib/path_source.cpp b/lib/path_source.cpp index 9d7dc0d04..8bc3dcf2b 100644 --- a/lib/path_source.cpp +++ b/lib/path_source.cpp @@ -23,7 +23,7 @@ using namespace villas::node; PathSource::PathSource(Path *p, Node *n) : node(n), path(p), masked(false) { int ret; - int pool_size = MAX(DEFAULT_QUEUE_LENGTH, 20 * node->in.vectorize); + int pool_size = std::max(DEFAULT_QUEUE_LENGTH, 20 * node->in.vectorize); ret = pool_init(&pool, pool_size, SAMPLE_LENGTH(node->getInputSignalsMaxCount()), node->getMemoryType()); diff --git a/lib/pool.cpp b/lib/pool.cpp index 784e56446..037b8b187 100644 --- a/lib/pool.cpp +++ b/lib/pool.cpp @@ -40,7 +40,7 @@ int villas::node::pool_init(struct Pool *p, size_t cnt, size_t blocksz, p->buffer_off = reinterpret_cast(buffer) - reinterpret_cast(p); - ret = queue_init(&p->queue, LOG2_CEIL(cnt), m); + ret = queue_init(&p->queue, std::bit_ceil(cnt), m); if (ret) return ret; diff --git a/lib/queue.cpp b/lib/queue.cpp index b85b54aa6..6e7823c28 100644 --- a/lib/queue.cpp +++ b/lib/queue.cpp @@ -42,9 +42,9 @@ using namespace villas; int villas::node::queue_init(struct CQueue *q, size_t size, struct memory::Type *m) { // Queue size must be 2 exponent - if (!IS_POW2(size)) { + if (!std::has_single_bit(size)) { size_t old_size = size; - size = LOG2_CEIL(size); + size = std::bit_ceil(size); auto logger = Log::get("queue"); logger->warn("A queue size was changed from {} to {}", old_size, size); diff --git a/lib/sample.cpp b/lib/sample.cpp index aeb5e665b..e9ad06f30 100644 --- a/lib/sample.cpp +++ b/lib/sample.cpp @@ -136,7 +136,7 @@ int villas::node::sample_decref(struct Sample *s) { } int villas::node::sample_copy(struct Sample *dst, const struct Sample *src) { - dst->length = MIN(src->length, dst->capacity); + dst->length = std::min(src->length, dst->capacity); dst->sequence = src->sequence; dst->flags = src->flags; diff --git a/src/villas-convert.cpp b/src/villas-convert.cpp index e3584631c..1bbcb89e2 100644 --- a/src/villas-convert.cpp +++ b/src/villas-convert.cpp @@ -37,7 +37,7 @@ class Convert : public Tool { if (ret) throw RuntimeError("Failed to initialize memory"); - for (unsigned i = 0; i < ARRAY_LEN(dirs); i++) { + for (unsigned i = 0; i < std::size(dirs); i++) { dirs[i].name = i == 0 ? "in" : "out"; dirs[i].format = "villas.human"; } @@ -108,7 +108,7 @@ class Convert : public Tool { int main() override { int ret; - for (unsigned i = 0; i < ARRAY_LEN(dirs); i++) { + for (unsigned i = 0; i < std::size(dirs); i++) { json_t *json_format; json_error_t err; std::string format = dirs[i].format; @@ -150,7 +150,7 @@ class Convert : public Tool { dirs[1].formatter->print(stdout, smps, ret); } - for (unsigned i = 0; i < ARRAY_LEN(dirs); i++) + for (unsigned i = 0; i < std::size(dirs); i++) delete dirs[i].formatter; return 0; diff --git a/src/villas-pipe.cpp b/src/villas-pipe.cpp index c7b3193f0..ca800d7a5 100644 --- a/src/villas-pipe.cpp +++ b/src/villas-pipe.cpp @@ -61,7 +61,7 @@ class PipeDirection { // Initialize memory unsigned pool_size = - LOG2_CEIL(MAX(node->out.vectorize, node->in.vectorize)); + std::bit_ceil(std::max({node->out.vectorize, node->in.vectorize, 16U})); int ret = pool_init(&pool, pool_size, SAMPLE_LENGTH(DEFAULT_SAMPLE_LENGTH), node->getMemoryType()); diff --git a/tests/unit/format.cpp b/tests/unit/format.cpp index 0a6e5e31f..2f953d2c5 100644 --- a/tests/unit/format.cpp +++ b/tests/unit/format.cpp @@ -105,7 +105,7 @@ void cr_assert_eq_sample(struct Sample *a, struct Sample *b, int flags) { } if (flags & (int)SampleFlags::HAS_DATA) { - for (unsigned j = 0; j < MIN(a->length, b->length); j++) { + for (unsigned j = 0; j < std::min(a->length, b->length); j++) { cr_assert_eq(sample_format(a, j), sample_format(b, j)); switch (sample_format(b, j)) { @@ -159,7 +159,7 @@ void cr_assert_eq_sample_raw(struct Sample *a, struct Sample *b, int flags, } if (flags & (int)SampleFlags::HAS_DATA) { - for (unsigned j = 0; j < MIN(a->length, b->length); j++) { + for (unsigned j = 0; j < std::min(a->length, b->length); j++) { cr_assert_eq(sample_format(a, j), sample_format(b, j)); switch (sample_format(b, j)) { diff --git a/tests/unit/queue.cpp b/tests/unit/queue.cpp index 349a4056e..daca7a464 100644 --- a/tests/unit/queue.cpp +++ b/tests/unit/queue.cpp @@ -260,7 +260,7 @@ ParameterizedTestParameters(queue, multi_threaded) { .batch_size = 10, .mt = &memory::mmap_hugetlb}}; - return cr_make_param_array(struct param, params, ARRAY_LEN(params)); + return cr_make_param_array(struct param, params, std::size(params)); } // cppcheck-suppress unknownMacro diff --git a/tests/unit/queue_signalled.cpp b/tests/unit/queue_signalled.cpp index 4d16a8983..0750928b5 100644 --- a/tests/unit/queue_signalled.cpp +++ b/tests/unit/queue_signalled.cpp @@ -18,7 +18,7 @@ using namespace villas::node; extern void init_memory(); -#define NUM_ELEM 1000 +constexpr static auto NUM_ELEM = 1000; struct param { enum QueueSignalledMode mode; @@ -48,7 +48,7 @@ static void *consumer(void *ctx) { void *data[NUM_ELEM]; for (intptr_t i = 0; i < NUM_ELEM;) { - ret = queue_signalled_pull_many(q, data, ARRAY_LEN(data)); + ret = queue_signalled_pull_many(q, data, std::size(data)); if (ret <= 0) return (void *)1; // Indicates an error to the parent thread @@ -104,7 +104,7 @@ ParameterizedTestParameters(queue_signalled, simple) { #endif }; - return cr_make_param_array(struct param, params, ARRAY_LEN(params)); + return cr_make_param_array(struct param, params, std::size(params)); } // cppcheck-suppress unknownMacro @@ -116,8 +116,9 @@ ParameterizedTest(struct param *param, queue_signalled, simple, .timeout = 5, pthread_t t1, t2; - ret = queue_signalled_init(&q, LOG2_CEIL(NUM_ELEM), &memory::heap, - param->mode, param->flags); + ret = queue_signalled_init(&q, + std::bit_ceil(static_cast(NUM_ELEM)), + &memory::heap, param->mode, param->flags); cr_assert_eq(ret, 0, "Failed to initialize queue: mode=%d, flags=%#x, ret=%d", (int)param->mode, param->flags, ret);