1
0
Fork 0

Added: base.h and arena allocator implementation

Browse Source
This commit is contained in:
rhuibertsjr 2024-04-21 11:11:28 +02:00
parent b0e1fadb3d
commit 55632d07eb
5 changed files with 107 additions and 0 deletions

View File

@ -1,3 +1,7 @@
21-04-2024 René Huiberts <rhuibertsjr@gmail.com>
Added: base.h and arena allocator implementation
21-04-2024 René Huiberts <rhuibertsjr@gmail.com>
Added: project layout

40
src/arena.c Normal file
View File

@ -0,0 +1,40 @@
internal Arena *
arena_initialize(u64 size)
{
Arena *result = 0;
void *backing_buffer = malloc(size);
if (backing_buffer != NULL)
{
result = (Arena*) backing_buffer;
result->buffer = backing_buffer;
result->offset = sizeof(Arena);
result->size = sizeof(Arena) + size;
}
return result;
}
internal void *
arena_allocate(Arena *arena, u64 size)
{
void *result = 0;
if (arena->offset + size <= arena->size)
{
result = (void*)((u64) arena->buffer + (u64) arena->offset);
arena->offset += size;
memset(result, 0, size);
}
return result;
}
internal void
arena_release(Arena *arena)
{
arena->offset = 0;
}

31
src/arena.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef ARENA_H
#define ARENA_H
typedef struct Arena Arena;
struct Arena {
u64 *buffer;
u64 offset;
u64 size;
};
internal Arena * arena_initialize(u64 size);
internal void * arena_allocate(Arena *arena, u64 size);
internal void arena_release(Arena *arena);
//= rhjr: arena helpers
#define arena_push_array(arena, type, count) \
(type*) arena_allocate((arena), sizeof(type) * (count))
#define arena_push(arena, type) \
(type*) arena_allocate((arena), sizeof(type))
//= rhjr: memory helpers
#define memory_zero(s,z) memset((s), 0, (z))
#define memory_zero_struct(s) memory_zero((s), sizeof(*(s)))
#define memory_copy(dst, src, length) memmove((dst), (src), (length))
#endif // ARENA_H

27
src/base.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef BASE_H
#define BASE_H
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define internal static
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
#define KB(b) ((b) << 10)
#define MB(b) ((b) << 20)
#define STATEMENT(S) do{ S } while(0)
#define ASSERT(c) STATEMENT( if (!(c)){ (*(volatile int*)0 = 0); } )
#endif // BASE_H

View File

@ -1,3 +1,8 @@
#include "base.h"
#include "arena.h"
#include "arena.c"
int
main(void)
{