kernel
Loading...
Searching...
No Matches
macros.h
Go to the documentation of this file.
1#ifndef X86_MATANEL_MACROS_H
2#define X86_MATANEL_MACROS_H
3#include <stdint.h>
4
10#ifndef CONTAINING_RECORD
11#define CONTAINING_RECORD(ptr, type, member) \
12 ((type *)((char *)(ptr) - offsetof(type, member)))
13#endif
14
15#undef SIZE_T_MAX
16#ifndef SIZE_T_MAX
17#define SIZE_T_MAX (size_t)-1
18#endif
19
20#undef UINT64_T_MAX
21#ifndef UINT64_T_MAX
22#define UINT64_T_MAX (uint64_t)-1
23#endif
24
25extern uint8_t kernel_start;
26#define LK_KERNEL_START &kernel_start
27
28extern uint8_t kernel_end;
29#define LK_KERNEL_END &kernel_end
30
31#define LK_KERNEL_SIZE (LK_KERNEL_END - LK_KERNEL_START)
32
33#ifndef _MSC_VER
34#define MAX(a, b) ({ \
35 __typeof__(a) _a = (a); \
36 __typeof__(b) _b = (b); \
37 _a > _b ? _a : _b; \
38})
39
40#define MIN(a, b) ({ \
41 __typeof__(a) _a = (a); \
42 __typeof__(b) _b = (b); \
43 _a > _b ? _b : _a; \
44})
45#else
46#define MAX(a,b) (0)
47#define MIN(a,b) (0)
48#endif
49
50#if defined __GNUC__
51#define RETADDR(level) __builtin_return_address(level)
52#else
53#define RETADDR(level) (void)(level)
54#endif
55
56// 'likely' hints to the compiler that the condition is expected to be true most of the time.
57// It allows the compiler to optimize branch prediction.
58#define likely(x) __builtin_expect(!!(x), 1)
59
60// 'unlikely' hints to the compiler that the condition is expected to be false most of the time.
61// Useful for error handling or rare cases.
62#define unlikely(x) __builtin_expect(!!(x), 0)
63
64#define FREEZE() __cli(); __hlt()
65
66#if !defined(__GNUC__)
67#define FIELD_OFFSET(t,f) ((uint32_t)(uint32_t*)&(((t*) 0)->f))
68#else
69#define FIELD_OFFSET(t,f) ((uint32_t)__builtin_offsetof(t,f))
70#endif
71
72#endif
uint8_t kernel_start
uint8_t kernel_end