summaryrefslogtreecommitdiff
path: root/utils.h
blob: 52a1078eaa218dc142cfe7dc1ba69ee390f044e9 (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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef fooutilshfoo
#define fooutilshfoo

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

struct list_head {
	struct list_head *next;
	struct list_head *prev;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)

static inline void list_init(struct list_head *list)
{
	list->next = list;
	list->prev = list;
}

static inline void list_del(struct list_head *list)
{
	list->next->prev = list->prev;
	list->prev->next = list->next;
}

static inline void list_add(struct list_head *new, struct list_head *list)
{
	list->next->prev = new;
	new->next = list->next;
	new->prev = list;
	list->next = new;
}

static inline void list_replace(struct list_head *old, struct list_head *new)
{
	old->prev->next = new;
	old->next->prev = new;
	new->next = old->next;
	new->prev = old->prev;
	old->next = old->prev = NULL;
}

static inline bool list_empty(struct list_head *list)
{
	return list->next == list;
}

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

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

#define list_entry(ptr, type, member)					\
	container_of(ptr, type, member)

#define list_first_entry(ptr, type, member)				\
	list_entry((ptr)->next, type, member)

#define list_next_entry(pos, member)					\
	list_entry((pos)->member.next, typeof(*(pos)), member)

#define list_for_each(pos, head)					\
	for (pos = (head)->next; pos != (head); pos = pos->next)

#define list_for_each_entry(pos, head, member)				\
	for (pos = list_first_entry(head, typeof(*pos), member);	\
	     &pos->member != (head);					\
	     pos = list_next_entry(pos, member))

#define list_for_each_entry_safe(pos, n, head, member)			\
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
	     n = list_entry(pos->member.next, typeof(*pos), member);	\
	     &pos->member != (head);					\
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))

#define zmalloc(s) __zmalloc(__func__, __LINE__, s)
void *__zmalloc(const char *fn, int line, size_t s);

#define xstrdup(s) __xstrdup(__func__, __LINE__, s)
char *__xstrdup(const char *fn, int line, const char *s);

#define xstrndup(s, n) __xstrndup(__func__, __LINE__, s, n)
char *__xstrndup(const char *fn, int line, const char *s, size_t n);

#define xfree(s) __xfree(__func__, __LINE__, s)
void __xfree(const char *fn, int line, void *ptr);

void debug_resource_usage();

#define ADDRSTRLEN (9 /*strlen("AF_INETX ")*/ + INET6_ADDRSTRLEN + 6 /*strlen(" 65535")*/ + 1)
struct sockaddr_in46 {
	union {
		struct sockaddr_storage storage;
		struct sockaddr_in in4;
		struct sockaddr_in6 in6;
	};
	socklen_t addrlen;
	struct list_head list;
};

uint16_t sockaddr_port(struct sockaddr_in46 *addr);

char *sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t buflen);

int strtou16_strict(const char *str, uint16_t *result);

static inline bool empty_str(const char *str)
{
	if (!str || str[0] == '\0')
		return true;
	else
		return false;
}

/*
#define _cleanup_(x) __attribute__((cleanup(x)))

#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func)				\
	static inline void func##p(type *p) {				\
		if (*p)							\
			func(*p);					\
	}								\
	struct __useless_struct_to_allow_trailing_semicolon__

static inline unsigned
strv_length(char **strv)
{
	unsigned len;

	for (len = 0; strv && *strv; strv++)
		len++;

	return len;
}

static inline void strv_free(char **l) {
	char **k;
	if (l) {
		for (k = l; *k; k++)
			free(k);
		free(l);
	}
}
DEFINE_TRIVIAL_CLEANUP_FUNC(char **, strv_free);
#define _cleanup_strv_free_ _cleanup_(strv_freep)

static inline void freep(void *p) {
	free(*(void**) p);
}
#define _cleanup_free_ _cleanup_(freep)

DEFINE_TRIVIAL_CLEANUP_FUNC(int, close);
#define _cleanup_close_ _cleanup_(closep)

DEFINE_TRIVIAL_CLEANUP_FUNC(FILE *, fclose);
#define _cleanup_fclose_ _cleanup_(fclosep)

DEFINE_TRIVIAL_CLEANUP_FUNC(DIR *, closedir);
#define _cleanup_closedir_ _cleanup_(closedirp)

*/

#endif