Add axl_memory
This commit is contained in:
parent
0b395b4f50
commit
664d68a2dc
4 changed files with 32 additions and 27 deletions
2
Makefile
2
Makefile
|
|
@ -2,7 +2,7 @@ CC = clang
|
||||||
CFLAGS = -Wall -Wextra -Werror -pedantic -std=c11 -nostdlib -static -Oz -ffreestanding
|
CFLAGS = -Wall -Wextra -Werror -pedantic -std=c11 -nostdlib -static -Oz -ffreestanding
|
||||||
LDFLAGS = -Wl,/SUBSYSTEM:CONSOLE,/ENTRY:_start -fuse-ld=lld
|
LDFLAGS = -Wl,/SUBSYSTEM:CONSOLE,/ENTRY:_start -fuse-ld=lld
|
||||||
LIBS = -lkernel32 -luser32
|
LIBS = -lkernel32 -luser32
|
||||||
SOURCES = main.c axl.c
|
SOURCES = main.c axl_memory.c
|
||||||
TARGET = prog.exe
|
TARGET = prog.exe
|
||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
|
||||||
14
axl.h
14
axl.h
|
|
@ -1,17 +1,7 @@
|
||||||
#ifndef AXL_H
|
#ifndef AXL_H
|
||||||
#define AXL_H
|
#define AXL_H
|
||||||
|
|
||||||
#ifndef AXL_HEAP_SIZE
|
#include "axl_types.h"
|
||||||
#define AXL_HEAP_SIZE 1024 * 1024 * 16
|
#include "axl_memory.h"
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
void axl_init(void);
|
|
||||||
void* axl_malloc(uint32_t size);
|
|
||||||
void* axl_memset(void* ptr, int8_t c, int32_t n);
|
|
||||||
void axl_free(void* ptr);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
#include "axl.h"
|
#include "axl_memory.h"
|
||||||
|
|
||||||
static uint8_t memory[AXL_HEAP_SIZE];
|
static u8 memory[AXL_HEAP_SIZE];
|
||||||
|
|
||||||
struct mb_header
|
struct mb_header
|
||||||
{
|
{
|
||||||
uint32_t size;
|
u32 size;
|
||||||
bool is_free;
|
b8 is_free;
|
||||||
struct mb_header* next;
|
struct mb_header* next;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -15,13 +15,13 @@ typedef struct mb_header mb_header;
|
||||||
|
|
||||||
static mb_header* root = 0;
|
static mb_header* root = 0;
|
||||||
|
|
||||||
void* axl_memset(void* ptr, int8_t c, int32_t n)
|
void* axl_memset(void* ptr, i8 c, u32 n)
|
||||||
{
|
{
|
||||||
if(ptr)
|
if(ptr)
|
||||||
{
|
{
|
||||||
for(int32_t i = 0; i < n; i++)
|
for(u32 i = 0; i < n; i++)
|
||||||
{
|
{
|
||||||
((int8_t*)ptr)[i] = c;
|
((i8*)ptr)[i] = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,7 +30,7 @@ void* axl_memset(void* ptr, int8_t c, int32_t n)
|
||||||
|
|
||||||
void axl_init(void)
|
void axl_init(void)
|
||||||
{
|
{
|
||||||
static bool axl_initialized = false;
|
static b8 axl_initialized = false;
|
||||||
|
|
||||||
if(!axl_initialized)
|
if(!axl_initialized)
|
||||||
{
|
{
|
||||||
|
|
@ -43,7 +43,7 @@ void axl_init(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mb_header* axl_find_mb(uint32_t req_size)
|
mb_header* axl_find_mb(u32 req_size)
|
||||||
{
|
{
|
||||||
mb_header* block = root;
|
mb_header* block = root;
|
||||||
|
|
||||||
|
|
@ -59,11 +59,11 @@ mb_header* axl_find_mb(uint32_t req_size)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void axl_split_mb(mb_header* block, uint32_t size)
|
void axl_split_mb(mb_header* block, u32 size)
|
||||||
{
|
{
|
||||||
if(block->size > size + MB_HEADER_SIZE)
|
if(block->size > size + MB_HEADER_SIZE)
|
||||||
{
|
{
|
||||||
mb_header* new_block = (mb_header*)((uint8_t*)block + MB_HEADER_SIZE + size);
|
mb_header* new_block = (mb_header*)((u8*)block + MB_HEADER_SIZE + size);
|
||||||
new_block->size = block->size - size - MB_HEADER_SIZE;
|
new_block->size = block->size - size - MB_HEADER_SIZE;
|
||||||
new_block->is_free = true;
|
new_block->is_free = true;
|
||||||
new_block->next = block->next;
|
new_block->next = block->next;
|
||||||
|
|
@ -73,7 +73,7 @@ void axl_split_mb(mb_header* block, uint32_t size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void* axl_malloc(uint32_t size)
|
void* axl_malloc(u32 size)
|
||||||
{
|
{
|
||||||
if(size > AXL_HEAP_SIZE - MB_HEADER_SIZE)
|
if(size > AXL_HEAP_SIZE - MB_HEADER_SIZE)
|
||||||
{
|
{
|
||||||
|
|
@ -90,7 +90,7 @@ void* axl_malloc(uint32_t size)
|
||||||
axl_split_mb(free_block, size);
|
axl_split_mb(free_block, size);
|
||||||
free_block->is_free = false;
|
free_block->is_free = false;
|
||||||
|
|
||||||
return (void*)((uint8_t*)free_block + MB_HEADER_SIZE);
|
return (void*)((u8*)free_block + MB_HEADER_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void axl_free(void* ptr)
|
void axl_free(void* ptr)
|
||||||
|
|
@ -100,7 +100,7 @@ void axl_free(void* ptr)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mb_header* block = (mb_header*)((uint8_t*)ptr - MB_HEADER_SIZE);
|
mb_header* block = (mb_header*)((u8*)ptr - MB_HEADER_SIZE);
|
||||||
mb_header* next_block = block->next;
|
mb_header* next_block = block->next;
|
||||||
block->is_free = true;
|
block->is_free = true;
|
||||||
|
|
||||||
15
axl_memory.h
Normal file
15
axl_memory.h
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
#ifndef AXL_MEMORY_H
|
||||||
|
#define AXL_MEMORY_H
|
||||||
|
|
||||||
|
#ifndef AXL_HEAP_SIZE
|
||||||
|
#define AXL_HEAP_SIZE 1024 * 1024 * 16
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "axl_types.h"
|
||||||
|
|
||||||
|
void axl_init(void);
|
||||||
|
void* axl_malloc(u32 size);
|
||||||
|
void* axl_memset(void* ptr, i8 c, u32 n);
|
||||||
|
void axl_free(void* ptr);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in a new issue