diff --git a/code/base/arena.h b/code/base/arena.h index d8c086d..28533f9 100644 --- a/code/base/arena.h +++ b/code/base/arena.h @@ -31,6 +31,15 @@ struct ArenaParams void* optional_backing_buffer; }; +/* NOTE(Ed): This is a combination of several concepts into a single interface: + * A arena 'block' of memory with segmented chaining of the blocks + * An OS virtual memory allocation scheme + * A push/pop stack allocation interface for the arena + + TODO(Ed): We need to lift the virtual memory tracking to its own data structure + and virtual memory interface utilizing memory_substrate.h +*/ + typedef struct Arena Arena; struct Arena { diff --git a/code/base/base_types.h b/code/base/base_types.h index b4fd10a..7ea7c01 100644 --- a/code/base/base_types.h +++ b/code/base/base_types.h @@ -6,7 +6,7 @@ # include "platform.h" #endif -#if defined( MD_COMPILER_MSVC ) +#if defined( COMPILER_MSVC ) # if _MSC_VER < 1300 typedef unsigned char U8; typedef signed char S8; diff --git a/code/base/constants.h b/code/base/constants.h index e22a8eb..1e4c299 100644 --- a/code/base/constants.h +++ b/code/base/constants.h @@ -40,12 +40,12 @@ #define MD_S64_MIN ( -0x7fffffffffffffffll - 1 ) #define MD_S64_MAX 0x7fffffffffffffffll -#if defined( MD_ARCH_32_BIT ) +#if defined( ARCH_32_BIT ) # define MD_USIZE_MIN MD_U32_MIN # define MD_USIZE_MAX MD_U32_MAX # define MD_ISIZE_MIN MD_S32_MIN # define MD_ISIZE_MAX MD_S32_MAX -#elif defined( MD_ARCH_64_BIT ) +#elif defined( ARCH_64_BIT ) # define MD_USIZE_MIN MD_U64_MIN # define MD_USIZE_MAX MD_U64_MAX # define MD_ISIZE_MIN MD_S64_MIN diff --git a/code/base/context_cracking.h b/code/base/context_cracking.h index 8e23ba2..cc48845 100644 --- a/code/base/context_cracking.h +++ b/code/base/context_cracking.h @@ -6,83 +6,83 @@ #if defined( _MSC_VER ) # pragma message("Detected MSVC") -// # define MD_COMPILER_CLANG 0 -# define MD_COMPILER_MSVC 1 -// # define MD_COMPILER_GCC 0 +// # define COMPILER_CLANG 0 +# define COMPILER_MSVC 1 +// # define COMPILER_GCC 0 # if _MSC_VER >= 1920 -# define MD_COMPILER_MSVC_YEAR 2019 +# define COMPILER_MSVC_YEAR 2019 # elif _MSC_VER >= 1910 -# define MD_COMPILER_MSVC_YEAR 2017 +# define COMPILER_MSVC_YEAR 2017 # elif _MSC_VER >= 1900 -# define MD_COMPILER_MSVC_YEAR 2015 +# define COMPILER_MSVC_YEAR 2015 # elif _MSC_VER >= 1800 -# define MD_COMPILER_MSVC_YEAR 2013 +# define COMPILER_MSVC_YEAR 2013 # elif _MSC_VER >= 1700 -# define MD_COMPILER_MSVC_YEAR 2012 +# define COMPILER_MSVC_YEAR 2012 # elif _MSC_VER >= 1600 -# define MD_COMPILER_MSVC_YEAR 2010 +# define COMPILER_MSVC_YEAR 2010 # elif _MSC_VER >= 1500 # define M_DCOMPILER_MSVC_YEAR 2008 # elif _MSC_VER >= 1400 -# define MD_COMPILER_MSVC_YEAR 2005 +# define COMPILER_MSVC_YEAR 2005 # else -# define MD_COMPILER_MSVC_YEAR 0 +# define COMPILER_MSVC_YEAR 0 # endif #elif defined( __GNUC__ ) # pragma message("Detected GCC") -// # define MD_COMPILER_CLANG 0 -// # define MD_COMPILER_MSVC 0 -# define MD_COMPILER_GCC 1 +// # define COMPILER_CLANG 0 +// # define COMPILER_MSVC 0 +# define COMPILER_GCC 1 #elif defined( __clang__ ) # pragma message("Detected CLANG") -# define MD_COMPILER_CLANG 1 -// # define MD_COMPILER_MSVC 0 -// # define MD_COMPILER_GCC 0 +# define COMPILER_CLANG 1 +// # define COMPILER_MSVC 0 +// # define COMPILER_GCC 0 #else # error Unknown compiler #endif #if defined( __has_attribute ) -# define MD_HAS_ATTRIBUTE( attribute ) __has_attribute( attribute ) +# define HAS_ATTRIBUTE( attribute ) __has_attribute( attribute ) #else -# define MD_HAS_ATTRIBUTE( attribute ) ( 0 ) +# define HAS_ATTRIBUTE( attribute ) ( 0 ) #endif -#if defined(MD_GCC_VERSION_CHECK) -# undef MD_GCC_VERSION_CHECK +#if defined(GCC_VERSION_CHECK) +# undef GCC_VERSION_CHECK #endif #if defined(GEN_GCC_VERSION) -# define MD_GCC_VERSION_CHECK(major,minor,patch) (GEN_GCC_VERSION >= GEN_VERSION_ENCODE(major, minor, patch)) +# define GCC_VERSION_CHECK(major,minor,patch) (GEN_GCC_VERSION >= GEN_VERSION_ENCODE(major, minor, patch)) #else -# define MD_GCC_VERSION_CHECK(major,minor,patch) (0) +# define GCC_VERSION_CHECK(major,minor,patch) (0) #endif #pragma endregion Compiler Vendor #pragma endregion Language -#if ! defined(MD_LANG_C) +#if ! defined(LANG_C) # ifdef __cplusplus -# define MD_LANG_C 0 -# define MD_LANG_CPP 1 +# define LANG_C 0 +# define LANG_CPP 1 # else # if defined(__STDC__) -# define MD_LANG_C 1 -# define MD_LANG_CPP 0 +# define LANG_C 1 +# define LANG_CPP 0 # else // Fallback for very old C compilers -# define MD_LANG_C 1 -# define MD_LANG_CPP 0 +# define LANG_C 1 +# define LANG_CPP 0 # endif # endif #endif -#if MD_LANG_C +#if LANG_C # pragma message("MD: Detected C") #endif -#if MD_LANG_CPP +#if LANG_CPP # pragma message("MD: Detected CPP") #endif @@ -90,56 +90,56 @@ #pragma region Hardware Architecture -#if MD_COMPILER_CLANG +#if COMPILER_CLANG # if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) -# define MD_ARCH_X64 1 +# define ARCH_X64 1 # elif defined(i386) || defined(__i386) || defined(__i386__) -# define MD_ARCH_X86 1 +# define ARCH_X86 1 # elif defined(__aarch64__) -# define MD_ARCH_ARM64 1 +# define ARCH_ARM64 1 # elif defined(__arm__) -# define MD_ARCH_ARM32 1 +# define ARCH_ARM32 1 # else # error Architecture not supported. # endif -#endif // MD_COMPILER_CLANG +#endif // COMPILER_CLANG -#if MD_COMPILER_MSVC +#if COMPILER_MSVC # if defined(_M_AMD64) -# define MD_ARCH_X64 1 +# define ARCH_X64 1 # elif defined(_M_IX86) -# define MD_ARCH_X86 1 +# define ARCH_X86 1 # elif defined(_M_ARM64) -# define MD_ARCH_ARM64 1 +# define ARCH_ARM64 1 # elif defined(_M_ARM) -# define MD_ARCH_ARM32 1 +# define ARCH_ARM32 1 # else # error Architecture not supported. # endif -#endif // MD_COMPILER_MSVC +#endif // COMPILER_MSVC -#if MD_COMPILER_GCC +#if COMPILER_GCC # if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) -# define MD_ARCH_X64 1 +# define ARCH_X64 1 # elif defined(i386) || defined(__i386) || defined(__i386__) -# define MD_ARCH_X86 1 +# define ARCH_X86 1 # elif defined(__aarch64__) -# define MD_ARCH_ARM64 1 +# define ARCH_ARM64 1 # elif defined(__arm__) -# define MD_ARCH_ARM32 1 +# define ARCH_ARM32 1 # else # error Architecture not supported. # endif -#endif // MD_COMPILER_GCC +#endif // COMPILER_GCC -#if defined(MD_ARCH_X64) -# define MD_ARCH_64BIT 1 -#elif defined(MD_ARCH_X86) -# define MD_ARCH_32BIT 1 +#if defined(ARCH_X64) +# define ARCH_64BIT 1 +#elif defined(ARCH_X86) +# define ARCH_32BIT 1 #endif -#if MD_ARCH_ARM32 || MD_ARCH_ARM64 || MD_ARCH_X64 || MD_ARCH_X86 -# define MD_ARCH_LITTLE_ENDIAN 1 +#if ARCH_ARM32 || ARCH_ARM64 || ARCH_X64 || ARCH_X86 +# define ARCH_LITTLE_ENDIAN 1 #else # error Endianness of this architecture not understood by context cracker. #endif @@ -149,46 +149,46 @@ #pragma region Operating System #if defined( _WIN32 ) || defined( _WIN64 ) -# ifndef MD_OS_WINDOWS -# define MD_OS_WINDOWS 1 +# ifndef OS_WINDOWS +# define OS_WINDOWS 1 # endif #elif defined( __APPLE__ ) && defined( __MACH__ ) -# ifndef MD_OS_OSX -# define MD_OS_OSX 1 +# ifndef OS_OSX +# define OS_OSX 1 # endif -# ifndef MD_OS_MACOS -# define MD_OS_MACOS 1 +# ifndef OS_MACOS +# define OS_MACOS 1 # endif #elif defined( __unix__ ) -# ifndef MD_OS_UNIX -# define MD_OS_UNIX 1 +# ifndef OS_UNIX +# define OS_UNIX 1 # endif # if defined( ANDROID ) || defined( __ANDROID__ ) -# ifndef MD_OS_ANDROID -# define MD_OS_ANDROID 1 +# ifndef OS_ANDROID +# define OS_ANDROID 1 # endif -# ifndef MD_OS_LINUX -# define MD_OS_LINUX 1 +# ifndef OS_LINUX +# define OS_LINUX 1 # endif # elif defined( __linux__ ) -# ifndef MD_OS_LINUX -# define MD_OS_LINUX 1 +# ifndef OS_LINUX +# define OS_LINUX 1 # endif # elif defined( __FreeBSD__ ) || defined( __FreeBSD_kernel__ ) -# ifndef MD_OS_FREEBSD -# define MD_OS_FREEBSD 1 +# ifndef OS_FREEBSD +# define OS_FREEBSD 1 # endif # elif defined( __OpenBSD__ ) -# ifndef MD_OS_OPENBSD -# define MD_OS_OPENBSD 1 +# ifndef OS_OPENBSD +# define OS_OPENBSD 1 # endif # elif defined( __EMSCRIPTEN__ ) -# ifndef MD_OS_EMSCRIPTEN -# define MD_OS_EMSCRIPTEN 1 +# ifndef OS_EMSCRIPTEN +# define OS_EMSCRIPTEN 1 # endif # elif defined( __CYGWIN__ ) -# ifndef MD_OS_CYGWIN -# define MD_OS_CYGWIN 1 +# ifndef OS_CYGWIN +# define OS_CYGWIN 1 # endif # else # error This UNIX operating system is not supported @@ -205,55 +205,55 @@ //////////////////////////////// //~ rjf: Zero All Undefined Options -#if !defined(MD_ARCH_32BIT) -# define MD_ARCH_32BIT 0 +#if !defined(ARCH_32BIT) +# define ARCH_32BIT 0 #endif -#if !defined(MD_ARCH_64BIT) -# define MD_ARCH_64BIT 0 +#if !defined(ARCH_64BIT) +# define ARCH_64BIT 0 #endif -#if !defined(MD_ARCH_X64) -# define MD_ARCH_X64 0 +#if !defined(ARCH_X64) +# define ARCH_X64 0 #endif -#if !defined(MD_ARCH_X86) -# define MD_ARCH_X86 0 +#if !defined(ARCH_X86) +# define ARCH_X86 0 #endif -#if !defined(MD_ARCH_ARM64) -# define MD_ARCH_ARM64 0 +#if !defined(ARCH_ARM64) +# define ARCH_ARM64 0 #endif -#if !defined(MD_ARCH_ARM32) -# define MD_ARCH_ARM32 0 +#if !defined(ARCH_ARM32) +# define ARCH_ARM32 0 #endif -#if !defined(MD_COMPILER_MSVC) -# define MD_COMPILER_MSVC 0 +#if !defined(COMPILER_MSVC) +# define COMPILER_MSVC 0 #endif -#if !defined(MD_COMPILER_GCC) -# define MD_COMPILER_GCC 0 +#if !defined(COMPILER_GCC) +# define COMPILER_GCC 0 #endif -#if !defined(MD_COMPILER_CLANG) -# define MD_COMPILER_CLANG 0 +#if !defined(COMPILER_CLANG) +# define COMPILER_CLANG 0 #endif -#if !defined(MD_OS_WINDOWS) -# define MD_OS_WINDOWS 0 +#if !defined(OS_WINDOWS) +# define OS_WINDOWS 0 #endif -#if !defined(MD_OS_LINUX) -# define MD_OS_LINUX 0 +#if !defined(OS_LINUX) +# define OS_LINUX 0 #endif -#if !defined(MD_OS_MAC) -# define MD_OS_MAC 0 +#if !defined(OS_MAC) +# define OS_MAC 0 #endif -#if !defined(MD_LANG_CPP) -# define MD_LANG_CPP 0 +#if !defined(LANG_CPP) +# define LANG_CPP 0 #endif -#if !defined(MD_LANG_C) -# define MD_LANG_C 0 +#if !defined(LANG_C) +# define LANG_C 0 #endif //////////////////////////////// //~ rjf: Unsupported Errors -#if MD_ARCH_X86 +#if ARCH_X86 # error You tried to build in x86 (32 bit) mode, but currently, only building in x64 (64 bit) mode is supported. #endif -#if ! MD_ARCH_X64 +#if ! ARCH_X64 # error You tried to build with an unsupported architecture. Currently, only building in x64 mode is supported. #endif diff --git a/code/base/linkage.h b/code/base/linkage.h index 513cecc..c313e84 100644 --- a/code/base/linkage.h +++ b/code/base/linkage.h @@ -4,7 +4,7 @@ #endif #ifndef MD_API -#if MD_COMPILER_MSVC +#if COMPILER_MSVC # ifdef MD_DYN_LINK # ifdef MD_DYN_EXPORT # define MD_API __declspec(dllexport) @@ -24,7 +24,7 @@ #endif // GEN_API #ifndef MD_API_C_BEGIN -# if MD_LANG_C +# if LANG_C # define MD_API_C_BEGIN # define MD_API_C_END # define MD_API_C diff --git a/code/base/macros.h b/code/base/macros.h index 729ec68..c2d323e 100644 --- a/code/base/macros.h +++ b/code/base/macros.h @@ -8,16 +8,16 @@ #define local_persist static #endif -#if MD_COMPILER_MSVC +#if COMPILER_MSVC # define thread_static __declspec(thread) -#elif MD_COMPILER_CLANG || MD_COMPILER_GCC +#elif COMPILER_CLANG || COMPILER_GCC # define thread_static __thread #endif //////////////////////////////// //~ rjf: Branch Predictor Hints -#if MD_COMPILER_CLANG +#if COMPILER_CLANG # define expect(expr, val) __builtin_expect((expr), (val)) #else # define expect(expr, val) (expr) @@ -29,7 +29,7 @@ //////////////////////////////// //~ erg: type casting -#if MD_LANG_CPP +#if LANG_CPP # ifndef ccast # define ccast( type, value ) ( const_cast< type >( (value) ) ) # endif @@ -57,8 +57,8 @@ # endif #endif -#if ! defined(typeof) && ( ! MD_LANG_C || __STDC_VERSION__ < 202311L) -# if ! MD_LANG_C +#if ! defined(typeof) && ( ! LANG_C || __STDC_VERSION__ < 202311L) +# if ! LANG_C # define typeof decltype # elif defined(_MSC_VER) # define typeof __typeof__ @@ -69,7 +69,7 @@ # endif #endif -#if MD_LANG_C +#if LANG_C # if __STDC_VERSION__ >= 202311L # define enum_underlying(type) : type # else @@ -79,7 +79,7 @@ # define enum_underlying(type) : type #endif -#if MD_LANG_C +#if LANG_C # ifndef nullptr # define nullptr NULL # endif @@ -89,13 +89,13 @@ # endif #endif -#if ! defined(MD_PARAM_DEFAULT) && MD_LANG_CPP +#if ! defined(MD_PARAM_DEFAULT) && LANG_CPP # define MD_PARAM_DEFAULT = {} #else # define MD_PARAM_DEFAULT #endif -#if MD_LANG_C +#if LANG_C # ifndef static_assert # undef static_assert # if __STDC_VERSION__ >= 201112L diff --git a/code/base/memory.h b/code/base/memory.h index 1923385..56acd5f 100644 --- a/code/base/memory.h +++ b/code/base/memory.h @@ -4,40 +4,75 @@ # include "linkage.h" # include "macros.h" # include "platform.h" -# include "memory_substrate.h" #endif + //////////////////////////////// //~ rjf: Units -#define KB(n) (((U64)(n)) << 10) -#define MB(n) (((U64)(n)) << 20) -#define GB(n) (((U64)(n)) << 30) -#define TB(n) (((U64)(n)) << 40) +#ifndef KILOBTYES +#define KILOBYTES( x ) ( ( x ) * ( S64 )( 1024 ) ) +#endif +#ifndef MEGABYTES +#define MEGABYTES( x ) ( MD_KILOBYTES( x ) * ( S64 )( 1024 ) ) +#endif +#ifndef GIGABYTES +#define GIGABYTES( x ) ( MD_MEGABYTES( x ) * ( S64 )( 1024 ) ) +#endif +#ifndef TERABYTES +#define TERABYTES( x ) ( MD_GIGABYTES( x ) * ( S64 )( 1024 ) ) +#endif +#ifndef KB +#define KB(n) (((U64)(n)) << 10) +#endif +#ifndef MB +#define MB(n) (((U64)(n)) << 20) +#endif +#ifndef GB +#define GB(n) (((U64)(n)) << 30) +#endif +#ifndef TB +#define TB(n) (((U64)(n)) << 40) +#endif + +#ifndef thosuand #define thousand(n) ((n) * 1000) +#endif +#ifndef million #define million(n) ((n) * 1000000) +#endif +#ifndef billion #define billion(n) ((n) * 1000000000) +#endif //////////////////////////////// //~ rjf: Clamps, Mins, Maxes +#ifndef min #define min(A,B) (((A) < (B)) ? (A) : (B)) +#endif +#ifndef max #define max(A,B) (((A) > (B)) ? (A) : (B)) +#endif +#ifndef clamp_top #define clamp_top(A,X) Min(A, X) +#endif +#ifndef clamp_bot #define clamp_bot(X,B) Max(X, B) +#endif #define clamp(A,X,B) (((X) < (A)) ? (A) : ((X) > (B)) ? (B) : (X)) //////////////////////////////// //~ rjf: Type -> Alignment -#if MD_COMPILER_MSVC +#if COMPILER_MSVC # define align_of(T) __alignof(T) -#elif MD_COMPILER_CLANG +#elif COMPILER_CLANG # define align_of(T) __alignof(T) -#elif MD_COMPILER_GCC +#elif COMPILER_GCC # define align_of(T) __alignof__(T) #else # error AlignOf not defined for this compiler. @@ -87,9 +122,9 @@ //////////////////////////////// //~ rjf: Asserts -#if MD_COMPILER_MSVC +#if COMPILER_MSVC # define trap() __debugbreak() -#elif MD_COMPILER_CLANG || COMPILER_GCC +#elif COMPILER_CLANG || COMPILER_GCC # define trap() __builtin_trap() #else # error Unknown trap intrinsic for this compiler. @@ -111,8 +146,8 @@ //////////////////////////////// //~ rjf: Atomic Operations -#if MD_OS_WINDOWS -# if MD_ARCH_X64 +#if OS_WINDOWS +# if ARCH_X64 # define ins_atomic_u64_eval(x) InterlockedAdd64((volatile __int64 *)(x), 0) # define ins_atomic_u64_inc_eval(x) InterlockedIncrement64((volatile __int64 *)(x)) # define ins_atomic_u64_dec_eval(x) InterlockedDecrement64((volatile __int64 *)(x)) @@ -126,8 +161,8 @@ # else # error Atomic intrinsics not defined for this operating system / architecture combination. # endif -#elif MD_OS_LINUX -# if MD_ARCH_X64 +#elif OS_LINUX +# if ARCH_X64 # define ins_atomic_u64_inc_eval(x) __sync_fetch_and_add((volatile U64 *)(x), 1) # else # error Atomic intrinsics not defined for this operating system / architecture combination. @@ -279,14 +314,14 @@ //////////////////////////////// //~ rjf: Address Sanitizer Markup -#if MD_COMPILER_MSVC +#if COMPILER_MSVC # if defined(__SANITIZE_ADDRESS__) # define ASAN_ENABLED 1 # define NO_ASAN __declspec(no_sanitize_address) # else # define NO_ASAN # endif -#elif MD_COMPILER_CLANG +#elif COMPILER_CLANG # if defined(__has_feature) # if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) # define ASAN_ENABLED 1 @@ -312,24 +347,36 @@ //////////////////////////////// //~ rjf: Misc. Helper Macros +#ifndef stringify #define stringify_(S) #S #define stringify(S) stringify_(S) +#endif +#ifndef glue #define glue_(A,B) A ## B #define glue(A,B) glue_(A,B) +#endif +#ifndef array_count #define array_count(a) (sizeof(a) / sizeof((a)[0])) +#endif +#ifndef ceil_integer_div #define ceil_integer_div(a,b) (((a) + (b) - 1) / (b)) +#endif +#ifndef swap #define swap(T, a, b) do { T t__ = a; a = b; b = t__; } while(0) +#endif -#if MD_ARCH_64BIT -# define int_from_ptr(ptr) ((U64)(ptr)) -#elif MD_ARCH_32BIT -# define int_from_ptr(ptr) ((U32)(ptr)) -#else -# error Missing pointer-to-integer cast for this architecture. +#ifndef int_from_ptr +# if ARCH_64BIT +# define int_from_ptr(ptr) ((U64)(ptr)) +# elif ARCH_32BIT +# define int_from_ptr(ptr) ((U32)(ptr)) +# else +# error Missing pointer-to-integer cast for this architecture. +# endif #endif #define ptr_from_int(i) (void*)((U8*)0 + (i)) @@ -343,14 +390,28 @@ #define extract_bit(word, idx) (((word) >> (idx)) & 1) -#if MD_LANG_CPP +#if LANG_CPP # define zero_struct {} #else # define zero_struct {0} #endif -#if MD_COMPILER_MSVC && MD_COMPILER_MSVC_YEAR < 2015 +#if COMPILER_MSVC && COMPILER_MSVC_YEAR < 2015 # define this_function_name "unknown" #else # define this_function_name __func__ #endif + +#if COMPILER_MSVC || (COMPILER_CLANG && OS_WINDOWS) +# pragma section(".rdata$", read) +# define read_only __declspec(allocate(".rdata$")) +#elif (COMPILER_CLANG && OS_LINUX) +# define read_only __attribute__((section(".rodata"))) +#else +// NOTE(rjf): I don't know of a useful way to do this in GCC land. +// __attribute__((section(".rodata"))) looked promising, but it introduces a +// strange warning about malformed section attributes, and it doesn't look +// like writing to that section reliably produces access violations, strangely +// enough. (It does on Clang) +# define read_only +#endif diff --git a/code/base/memory_substrate.h b/code/base/memory_substrate.h index ef1f673..ad493c4 100644 --- a/code/base/memory_substrate.h +++ b/code/base/memory_substrate.h @@ -3,8 +3,9 @@ # include "context_cracking.h" # include "linkage.h" # include "macros.h" -# include "linkage.h" +# include "platform.h" # include "base_types.h" +# include "memory.h" #endif // This provides an alterntive memory strategy to HMH/Casey Muratori/RJF styled arenas @@ -12,29 +13,10 @@ // is related to the gb headers an thus the Odin-lang memory strategy // Users can override the underlying memory allocator used, even for the HMH arena memory strategy. -#define MD_KILOBYTES( x ) ( ( x ) * ( S64 )( 1024 ) ) -#define MD_MEGABYTES( x ) ( MD_KILOBYTES( x ) * ( S64 )( 1024 ) ) -#define MD_GIGABYTES( x ) ( MD_MEGABYTES( x ) * ( S64 )( 1024 ) ) -#define MD_TERABYTES( x ) ( MD_GIGABYTES( x ) * ( S64 )( 1024 ) ) - #define MD__ONES ( scast( GEN_NS usize, - 1) / MD_U8_MAX ) #define MD__HIGHS ( MD__ONES * ( MD_U8_MAX / 2 + 1 ) ) #define MD__HAS_ZERO( x ) ( ( ( x ) - MD__ONES ) & ~( x ) & MD__HIGHS ) -#if MD_COMPILER_MSVC || (MD_COMPILER_CLANG && MD_OS_WINDOWS) -# pragma section(".rdata$", read) -# define read_only __declspec(allocate(".rdata$")) -#elif (MD_COMPILER_CLANG && MD_OS_LINUX) -# define read_only __attribute__((section(".rodata"))) -#else -// NOTE(rjf): I don't know of a useful way to do this in GCC land. -// __attribute__((section(".rodata"))) looked promising, but it introduces a -// strange warning about malformed section attributes, and it doesn't look -// like writing to that section reliably produces access violations, strangely -// enough. (It does on Clang) -# define read_only -#endif - typedef U32 AllocType; enum AllocType enum_underlying(U32) { @@ -114,3 +96,17 @@ MD_API void* heap_allocator_proc( void* allocator_data, AllocType type, SSIZE si //! Helper to free memory allocated by heap allocator. #define mfree( ptr ) free( heap(), ptr ) + +typedef struct VMem; +struct VMem { + U32 cmt_size; + U32 res_size; + U64 base_pos; +}; + +AllocatorInfo vm_allocator(VMem* vm) { + AllocatorInfo info = { vm_allocator_proc, vm } + return info +} + +MD_API void* vm_allocator_proc(void * allocator_data, AllocType type, SSIZE size, SSIZE alignment, void* old_memory, SSIZE old_size, U64 flags); diff --git a/code/base/namespace.h b/code/base/namespace.h index ca87a67..a2d009b 100644 --- a/code/base/namespace.h +++ b/code/base/namespace.h @@ -4,8 +4,8 @@ #endif // C++ namespace support -#if defined(MD_DONT_USE_NAMESPACE) || MD_LANG_C -# if MD_LANG_C +#if defined(MD_DONT_USE_NAMESPACE) || LANG_C +# if LANG_C # define MD_NS # define MD_NS_BEGIN # define MD_NS_END diff --git a/code/base/platform.h b/code/base/platform.h index 332ea61..e2dbd10 100644 --- a/code/base/platform.h +++ b/code/base/platform.h @@ -8,7 +8,7 @@ #include #include -#if defined( MD_OS_WINDOWS ) +#if defined( OS_WINDOWS ) # include # include # include @@ -28,7 +28,7 @@ # undef VC_EXTRALEAN #endif -#if MD_LANG_C +#if LANG_C # include # include #endif diff --git a/code/base/strings.h b/code/base/strings.h index 3706b37..9466da3 100644 --- a/code/base/strings.h +++ b/code/base/strings.h @@ -102,9 +102,9 @@ typedef enum PathStyle PathStyle_WindowsAbsolute, PathStyle_UnixAbsolute, -#if MD_OS_WINDOWS +#if OS_WINDOWS PathStyle_SystemAbsolute = PathStyle_WindowsAbsolute -#elif MD_OS_LINUX +#elif OS_LINUX PathStyle_SystemAbsolute = PathStyle_UnixAbsolute #else # error "absolute path style is undefined for this OS" diff --git a/examples/integration/multi_threaded.c b/examples/integration/multi_threaded.c index 2624546..3ec50b7 100644 --- a/examples/integration/multi_threaded.c +++ b/examples/integration/multi_threaded.c @@ -19,9 +19,9 @@ #include "md.h" #include "md.c" -#if MD_OS_WINDOWS +#if OS_WINDOWS # include -#elif MD_OS_MAC || MD_OS_LINUX +#elif OS_MAC || OS_LINUX # include #else # error Not implemented for this OS @@ -34,7 +34,7 @@ #if MD_COMPILER_CL # include # define atomic_inc_then_eval_u64(p) _InterlockedIncrement64((volatile __int64*)p) -#elif MD_COMPILER_CLANG || MD_COMPILER_GCC +#elif COMPILER_CLANG || COMPILER_GCC # define atomic_inc_then_eval_u64(p) __sync_add_and_fetch(p, 1) #else # error Not implemented for this compiler @@ -95,14 +95,14 @@ parse_worker_loop(ThreadData *thread_data) atomic_inc_then_eval_u64(&task->thread_counter); } -#if MD_OS_WINDOWS +#if OS_WINDOWS DWORD parse_worker_win32(LPVOID parameter) { parse_worker_loop((ThreadData*)parameter); return(0); } -#elif MD_OS_MAC || MD_OS_LINUX +#elif OS_MAC || OS_LINUX void * parse_worker_pthread(void *parameter) { @@ -143,13 +143,13 @@ main(int argc, char **argv) // launch the worker threads // (no worker thread 0) -#if MD_OS_WINDOWS +#if OS_WINDOWS HANDLE handles[THREAD_COUNT]; for (int i = 1; i < THREAD_COUNT; i += 1) { handles[i] = CreateThread(0, 0, &parse_worker_win32, threads + i, 0, 0); } -#elif MD_OS_MAC || MD_OS_LINUX +#elif OS_MAC || OS_LINUX pthread_t handles[THREAD_COUNT]; for (int i = 1; i < THREAD_COUNT; i += 1) { @@ -163,13 +163,13 @@ main(int argc, char **argv) parse_worker_loop(&threads[0]); // wait for all threads to be finished -#if MD_OS_WINDOWS +#if OS_WINDOWS WaitForMultipleObjects(THREAD_COUNT-1, handles+1, TRUE, INFINITE); for (int i = 1; i < THREAD_COUNT; i += 1) { CloseHandle(handles[i]); } -#elif MD_OS_MAC || MD_OS_LINUX +#elif OS_MAC || OS_LINUX for (int i = 1; i < THREAD_COUNT; i += 1) { pthread_join(handles[i], 0); diff --git a/gen_c11/c11.refactor b/gen_c11/c11.refactor index 200a6b6..5b7a9da 100644 --- a/gen_c11/c11.refactor +++ b/gen_c11/c11.refactor @@ -15,11 +15,21 @@ // base module -word global, md_global -word internal, md_internal +word HAS_ATTRIBUTE, MD_HAS_ATTRIBUTE + +namespace ARCH_, MD_ARCH_ +namespace COMPILER_, MD_COMPILER_ +namespace GCC_, MD_GCC_ +namespace LANG_, MD_LANG_ +namespace OS_, MD_OS_ + +word global, md_global +word internal, md_internal + word local_persist, md_local_persist word thread_static, md_thread_static +word expect, md_expect word likely, md_likely word unlikely, md_unlikely @@ -56,6 +66,54 @@ word B8, MD_B8 word B16, MD_B16 word B32, MD_B32 +word KILOBYTES, MD_KILOBYTES +word MEGABYTES, MD_MEGABYTES +word GIGABYTES, MD_GIGABYTES +word TERABYTES, MD_TERABYTES + +word KB, MD_KB +word MB, MD_MB +word GB, MD_GB +word TB, MD_TB + +word thosuand, md_thousand +word million, md_million +word billion, md_billion + +word min, md_min +word max, md_max + +word clamp_top, md_clamp_top +word clamp_bot, md_clamp_bot +word clamp, md_clamp + +word align_of, md_align_of +word offset_of, md_offset_of +word member_from_offset, md_member_from_offset +word cast_from_member, md_cast_from_member + +word defer_loop, md_defer_loop +word defer_loop_checked, md_defer_loop_checked + +word each_enum_val, md_each_enum_val +word each_non_zero_enum_val, md_each_non_zero_enum_val + +word memory_copy, md_memory_copy +word memory_set, md_memory_set +word memory_compare, md_memory_compare +word memory_str_len, md_memory_str_len + +word memory_copy_struct, md_memory_copy_struct +word memory_copy_array, md_memory_copy_array +word memory_copy_type, md_memory_copy_type + +word memory_zero, md_memory_zero +word memory_zero_struct, md_memory_zero_struct +word memroy_zero_array, md_memory_zero_array +word memory_zero_type, md_memory_zero_type + + + word swap, md_swap word readonly, md_readonly diff --git a/source/md.c b/source/md.c index 6be1e69..7bb4bdb 100644 --- a/source/md.c +++ b/source/md.c @@ -127,13 +127,13 @@ MD_CRT_LoadEntireFile(MD_Arena *arena, MD_String8 filename) //////////////////////////////////////////////////////////////////////////////// //- win32 header -#if (MD_DEFAULT_FILE_ITER || MD_2DEFAULT_MEMORY) && MD_OS_WINDOWS +#if (MD_DEFAULT_FILE_ITER || MD_2DEFAULT_MEMORY) && OS_WINDOWS # include # pragma comment(lib, "User32.lib") #endif //- win32 "file iteration" -#if MD_DEFAULT_FILE_ITER && MD_OS_WINDOWS +#if MD_DEFAULT_FILE_ITER && OS_WINDOWS #if !defined(MD_IMPL_FileIterBegin) # define MD_IMPL_FileIterBegin MD_WIN32_FileIterBegin @@ -230,7 +230,7 @@ MD_WIN32_FileIterEnd(MD_FileIter *it) #endif //- win32 "low level memory" -#if MD_DEFAULT_MEMORY && MD_OS_WINDOWS +#if MD_DEFAULT_MEMORY && OS_WINDOWS #if !defined(MD_IMPL_Reserve) # define MD_IMPL_Reserve MD_WIN32_Reserve @@ -279,7 +279,7 @@ MD_WIN32_Release(void *ptr, MD_u64 size) //////////////////////////////////////////////////////////////////////////////// //- linux headers -#if (MD_DEFAULT_FILE_ITER || MD_DEFAULT_MEMORY) && (MD_OS_LINUX || MD_OS_MAC) +#if (MD_DEFAULT_FILE_ITER || MD_DEFAULT_MEMORY) && (OS_LINUX || OS_MAC) # include # include # include @@ -300,7 +300,7 @@ MD_WIN32_Release(void *ptr, MD_u64 size) #endif //- linux "file iteration" -#if MD_DEFAULT_FILE_ITER && MD_OS_LINUX +#if MD_DEFAULT_FILE_ITER && OS_LINUX #if !defined(MD_IMPL_FileIterIncrement) # define MD_IMPL_FileIterIncrement MD_LINUX_FileIterIncrement @@ -368,7 +368,7 @@ MD_LINUX_FileIterIncrement(MD_Arena *arena, MD_FileIter *opaque_it, MD_String8 p #endif //- linux "low level memory" -#if MD_DEFAULT_MEMORY && (MD_OS_LINUX || MD_OS_MAC) +#if MD_DEFAULT_MEMORY && (OS_LINUX || OS_MAC) #if !defined(MD_IMPL_Reserve) # define MD_IMPL_Reserve MD_LINUX_Reserve diff --git a/source/md.h b/source/md.h index ff44627..65a3fde 100644 --- a/source/md.h +++ b/source/md.h @@ -92,26 +92,26 @@ #if defined(__clang__) -# define MD_COMPILER_CLANG 1 +# define COMPILER_CLANG 1 # if defined(__APPLE__) && defined(__MACH__) -# define MD_OS_MAC 1 +# define OS_MAC 1 # elif defined(__gnu_linux__) -# define MD_OS_LINUX 1 +# define OS_LINUX 1 # elif defined(_WIN32) -# define MD_OS_WINDOWS 1 +# define OS_WINDOWS 1 # else # error This compiler/platform combo is not supported yet # endif # if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) -# define MD_ARCH_X64 1 +# define ARCH_X64 1 # elif defined(i386) || defined(__i386) || defined(__i386__) -# define MD_ARCH_X86 1 +# define ARCH_X86 1 # elif defined(__aarch64__) -# define MD_ARCH_ARM64 1 +# define ARCH_ARM64 1 # elif defined(__arm__) -# define MD_ARCH_ARM32 1 +# define ARCH_ARM32 1 # else # error architecture not supported yet # endif @@ -121,19 +121,19 @@ # define MD_COMPILER_CL 1 # if defined(_WIN32) -# define MD_OS_WINDOWS 1 +# define OS_WINDOWS 1 # else # error This compiler/platform combo is not supported yet # endif # if defined(_M_AMD64) -# define MD_ARCH_X64 1 +# define ARCH_X64 1 # elif defined(_M_IX86) -# define MD_ARCH_X86 1 +# define ARCH_X86 1 # elif defined(_M_ARM64) -# define MD_ARCH_ARM64 1 +# define ARCH_ARM64 1 # elif defined(_M_ARM) -# define MD_ARCH_ARM32 1 +# define ARCH_ARM32 1 # else # error architecture not supported yet # endif @@ -160,22 +160,22 @@ #elif defined(__GNUC__) || defined(__GNUG__) -# define MD_COMPILER_GCC 1 +# define COMPILER_GCC 1 # if defined(__gnu_linux__) -# define MD_OS_LINUX 1 +# define OS_LINUX 1 # else # error This compiler/platform combo is not supported yet # endif # if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) -# define MD_ARCH_X64 1 +# define ARCH_X64 1 # elif defined(i386) || defined(__i386) || defined(__i386__) -# define MD_ARCH_X86 1 +# define ARCH_X86 1 # elif defined(__aarch64__) -# define MD_ARCH_ARM64 1 +# define ARCH_ARM64 1 # elif defined(__arm__) -# define MD_ARCH_ARM32 1 +# define ARCH_ARM32 1 # else # error architecture not supported yet # endif @@ -184,14 +184,14 @@ # error This compiler is not supported yet #endif -#if defined(MD_ARCH_X64) -# define MD_ARCH_64BIT 1 -#elif defined(MD_ARCH_X86) -# define MD_ARCH_32BIT 1 +#if defined(ARCH_X64) +# define ARCH_64BIT 1 +#elif defined(ARCH_X86) +# define ARCH_32BIT 1 #endif #if defined(__cplusplus) -# define MD_LANG_CPP 1 +# define LANG_CPP 1 // We can't get this 100% correct thanks to Microsoft's compiler. // So this check lets us pre-define MD_CPP_VERSION if we have to. @@ -242,64 +242,64 @@ # endif #else -# define MD_LANG_C 1 +# define LANG_C 1 #endif // zeroify -#if !defined(MD_ARCH_32BIT) -# define MD_ARCH_32BIT 0 +#if !defined(ARCH_32BIT) +# define ARCH_32BIT 0 #endif -#if !defined(MD_ARCH_64BIT) -# define MD_ARCH_64BIT 0 +#if !defined(ARCH_64BIT) +# define ARCH_64BIT 0 #endif -#if !defined(MD_ARCH_X64) -# define MD_ARCH_X64 0 +#if !defined(ARCH_X64) +# define ARCH_X64 0 #endif -#if !defined(MD_ARCH_X86) -# define MD_ARCH_X86 0 +#if !defined(ARCH_X86) +# define ARCH_X86 0 #endif -#if !defined(MD_ARCH_ARM64) -# define MD_ARCH_ARM64 0 +#if !defined(ARCH_ARM64) +# define ARCH_ARM64 0 #endif -#if !defined(MD_ARCH_ARM32) -# define MD_ARCH_ARM32 0 +#if !defined(ARCH_ARM32) +# define ARCH_ARM32 0 #endif #if !defined(MD_COMPILER_CL) # define MD_COMPILER_CL 0 #endif -#if !defined(MD_COMPILER_GCC) -# define MD_COMPILER_GCC 0 +#if !defined(COMPILER_GCC) +# define COMPILER_GCC 0 #endif -#if !defined(MD_COMPILER_CLANG) -# define MD_COMPILER_CLANG 0 +#if !defined(COMPILER_CLANG) +# define COMPILER_CLANG 0 #endif -#if !defined(MD_OS_WINDOWS) -# define MD_OS_WINDOWS 0 +#if !defined(OS_WINDOWS) +# define OS_WINDOWS 0 #endif -#if !defined(MD_OS_LINUX) -# define MD_OS_LINUX 0 +#if !defined(OS_LINUX) +# define OS_LINUX 0 #endif -#if !defined(MD_OS_MAC) -# define MD_OS_MAC 0 +#if !defined(OS_MAC) +# define OS_MAC 0 #endif -#if !defined(MD_LANG_C) -# define MD_LANG_C 0 +#if !defined(LANG_C) +# define LANG_C 0 #endif -#if !defined(MD_LANG_CPP) -# define MD_LANG_CPP 0 +#if !defined(LANG_CPP) +# define LANG_CPP 0 #endif #if !defined(MD_CPP_VERSION) # define MD_CPP_VERSION 0 #endif -#if MD_LANG_CPP +#if LANG_CPP # define MD_ZERO_STRUCT {} #else # define MD_ZERO_STRUCT {0} #endif -#if MD_LANG_C +#if LANG_C # define MD_C_LINKAGE_BEGIN # define MD_C_LINKAGE_END #else @@ -309,7 +309,7 @@ #if MD_COMPILER_CL # define MD_THREAD_LOCAL __declspec(thread) -#elif MD_COMPILER_GCC || MD_COMPILER_CLANG +#elif COMPILER_GCC || COMPILER_CLANG # define MD_THREAD_LOCAL __thread #endif @@ -968,9 +968,9 @@ MD_FUNCTION MD_u64 MD_CalculateCStringLength(char *cstr); MD_FUNCTION MD_String8 MD_S8(MD_u8 *str, MD_u64 size); #define MD_S8CString(s) MD_S8((MD_u8 *)(s), MD_CalculateCStringLength(s)) -#if MD_LANG_C +#if LANG_C # define MD_S8Lit(s) (MD_String8){(MD_u8 *)(s), sizeof(s)-1} -#elif MD_LANG_CPP +#elif LANG_CPP # define MD_S8Lit(s) MD_S8((MD_u8*)(s), sizeof(s) - 1) #endif #define MD_S8LitComp(s) {(MD_u8 *)(s), sizeof(s)-1}