File: common.h

package info (click to toggle)
netconsd 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 344 kB
  • sloc: ansic: 2,650; cpp: 111; python: 107; makefile: 98
file content (81 lines) | stat: -rw-r--r-- 1,780 bytes parent folder | download
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

#ifndef __COMMON_H__
#define __COMMON_H__

#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "log.h"
#include "jhash.h"

#define min(x, y) ({							\
	typeof(x) _min1 = (x);						\
	typeof(y) _min2 = (y);						\
	(void) (&_min1 == &_min2);					\
	_min1 < _min2 ? _min1 : _min2; })

#define max(x, y) ({							\
	typeof(x) _max1 = (x);						\
	typeof(y) _max2 = (y);						\
	(void) (&_max1 == &_max2);					\
	_max1 > _max2 ? _max1 : _max2; })

#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)

#define container_of(ptr, type, member) ({			\
	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
	(type *)( (char *)__mptr - __builtin_offsetof(type,member) );})

static inline void *zalloc(size_t n)
{
	return calloc(1, n);
}

#define assert_pthread_mutex_locked(m)					\
do {									\
	fatal_on(pthread_mutex_trylock(m) != EBUSY, "UNLOCKED!\n");	\
} while (0)

static inline uint64_t now_ms(clockid_t clock)
{
	struct timespec t;
	int ret;

	ret = clock_gettime(clock, &t);
	fatal_on(ret, "Oops, clock_gettime() barfed: %m (-%d)\n", errno);

	return t.tv_sec * 1000LL + t.tv_nsec / 1000000L;
}

static inline uint64_t now_mono_ms(void)
{
	return now_ms(CLOCK_MONOTONIC);
}

static inline uint64_t now_real_ms(void)
{
	return now_ms(CLOCK_REALTIME);
}

struct netconsd_params {
	int nr_workers;
	int nr_listeners;
	int mmsg_batch;
	unsigned int gc_int_ms;
	unsigned int gc_age_ms;
	struct sockaddr_in6 listen_addr;
};

#endif /* __COMMON_H__ */