diff --git a/common/include/villas/dsp/window.hpp b/common/include/villas/dsp/window.hpp index 1a73b8bff..ed7669be2 100644 --- a/common/include/villas/dsp/window.hpp +++ b/common/include/villas/dsp/window.hpp @@ -23,7 +23,7 @@ #pragma once -#include +#include namespace villas { namespace dsp { diff --git a/common/include/villas/utils.h b/common/include/villas/utils.h deleted file mode 100644 index a5119015c..000000000 --- a/common/include/villas/utils.h +++ /dev/null @@ -1,202 +0,0 @@ -/** Various helper functions. - * - * @file - * @author Steffen Vogel - * @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC - * @license GNU General Public License (version 3) - * - * VILLAScommon - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - *********************************************************************************/ - -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include -#include -#include -#include - -#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 - -/* Some color escape codes for pretty log messages */ -#define CLR(clr, str) "\e[" XSTR(clr) "m" str "\e[0m" -#define CLR_GRY(str) CLR(30, str) /**< Print str in gray */ -#define CLR_RED(str) CLR(31, str) /**< Print str in red */ -#define CLR_GRN(str) CLR(32, str) /**< Print str in green */ -#define CLR_YEL(str) CLR(33, str) /**< Print str in yellow */ -#define CLR_BLU(str) CLR(34, str) /**< Print str in blue */ -#define CLR_MAG(str) CLR(35, str) /**< Print str in magenta */ -#define CLR_CYN(str) CLR(36, str) /**< Print str in cyan */ -#define CLR_WHT(str) CLR(37, str) /**< Print str in white */ -#define CLR_BLD(str) CLR( 1, str) /**< Print str in bold */ - -/* 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) - -#ifdef __cplusplus - #define SWAP(x,y) do {\ - auto &_x = x; \ - auto &_y = y; \ - x = _y; \ - y = _x; \ -} while(0) -#else - #define SWAP(x,y) do {\ - __auto_type _x = x; \ - __auto_type _y = y; \ - x = _y; \ - y = _x; \ -} while(0) -#endif - -/** Round-up integer division */ -#define CEIL(x, y) (((x) + (y) - 1) / (y)) - -/** Get nearest up-rounded power of 2 */ -#define LOG2_CEIL(x) (1 << (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; }) - -#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 */ -#define BITMASK(h, l) (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONGLONG - 1 - (h)))) -#define BIT(nr) (1UL << (nr)) - -/** Normal random variate generator using the Box-Muller method - * - * @param m Mean - * @param s Standard deviation - * @return Normal variate random variable (Gaussian) - */ -double box_muller(float m, float s); - -/** Double precission uniform random variable */ -double randf(); - -/** Concat formatted string to an existing string. - * - * This function uses realloc() to resize the destination. - * Please make sure to only on dynamic allocated destionations!!! - * - * @param dest A pointer to a malloc() allocated memory region - * @param fmt A format string like for printf() - * @param ... Optional parameters like for printf() - * @retval The the new value of the dest buffer. - */ -char * strcatf(char **dest, const char *fmt, ...) - __attribute__ ((format(printf, 2, 3))); - -/** Variadic version of strcatf() */ -char * vstrcatf(char **dest, const char *fmt, va_list va) - __attribute__ ((format(printf, 2, 0))); - -char * strf(const char *fmt, ...); -char * vstrf(const char *fmt, va_list va); - -/** Allocate and initialize memory. */ -void * alloc(size_t bytes); - -/** Allocate and copy memory. */ -void * memdup(const void *src, size_t bytes); - -/** Call quit() in the main thread. */ -void die(); - -/** 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 - -/** Get log2 of long long integers */ -static inline int log2i(long long x) { - if (x == 0) - return 1; - - return sizeof(x) * 8 - __builtin_clzll(x) - 1; -} - -/** Send signal \p sig to main thread. */ -void killme(int sig); - -pid_t spawn(const char *name, char *const argv[]); - -/** Determines the string length as printed on the screen (ignores escable sequences). */ -size_t strlenp(const char *str); - -#ifdef __cplusplus -} -#endif diff --git a/common/include/villas/utils.hpp b/common/include/villas/utils.hpp index 3af9d8552..847116e94 100644 --- a/common/include/villas/utils.hpp +++ b/common/include/villas/utils.hpp @@ -29,18 +29,111 @@ #include + +#include +#include +#include +#include +#include +#include + +#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 +#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 &_x = x; \ + auto &_y = y; \ + x = _y; \ + y = _x; \ +} 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 << (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; }) + +#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 */ +#define BITMASK(h, l) (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONGLONG - 1 - (h)))) +#define BIT(nr) (1UL << (nr)) + namespace villas { namespace utils { std::vector tokenize(std::string s, std::string delimiter); - template void assertExcept(bool condition, const T& exception) { - if(not condition) + if (not condition) throw exception; } @@ -53,6 +146,63 @@ ssize_t read_random(char *buf, size_t len); /** Remove ANSI control sequences for colored output. */ char * decolor(char *str); +/** Normal random variate generator using the Box-Muller method + * + * @param m Mean + * @param s Standard deviation + * @return Normal variate random variable (Gaussian) + */ +double box_muller(float m, float s); + +/** Double precission uniform random variable */ +double randf(); + +/** Concat formatted string to an existing string. + * + * This function uses realloc() to resize the destination. + * Please make sure to only on dynamic allocated destionations!!! + * + * @param dest A pointer to a malloc() allocated memory region + * @param fmt A format string like for printf() + * @param ... Optional parameters like for printf() + * @retval The the new value of the dest buffer. + */ +char * strcatf(char **dest, const char *fmt, ...) + __attribute__ ((format(printf, 2, 3))); + +/** Variadic version of strcatf() */ +char * vstrcatf(char **dest, const char *fmt, va_list va) + __attribute__ ((format(printf, 2, 0))); + +char * strf(const char *fmt, ...); +char * vstrf(const char *fmt, va_list va); + +/** Allocate and initialize memory. */ +void * alloc(size_t bytes); + +/** Allocate and copy memory. */ +void * memdup(const void *src, size_t bytes); + +/** Call quit() in the main thread. */ +void die(); + +/** Get log2 of long long integers */ +static inline int log2i(long long x) { + if (x == 0) + return 1; + + return sizeof(x) * 8 - __builtin_clzll(x) - 1; +} + +/** Send signal \p sig to main thread. */ +void killme(int sig); + +pid_t spawn(const char *name, char *const argv[]); + +/** Determines the string length as printed on the screen (ignores escable sequences). */ +size_t strlenp(const char *str); + } // namespace utils } // namespace villas +using namespace villas::utils; diff --git a/common/lib/advio.cpp b/common/lib/advio.cpp index 2169003f8..9dd2491c1 100644 --- a/common/lib/advio.cpp +++ b/common/lib/advio.cpp @@ -32,7 +32,7 @@ #include -#include +#include #include #include #include diff --git a/common/lib/hist.cpp b/common/lib/hist.cpp index 9f93e93d7..dd7b9e014 100644 --- a/common/lib/hist.cpp +++ b/common/lib/hist.cpp @@ -27,7 +27,7 @@ #include #include -#include +#include #include #include #include diff --git a/common/lib/kernel/kernel.cpp b/common/lib/kernel/kernel.cpp index 28ee836e1..e94b6bec2 100644 --- a/common/lib/kernel/kernel.cpp +++ b/common/lib/kernel/kernel.cpp @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include diff --git a/common/lib/kernel/pci.cpp b/common/lib/kernel/pci.cpp index 6485b828c..61a3cc21b 100644 --- a/common/lib/kernel/pci.cpp +++ b/common/lib/kernel/pci.cpp @@ -27,7 +27,7 @@ #include #include -#include +#include #include #include diff --git a/common/lib/kernel/rt.cpp b/common/lib/kernel/rt.cpp index ede2171d0..f13cc95f2 100644 --- a/common/lib/kernel/rt.cpp +++ b/common/lib/kernel/rt.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include diff --git a/common/lib/kernel/vfio_legacy.cpp b/common/lib/kernel/vfio_legacy.cpp index d06c86efc..ea802feee 100644 --- a/common/lib/kernel/vfio_legacy.cpp +++ b/common/lib/kernel/vfio_legacy.cpp @@ -14,7 +14,7 @@ #include #include -#include +#include #include #include #include diff --git a/common/lib/list.cpp b/common/lib/list.cpp index d66520c33..32cab1e44 100644 --- a/common/lib/list.cpp +++ b/common/lib/list.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include /* Compare functions */ static int cmp_lookup(const void *a, const void *b) { diff --git a/common/lib/log_legacy.cpp b/common/lib/log_legacy.cpp index 9e0f42ed7..4b6e68902 100644 --- a/common/lib/log_legacy.cpp +++ b/common/lib/log_legacy.cpp @@ -24,7 +24,7 @@ #include #include -#include +#include #include #pragma GCC diagnostic push diff --git a/common/lib/table.cpp b/common/lib/table.cpp index 714713253..b39af26b1 100644 --- a/common/lib/table.cpp +++ b/common/lib/table.cpp @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include #include diff --git a/common/lib/task.cpp b/common/lib/task.cpp index eb71bdac3..2a4336400 100644 --- a/common/lib/task.cpp +++ b/common/lib/task.cpp @@ -24,7 +24,7 @@ #include #include -#include +#include #include #include diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index c7610f161..24569795a 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -37,16 +37,16 @@ #include #include -#include #include +#include #include #include +static pthread_t main_thread; + namespace villas { namespace utils { -static pthread_t main_thread; - std::vector tokenize(std::string s, std::string delimiter) { @@ -169,8 +169,6 @@ char * decolor(char *str) return str; } -extern "C" { - void killme(int sig) { /* Send only to main thread in case the ID was initilized by signals_init() */ @@ -180,11 +178,6 @@ void killme(int sig) kill(0, sig); } -} - -} /* namespace utils */ -} /* namespace villas */ - double box_muller(float m, float s) { double x1, x2, y1; @@ -217,14 +210,15 @@ double randf() return (double) random() / RAND_MAX; } -char * strcatf(char **dest, const char *fmt, ...) +void * alloc(size_t bytes) { - va_list ap; - va_start(ap, fmt); - vstrcatf(dest, fmt, ap); - va_end(ap); + void *p = malloc(bytes); + if (!p) + error("Failed to allocate memory"); - return *dest; + memset(p, 0, bytes); + + return p; } char * vstrcatf(char **dest, const char *fmt, va_list ap) @@ -242,6 +236,16 @@ char * vstrcatf(char **dest, const char *fmt, va_list ap) return *dest; } +char * strcatf(char **dest, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vstrcatf(dest, fmt, ap); + va_end(ap); + + return *dest; +} + char * strf(const char *fmt, ...) { char *buf = nullptr; @@ -263,17 +267,6 @@ char * vstrf(const char *fmt, va_list va) return buf; } -void * alloc(size_t bytes) -{ - void *p = malloc(bytes); - if (!p) - error("Failed to allocate memory"); - - memset(p, 0, bytes); - - return p; -} - void * memdup(const void *src, size_t bytes) { void *dst = alloc(bytes); @@ -334,3 +327,6 @@ size_t strlenp(const char *str) return sz; } + +} /* namespace utils */ +} /* namespace villas */ diff --git a/common/tests/unit/advio.cpp b/common/tests/unit/advio.cpp index 99b69225c..1a4197d46 100644 --- a/common/tests/unit/advio.cpp +++ b/common/tests/unit/advio.cpp @@ -24,7 +24,7 @@ #include -#include +#include #include #include diff --git a/common/tests/unit/hist.cpp b/common/tests/unit/hist.cpp index 89dc17ccc..541d55709 100644 --- a/common/tests/unit/hist.cpp +++ b/common/tests/unit/hist.cpp @@ -23,7 +23,7 @@ #include #include -#include +#include const double test_data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; diff --git a/common/tests/unit/list.cpp b/common/tests/unit/list.cpp index cf708ff80..8963e316a 100644 --- a/common/tests/unit/list.cpp +++ b/common/tests/unit/list.cpp @@ -24,7 +24,7 @@ #include #include -#include +#include #include static const char *nouns[] = { "time", "person", "year", "way", "day", "thing", "man", "world", "life", "hand", "part", "child", "eye", "woman", "place", "work", "week", "case", "point", "government", "company", "number", "group", "problem", "fact" }; diff --git a/common/tests/unit/tsc.cpp b/common/tests/unit/tsc.cpp index ff8a2d13b..4b6e908e7 100644 --- a/common/tests/unit/tsc.cpp +++ b/common/tests/unit/tsc.cpp @@ -22,7 +22,7 @@ #include -#include +#include #include #include diff --git a/common/tests/unit/utils.cpp b/common/tests/unit/utils.cpp index de03cdf2e..021e7a5e4 100644 --- a/common/tests/unit/utils.cpp +++ b/common/tests/unit/utils.cpp @@ -23,7 +23,6 @@ #include #include -#include #include #include #include