File: useful.h

package info (click to toggle)
iwd 3.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,176 kB
  • sloc: ansic: 140,705; sh: 5,514; makefile: 714
file content (89 lines) | stat: -rw-r--r-- 1,979 bytes parent folder | download | duplicates (10)
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
/*
 * Embedded Linux library
 * Copyright (C) 2021  Intel Corporation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#define align_len(len, boundary) (((len)+(boundary)-1) & ~((boundary)-1))

#define likely(x)   __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

#define SWAP(l, r) \
	do { typeof(l) __tmp = (l); (l) = (r); (r) = __tmp; } while (0)

#ifndef __always_inline
#define __always_inline inline __attribute__((always_inline))
#endif

static inline size_t minsize(size_t a, size_t b)
{
	if (a <= b)
		return a;

	return b;
}

static inline size_t maxsize(size_t a, size_t b)
{
	if (a >= b)
		return a;

	return b;
}

static inline void set_bit(void *addr, unsigned int bit)
{
	unsigned char *field = addr;
	field[bit / 8] |= 1U << (bit % 8);
}

static inline int test_bit(const void *addr, unsigned int bit)
{
	const unsigned char *field = addr;
	return (field[bit / 8] & (1U << (bit % 8))) != 0;
}

static inline unsigned char bit_field(const unsigned char oct,
					unsigned int start, unsigned int n_bits)
{
	unsigned char mask = (1U << n_bits) - 1U;
	return (oct >> start) & mask;
}

/* Must be called with n >= 2 and n <= ULONG_MAX / 2 + 1 */
static inline unsigned long roundup_pow_of_two(unsigned long n)
{
	return 1UL << (sizeof(unsigned long) * 8 - __builtin_clzl(n - 1));
}

#define DIV_ROUND_CLOSEST(x, divisor)			\
({							\
	typeof(divisor) _d = (divisor);			\
	typeof(x) _x = (x) + _d / 2;			\
	_x / _d;					\
})

#ifndef _auto_
#define _auto_(func)					\
	__L_AUTODESTRUCT(func)
#endif

/*
 * Trick the compiler into thinking that var might be changed somehow by
 * the asm
 */
#define DO_NOT_OPTIMIZE(var) \
	__asm__ ("" : "=r" (var) : "0" (var));

static inline int secure_select(int select_left, int l, int r)
{
	int mask = -(!!select_left);

	return r ^ ((l ^ r) & mask);
}

#define struct_alloc(structname, ...) \
	(struct structname *) l_memdup(&(struct structname) { __VA_ARGS__ }, \
					sizeof(struct structname))