|
|
1.1 root 1: /*
2: * Copyright (c) 2009, 2010, 2013 Richard Braun.
3: *
4: * This program is free software: you can redistribute it and/or modify
5: * it under the terms of the GNU General Public License as published by
6: * the Free Software Foundation, either version 3 of the License, or
7: * (at your option) any later version.
8: *
9: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program. If not, see <http://www.gnu.org/licenses/>.
16: *
17: *
18: * Helper macros.
19: */
20:
21: #ifndef _KERN_MACROS_H
22: #define _KERN_MACROS_H
23:
24: #define MACRO_BEGIN ({
25: #define MACRO_END })
26: #define MACRO_RETURN if (1) return
27:
28: #define __QUOTE(x) #x
29: #define QUOTE(x) __QUOTE(x)
30:
31: #ifdef __ASSEMBLER__
32: #define DECL_CONST(x, s) x
33: #else /* __ASSEMBLER__ */
34: #define __DECL_CONST(x, s) x##s
35: #define DECL_CONST(x, s) __DECL_CONST(x, s)
36: #endif /* __ASSEMBLER__ */
37:
38: #define STRLEN(x) (sizeof(x) - 1)
39: #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
40:
41: #define MIN(a, b) ((a) < (b) ? (a) : (b))
42: #define MAX(a, b) ((a) > (b) ? (a) : (b))
43:
44: #define DIV_CEIL(n, d) (((n) + (d) - 1) / (d))
45:
46: #define P2ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
47: #define ISP2(x) P2ALIGNED(x, x)
48: #define P2ALIGN(x, a) ((x) & -(a))
49: #define P2ROUND(x, a) (-(-(x) & -(a)))
50: #define P2END(x, a) (-(~(x) & -(a)))
51:
52: #define structof(ptr, type, member) \
53: ((type *)((char *)(ptr) - offsetof(type, member)))
54:
55: #define access_once(x) (*(volatile typeof(x) *)&(x))
56:
57: #define alignof(x) __alignof__(x)
58:
59: #ifndef likely
60: #define likely(expr) __builtin_expect(!!(expr), 1)
61: #endif /* likely */
62: #ifndef unlikely
63: #define unlikely(expr) __builtin_expect(!!(expr), 0)
64: #endif /* unlikely */
65:
66: #ifndef barrier
67: #define barrier() asm volatile("" : : : "memory")
68: #endif /* barrier */
69:
70: #define __noreturn __attribute__((noreturn))
71: #define __aligned(x) __attribute__((aligned(x)))
72: #define __always_inline inline __attribute__((always_inline))
73: #ifndef __section
74: #define __section(x) __attribute__((section(x)))
75: #endif /* __section */
76: #define __packed __attribute__((packed))
77: #define __alias(x) __attribute__((alias(x)))
78:
79: #define __format_printf(fmt, args) \
80: __attribute__((format(printf, fmt, args)))
81:
82: #endif /* _KERN_MACROS_H */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.