mirror of
https://github.com/Ed94/pikuma_ps1.git
synced 2026-06-01 18:41:13 -07:00
Sorting Primitives into the OT
Started to add some ergonomic definitions into duffle.
This commit is contained in:
+43
-2
@@ -1,5 +1,6 @@
|
||||
#ifdef INTELLISENSE_DIRECTIVES
|
||||
# pragma once
|
||||
# include "assert.h"
|
||||
#endif
|
||||
|
||||
typedef unsigned char U8;
|
||||
@@ -27,7 +28,7 @@ enum {
|
||||
|
||||
#define alignas _Alignas
|
||||
#define alignof _Alignof
|
||||
#define byte_pad(amount, ...) Byte glue(_PAD_, __VA_ARGS__) [amount]
|
||||
#define byte_pad(amount, ...) BYTE glue(_PAD_, __VA_ARGS__) [amount]
|
||||
#define farray_len(array) (SSIZE)sizeof(array) / size_of( typeof((array)[0]))
|
||||
#define farray_init(type, ...) (type[]){__VA_ARGS__}
|
||||
#define def_farray(type, len) type A ## len ## _ ## type[len]
|
||||
@@ -38,7 +39,10 @@ enum {
|
||||
#define opt_args(symbol, ...) &(symbol){__VA_ARGS__}
|
||||
#define ret_type(type) type
|
||||
#define local_persist static
|
||||
#define global static
|
||||
#define internal static
|
||||
#define global
|
||||
#define gknown
|
||||
#define ct_lit
|
||||
#define offset_of(type, member) cast(SSIZE, & (((type*) 0)->member))
|
||||
#define static_assert _Static_assert
|
||||
#define typeof __typeof__
|
||||
@@ -70,3 +74,40 @@ typedef def_span(U32);
|
||||
typedef def_span(SSIZE);
|
||||
|
||||
typedef void def_proc(VoidFn) (void);
|
||||
|
||||
#define def_Slice(type) \
|
||||
def_struct(tmpl(Slice,type)) { \
|
||||
type* ptr; \
|
||||
SSIZE len; \
|
||||
}
|
||||
#define slice_assert(slice) do { assert((slice).ptr != nullptr); assert((slice).len > 0); } while(0)
|
||||
#define slice_end(slice) ((slice).ptr + (slice).len)
|
||||
#define size_of_slice_type(slice) size_of( * (slice).ptr )
|
||||
|
||||
typedef def_Slice(void);
|
||||
typedef def_Slice(BYTE);
|
||||
#define slice_byte(slice) ((Slice_BYTE){cast(Byte*, (slice).ptr), (slice).len * size_of_slice_type(slice)})
|
||||
#define slice_fmem(mem) ((Slice_BYTE){ mem, size_of(mem) })
|
||||
|
||||
void slice__copy(Slice_BYTE dest, SSIZE dest_typewidth, Slice_BYTE src, SSIZE src_typewidth);
|
||||
void slice__zero(Slice_BYTE mem, SSIZE typewidth);
|
||||
#define slice_copy(dest, src) do { \
|
||||
static_assert(typeof_same(dest, src)); \
|
||||
slice__copy(slice_byte(dest), size_of_slice_type(dest), slice_byte(src), size_of_slice_type(src)); \
|
||||
} while (0)
|
||||
#define slice_zero(slice) slice__zero(slice_byte(slice), size_of_slice_type(slice))
|
||||
|
||||
#define slice_iter(container, iter) \
|
||||
typeof((container).ptr) iter = (container).ptr; \
|
||||
iter != slice_end(container); \
|
||||
++ iter
|
||||
#define slice_arg_from_array(type, ...) & (tmpl(Slice,type)) { \
|
||||
.ptr = farray_init(type, __VA_ARGS__), \
|
||||
.len = farray_len( farray_init(type, __VA_ARGS__)) \
|
||||
}
|
||||
|
||||
typedef unsigned char UTF8;
|
||||
typedef def_Slice(UTF8);
|
||||
typedef Slice_UTF8 Str8;
|
||||
typedef def_Slice(Str8);
|
||||
#define txt(string_literal) (Str8){ (UTF8*) string_literal, size_of(string_literal) - 1 }
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifdef INTELLISENSE_DIRECTIVES
|
||||
# pragma once
|
||||
# include "dsl.h"
|
||||
# include "memory.h"
|
||||
# include "strings.h"
|
||||
#endif
|
||||
|
||||
typedef def_struct(Opts_farena) {
|
||||
Str8 type_name;
|
||||
SSIZE alignment;
|
||||
};
|
||||
typedef def_struct(FArena) {
|
||||
void* start;
|
||||
SSIZE capacity;
|
||||
SSIZE used;
|
||||
};
|
||||
FArena farena_make (Slice_BYTE mem);
|
||||
void farena_init (FArena* arena, Slice_BYTE byte);
|
||||
Slice_BYTE farena__push (FArena* arena, SSIZE amount, SSIZE type_width, Opts_farena* opts);
|
||||
void farena_reset (FArena* arena);
|
||||
void farena_rewind(FArena* arena, AllocatorSP save_point);
|
||||
AllocatorSP farena_save (FArena arena);
|
||||
|
||||
// void farena_allocator_proc(AllocatorProc_In in, AllocatorProc_Out* out);
|
||||
// #define ainfo_farena(arena) (AllocatorInfo){ .proc = farena_allocator_proc, .data = & arena }
|
||||
|
||||
#define farena_push(arena, type, ...) \
|
||||
cast(type*, farena__push(arena, size_of(type), 1, opt_args(Opts_farena_push, lit(stringify(type)), __VA_ARGS__))).ptr
|
||||
|
||||
#define farena_push_array(arena, type, amount, ...) \
|
||||
(Slice ## type){ farena__push(arena, size_of(type), amount, opt_args(Opts_farena_push, lit(stringify(type)), __VA_ARGS__)).ptr, amount }
|
||||
@@ -3,6 +3,10 @@
|
||||
# include "dsl.h"
|
||||
#endif
|
||||
|
||||
#define min(A, B) (((A) < (B)) ? (A) : (B))
|
||||
#define max(A, B) (((A) > (B)) ? (A) : (B))
|
||||
#define clamp_bot(X, B) max(X, B)
|
||||
|
||||
typedef def_farray(S16, 2);
|
||||
typedef def_farray(S32, 2);
|
||||
// typedef def_farray(F32, 2);
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
#ifdef INTELLISENSE_DIRECTIVES
|
||||
# pragma once
|
||||
# include "dsl.h"
|
||||
#endif
|
||||
|
||||
inline SSIZE align_pow2(SSIZE x, SSIZE b) {
|
||||
assert(b != 0);
|
||||
assert((b & (b - 1)) == 0); // Check power of 2
|
||||
return ((x + b - 1) & (~(b - 1)));
|
||||
}
|
||||
|
||||
#define align_struct(type_width) ((SSIZE)(((type_width) + 3) & ~3))
|
||||
|
||||
#define assert_bounds(point, start, end) do { \
|
||||
SSIZE pos_point = cast(SSIZE, point); \
|
||||
SSIZE pos_start = cast(SSIZE, start); \
|
||||
SSIZE pos_end = cast(SSIZE, end); \
|
||||
assert(pos_start <= pos_point); \
|
||||
assert(pos_point <= pos_end); \
|
||||
} while(0)
|
||||
|
||||
// void* memory_copy (void* restrict dest, void const* restrict src, USIZE length);
|
||||
// void* memory_copy_overlapping(void* restrict dest, void const* restrict src, USIZE length);
|
||||
// B32 memory_zero (void* dest, USIZE length);
|
||||
|
||||
#define check_nil(nil, p) ((p) == 0 || (p) == nil)
|
||||
#define set_nil(nil, p) ((p) = nil)
|
||||
|
||||
#define sll_stack_push_n(f, n, next) do { (n)->next = (f); (f) = (n); } while(0)
|
||||
|
||||
#define sll_queue_push_nz(nil, f, l, n, next) \
|
||||
( \
|
||||
check_nil(nil, f) ? ( \
|
||||
(f) = (l) = (n), \
|
||||
set_nil(nil, (n)->next) \
|
||||
) \
|
||||
: ( \
|
||||
(l)->next=(n), \
|
||||
(l) = (n), \
|
||||
set_nil(nil,(n)->next) \
|
||||
) \
|
||||
)
|
||||
#define sll_queue_push_n(f, l, n, next) sll_queue_push_nz(0, f, l, n, next)
|
||||
|
||||
#pragma region Allocator Interface
|
||||
typedef def_enum(U32, AllocatorOp) {
|
||||
AllocatorOp_Alloc_NoZero = 0, // If Alloc exist, so must No_Zero
|
||||
AllocatorOp_Alloc,
|
||||
AllocatorOp_Free,
|
||||
AllocatorOp_Reset,
|
||||
AllocatorOp_Grow_NoZero,
|
||||
AllocatorOp_Grow,
|
||||
AllocatorOp_Shrink,
|
||||
AllocatorOp_Rewind,
|
||||
AllocatorOp_SavePoint,
|
||||
AllocatorOp_Query, // Must always be implemented
|
||||
};
|
||||
typedef def_enum(U32, AllocatorQueryFlags) {
|
||||
AllocatorQuery_Alloc = (1 << 0),
|
||||
AllocatorQuery_Free = (1 << 1),
|
||||
// Wipe the allocator's state
|
||||
AllocatorQuery_Reset = (1 << 2),
|
||||
// Supports both grow and shrink
|
||||
AllocatorQuery_Shrink = (1 << 4),
|
||||
AllocatorQuery_Grow = (1 << 5),
|
||||
AllocatorQuery_Resize = AllocatorQuery_Grow | AllocatorQuery_Shrink,
|
||||
// Ability to rewind to a save point (ex: arenas, stack), must also be able to save such a point
|
||||
AllocatorQuery_Rewind = (1 << 6),
|
||||
};
|
||||
typedef struct AllocatorProc_In AllocatorProc_In;
|
||||
typedef struct AllocatorProc_Out AllocatorProc_Out;
|
||||
typedef void def_proc(AllocatorProc) (AllocatorProc_In In, AllocatorProc_Out* Out);
|
||||
typedef def_struct(AllocatorSP) {
|
||||
AllocatorProc* type_sig;
|
||||
SSIZE slot;
|
||||
};
|
||||
struct AllocatorProc_In {
|
||||
void* data;
|
||||
SSIZE requested_size;
|
||||
SSIZE alignment;
|
||||
union {
|
||||
Slice_BYTE old_allocation;
|
||||
AllocatorSP save_point;
|
||||
};
|
||||
AllocatorOp op;
|
||||
byte_pad(4);
|
||||
};
|
||||
struct AllocatorProc_Out {
|
||||
union {
|
||||
Slice_BYTE allocation;
|
||||
AllocatorSP save_point;
|
||||
};
|
||||
AllocatorQueryFlags features;
|
||||
SSIZE left; // Contiguous memory left
|
||||
SSIZE max_alloc;
|
||||
SSIZE min_alloc;
|
||||
B32 continuity_break; // Whether this allocation broke continuity with the previous (address space wise)
|
||||
byte_pad(4);
|
||||
};
|
||||
typedef def_struct(AllocatorInfo) {
|
||||
AllocatorProc* proc;
|
||||
void* data;
|
||||
};
|
||||
static_assert(size_of(AllocatorSP) <= size_of(Slice_BYTE));
|
||||
typedef def_struct(AllocatorQueryInfo) {
|
||||
AllocatorSP save_point;
|
||||
AllocatorQueryFlags features;
|
||||
SSIZE left; // Contiguous memory left
|
||||
SSIZE max_alloc;
|
||||
SSIZE min_alloc;
|
||||
B32 continuity_break; // Whether this allocation broke continuity with the previous (address space wise)
|
||||
byte_pad(4);
|
||||
};
|
||||
static_assert(size_of(AllocatorProc_Out) == size_of(AllocatorQueryInfo));
|
||||
|
||||
#define MEMORY_ALIGNMENT_DEFAULT (2 * size_of(void*))
|
||||
|
||||
AllocatorQueryInfo allocator_query(AllocatorInfo ainfo);
|
||||
|
||||
void mem_free (AllocatorInfo ainfo, Slice_BYTE mem);
|
||||
void mem_reset (AllocatorInfo ainfo);
|
||||
void mem_rewind (AllocatorInfo ainfo, AllocatorSP save_point);
|
||||
AllocatorSP mem_save_point(AllocatorInfo ainfo);
|
||||
|
||||
typedef def_struct(Opts_mem_alloc) { SSIZE alignment; B32 no_zero; byte_pad(4); };
|
||||
typedef def_struct(Opts_mem_grow) { SSIZE alignment; B32 no_zero; byte_pad(4); };
|
||||
typedef def_struct(Opts_mem_shrink) { SSIZE alignment; };
|
||||
typedef def_struct(Opts_mem_resize) { SSIZE alignment; B32 no_zero; byte_pad(4); };
|
||||
|
||||
Slice_BYTE mem__alloc (AllocatorInfo ainfo, SSIZE size, Opts_mem_alloc* opts);
|
||||
Slice_BYTE mem__grow (AllocatorInfo ainfo, Slice_BYTE mem, SSIZE size, Opts_mem_grow* opts);
|
||||
Slice_BYTE mem__resize(AllocatorInfo ainfo, Slice_BYTE mem, SSIZE size, Opts_mem_resize* opts);
|
||||
Slice_BYTE mem__shrink(AllocatorInfo ainfo, Slice_BYTE mem, SSIZE size, Opts_mem_shrink* opts);
|
||||
|
||||
#define mem_alloc(ainfo, size, ...) mem__alloc (ainfo, size, opt_args(Opts_mem_alloc, __VA_ARGS__))
|
||||
#define mem_grow(ainfo, mem, size, ...) mem__grow (ainfo, mem, size, opt_args(Opts_mem_grow, __VA_ARGS__))
|
||||
#define mem_resize(ainfo, mem, size, ...) mem__resize(ainfo, mem, size, opt_args(Opts_mem_resize, __VA_ARGS__))
|
||||
#define mem_shrink(ainfo, mem, size, ...) mem__shrink(ainfo, mem, size, opt_args(Opts_mem_shrink, __VA_ARGS__))
|
||||
|
||||
#define alloc_type(ainfo, type, ...) (type*) mem__alloc(ainfo, size_of(type), opt_args(Opts_mem_alloc, __VA_ARGS__)).ptr
|
||||
#define alloc_slice(ainfo, type, num, ...) (tmpl(Slice,type)){ mem__alloc(ainfo, size_of(type) * num, opt_args(Opts_mem_alloc, __VA_ARGS__)).ptr, num }
|
||||
#pragma endregion Allocator Interface
|
||||
@@ -0,0 +1,5 @@
|
||||
#ifdef INTELLISENSE_DIRECTIVES
|
||||
# pragma once
|
||||
# include "dsl.h"
|
||||
# include "memory.h"
|
||||
#endif
|
||||
Reference in New Issue
Block a user