mirror of
https://github.com/Ed94/metadesk.git
synced 2026-08-01 20:28:13 +00:00
starting to offload MD namespace for repo to gen_c11 .refactor script and gen_cpp17 to namespace macros
This commit is contained in:
@@ -30,6 +30,24 @@
|
||||
#define local_persist static
|
||||
#endif
|
||||
|
||||
#if MD_COMPILER_MSVC
|
||||
# define thread_static __declspec(thread)
|
||||
#elif MD_COMPILER_CLANG || MD_COMPILER_GCC
|
||||
# define thread_static __thread
|
||||
#endif
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: Branch Predictor Hints
|
||||
|
||||
#if defined(__clang__)
|
||||
# define expect(expr, val) __builtin_expect((expr), (val))
|
||||
#else
|
||||
# define expect(expr, val) (expr)
|
||||
#endif
|
||||
|
||||
#define likely(expr) expect(expr,1)
|
||||
#define unlikely(expr) expect(expr,0)
|
||||
|
||||
#if MD_COMPILER_CPP
|
||||
# ifndef ccast
|
||||
# define ccast( type, value ) ( const_cast< type >( (value) ) )
|
||||
@@ -106,6 +124,17 @@
|
||||
# define MD_PARAM_DEFAULT
|
||||
#endif
|
||||
|
||||
#if MD_COMPILER_C
|
||||
#ifndef static_assert
|
||||
#undef static_assert
|
||||
#if MD_COMPILER_C && __STDC_VERSION__ >= 201112L
|
||||
#define static_assert(condition, message) _Static_assert(condition, message)
|
||||
#else
|
||||
#define static_assert(condition, message) typedef char static_assertion_##__LINE__[(condition)?1:-1]
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if MD_DONT_USE_NAMESPACE || MD_COMPILER_C
|
||||
# if MD_COMPILER_C
|
||||
# define MD_NS
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
#if MD_INTELLISENE_DIRECTIVES
|
||||
#pragma once
|
||||
#include "base_types.h"
|
||||
#endif
|
||||
|
||||
#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 )
|
||||
|
||||
#define swap(a, b) \
|
||||
do \
|
||||
{ \
|
||||
typeof(a) tmp = a; \
|
||||
a = b; \
|
||||
b = tmp; \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
#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
|
||||
|
||||
|
||||
+13
-12
@@ -1,13 +1,16 @@
|
||||
#ifdef MD_INTELLISENSE_DIRECTIVES
|
||||
#pragma once
|
||||
#include "base_types.h"
|
||||
#include "macros.h"
|
||||
#endif
|
||||
|
||||
// Copyright (c) 2024 Epic Games Tools
|
||||
// Licensed under the MIT license (https://opensource.org/license/mit/)
|
||||
|
||||
#ifndef BASE_ARENA_H
|
||||
#define BASE_ARENA_H
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: Constants
|
||||
|
||||
#define ARENA_HEADER_SIZE 64
|
||||
#define MD_ARENA_HEADER_SIZE 64
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: Types
|
||||
@@ -41,10 +44,10 @@ struct Arena
|
||||
U64 cmt;
|
||||
U64 res;
|
||||
};
|
||||
StaticAssert(sizeof(Arena) <= ARENA_HEADER_SIZE, arena_header_size_check);
|
||||
static_assert(sizeof(Arena) <= MD_ARENA_HEADER_SIZE, "sizeof(Arena) <= MD_ARENA_HEADER_SIZE");
|
||||
|
||||
typedef struct Temp Temp;
|
||||
struct Temp
|
||||
typedef struct TempArena TempArena;
|
||||
struct TempArena
|
||||
{
|
||||
Arena *arena;
|
||||
U64 pos;
|
||||
@@ -56,7 +59,7 @@ struct Temp
|
||||
//- rjf: arena creation/destruction
|
||||
internal Arena *arena_alloc_(ArenaParams *params);
|
||||
#define arena_alloc(...) arena_alloc_(&(ArenaParams){.reserve_size = MB(64), .commit_size = KB(64), __VA_ARGS__})
|
||||
internal void arena_release(Arena *arena);
|
||||
internal void rarena_release(Arena *arena);
|
||||
|
||||
//- rjf: arena push/pop/pos core functions
|
||||
internal void *arena_push(Arena *arena, U64 size, U64 align);
|
||||
@@ -68,13 +71,11 @@ internal void arena_clear(Arena *arena);
|
||||
internal void arena_pop(Arena *arena, U64 amt);
|
||||
|
||||
//- rjf: temporary arena scopes
|
||||
internal Temp temp_begin(Arena *arena);
|
||||
internal void temp_end(Temp temp);
|
||||
internal TempArena temp_arena_begin(Arena *arena);
|
||||
internal void temp_arena_end(TempArena temp);
|
||||
|
||||
//- rjf: push helper macros
|
||||
#define push_array_no_zero_aligned(a, T, c, align) (T *)arena_push((a), sizeof(T)*(c), (align))
|
||||
#define push_array_aligned(a, T, c, align) (T *)MemoryZero(push_array_no_zero_aligned(a, T, c, align), sizeof(T)*(c))
|
||||
#define push_array_no_zero(a, T, c) push_array_no_zero_aligned(a, T, c, Max(8, AlignOf(T)))
|
||||
#define push_array(a, T, c) push_array_aligned(a, T, c, Max(8, AlignOf(T)))
|
||||
|
||||
#endif // BASE_ARENA_H
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
#ifdef MD_INTELLISENSE_DIRECTIVES
|
||||
#pragma once
|
||||
#include "base_types.h"
|
||||
#endif
|
||||
|
||||
#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 )
|
||||
|
||||
#define swap(a, b) \
|
||||
do \
|
||||
{ \
|
||||
typeof(a) tmp = a; \
|
||||
a = b; \
|
||||
b = tmp; \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
#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
|
||||
|
||||
enum AllocType : u8
|
||||
{
|
||||
EAllocType_ALLOC,
|
||||
EAllocType_FREE,
|
||||
EAllocType_FREE_ALL,
|
||||
EAllocType_RESIZE,
|
||||
};
|
||||
|
||||
typedef void*(AllocatorProc)( void* allocator_data, AllocType type, SSIZE size, SSIZE alignment, void* old_memory, SSIZE old_size, U64 flags );
|
||||
|
||||
struct AllocatorInfo
|
||||
{
|
||||
AllocatorProc* proc;
|
||||
void* data;
|
||||
};
|
||||
|
||||
enum AllocFlag
|
||||
{
|
||||
ALLOCATOR_FLAG_CLEAR_TO_ZERO = 0,
|
||||
};
|
||||
|
||||
#ifndef MD_DEFAULT_MEMORY_ALIGNMENT
|
||||
# define MD_DEFAULT_MEMORY_ALIGNMENT ( 2 * size_of( void* ) )
|
||||
#endif
|
||||
|
||||
#ifndef MD_DEFAULT_ALLOCATOR_FLAGS
|
||||
# define MD_DEFAULT_ALLOCATOR_FLAGS ( ALLOCATOR_FLAG_CLEAR_TO_ZERO )
|
||||
#endif
|
||||
|
||||
//! Allocate memory with default alignment.
|
||||
void* alloc( AllocatorInfo a, SSIZE size );
|
||||
|
||||
//! Allocate memory with specified alignment.
|
||||
void* alloc_align( AllocatorInfo a, SSIZE size, SSIZE alignment );
|
||||
|
||||
//! Free allocated memory.
|
||||
void allocator_free( AllocatorInfo a, void* ptr );
|
||||
|
||||
//! Free all memory allocated by an allocator.
|
||||
void free_all( AllocatorInfo a );
|
||||
|
||||
//! Resize an allocated memory.
|
||||
void* resize( AllocatorInfo a, void* ptr, SSIZE old_size, SSIZE new_size );
|
||||
|
||||
//! Resize an allocated memory with specified alignment.
|
||||
void* resize_align( AllocatorInfo a, void* ptr, SSIZE old_size, SSIZE new_size, SSIZE alignment );
|
||||
|
||||
//! Allocate memory for an item.
|
||||
#define alloc_item( allocator_, Type ) ( Type* )alloc( allocator_, size_of( Type ) )
|
||||
|
||||
//! Allocate memory for an array of items.
|
||||
#define alloc_array( allocator_, Type, count ) ( Type* )alloc( allocator_, size_of( Type ) * ( count ) )
|
||||
|
||||
/* heap memory analysis tools */
|
||||
/* define GEN_HEAP_ANALYSIS to enable this feature */
|
||||
/* call zpl_heap_stats_init at the beginning of the entry point */
|
||||
/* you can call zpl_heap_stats_check near the end of the execution to validate any possible leaks */
|
||||
MD_API void heap_stats_init( void );
|
||||
MD_API SSIZE heap_stats_used_memory( void );
|
||||
MD_API SSIZE heap_stats_alloc_count( void );
|
||||
MD_API void heap_stats_check( void );
|
||||
|
||||
//! Allocate/Resize memory using default options.
|
||||
|
||||
//! Use this if you don't need a "fancy" resize allocation
|
||||
void* default_resize_align( AllocatorInfo a, void* ptr, SSIZE old_size, SSIZE new_size, SSIZE alignment );
|
||||
|
||||
MD_API void* heap_allocator_proc( void* allocator_data, AllocType type, SSIZE size, SSIZE alignment, void* old_memory, SSIZE old_size, U64 flags );
|
||||
|
||||
//! The heap allocator backed by operating system's memory manager.
|
||||
constexpr AllocatorInfo heap( void ) { AllocatorInfo allocator = { heap_allocator_proc, nullptr }; return allocator; }
|
||||
|
||||
//! Helper to allocate memory using heap allocator.
|
||||
#define malloc( sz ) alloc( heap(), sz )
|
||||
|
||||
//! Helper to free memory allocated by heap allocator.
|
||||
#define mfree( ptr ) free( heap(), ptr )
|
||||
|
||||
Reference in New Issue
Block a user