From 5de227689402736235f6bdb932fa9ad5f939fcb8 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Wed, 23 Nov 2016 19:19:20 -0800 Subject: util: put convenience helpers into util.[ch] Also introduces get_ncpus(), in preparation for threaded rendering. --- util.c | 22 ++++++++++++++++++++++ util.h | 26 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 util.c create mode 100644 util.h diff --git a/util.c b/util.c new file mode 100644 index 0000000..c07589e --- /dev/null +++ b/util.c @@ -0,0 +1,22 @@ +#include +#include +#include + +#include "util.h" + +#define SYSFS_CPU "/sys/devices/system/cpu/cpu" +#define MAXCPUS 1024 + +unsigned get_ncpus(void) +{ + char path[cstrlen(SYSFS_CPU "1024") + 1]; + unsigned n; + + for (n = 0; n < MAXCPUS; n++) { + snprintf(path, sizeof(path), "%s%u", SYSFS_CPU, n); + if (access(path, F_OK) == -1) + break; + } + + return n == 0 ? 1 : n; +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..c82b623 --- /dev/null +++ b/util.h @@ -0,0 +1,26 @@ +#ifndef _UTIL_H +#define _UTIL_H + +#include +#include +#include +#include + +#define exit_if(_cond, _fmt, ...) \ + if (_cond) { \ + fprintf(stderr, "Fatal error: " _fmt "\n", ##__VA_ARGS__); \ + exit(EXIT_FAILURE); \ + } + +#define pexit_if(_cond, _fmt, ...) \ + exit_if(_cond, _fmt ": %s", ##__VA_ARGS__, strerror(errno)) + +#define nelems(_array) \ + (sizeof(_array) / sizeof(_array[0])) + +#define cstrlen(_str) \ + (sizeof(_str) - 1) + +unsigned get_ncpus(void); + +#endif /* _UTIL_H */ -- cgit v1.2.3