engine: add platform detection header

I do not want compiler attributes like __clang__ or __linux__ scattered
everywhere to detect the platform Ballistic is running on, so I added
preprocessor directives to make things a lot cleaner.

Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
Ronald Caesar
2026-01-14 18:58:53 -04:00
parent 02501ae7a4
commit 37cb629909
2 changed files with 32 additions and 6 deletions

View File

@@ -6,6 +6,8 @@
#ifndef BALLISTIC_ATTRIBUTES_H
#define BALLISTIC_ATTRIBUTES_H
#include "bal_platform.h"
/*!
* BAL_HOT()/BAL_COLD()
* Marks a function as hot or cold. Hot makes the compiller optimize it more
@@ -15,7 +17,7 @@
* BAL_HOT bal_error_t emit_instruction(...);
*/
#if defined(__GNUC__) || defined(__clang__)
#if BAL_COMPILER_GCC
#define BAL_HOT __attribute__((hot))
#define BAL_COLD __attribute__((cold))
@@ -34,7 +36,7 @@
* Usage: if (BAL_UNLIKELY(ptr == NULL)) { ... }
*/
#if defined(__GNUC__) || defined(__clang__)
#if BAL_COMPILER_GCC
#define BAL_LIKELY(x) __builtin_expect(!!(x), 1)
#define BAL_UNLIKELY(x) __builtin_expect(!!(x), 0)
@@ -53,11 +55,11 @@
* Usage: BAL_ALIGNED(64) struct data { ... };
*/
#if defined(__GNUC__) || defined(__clang__)
#if BAL_COMPILER_GCC
#define BAL_ALIGNED(x) __attribute__((aligned(x)))
#elif defined(_MSC_VER)
#elif BAL_COMPILER_MSVC
#define BAL_ALIGNED(x) __declspec(align(x))
@@ -73,11 +75,11 @@
* current scope.
*/
#if defined(__GNUC__) || defined(__clang__)
#if BAL_COMPILER_GCC
#define BAL_RESTRICT __restrict__
#elif defined(_MSC_VER)
#elif BAL_COMPILER_MSVC
#define BAL_RESTRICT __restrict

24
include/bal_platform.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef BALLISTIC_PLATFORM_H
#define BALLISTIC_PLATFORM_H
#if defined(_WIN32) || defined(_WIN64)
#define BAL_PLATFORM_WINDOWS 1
#define BAL_PLATFORM_POSIX 0
#elif defined(__linux__) || defined(__APPLE__)
#define BAL_PLATFORM_WINDOWS 0
#define BAL_PLATFORM_POSIX 1
#else
#error "Unknown Platform"
#endif
#if defined(_MSC_VER)
#define BAL_COMPILER_MSVC 1
#define BAL_COMPILER_GCC 0
#elif defined(__GNUC__) || defined(__clang__)
#define BAL_COMPILER_MSVC 0
#define BAL_COMPILER_GCC 1
#else
#error "Unknown Compiler"
#endif
#endif /* BALLISTIC_PLATFORM_H */