/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See the file COPYING * * 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. * * Copyright (C) 2017 Frank van Maarseveen */ #ifndef _COMMON_H_ #define _COMMON_H_ #include #include #include typedef enum { false, true } bool_t; #define ARRAYSIZE(x) (sizeof (x) / sizeof ((x)[0])) /* * Cast a member of a structure out to the containing structure * * ptr: the pointer to the member. * type: the type of the container struct this is embedded in. * member: the name of the member within the struct. * * Example: * * struct foo { * int i; * struct timer t; * }; * * void f(struct timer *p) * { * // when f() is called with the address of member * // `t' of a struct foo then the following is permitted: * struct foo *mydata = container_of(p, struct foo, t); * } * */ #define container_of(ptr, type, member) \ ((type *)( (char *)(ptr) - offsetof(type, member) )) extern const char * me; extern int opt_v; extern const struct timespec TS_TIMESPEC_ZERO; void out(const char *fmt, ...) __attribute__((format(printf, 1, 2))); void err(const char *fmt, ...) __attribute__((format(printf, 1, 2))); void die(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn)); void * new(size_t size); char * newstr(const char *s); char * strip(char *p); const char * time2str(time_t t); struct timespec ts_get(const struct timespec *epoch); void ts_add(struct timespec *t1, const struct timespec *t2); void ts_addns(struct timespec *t, long ns); void ts_sub(struct timespec *t1, const struct timespec *t2); bool_t ts_after(const struct timespec *t1, const struct timespec *t2); static inline bool_t ts_isset(const struct timespec *t) { return t->tv_sec || t->tv_nsec; } static inline bool_t ts_after_or_equal(const struct timespec *t1, const struct timespec *t2) { return !ts_after(t2, t1); } static inline bool_t ts_before(const struct timespec *t1, const struct timespec *t2) { return ts_after(t2, t1); } static inline bool_t ts_before_or_equal(const struct timespec *t1, const struct timespec *t2) { return !ts_after(t1, t2); } #endif