From 4748386b4e22a5bfe0c4d3f0d65c43f37e09536e Mon Sep 17 00:00:00 2001 From: Fabrice Date: Thu, 16 Apr 2026 17:01:13 +0200 Subject: progress on the allocator --- MODULE.bazel | 2 + format.sh | 2 + omni/BUILD.bazel | 4 +- omni/assert.h | 29 +++++++++++ omni/intrin.h | 7 +++ omni/memory.h | 25 ++++++++++ omni/platform.h | 147 +++++++++++++++++++++++++------------------------------ 7 files changed, 134 insertions(+), 82 deletions(-) create mode 100755 format.sh create mode 100644 omni/assert.h create mode 100644 omni/intrin.h create mode 100644 omni/memory.h diff --git a/MODULE.bazel b/MODULE.bazel index b3acfe2..4f656cf 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,3 +1,5 @@ module(name = "omni") +# TODO: remove or use +# bazel_dep(name = "bazel_skylib", version = "1.9.0") bazel_dep(name = "rules_cc", version = "0.2.17") diff --git a/format.sh b/format.sh new file mode 100755 index 0000000..cada96b --- /dev/null +++ b/format.sh @@ -0,0 +1,2 @@ +#!/bin/bash +clang-format -i $(find omni -name "*.h" -o -name "*.c") diff --git a/omni/BUILD.bazel b/omni/BUILD.bazel index ca62d73..9278b8f 100644 --- a/omni/BUILD.bazel +++ b/omni/BUILD.bazel @@ -4,7 +4,9 @@ cc_library( name = "omni_impl", hdrs = [ ":stdint.h", - ":platform.h" + ":platform.h", + ":intrin.h", + ":memory.h" ], visibility = ["//visibility:public"] ) diff --git a/omni/assert.h b/omni/assert.h new file mode 100644 index 0000000..f3e865b --- /dev/null +++ b/omni/assert.h @@ -0,0 +1,29 @@ +#ifndef OMNI_ASSERT_H +#define OMNI_ASSERT_H + +#include +#include + +#include "stdint.h" + +template +[[noreturn]] void panic_impl(const char* file, u32 line, const char* fmt, + Args arguments) { + fprintf(stderr, "PANIC at %s:%d: ", file, line); + fprintf(stderr, fmt, args...); + fputs("\n", stderr); + abort(); +} + +#define panic(...) panic_impl(__FILE__, __LINE__, __VA_ARGS__) + +#ifndef NDEBUG +# define assert(cond) \ + do { \ + if (!(cond)) panic("assertion failed: %s", #cond); \ + } while (0); +#else +# define assert(cond) unused(cond) +#endif + +#endif diff --git a/omni/intrin.h b/omni/intrin.h new file mode 100644 index 0000000..b731c15 --- /dev/null +++ b/omni/intrin.h @@ -0,0 +1,7 @@ +#ifndef OMNI_INTRIN_H +#define OMNI_INTRIN_H + +#define cast(T, x) ((T)(x)) +#define unused(x) cast(void, (x)) + +#endif diff --git a/omni/memory.h b/omni/memory.h new file mode 100644 index 0000000..def1cc0 --- /dev/null +++ b/omni/memory.h @@ -0,0 +1,25 @@ +#ifndef OMNI_MEMORY_H +#define OMNI_MEMORY_H + +#include "stdint.h" + +struct Layout { + usize size; + usize align; + + template + constexpr static Layout of(usize n) noexcept { + return Layout{n * sizeof(T), alignof(T)}; + } + + static bool from(usize size, usize align) noexcept { + return Layout{size, align}; + } +}; + +struct Allocator { + virtual u8* allocate(Layout layout) noexcept; + virtual void deallocate(u8* ptr); +}; + +#endif diff --git a/omni/platform.h b/omni/platform.h index 5410f6d..b762791 100644 --- a/omni/platform.h +++ b/omni/platform.h @@ -1,5 +1,5 @@ #ifndef OMNI_PLATFORM_H -# define OMNI_PLATFORM_H +#define OMNI_PLATFORM_H /* * @@ -7,20 +7,17 @@ * */ -# undef OMNI_CC_MSVC -# undef OMNI_CC_GNU +#if defined(_MSC_VER) +# define OMNI_CC_MSVC +#endif -# if defined(_MSC_VER) -# define OMNI_CC_MSVC -# endif +#if defined(__GNUC__) || defined(__clang__) +# define OMNI_CC_GNU +#endif -# if defined(__GNUC__) || defined(__clang__) -# define OMNI_CC_GNU -# endif - -# if !defined(OMNI_CC_GNU) && !defined(OMNI_CC_MSVC) -# error "compiler not supported" -# endif +#if !defined(OMNI_CC_GNU) && !defined(OMNI_CC_MSVC) +# error "compiler not supported" +#endif /* * @@ -28,34 +25,29 @@ * */ -# undef OMNI_ARCH_X86 -# undef OMNI_ARCH_ARM -# undef OMNI_ARCH_64BIT -# undef OMNI_ARCH_32BIT - -# if defined(__i386__) || defined(_M_IX86) -# define OMNI_ARCH_X86 -# define OMNI_ARCH_32BIT -# endif +#if defined(__i386__) || defined(_M_IX86) +# define OMNI_ARCH_X86 +# define OMNI_ARCH_32BIT +#endif -# if defined(__x86_64__) || defined(_M_X64) -# define OMNI_ARCH_X86 -# define OMNI_ARCH_64BIT -# endif +#if defined(__x86_64__) || defined(_M_X64) +# define OMNI_ARCH_X86 +# define OMNI_ARCH_64BIT +#endif -# if defined(__arm__) || defined(_M_ARM) -# define OMNI_ARCH_ARM -# define OMNI_ARCH_32BIT -# endif +#if defined(__arm__) || defined(_M_ARM) +# define OMNI_ARCH_ARM +# define OMNI_ARCH_32BIT +#endif -# if defined(__aarch64__) || defined(_M_ARM64) || defined(__arm64__) -# define OMNI_ARCH_ARM -# define OMNI_ARCH_64BIT -# endif +#if defined(__aarch64__) || defined(_M_ARM64) || defined(__arm64__) +# define OMNI_ARCH_ARM +# define OMNI_ARCH_64BIT +#endif -# if !defined(OMNI_ARCH_X86) && !defined(OMNI_ARCH_ARM) -# error "target architecture is not supported" -# endif +#if !defined(OMNI_ARCH_X86) && !defined(OMNI_ARCH_ARM) +# error "target architecture is not supported" +#endif /* * @@ -63,33 +55,28 @@ * */ -# undef OMNI_OS_WIN -# undef OMNI_OS_LINUX -# undef OMNI_OS_FREEBSD -# undef OMNI_OS_OSX +#if defined(_WIN32) || defined(_WIN64) +# define OMNI_OS_WIN +#endif -# if defined(_WIN32) || defined(_WIN64) -# define OMNI_OS_WIN -# endif +#if defined(__linux__) +# define OMNI_OS_LINUX +# define OMNI_OS_UNIX +#endif -# if defined(__linux__) -# define OMNI_OS_LINUX -# define OMNI_OS_UNIX -# endif +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +# define OMNI_OS_FREEBSD +# define OMNI_OS_UNIX +#endif -# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -# define OMNI_OS_FREEBSD -# define OMNI_OS_UNIX -# endif +#if defined(__APPLE__) && defined(__MACH__) +# define OMNI_OS_OSX +# define OMNI_OS_UNIX +#endif -# if defined(__APPLE__) && defined(__MACH__) -# define OMNI_OS_OSX -# define OMNI_OS_UNIX -# endif - -# if !defined(OMNI_OS_UNIX) && !defined(OMNI_OS_WIN) -# error "target os not supported" -# endif +#if !defined(OMNI_OS_UNIX) && !defined(OMNI_OS_WIN) +# error "target os not supported" +#endif /* * @@ -98,29 +85,27 @@ * */ -# undef OMNI_CXX_VER_MIN -# undef OMNI_CXX_VER -# undef OMNI_CC_VER - -# define OMNI_CC_VER __STDC_VERSION__ +#define OMNI_CC_VER __STDC_VERSION__ -# if defined(OMNI_CC_MSVC) -# if defined(_MSVC_LANG) -# define OMNI_CXX_VER _MSVC_LANG -# else -# define OMNI_CXX_VER 0 -# endif +#if defined(OMNI_CC_MSVC) +# if defined(_MSVC_LANG) +# define OMNI_CXX_VER _MSVC_LANG +# else +# define OMNI_CXX_VER 0 # endif +#endif -# if defined(OMNI_CC_GNU) -# if defined(__cplusplus) -# define OMNI_CXX_VER __cplusplus -# else -# define OMNI_CXX_VER 0 -# endif +#if defined(OMNI_CC_GNU) +# if defined(__cplusplus) +# define OMNI_CXX_VER __cplusplus +# else +# define OMNI_CXX_VER 0 # endif +#endif -# define OMNI_CXX_VER_MIN 201103L -# if OMNI_CXX_VER_MIN > OMNI_CXX_VER -# error "atleast C++11 is required" -# endif +#define OMNI_CXX_VER_MIN 201103L +#if OMNI_CXX_VER_MIN > OMNI_CXX_VER +# error "atleast C++11 is required" +#endif + +#endif -- cgit v1.2.3