/** General purpose helper functions. * * @author Steffen Vogel * @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC * This file is part of S2SS. All Rights Reserved. Proprietary and confidential. * Unauthorized copying of this file, via any medium is strictly prohibited. *********************************************************************************/ #include #include #include #include #include #include #include #include #include "config.h" #include "cfg.h" #include "utils.h" int version_parse(const char *s, struct version *v) { return sscanf(s, "%u.%u", &v->major, &v->minor) != 2; } int version_compare(struct version *a, struct version *b) { int major = a->major - b->major; int minor = a->minor - b->minor; return major ? major : minor; } int strftimespec(char *s, uint max, const char *format, struct timespec *ts) { struct tm t; if (localtime_r(&(ts->tv_sec), &t) == NULL) return -1; return strftime(s, max, format, &t); } double box_muller(float m, float s) { double x1, x2, y1; static double y2; static int use_last = 0; if (use_last) { /* use value from previous call */ y1 = y2; use_last = 0; } else { double w; do { x1 = 2.0 * randf() - 1.0; x2 = 2.0 * randf() - 1.0; w = x1*x1 + x2*x2; } while (w >= 1.0); w = sqrt(-2.0 * log(w) / w); y1 = x1 * w; y2 = x2 * w; use_last = 1; } return m + y1 * s; } double randf() { return (double) random() / RAND_MAX; } /** This global variable holds the main thread ID. * Its used by die(). */ pthread_t _mtid; void die() { if (pthread_equal(_mtid, pthread_self())) exit(EXIT_FAILURE); else pthread_kill(_mtid, SIGINT); } char * strcatf(char **dest, const char *fmt, ...) { va_list ap; va_start(ap, fmt); vstrcatf(dest, fmt, ap); va_end(ap); return *dest; } char * vstrcatf(char **dest, const char *fmt, va_list ap) { char *tmp; int n = *dest ? strlen(*dest) : 0; int i = vasprintf(&tmp, fmt, ap); *dest = (char *)(realloc(*dest, n + i + 1)); if (*dest != NULL) strncpy(*dest+n, tmp, i + 1); free(tmp); return *dest; } cpu_set_t to_cpu_set(int set) { cpu_set_t cset; CPU_ZERO(&cset); for (int i = 0; i < sizeof(set) * 8; i++) { if (set & (1L << i)) CPU_SET(i, &cset); } return cset; } 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); memcpy(dst, src, bytes); return dst; }