diff options
| author | Peter Fors <peter.fors@mindkiller.com> | 2025-03-29 20:11:56 +0100 |
|---|---|---|
| committer | Peter Fors <peter.fors@mindkiller.com> | 2025-03-29 20:11:56 +0100 |
| commit | d5486a5af100fb37fac08b60d862ac14943853ce (patch) | |
| tree | e2206f620745ba49c94f0f29d180587e03bc9fda /base/common.h | |
| parent | ee4f15400998ca704c6ad8fc537f0d924930fabd (diff) | |
add base code for windowing and opengl crt-shader.
Diffstat (limited to 'base/common.h')
| -rw-r--r-- | base/common.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/base/common.h b/base/common.h new file mode 100644 index 0000000..2138908 --- /dev/null +++ b/base/common.h @@ -0,0 +1,55 @@ +
+
+#ifdef _WIN32
+#include <intrin.h>
+#include <windows.h>
+#include <malloc.h> // For _aligned_malloc and _aligned_free on Windows
+#define aligned_alloc(align, size) _aligned_malloc(size, align)
+#define aligned_free _aligned_free
+
+#elif __linux__
+#include <sys/prctl.h>
+#include <sys/resource.h>
+#include <unistd.h>
+#include <stdlib.h> // For aligned_alloc on Linux
+#define aligned_free free
+#endif
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0]))
+#endif
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+
+#if defined(__GNUC__) || defined(__clang__) || defined(__TINYC__)
+#define ALIGNED(x) __attribute__((aligned(x)))
+#elif defined(_MSC_VER)
+#define ALIGNED(x) __declspec(align(x))
+#else
+#define ALIGNED(x) /* No alignment support */
+#endif
+
+#if defined(__GNUC__) || defined(__clang__)
+#define ASSUME(condition) if (!(condition)) __builtin_unreachable()
+#elif defined(_MSC_VER)
+#define ASSUME(condition) __assume(condition)
+#else
+#define ASSUME(condition) ((void)0) /* Fallback: No-op */
+#endif
+
+#define UNREACHABLE(cond) do { if(cond) __builtin_unreachable(); } while(0)
+
+#define DEFAULT_ALIGNMENT 64
+
+static void *mks_alloc(size_t size) {
+ size = (size + (DEFAULT_ALIGNMENT - 1)) & ~(DEFAULT_ALIGNMENT - 1);
+ void *ptr = aligned_alloc(DEFAULT_ALIGNMENT, size);
+ state.total_allocated += size;
+ memset(ptr, 0, size);
+ return ptr;
+}
+
+static void mks_free(void *ptr) {
+ aligned_free(ptr);
+}
|
