Added: arena allocator for memory allocations
This commit is contained in:
		
							parent
							
								
									d8c55982a7
								
							
						
					
					
						commit
						2488680376
					
				| 
						 | 
				
			
			@ -8,6 +8,6 @@ pushd .\build
 | 
			
		|||
 | 
			
		||||
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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -1,23 +1,33 @@
 | 
			
		|||
j
 | 
			
		||||
 | 
			
		||||
//= rhjr: [h]
 | 
			
		||||
 | 
			
		||||
#include "base.h"
 | 
			
		||||
#include "eightysix.h"
 | 
			
		||||
#include "win32_platform.h"
 | 
			
		||||
#include "platform/win32_platform.h"
 | 
			
		||||
 | 
			
		||||
//= 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
 | 
			
		||||
main(void)
 | 
			
		||||
{
 | 
			
		||||
  Arena arena = {0};
 | 
			
		||||
  arena.backing_buffer = &backing_buffer[0];
 | 
			
		||||
  arena.offset = 0;
 | 
			
		||||
  arena.size = 2048;
 | 
			
		||||
  arena_init(&arena, &backing_buffer[0], ARENA_DEFAULT_SIZE);
 | 
			
		||||
 | 
			
		||||
  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;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,46 +1,6 @@
 | 
			
		|||
#ifndef 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.
 | 
			
		||||
// Intel 8086 manual, pg. 4-22
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
 | 
			
		||||
String8 *
 | 
			
		||||
platform_write_file()
 | 
			
		||||
platform_write_file(Arena *arena)
 | 
			
		||||
{
 | 
			
		||||
  String8 *result;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user