repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
opencode/test-c-skills/src/base.h
RawFile outline
- U8 line 24
- U16 line 25
- U32 line 26
- U64 line 27
- S32 line 28
- S64 line 29
- F32 line 30
- F64 line 31
- B32 line 32
- String8 line 132
- String8Node line 145
- String8List line 151
- Arena line 163
- Arena line 165
- ArenaFlags line 172
- arena_create_explicit line 178
- arena_destroy line 179
- arena_alloc line 180
- arena_alloc_zero line 181
- arena_realloc line 182
- arena_clear line 183
- arena_release line 186
- arena_push line 187
- arena_alloc_ line 188
- arena_pos line 189
- arena_pop_to line 190
- Temp line 193
- temp_begin line 199
- temp_end line 200
- scratch_arena line 203
- scratch_init line 204
- scratch_alloc line 205
- Rng1U64 line 217
- rng_1u64 line 222
- rng_1u64_f64 line 223
- rng_1u64_seed line 224
- OS_Handle line 230
- OS_SystemInfo line 235
- LogLevel line 268
- log_message line 277
#ifndef BASE_H
#define BASE_H
/*
* base.h - Base types, macros, and utilities for C skill examples
*/
// ============================================================================
// Standard Library Includes
// ============================================================================
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
// ============================================================================
// Basic Types
// ============================================================================
typedef uint8_t U8;
typedef uint16_t U16;
typedef uint32_t U32;
typedef uint64_t U64;
typedef int32_t S32;
typedef int64_t S64;
typedef float F32;
typedef double F64;
typedef bool B32;
// ============================================================================
// Visibility Macros
// ============================================================================
#if defined(__cplusplus)
#define internal /* internal linkage */
#define global /* global scope */
#define local_persist static /* persistent local storage */
#elif defined(_MSC_VER)
#define internal static
#define global /* global scope */
#define local_persist static
#else
#define internal static
#define global /* global scope */
#define local_persist static
#endif
// ============================================================================
// Compiler-Specific Visibility
// ============================================================================
#if defined(__GNUC__) || defined(__clang__)
#define export __attribute__((visibility("default")))
#else
#define export
#endif
// ============================================================================
// Utility Macros
// ============================================================================
#define ArrayCount(arr) (sizeof(arr) / sizeof((arr)[0]))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Clamp(v, lo, hi) (Max((lo), Min((hi), (v))))
#define ClampTop(v, hi) Min((v), (hi))
#define AlignPow2(value, align) (((value) + (align) - 1) & ~((align) - 1))
#define AlignDownPow2(value, align) ((value) & ~((align) - 1))
// ============================================================================
// Memory Macros
// ============================================================================
#define MemoryZero(ptr, size) memset((ptr), 0, (size))
#define MemoryZeroStruct(ptr) MemoryZero((ptr), sizeof(*(ptr)))
#define MemoryCopy(dst, src, size) memcpy((dst), (src), (size))
#define MemoryCopyStruct(dst, src) MemoryCopy((dst), (src), sizeof(*(dst)))
#define MemoryMove(dst, src, size) memmove((dst), (src), (size))
// ============================================================================
// Memory Size Macros
// ============================================================================
#define KB(x) ((U64)(x) * 1024ULL)
#define MB(x) ((U64)(x) * 1024ULL * 1024ULL)
#define GB(x) ((U64)(x) * 1024ULL * 1024ULL * 1024ULL)
// ============================================================================
// Assert Macros
// ============================================================================
#if defined(BASE_DEBUG)
#include <assert.h>
#define Assert(expr) assert(expr)
#define AssertAlways(expr) assert(expr)
#else
#define Assert(expr) ((void)0)
#define AssertAlways(expr) ((void)0)
#endif
#define Trap() (*(volatile int *)0 = 0)
#define InvalidPath Trap()
#define InvalidPathIf(expr) if (expr) { InvalidPath; }
#define NotImplemented AssertAlways(!"NotImplemented")
#define NotImplementedIf(expr) if (expr) { NotImplemented; }
// ============================================================================
// Branch Prediction Hints
// ============================================================================
#if defined(__GNUC__) || defined(__clang__)
#define Likely(x) __builtin_expect(!!(x), 1)
#define Unlikely(x) __builtin_expect(!!(x), 0)
#else
#define Likely(x) (x)
#define Unlikely(x) (x)
#endif
// ============================================================================
// String8 Type
// ============================================================================
typedef struct String8
{
U8 *str;
U64 len;
} String8;
#define str8_lit(s) ((String8){(U8 *)(s), sizeof(s) - 1})
#define str8v_lit(s) ((String8){(U8 *)(s), (U64)strlen((const char *)(s))})
// ============================================================================
// String8 List
// ============================================================================
typedef struct String8Node
{
String8 string;
struct String8Node *next;
} String8Node;
typedef struct String8List
{
String8Node *first;
String8Node *last;
U64 count;
U64 total_size;
} String8List;
// ============================================================================
// Arena Type (Memory Allocator)
// ============================================================================
typedef struct Arena Arena;
struct Arena
{
U8 *buffer;
U64 capacity;
U64 pos;
};
typedef enum ArenaFlags
{
ArenaFlag_ClearToZero = (1 << 0),
} ArenaFlags;
// Arena forward declarations
Arena *arena_create_explicit(U64 capacity, ArenaFlags flags);
void arena_destroy(Arena *arena);
void *arena_alloc(Arena *arena, U64 size);
void *arena_alloc_zero(Arena *arena, U64 size);
void *arena_realloc(Arena *arena, void *old_ptr, U64 old_size, U64 new_size);
void arena_clear(Arena *arena);
// Additional arena functions from base.c
void arena_release(Arena *arena);
void *arena_push(Arena *arena, U64 size);
void *arena_alloc_(Arena *arena, U64 size, U64 align);
U64 arena_pos(Arena *arena);
void arena_pop_to(Arena *arena, U64 pos);
// Temp arena for temporary allocations
typedef struct Temp
{
Arena *arena;
U64 pos;
} Temp;
Temp temp_begin(Arena *arena);
void temp_end(Temp temp);
// Scratch arena
extern Arena scratch_arena;
void scratch_init(void);
void *scratch_alloc(U64 size, U64 align);
// Convenience macros
#define arena_create(cap) arena_create_explicit(cap, ArenaFlag_ClearToZero)
#define arena_push_type(arena, type) ((type *)arena_push(arena, sizeof(type)))
#define arena_push_array(arena, type, count) ((type *)arena_push(arena, sizeof(type) * (count)))
#define arena_push_zero(arena, type) ((type *)arena_alloc_zero(arena, sizeof(type)))
// ============================================================================
// Random Number Generator (xorshift64)
// ============================================================================
typedef struct Rng1U64
{
U64 state;
} Rng1U64;
U64 rng_1u64(Rng1U64 *rng);
F64 rng_1u64_f64(Rng1U64 *rng);
void rng_1u64_seed(Rng1U64 *rng, U64 seed);
// ============================================================================
// OS Types (Simplified)
// ============================================================================
typedef struct OS_Handle
{
U64 id;
} OS_Handle;
typedef struct OS_SystemInfo
{
U32 processor_count;
U64 total_memory;
U64 page_size;
char *os_name;
char *os_version;
} OS_SystemInfo;
// ============================================================================
// OS Handle Utilities
// ============================================================================
#define os_handle_is_invalid(h) ((h).id == 0)
// ============================================================================
// Token Pasting Utilities
// ============================================================================
#define CONCAT_IMPL(a, b) a##b
#define CONCAT(a, b) CONCAT_IMPL(a, b)
#define CONCAT3(a, b, c) CONCAT(CONCAT(a, b), c)
// ============================================================================
// String Conversion
// ============================================================================
#define str8_from_cstr(cstr) ((String8){(U8 *)(cstr), (U64)strlen(cstr)})
// ============================================================================
// Logging (Basic)
// ============================================================================
typedef enum LogLevel
{
LogLevel_Trace = 0,
LogLevel_Debug = 1,
LogLevel_Info = 2,
LogLevel_Warn = 3,
LogLevel_Error = 4,
} LogLevel;
void log_message(LogLevel level, const char *file, int line, const char *fmt, ...);
#define LOG_TRACE(fmt, ...) log_message(LogLevel_Trace, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define LOG_DEBUG(fmt, ...) log_message(LogLevel_Debug, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define LOG_INFO(fmt, ...) log_message(LogLevel_Info, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define LOG_WARN(fmt, ...) log_message(LogLevel_Warn, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define LOG_ERROR(fmt, ...) log_message(LogLevel_Error, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
// ============================================================================
// DLL/Linked List Macros
// ============================================================================
#define DLLInsert_NP(f, l, n, next, prev) \
do { \
(n)->next = NULL; \
(n)->prev = NULL; \
if ((f) == NULL) { \
(f) = (l) = (n); \
} else { \
(l)->next = (n); \
(n)->prev = (l); \
(l) = (n); \
} \
} while (0)
#define DLLRemove_NP(f, l, n, next, prev) \
do { \
if ((n)->prev) (n)->prev->next = (n)->next; \
else (f) = (n)->next; \
if ((n)->next) (n)->next->prev = (n)->prev; \
else (l) = (n)->prev; \
(n)->next = (n)->prev = NULL; \
} while (0)
// ============================================================================
// Intrinsics
// ============================================================================
#if defined(__GNUC__) || defined(__clang__)
#define clz(x) __builtin_clzll(x)
#define ctz(x) __builtin_ctzll(x)
#define popcount(x) __builtin_popcountll(x)
#elif defined(_MSC_VER)
#include <intrin.h>
#define clz(x) __intrinsic_clz64(x)
#define ctz(x) __builtin_ctzll(x)
#define popcount(x) __popcnt64(x)
#else
#define clz(x) (64 - 1 - 32 - 1 - 16 - 1 - 8 - 1 - 4 - 1 - 2 - 1 - 1)
#define ctz(x) 0
#define popcount(x) 0
#endif
// ============================================================================
// Function Attributes (for future use)
// ============================================================================
#if defined(__GNUC__) || defined(__clang__)
#define force_inline __attribute__((always_inline)) inline
#define no_inline __attribute__((noinline))
#define unused __attribute__((unused))
#define pure __attribute__((pure))
#define const_func __attribute__((const))
#elif defined(_MSC_VER)
#define force_inline __forceinline
#define no_inline __declspec(noinline)
#define unused
#define pure
#define const_func
#else
#define force_inline inline
#define no_inline
#define unused
#define pure
#define const_func
#endif
// ============================================================================
// End Include Guard
// ============================================================================
#endif // BASE_H