summaryrefslogtreecommitdiff
path: root/utils.h
blob: 62805b4df5bec4669a0a528a1a1fbc646a2de7a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <time.h>
#include <inttypes.h>

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))

#define IP(a,b,c,d) ((a)*0x1000000u+(b)*0x10000u+(c)*0x100u+(d)*1u)

void set_nonblocking(int sock);
void set_nodelay(int fd);
void write_all(int fd, const void *buf, size_t buf_size);

static inline uint64_t
get_time_us(void)
{
	struct timespec ts;
	clock_gettime(CLOCK_MONOTONIC, &ts);
	return (uint64_t) ts.tv_sec * 1000000u + ts.tv_nsec / 1000u;
}

typedef struct {
	const char *suffix;
	double scale;
} unit;

int parse_value(const char *s, int min, int max, const unit *units);