summaryrefslogtreecommitdiff
path: root/source/base/base_core.h
blob: 9d0f5e73ea3cb7d7adf6cd8ebd3f377de4271248 (plain)
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 roundup_4(n) (((n) + 3) & -4)

#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

#if COMPILER_GCC
#define breakpoint() __asm__ volatile("int3")
#endif
#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))

//-
#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
#define ReadOnly __attribute__((section(".const")))
#endif // PLATFORM_APPLE

#endif

//-

//- 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;

//- disable certain msvc errors
#if COMPILER_MSVC
#pragma warning(disable : 6246) // disable the same name within a certain scope
#endif

//-

#define Assert(expression) _assert(expression)

// NOTE: apparantly in macros this strinifys whataever you put after the #
#define AssertLog(expression, ...) do { \
        if (!expression) { \
            Log("%s:%d: %s\n", __FILE__, __LINE__, #expression); \
            Log(__VA_ARGS__);\
            Assert(false);\
        }\
    } \
    while(0)

internal void _assert(bool expression);


internal void _assert(bool expression)
{

    if (!expression) {
        breakpoint();
#if COMPILER_MSVC
        __fastfail(0);
#else
        __builtin_trap(); // NOTE: always stop, breakpoint() is empty on arm without DEBUG
#endif
    }
}






#endif //  BASE_CORE_H