1
0
Fork 0

Added: arena allocator for memory allocations

Browse Source
This commit is contained in:
rhjr 2024-11-26 21:27:24 +01:00
parent d8c55982a7
commit 2488680376
7 changed files with 102 additions and 49 deletions

View File

@ -8,6 +8,6 @@ pushd .\build
set links= kernel32.lib set links= kernel32.lib
cl ..\src\eightysix.c /Feeightysix.exe /nologo /link %links% cl /Zi ..\src\eightysix.c /Feeightysix.exe /nologo /link %links%
popd .\build popd .\build

29
src/base.c Normal file
View File

@ -0,0 +1,29 @@
internal void
arena_init(Arena *arena, u8 *backing_buffer, i32 size)
{
arena->backing_buffer = backing_buffer;
arena->offset = 0;
arena->size = size;
}
internal void *
arena_alloc(Arena *arena, i32 size)
{
void *result = 0;
if (arena->offset + size <= arena->size) {
result = &arena->backing_buffer[arena->offset];
arena->offset += size;
} else {
printf("Arena ran out of memory\n");
}
return result;
}
internal void
arena_release(Arena *arena)
{
arena->offset = 0;
}

54
src/base.h Normal file
View File

@ -0,0 +1,54 @@
#ifndef BASE_H
#define BASE_H
#include <stdio.h>
#include <stdint.h>
//= rhjr: types
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;
typedef uint8_t b8;
typedef uint16_t b16;
typedef uint32_t b32;
typedef uint64_t b64;
#define internal static
#define global static
//= rhjr: allocator
#define ARENA_DEFAULT_SIZE 2048
typedef struct Arena
{
u8 *backing_buffer;
i32 offset;
i32 size;
}
Arena;
internal void arena_init(Arena *arena, u8 *backing_buffer, i32 size);
internal void * arena_alloc(Arena *arena, i32 size);
internal void arena_release(Arena *arena);
//= rhjr: strings
typedef struct String8
{
u8* contents;
i32 length;
}
String8;
#endif // BASE_H

View File

@ -1,23 +1,33 @@
j
//= rhjr: [h] //= rhjr: [h]
#include "base.h"
#include "eightysix.h" #include "eightysix.h"
#include "win32_platform.h" #include "platform/win32_platform.h"
//= rhjr: [c] //= rhjr: [c]
#include "win32_platform.c" #include "base.c"
#include "platform/win32_platform.c"
u8 backing_buffer[2048]; u8 backing_buffer[ARENA_DEFAULT_SIZE];
int int
main(void) main(void)
{ {
Arena arena = {0}; Arena arena = {0};
arena.backing_buffer = &backing_buffer[0]; arena_init(&arena, &backing_buffer[0], ARENA_DEFAULT_SIZE);
arena.offset = 0;
arena.size = 2048;
i64 *idx = arena_alloc(&arena, sizeof(i64));
*idx = 69;
i64 *idx2 = arena_alloc(&arena, 2 * sizeof(i64));
*idx2++ = 69;
i64 *idx3 = arena_alloc(&arena, sizeof(i64));
*idx3 = 124;
*idx2 = 139;
return 0; return 0;
} }

View File

@ -1,46 +1,6 @@
#ifndef EIGHTYSIX_CORE_H #ifndef EIGHTYSIX_CORE_H
#define EIGHTYSIX_CORE_H #define EIGHTYSIX_CORE_H
#include <stdio.h>
#include <stdint.h>
//= rhjr: types
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;
typedef uint8_t b8;
typedef uint16_t b16;
typedef uint32_t b32;
typedef uint64_t b64;
#define internal static
#define global static
typedef struct String8
{
u8* contents;
i32 length;
}
String8;
//= rhjr: allocator
typedef struct Arena
{
u8 *backing_buffer;
i32 offset;
i32 size;
}
Arena;
//= rhjr: instructions. //= rhjr: instructions.
// Intel 8086 manual, pg. 4-22 // Intel 8086 manual, pg. 4-22

View File

@ -1,6 +1,6 @@
String8 * String8 *
platform_write_file() platform_write_file(Arena *arena)
{ {
String8 *result; String8 *result;