summaryrefslogtreecommitdiffstats
path: root/src/base/base_core.h
blob: d2740d9ad9427b6c415a5bcdae1361994174c3c6 (plain) (blame)
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
#ifndef BASE_CORE_H
#define BASE_CORE_H

//- compiler definitions
#if defined(_MSC_VER)
#    define COMPILER_MSVC 1
#elif defined(__clang__)
#    define COMPILER_CLANG 1
#elif defined(__GNUC__)
#    define COMPILER_GCC 1
#else
#    error Unsupported compiler
#endif

//- architecture definitions

#if defined(__arm__) || defined(__aarch64__)
#define ARCH_ARM 1
#elif defined(__i386__) || defined(__x86_64__)
#define ARCH_x86_64 1
#endif

//- compiler definitions

//- core stuff

/* assert an expression and output the file and the line */
#define internal static
#define global_variable static
#define local_persist static

#define arena_align (2 * sizeof(void *))

#define Min(a, b) (((a) < (b)) ? (a) : (b))
#define Max(a, b) (((a) > (b)) ? (a) : (b))

#define Unused(x) (void)(x)
#define Nil 0

#define Deprecated __attribute__((__deprecated__))

//- breakpoints
#if ARCH_ARM
#define breakpoint() __asm__ volatile("brk #0")
#elif ARCH_x86_64
#define breakpoint() __asm__ volatile("int3")
#endif

#if COMPILER_MSVC
#include <intrin.h>
#define breakpoint() __debugbreak()
#endif

//-
#define MemCpy(dest, src, len) memcpy((dest), (src), (len))
#define MemSet(dest, len) memset((dest), (1), (len))
#define MemSetZero(dest, len) memset((dest), (0), (len))

//- attributes macros
#if COMPILER_MSVC
#pragma section("read_only", read)
#define ReadOnly __declspec(allocate("read_only"))
#elif COMPILER_GCC || COMPILER_CLANG
#if PLATFORM_APPLE
#define ReadOnly __attribute__((section("__TEXT,__const")))
#else
// NOTE: adding #  to comment out the asmw arning message produced by gcc
// https://lkml.iu.edu/hypermail/linux/kernel/0105.2/1108.html
#define ReadOnly __attribute__((section(".rodata#")))
#endif // PLATFORM_APPLE
#else
#define ReadOnly
#endif // COMPILER_MSVC


//- attributes macros

// havving issues with compiler analyzer telling me that my arena allocaters could potentially return a non null value
// the solution i found to this problem we're these 2:

// https://clang.llvm.org/docs/AttributeReference.html#returns-nonnull
// https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#Common-Function-Attributes

#if COMPILER_CLANG || COMPILER_GCC
#define RETURNS_NON_NULL_VALUE __attribute__((returns_nonnull))
#else
#define RETURNS_NON_NULL_VALUE
#endif // COMPILER_CLANG || COMPILER_GCC

#if COMPILER_GCC || COMPILER_CLANG
#define SHOULD_BE_NON_NULL(...) __attribute__((nonnull (__VA_ARGS__)))
#else
#define SHOULD_BE_NON_NULL(...)
#endif // COMPILER_GCC || COMPILER_CLANG


//-
#if COMPILER_GCC || COMPILER_CLANG

#define Assert(x)  \
do             \
{              \
if(!(x))   \
{          \
breakpoint();            \
}          \
} while(0)
#else
#define Assert(x)  \
do             \
{              \
if(!(x))   \
{          \
breakpoint();            \
}          \
} while(0)
#endif // COMPILER_GCC


//- types

typedef uint64_t u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t  u8;

typedef int8_t  s8;
typedef int16_t s16;

typedef int32_t s32;
typedef int64_t s64;

typedef float  f32;
typedef double f64;

typedef s32 b32;
typedef s16 b16;
typedef s8  b8;

typedef uintptr_t umm;
typedef intptr_t  smm;

#endif //  BASE_CORE_H