128 lines
3.2 KiB
C
128 lines
3.2 KiB
C
#ifndef EXAMPLES_BASE_H
|
|
#define EXAMPLES_BASE_H
|
|
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
////////////////////////////////
|
|
// Typical Base Stuff
|
|
|
|
#include <stdint.h>
|
|
typedef int8_t S8;
|
|
typedef int16_t S16;
|
|
typedef int32_t S32;
|
|
typedef int64_t S64;
|
|
typedef uint8_t U8;
|
|
typedef uint16_t U16;
|
|
typedef uint32_t U32;
|
|
typedef uint64_t U64;
|
|
typedef S8 B8;
|
|
typedef S16 B16;
|
|
typedef S32 B32;
|
|
typedef S64 B64;
|
|
typedef float F32;
|
|
typedef double F64;
|
|
|
|
typedef struct String8{
|
|
U8 *str;
|
|
U64 size;
|
|
} String8;
|
|
|
|
#define str8_const(S) {(U8*)(S), (U64)(sizeof(S) - 1)}
|
|
#define str8(s,z) (String8){(s), (z)}
|
|
|
|
static B32 str8_match(String8 a, String8 b);
|
|
|
|
typedef struct Arena{
|
|
void *memory;
|
|
U64 pos;
|
|
U64 cap;
|
|
} Arena;
|
|
|
|
static Arena arena_alloc(U64 cap);
|
|
static void* arena_push(Arena *arena, U64 size);
|
|
static void arena_pop_to(Arena *arena, U64 pos);
|
|
static void arena_pop_amount(Arena *arena, U64 amount);
|
|
static U64 arena_pos(Arena *arena);
|
|
|
|
#define push_array(a,T,c) (T*)arena_push((a), sizeof(T)*(c))
|
|
|
|
#define Min(a,b) ((a)<(b))?(a):(b)
|
|
|
|
// Macros: Linked Lists
|
|
|
|
#define DLLPushBack_NPZ(f,l,n,next,prev,nil)\
|
|
(((f) == (nil))?\
|
|
((f)=(l)=(n),(n)->next=(n)->prev=(nil)):\
|
|
((n)->prev=(l),(l)->next=(n),(l)=(n),(n)->next=(nil)))
|
|
|
|
#define DLLPushBack(f,l,n) DLLPushBack_NPZ(f,l,n,next,prev,0)
|
|
#define DLLPushFront(f,l,n) DLLPushBack_NPZ(l,f,n,prev,next,0)
|
|
|
|
#define DLLInsert_NPZ(f,l,p,n,next,prev,nil) \
|
|
(((p) != (l))?\
|
|
((n)->next = (p)->next,\
|
|
(n)->prev = (p),\
|
|
(p)->next->prev = (n),\
|
|
(p)->next = (n))\
|
|
:((n)->next = (nil),\
|
|
(n)->prev = (l),\
|
|
(l)->next = (n),\
|
|
(l) = (n)))
|
|
|
|
#define DLLInsert(f,l,p,n) DLLInsert_NPZ(f,l,p,n,next,prev,0)
|
|
|
|
#define DLLRemove_NPZ(f,l,n,next,prev,nil)\
|
|
((f)==(n)?\
|
|
((f)==(l)?\
|
|
((f)=(l)=(nil)):\
|
|
((f)=(f)->next,(f)->prev=(nil))):\
|
|
(l)==(n)?\
|
|
((l)=(l)->prev,(l)->next=(nil)):\
|
|
((n)->next->prev=(n)->prev,\
|
|
(n)->prev->next=(n)->next))
|
|
|
|
#define DLLRemove(f,l,n) DLLRemove_NPZ(f,l,n,next,prev,0)
|
|
|
|
#define SLLQueuePush_NZ(f,l,n,next,nil) (((f)==(nil)?\
|
|
(f)=(l)=(n):\
|
|
((l)->next=(n),(l)=(n))),\
|
|
(n)->next=(nil))
|
|
#define SLLQueuePush(f,l,n) SLLQueuePush_NZ(f,l,n,next,0)
|
|
|
|
#define SLLQueuePushFront_NZ(f,l,n,next,nil) ((f)==(nil)?\
|
|
((f)=(l)=(n),(n)->next=(nil)):\
|
|
((n)->next=(f),(f)=(n)))
|
|
#define SLLQueuePushFront(f,l,n) SLLQueuePushFront_NZ(f,l,n,next,0)
|
|
|
|
#define SLLQueuePop_NZ(f,l,next,nil) ((f)==(l)?\
|
|
(f)=(l)=(nil):\
|
|
((f)=(f)->next))
|
|
#define SLLQueuePop(f,l) SLLQueuePop_NZ(f,l,next,0)
|
|
|
|
#define SLLStackPush_N(f,n,next) ((n)->next=(f),(f)=(n))
|
|
#define SLLStackPush(f,n) SLLStackPush_N(f,n,next)
|
|
|
|
#define SLLStackPop_NZ(f,next,nil) ((f)==(nil)?(nil):\
|
|
((f)=(f)->next))
|
|
#define SLLStackPop(f) SLLStackPop_NZ(f,next,0)
|
|
|
|
|
|
////////////////////////////////
|
|
// Symbol-Set Specific Upgrades
|
|
|
|
static void*copy_strided(void *dst, U32 dst_stride,
|
|
void *src, U32 src_stride,
|
|
U32 count);
|
|
|
|
static void*arena_push_symbol_set__raw(Arena *arena,
|
|
U32 size, void *src, U32 src_stride, U32 count);
|
|
|
|
#define arena_push_symbol_set(ar,S) (SyType(S)*) \
|
|
arena_push_symbol_set__raw((ar), sizeof(SyType(S)), SyFirst(S), SyStride(S), SyCount(S))
|
|
|
|
|
|
#endif //EXAMPLES_BASE_H
|