| 1 |
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ |
| 2 |
|
| 3 |
#ifndef __LINUX_KERNEL_H |
| 4 |
#define __LINUX_KERNEL_H |
| 5 |
|
| 6 |
#include <linux/compiler.h> |
| 7 |
|
| 8 |
#ifndef offsetof |
| 9 |
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| 10 |
#endif |
| 11 |
|
| 12 |
#ifndef container_of |
| 13 |
#define container_of(ptr, type, member) ({ \ |
| 14 |
const typeof(((type *)0)->member) * __mptr = (ptr); \ |
| 15 |
(type *)((char *)__mptr - offsetof(type, member)); }) |
| 16 |
#endif |
| 17 |
|
| 18 |
#ifndef max |
| 19 |
#define max(x, y) ({ \ |
| 20 |
typeof(x) _max1 = (x); \ |
| 21 |
typeof(y) _max2 = (y); \ |
| 22 |
(void) (&_max1 == &_max2); \ |
| 23 |
_max1 > _max2 ? _max1 : _max2; }) |
| 24 |
#endif |
| 25 |
|
| 26 |
#ifndef min |
| 27 |
#define min(x, y) ({ \ |
| 28 |
typeof(x) _min1 = (x); \ |
| 29 |
typeof(y) _min2 = (y); \ |
| 30 |
(void) (&_min1 == &_min2); \ |
| 31 |
_min1 < _min2 ? _min1 : _min2; }) |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifndef roundup |
| 35 |
#define roundup(x, y) ( \ |
| 36 |
{ \ |
| 37 |
const typeof(y) __y = y; \ |
| 38 |
(((x) + (__y - 1)) / __y) * __y; \ |
| 39 |
} \ |
| 40 |
) |
| 41 |
#endif |
| 42 |
|
| 43 |
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) |
| 44 |
#define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
| 45 |
|
| 46 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 47 |
#define be32_to_cpu(x) __builtin_bswap32(x) |
| 48 |
#define cpu_to_be32(x) __builtin_bswap32(x) |
| 49 |
#define be64_to_cpu(x) __builtin_bswap64(x) |
| 50 |
#define cpu_to_be64(x) __builtin_bswap64(x) |
| 51 |
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 52 |
#define be32_to_cpu(x) (x) |
| 53 |
#define cpu_to_be32(x) (x) |
| 54 |
#define be64_to_cpu(x) (x) |
| 55 |
#define cpu_to_be64(x) (x) |
| 56 |
#else |
| 57 |
# error "__BYTE_ORDER__ undefined or invalid" |
| 58 |
#endif |
| 59 |
|
| 60 |
#endif |