Commit a4dc8b8c authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

use C++ compiler

parent a72a0e23
...@@ -121,7 +121,7 @@ int vlist_contains(struct vlist *l, void *p); ...@@ -121,7 +121,7 @@ int vlist_contains(struct vlist *l, void *p);
void vlist_sort(struct vlist *l, cmp_cb_t cmp); void vlist_sort(struct vlist *l, cmp_cb_t cmp);
/** Set single element in list */ /** Set single element in list */
int vlist_set(struct vlist *l, int index, void *value); int vlist_set(struct vlist *l, unsigned index, void *value);
/** Return index in list for value. /** Return index in list for value.
* *
......
...@@ -27,38 +27,72 @@ ...@@ -27,38 +27,72 @@
#pragma once #pragma once
#ifdef __cplusplus #include <vector>
extern "C" { #include <string>
#endif
struct table_column { class Table;
int width; /**< Width of the column. */
char *title; /**< The title as shown in the table header. */ class TableColumn {
char *format; /**< The format which is used to print the table rows. */
char *unit; /**< An optional unit which will be shown in the table header. */ friend Table;
enum { public:
TABLE_ALIGN_LEFT, enum align {
TABLE_ALIGN_RIGHT LEFT,
} align; RIGHT
};
protected:
int _width; /**< The real width of this column. Calculated by table_header() */ int _width; /**< The real width of this column. Calculated by table_header() */
int width; /**< Width of the column. */
public:
TableColumn(int w, enum align a, const std::string &t, const std::string &f, const std::string &u = "") :
width(w),
title(t),
format(f),
unit(u),
align(a)
{ }
std::string title; /**< The title as shown in the table header. */
std::string format; /**< The format which is used to print the table rows. */
std::string unit; /**< An optional unit which will be shown in the table header. */
enum align align;
int getWidth() const
{
return _width;
}
}; };
struct table { class Table {
int ncols;
protected:
int resize(int w);
int width; int width;
struct table_column *cols;
}; std::vector<TableColumn> columns;
public:
Table(const std::vector<TableColumn> &cols) :
width(-1),
columns(cols)
{ }
/** Print a table header consisting of \p n columns. */ /** Print a table header consisting of \p n columns. */
void table_header(struct table *t); void header();
/** Print table rows. */ /** Print table rows. */
void table_row(struct table *t, ...); void row(int count, ...);
/** @} */ int getWidth() const
{
return width;
}
};
#ifdef __cplusplus /** @} */
} \ No newline at end of file
#endif
...@@ -21,34 +21,33 @@ ...@@ -21,34 +21,33 @@
############################################################################## ##############################################################################
add_library(villas-common SHARED add_library(villas-common SHARED
advio.c advio.cpp
bitset.c bitset.cpp
buffer.c buffer.cpp
json_buffer.cpp json_buffer.cpp
compat.c compat.cpp
crypt.c crypt.cpp
hash_table.c hash_table.cpp
hist.c hist.cpp
kernel/kernel.c kernel/kernel.cpp
kernel/kernel.cpp kernel/kernel.cpp
kernel/rt.cpp kernel/rt.cpp
list.c list.cpp
log.cpp log.cpp
log_legacy.cpp log_legacy.cpp
memory.cpp memory.cpp
memory_manager.cpp memory_manager.cpp
plugin.cpp plugin.cpp
table.c table.cpp
task.c task.cpp
timing.c timing.cpp
utils.c
utils.cpp utils.cpp
cpuset.cpp cpuset.cpp
terminal.cpp terminal.cpp
version.cpp version.cpp
copyright.cpp copyright.cpp
common.c common.cpp
window.c window.cpp
) )
execute_process( execute_process(
...@@ -58,13 +57,14 @@ execute_process( ...@@ -58,13 +57,14 @@ execute_process(
) )
if(ARCH STREQUAL "x86_64") if(ARCH STREQUAL "x86_64")
target_sources(villas-common PRIVATE tsc.c) target_sources(villas-common PRIVATE tsc.cpp)
endif() endif()
if(CMAKE_SYSTEM_NAME STREQUAL Linux) if(CMAKE_SYSTEM_NAME STREQUAL Linux)
target_sources(villas-common PRIVATE target_sources(villas-common PRIVATE
kernel/pci.c kernel/pci.cpp
kernel/vfio.cpp kernel/vfio.cpp
# kernel/vfio_legacy.cpp
) )
endif() endif()
......
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
#define BAR_WIDTH 60 /**< How wide you want the progress meter to be. */ #define BAR_WIDTH 60 /**< How wide you want the progress meter to be. */
static int advio_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp) static int advio_trace(CURL * /* handle */, curl_infotype type, char *data, size_t size, void * /* userp */)
{ {
const char *text; const char *text;
...@@ -83,7 +83,7 @@ static int advio_trace(CURL *handle, curl_infotype type, char *data, size_t size ...@@ -83,7 +83,7 @@ static int advio_trace(CURL *handle, curl_infotype type, char *data, size_t size
static char * advio_human_time(double t, char *buf, size_t len) static char * advio_human_time(double t, char *buf, size_t len)
{ {
int i = 0; unsigned i = 0;
const char *units[] = { "secs", "mins", "hrs", "days", "weeks", "months", "years" }; const char *units[] = { "secs", "mins", "hrs", "days", "weeks", "months", "years" };
int divs[] = { 60, 60, 24, 7, 4, 12 }; int divs[] = { 60, 60, 24, 7, 4, 12 };
...@@ -99,7 +99,7 @@ static char * advio_human_time(double t, char *buf, size_t len) ...@@ -99,7 +99,7 @@ static char * advio_human_time(double t, char *buf, size_t len)
static char * advio_human_size(double s, char *buf, size_t len) static char * advio_human_size(double s, char *buf, size_t len)
{ {
int i = 0; unsigned i = 0;
const char *units[] = { "B", "kiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" }; const char *units[] = { "B", "kiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" };
while (s > 1024 && i < ARRAY_LEN(units)) { while (s > 1024 && i < ARRAY_LEN(units)) {
...@@ -180,14 +180,14 @@ static int advio_xferinfo(void *p, curl_off_t dl_total_bytes, curl_off_t dl_byte ...@@ -180,14 +180,14 @@ static int advio_xferinfo(void *p, curl_off_t dl_total_bytes, curl_off_t dl_byte
int aislocal(const char *uri) int aislocal(const char *uri)
{ {
char *sep; const char *sep;
const char *supported_schemas[] = { "file", "http", "https", "tftp", "ftp", "scp", "sftp", "smb", "smbs" }; const char *supported_schemas[] = { "file", "http", "https", "tftp", "ftp", "scp", "sftp", "smb", "smbs" };
sep = strstr(uri, "://"); sep = strstr(uri, "://");
if (!sep) if (!sep)
return 1; /* no schema, we assume its a local file */ return 1; /* no schema, we assume its a local file */
for (int i = 0; i < ARRAY_LEN(supported_schemas); i++) { for (unsigned i = 0; i < ARRAY_LEN(supported_schemas); i++) {
if (!strncmp(supported_schemas[i], uri, sep - uri)) if (!strncmp(supported_schemas[i], uri, sep - uri))
return 0; return 0;
} }
...@@ -198,9 +198,10 @@ int aislocal(const char *uri) ...@@ -198,9 +198,10 @@ int aislocal(const char *uri)
AFILE * afopen(const char *uri, const char *mode) AFILE * afopen(const char *uri, const char *mode)
{ {
int ret; int ret;
char *sep, *cwd; char *cwd;
const char *sep;
AFILE *af = alloc(sizeof(AFILE)); AFILE *af = (AFILE *) alloc(sizeof(AFILE));
snprintf(af->mode, sizeof(af->mode), "%s", mode); snprintf(af->mode, sizeof(af->mode), "%s", mode);
...@@ -290,29 +291,29 @@ int afclose(AFILE *af) ...@@ -290,29 +291,29 @@ int afclose(AFILE *af)
int afseek(AFILE *af, long offset, int origin) int afseek(AFILE *af, long offset, int origin)
{ {
long new, cur = aftell(af); long new_seek, cur_seek = aftell(af);
switch (origin) { switch (origin) {
case SEEK_SET: case SEEK_SET:
new = offset; new_seek = offset;
break; break;
case SEEK_END: case SEEK_END:
fseek(af->file, 0, SEEK_END); fseek(af->file, 0, SEEK_END);
new = aftell(af); new_seek = aftell(af);
fseek(af->file, cur, SEEK_SET); fseek(af->file, cur_seek, SEEK_SET);
break; break;
case SEEK_CUR: case SEEK_CUR:
new = cur + offset; new_seek = cur_seek + offset;
break; break;
default: default:
return -1; return -1;
} }
if (new < af->uploaded) if (new_seek < af->uploaded)
af->uploaded = new; af->uploaded = new_seek;
return fseek(af->file, offset, origin); return fseek(af->file, offset, origin);
} }
...@@ -403,6 +404,7 @@ int adownload(AFILE *af, int resume) ...@@ -403,6 +404,7 @@ int adownload(AFILE *af, int resume)
double total_bytes = 0, total_time = 0; double total_bytes = 0, total_time = 0;
char buf[2][32]; char buf[2][32];
char *total_bytes_human, *total_time_human;
pos = aftell(af); pos = aftell(af);
...@@ -428,8 +430,8 @@ int adownload(AFILE *af, int resume) ...@@ -428,8 +430,8 @@ int adownload(AFILE *af, int resume)
curl_easy_getinfo(af->curl, CURLINFO_SIZE_DOWNLOAD, &total_bytes); curl_easy_getinfo(af->curl, CURLINFO_SIZE_DOWNLOAD, &total_bytes);
curl_easy_getinfo(af->curl, CURLINFO_TOTAL_TIME, &total_time); curl_easy_getinfo(af->curl, CURLINFO_TOTAL_TIME, &total_time);
char *total_bytes_human = advio_human_size(total_bytes, buf[0], sizeof(buf[0])); total_bytes_human = advio_human_size(total_bytes, buf[0], sizeof(buf[0]));
char *total_time_human = advio_human_time(total_time, buf[1], sizeof(buf[1])); total_time_human = advio_human_time(total_time, buf[1], sizeof(buf[1]));
info("Finished download of %s in %s", total_bytes_human, total_time_human); info("Finished download of %s in %s", total_bytes_human, total_time_human);
...@@ -460,6 +462,7 @@ int adownload(AFILE *af, int resume) ...@@ -460,6 +462,7 @@ int adownload(AFILE *af, int resume)
af->file = fopen(af->uri, af->mode); af->file = fopen(af->uri, af->mode);
if (!af->file) if (!af->file)
return -1; return -1;
break;
default: default:
error("Failed to download file: %s: %s", af->uri, curl_easy_strerror(res)); error("Failed to download file: %s: %s", af->uri, curl_easy_strerror(res));
......
...@@ -34,7 +34,7 @@ int bitset_init(struct bitset *b, size_t dim) ...@@ -34,7 +34,7 @@ int bitset_init(struct bitset *b, size_t dim)
{ {
int s = bitset_nslots(dim); int s = bitset_nslots(dim);
b->set = alloc(s * CHAR_BIT); b->set = (char *) alloc(s * CHAR_BIT);
b->dimension = dim; b->dimension = dim;
return 0; return 0;
...@@ -163,7 +163,7 @@ char * bitset_dump(struct bitset *b) ...@@ -163,7 +163,7 @@ char * bitset_dump(struct bitset *b)
{ {
char *str = NULL; char *str = NULL;
for (int i = 0; i < b->dimension; i++) for (unsigned i = 0; i < b->dimension; i++)
strcatf(&str, "%d", bitset_test(b, i)); strcatf(&str, "%d", bitset_test(b, i));
return str; return str;
......
...@@ -30,7 +30,7 @@ int buffer_init(struct buffer *b, size_t size) ...@@ -30,7 +30,7 @@ int buffer_init(struct buffer *b, size_t size)
{ {
b->len = 0; b->len = 0;
b->size = size; b->size = size;
b->buf = malloc(size); b->buf = (char *) malloc(size);
if (!b->buf) if (!b->buf)
return -1; return -1;
...@@ -58,7 +58,7 @@ int buffer_append(struct buffer *b, const char *data, size_t len) ...@@ -58,7 +58,7 @@ int buffer_append(struct buffer *b, const char *data, size_t len)
{ {
if (b->len + len > b->size) { if (b->len + len > b->size) {
b->size = b->len + len; b->size = b->len + len;
b->buf = realloc(b->buf, b->size); b->buf = (char *) realloc(b->buf, b->size);
if (!b->buf) if (!b->buf)
return -1; return -1;
} }
...@@ -85,7 +85,7 @@ int buffer_append_json(struct buffer *b, json_t *j) ...@@ -85,7 +85,7 @@ int buffer_append_json(struct buffer *b, json_t *j)
retry: len = json_dumpb(j, b->buf + b->len, b->size - b->len, 0); retry: len = json_dumpb(j, b->buf + b->len, b->size - b->len, 0);
if (b->size < b->len + len) { if (b->size < b->len + len) {
b->buf = realloc(b->buf, b->len + len); b->buf = (char *) realloc(b->buf, b->len + len);
if (!b->buf) if (!b->buf)
return -1; return -1;
......
...@@ -43,7 +43,7 @@ int hash_table_init(struct hash_table *ht, size_t size) ...@@ -43,7 +43,7 @@ int hash_table_init(struct hash_table *ht, size_t size)
if (ret) if (ret)
return ret; return ret;
ht->table = alloc(len); ht->table = (struct hash_table_entry **) alloc(len);
memset(ht->table, 0, len); memset(ht->table, 0, len);
...@@ -62,7 +62,7 @@ int hash_table_destroy(struct hash_table *ht, dtor_cb_t dtor, bool release) ...@@ -62,7 +62,7 @@ int hash_table_destroy(struct hash_table *ht, dtor_cb_t dtor, bool release)
pthread_mutex_lock(&ht->lock); pthread_mutex_lock(&ht->lock);
for (int i = 0; i < ht->size; i++) { for (unsigned i = 0; i < ht->size; i++) {
for (cur = ht->table[i]; cur; cur = next) { for (cur = ht->table[i]; cur; cur = next) {
if (dtor) if (dtor)
dtor(cur->data); dtor(cur->data);
...@@ -106,7 +106,7 @@ int hash_table_insert(struct hash_table *ht, const void *key, void *data) ...@@ -106,7 +106,7 @@ int hash_table_insert(struct hash_table *ht, const void *key, void *data)
if (cur) if (cur)
ret = -1; ret = -1;
else { else {
hte = alloc(sizeof(struct hash_table_entry)); hte = (struct hash_table_entry *) alloc(sizeof(struct hash_table_entry));
if (hte) { if (hte) {
hte->key = key; hte->key = key;
hte->data = data; hte->data = data;
...@@ -191,7 +191,7 @@ void hash_table_dump(struct hash_table *ht) ...@@ -191,7 +191,7 @@ void hash_table_dump(struct hash_table *ht)
pthread_mutex_lock(&ht->lock); pthread_mutex_lock(&ht->lock);
for (int i = 0; i < ht->size; i++) { for (unsigned i = 0; i < ht->size; i++) {
char *strlst = NULL; char *strlst = NULL;
for (hte = ht->table[i]; hte; hte = hte->next) for (hte = ht->table[i]; hte; hte = hte->next)
......
...@@ -28,9 +28,9 @@ ...@@ -28,9 +28,9 @@
#include <time.h> #include <time.h>
#include <villas/utils.h> #include <villas/utils.h>
#include <villas/hist.h> #include <villas/hist.hpp>
#include <villas/config.h> #include <villas/config.h>
#include <villas/table.h> #include <villas/table.hpp>
#define VAL(h, i) ((h)->low + (i) * (h)->resolution) #define VAL(h, i) ((h)->low + (i) * (h)->resolution)
#define INDEX(h, v) round((v - (h)->low) / (h)->resolution) #define INDEX(h, v) round((v - (h)->low) / (h)->resolution)
...@@ -40,7 +40,7 @@ int hist_init(struct hist *h, int buckets, hist_cnt_t warmup) ...@@ -40,7 +40,7 @@ int hist_init(struct hist *h, int buckets, hist_cnt_t warmup)
h->length = buckets; h->length = buckets;
h->warmup = warmup; h->warmup = warmup;
h->data = buckets ? alloc(h->length * sizeof(hist_cnt_t)) : NULL; h->data = (hist_cnt_t *) (buckets ? alloc(h->length * sizeof(hist_cnt_t)) : nullptr);
hist_reset(h); hist_reset(h);
...@@ -51,7 +51,7 @@ int hist_destroy(struct hist *h) ...@@ -51,7 +51,7 @@ int hist_destroy(struct hist *h)
{ {
if (h->data) { if (h->data) {
free(h->data); free(h->data);
h->data = NULL; h->data = nullptr;
} }
return 0; return 0;
...@@ -83,7 +83,7 @@ void hist_put(struct hist *h, double value) ...@@ -83,7 +83,7 @@ void hist_put(struct hist *h, double value)
h->higher++; h->higher++;
else if (idx < 0) else if (idx < 0)
h->lower++; h->lower++;
else if (h->data != NULL) else if (h->data != nullptr)
h->data[idx]++; h->data[idx]++;
} }
...@@ -169,30 +169,27 @@ void hist_plot(const struct hist *h) ...@@ -169,30 +169,27 @@ void hist_plot(const struct hist *h)
max = h->data[i]; max = h->data[i];
} }
struct table_column cols[] = { std::vector<TableColumn> cols = {
{ -9, "Value", "%+9.3g", NULL, TABLE_ALIGN_RIGHT }, { -9, TableColumn::align::RIGHT, "Value", "%+9.3g" },
{ -6, "Count", "%6ju", NULL, TABLE_ALIGN_RIGHT }, { -6, TableColumn::align::RIGHT, "Count", "%6ju" },
{ 0, "Plot", "%s", "occurences", TABLE_ALIGN_LEFT } { 0, TableColumn::align::LEFT, "Plot", "%s", "occurences" }
}; };
struct table table = { Table table = Table(cols);
.ncols = ARRAY_LEN(cols),
.cols = cols
};
/* Print plot */ /* Print plot */
table_header(&table); table.header();
for (int i = 0; i < h->length; i++) { for (int i = 0; i < h->length; i++) {
double value = VAL(h, i); double value = VAL(h, i);
hist_cnt_t cnt = h->data[i]; hist_cnt_t cnt = h->data[i];
int bar = cols[2]._width * ((double) cnt / max); int bar = cols[2].getWidth() * ((double) cnt / max);
char *buf = strf("%s", ""); char *buf = strf("%s", "");
for (int i = 0; i < bar; i++) for (int i = 0; i < bar; i++)
buf = strcatf(&buf, "\u2588"); buf = strcatf(&buf, "\u2588");
table_row(&table, value, cnt, buf); table.row(value, cnt, buf);
free(buf); free(buf);