Add axl_memcpy, axl_realloc
This commit is contained in:
parent
bc947a6c7f
commit
a1629cfd28
3 changed files with 38 additions and 4 deletions
35
axl_memory.c
35
axl_memory.c
|
|
@ -46,6 +46,11 @@ void axl_init(void)
|
|||
}
|
||||
}
|
||||
|
||||
mb_header* axl_get_mb_header(void* ptr)
|
||||
{
|
||||
return (mb_header*)((u8*)ptr - MB_HEADER_SIZE);
|
||||
}
|
||||
|
||||
mb_header* axl_find_mb(u32 req_size)
|
||||
{
|
||||
mb_header* block = nomad ? nomad : root;
|
||||
|
|
@ -122,6 +127,34 @@ void* axl_malloc(u32 size)
|
|||
return (void*)((u8*)free_block + MB_HEADER_SIZE);
|
||||
}
|
||||
|
||||
void* axl_memcpy(void* dst, const void* src, u32 count)
|
||||
{
|
||||
for(u32 i = 0; i < count; i++)
|
||||
{
|
||||
*((u8*)dst + i) = *((u8*)src + i);
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
void* axl_realloc(void* ptr, u32 size)
|
||||
{
|
||||
void* new = axl_malloc(size);
|
||||
|
||||
if(!new)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mb_header* old = axl_get_mb_header(ptr);
|
||||
size = size < old->size ? size : old->size;
|
||||
axl_memcpy(new, ptr, size);
|
||||
|
||||
axl_free(ptr);
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
void axl_free(void* ptr)
|
||||
{
|
||||
if(!ptr)
|
||||
|
|
@ -129,7 +162,7 @@ void axl_free(void* ptr)
|
|||
return;
|
||||
}
|
||||
|
||||
mb_header* block = (mb_header*)((u8*)ptr - MB_HEADER_SIZE);
|
||||
mb_header* block = axl_get_mb_header(ptr);
|
||||
mb_header* next_block = block->next;
|
||||
mb_header* prev_block = block->prev;
|
||||
block->is_free = true;
|
||||
|
|
|
|||
|
|
@ -2,13 +2,14 @@
|
|||
#define AXL_MEMORY_H
|
||||
|
||||
#ifndef AXL_HEAP_SIZE
|
||||
#define AXL_HEAP_SIZE 1024 * 1024 * 16
|
||||
#define AXL_HEAP_SIZE 1024 * 1024 * 16
|
||||
#endif
|
||||
|
||||
#include "axl_types.h"
|
||||
|
||||
void axl_init(void);
|
||||
void* axl_malloc(u32 size);
|
||||
void* axl_realloc(void* ptr, u32 size);
|
||||
void* axl_memset(void* ptr, i8 c, u32 n);
|
||||
void axl_free(void* ptr);
|
||||
|
||||
|
|
|
|||
4
makefile
4
makefile
|
|
@ -1,11 +1,11 @@
|
|||
CC = clang
|
||||
CFLAGS = -Wall -Wextra -Werror -pedantic -std=c11 -nostdlib -static -Oz -ffreestanding
|
||||
LDFLAGS = -Wl,/SUBSYSTEM:CONSOLE,/ENTRY:_start -fuse-ld=lld
|
||||
LIBS = -lkernel32 -luser32
|
||||
LIBS = -lkernel32
|
||||
SOURCES = main.c axl_memory.c
|
||||
TARGET = prog.exe
|
||||
|
||||
all: $(TARGET)
|
||||
all: clean $(TARGET)
|
||||
|
||||
$(TARGET): $(SOURCES)
|
||||
$(CC) $(CFLAGS) $(SOURCES) -o $(TARGET) $(LDFLAGS) $(LIBS)
|
||||
|
|
|
|||
Loading…
Reference in a new issue