make code follow style, improve build script

This commit is contained in:
Abdulmujeeb Raji 2025-01-17 11:27:55 +00:00
parent 9da74ced12
commit 70bbccff71
Signed by: midnadimple
GPG key ID: EB02C582F8C3962B
5 changed files with 42 additions and 35 deletions

View file

@ -184,7 +184,7 @@ typedef U32 B32;
#define STMNT(s) do{ s }while(0)
#define ASSERT(x, msg) STMNT(if (!x) OS_DEBUGBREAK();)
#define ASSERT(x, msg) STMNT(if (!x) os_DEBUGBREAK();)
#define CONCAT_(a,b) a##b
#define CONCAT(a,b) CONCAT_(a,b)

View file

@ -1,40 +1,43 @@
#include "bastd.c"
#include "..\..\bastd.c"
int
main(void)
{
ISize buffer_size = 100 * sizeof(U8);
M_Arena arena = M_Arena_create(OS_Alloc(buffer_size), buffer_size);
M_Allocator permanent_allocator = M_ARENA_ALLOCATOR(arena);
m_Arena arena = m_Arena_create(OS_Alloc(buffer_size), buffer_size);
m_Allocator permanent_allocator = m_ARENA_ALLOCATOR(arena);
{
/* By cloning an arena at the start of the scope, all changes to the
arena are now local to the scope, so all allocations with be freed
at the end automatically.
*/
M_Arena scratch_arena = arena;
M_Allocator scratch_allocator = M_ARENA_ALLOCATOR(scratch_arena);
m_Arena scratch_arena = arena;
m_Allocator scratch_allocator = m_ARENA_ALLOCATOR(scratch_arena);
U8* array = M_MAKE(U8, 20, scratch_allocator);
U8* array = m_MAKE(U8, 20, scratch_allocator);
for (int i = 0; i < 20; i++) {
array[i] = i;
}
} // Auto clears here
OS_DEBUGBREAK();
os_DEBUGBREAK();
U8* array = M_MAKE(U8, 10, permanent_allocator);
U8* array = m_MAKE(U8, 10, permanent_allocator);
for (int i = 10; i-- > 0;) {
array[10 - i] = i;
}
OS_DEBUGBREAK();
os_DEBUGBREAK();
array = M_RESIZE(array, 10, 20, permanent_allocator);
array = m_RESIZE(array, 10, 20, permanent_allocator);
for (int i = 20; i-- > 0;) {
array[20 - i] = i;
}
OS_DEBUGBREAK();
os_DEBUGBREAK();
M_RELEASE(array, 20, permanent_allocator);
OS_DEBUGBREAK();
m_RELEASE(array, 20, permanent_allocator);
F64 *vector2 = m_MAKE(F32, 2, permanent_allocator);
vector2[0] = 0x1.921fb6p+1f;
vector2[1] = 490875;
os_DEBUGBREAK();
}

View file

@ -2,7 +2,7 @@
#define BASTD_MEM_C
FUNCTION void *
M_memorySet(void *buffer, U8 value, ISize length)
m_memorySet(void *buffer, U8 value, ISize length)
{
U8* p = buffer;
while (length-- > 0) {
@ -12,7 +12,7 @@ M_memorySet(void *buffer, U8 value, ISize length)
}
FUNCTION void *
M_memoryCopy(void *dst, void *src, size_t n)
m_memoryCopy(void *dst, void *src, size_t n)
{
U8 *s = (U8 *)src;
U8 *d = (U8 *)dst;
@ -30,25 +30,25 @@ M_memoryCopy(void *dst, void *src, size_t n)
If new_size > 0, function like realloc and either resize or create
If new_size <= 0, function like free and clear the memory
*/
typedef void *(*M_AllocFunc)(void *ctx, void *ptr, ISize old_size, ISize new_size);
typedef void *(*m_AllocFunc)(void *ctx, void *ptr, ISize old_size, ISize new_size);
typedef struct M_Allocator M_Allocator;
struct M_Allocator {
M_AllocFunc alloc;
typedef struct m_Allocator m_Allocator;
struct m_Allocator {
m_AllocFunc alloc;
void* ctx;
};
#define M_MAKE(T, n, a) ((T *)((a).alloc((a).ctx, NIL, 0, sizeof(T) * n)))
#define M_RESIZE(p, o, n, a) ((a).alloc((a).ctx, p, sizeof(*(p)) * o, sizeof(*(p)) * n))
#define M_RELEASE(p, s, a) ((a).alloc((a).ctx, p, sizeof(*(p)) * s, 0))
#define m_MAKE(T, n, a) ((T *)((a).alloc((a).ctx, NIL, 0, sizeof(T) * n)))
#define m_RESIZE(p, o, n, a) ((a).alloc((a).ctx, p, sizeof(*(p)) * o, sizeof(*(p)) * n))
#define m_RELEASE(p, s, a) ((a).alloc((a).ctx, p, sizeof(*(p)) * s, 0))
/* Linear Allocator (Arena)
Allocates variably-sized regions from a fixed-size block of memory.
A set of regions is freed by setting the allocator's offset to an earlier
value.
*/
typedef struct M_Arena M_Arena;
struct M_Arena {
typedef struct m_Arena m_Arena;
struct m_Arena {
U8* beg;
U8* end;
};
@ -56,9 +56,9 @@ struct M_Arena {
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
FUNCTION void *
M_Arena_alloc(void *ctx, void *ptr, ISize old_size, ISize new_size)
m_Arena_alloc(void *ctx, void *ptr, ISize old_size, ISize new_size)
{
M_Arena *a = (M_Arena *)ctx;
m_Arena *a = (m_Arena *)ctx;
if (new_size <= 0) {
/* Arena can only free the most recent block. This allows to follow stack pattern.
@ -90,11 +90,11 @@ M_Arena_alloc(void *ctx, void *ptr, ISize old_size, ISize new_size)
// New allocation;
p = a->beg + padding;
a->beg += padding + new_size;
p = M_memorySet(p, 0, new_size);
p = m_memorySet(p, 0, new_size);
if (ptr != NIL) {
// Arbitrary block, copy data from old pointer
p = M_memoryCopy(p, ptr, old_size);
p = m_memoryCopy(p, ptr, old_size);
}
}
@ -102,15 +102,15 @@ M_Arena_alloc(void *ctx, void *ptr, ISize old_size, ISize new_size)
}
}
FUNCTION M_Arena
M_Arena_create(void* buffer, ISize capacity)
FUNCTION m_Arena
m_Arena_create(void* buffer, ISize capacity)
{
M_Arena arena = {0};
m_Arena arena = {0};
arena.beg = (U8 *)buffer;
arena.end = arena.beg ? arena.beg + capacity : 0;
return arena;
}
#define M_ARENA_ALLOCATOR(a) (M_Allocator){M_Arena_alloc, (void *)&(a)}
#define m_ARENA_ALLOCATOR(a) (m_Allocator){m_Arena_alloc, (void *)&(a)}
#endif//BASTD_MEM_C

View file

@ -3,7 +3,7 @@
#include <windows.h>
#define OS_DEBUGBREAK() __debugbreak();
#define os_DEBUGBREAK() __debugbreak();
FUNCTION void
OS_Abort(S8 msg)

View file

@ -3,6 +3,10 @@
if not exist build\ mkdir build
pushd build
cl /Zi ..\main.c
REM Uncomment one of these to run an example. Try it! They're great documentation.
REM cl /Zi ..\bastd\examples\memory.c
REM This builds your application with debug symbols.
cl /Zi ..\bastd\examples\memory.c
popd