mirror of
https://github.com/pound-emu/ballistic.git
synced 2026-01-31 01:15:21 +01:00
memory: add default allocator
Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
@@ -37,6 +37,7 @@ add_library(Ballistic STATIC
|
||||
src/decoder_table_gen.c
|
||||
src/bal_engine.c
|
||||
src/bal_translator.c
|
||||
src/bal_memory.c
|
||||
)
|
||||
|
||||
target_include_directories(Ballistic PUBLIC include)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#ifndef BALLISTIC_MEMORY_H
|
||||
#define BALLISTIC_MEMORY_H
|
||||
|
||||
#include "bal_attributes.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
@@ -57,6 +58,15 @@ typedef struct
|
||||
|
||||
} bal_allocator_t;
|
||||
|
||||
/*!
|
||||
* @brief Populates an allocator struct with the default implementation.
|
||||
*
|
||||
* @param[out] allocator The strict to populate. Must not be NULL.
|
||||
*
|
||||
* @warn Only supports Windows and POSIX systems.
|
||||
*/
|
||||
BAL_COLD void get_default_allocator(bal_allocator_t *out_allocator);
|
||||
|
||||
#endif /* BALLISTIC_MEMORY_H */
|
||||
|
||||
/*** end of file ***/
|
||||
|
||||
65
src/bal_memory.c
Normal file
65
src/bal_memory.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "bal_memory.h"
|
||||
#include "bal_platform.h"
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void *default_allocate(void *, size_t, size_t);
|
||||
void default_free(void *, void *, size_t);
|
||||
|
||||
void
|
||||
get_default_allocator (bal_allocator_t *out_allocator)
|
||||
{
|
||||
out_allocator->allocator = NULL;
|
||||
out_allocator->allocate = default_allocate;
|
||||
out_allocator->free = default_free;
|
||||
}
|
||||
|
||||
#if BAL_PLATFORM_POSIX
|
||||
|
||||
void *
|
||||
default_allocate (void *allocator, size_t alignment, size_t size)
|
||||
{
|
||||
if (0 == size)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *memory = aligned_alloc(alignment, size);
|
||||
|
||||
return memory;
|
||||
}
|
||||
|
||||
void
|
||||
default_free (void *allocator, void *pointer, size_t size)
|
||||
{
|
||||
free(pointer);
|
||||
}
|
||||
|
||||
#endif /* BAL_PLATFORM_POSIX */
|
||||
|
||||
#if BAL_PLATFORM_WINDOWS
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
void *
|
||||
default_allocate (void *allocator, size_t alignment, size_t size)
|
||||
{
|
||||
if (0 == size)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *memory = _aligned_malloc(alignment, size);
|
||||
|
||||
return memory;
|
||||
}
|
||||
|
||||
void
|
||||
default_free (void *allocator, void *pointer, size_t size)
|
||||
{
|
||||
_aligned_free(pointer);
|
||||
}
|
||||
|
||||
#endif /* BAL_PLATFORM_WINDOWS */
|
||||
|
||||
/*** end of file ***/
|
||||
Reference in New Issue
Block a user