Moved arena allocator into own files in platform-folder

This commit is contained in:
Eric-Paul Ickhorn 2024-01-19 20:41:13 +01:00
parent dd80cb179e
commit 8b65c5ddb3
Signed by: epickh
GPG Key ID: F5EBBE013924D95F
4 changed files with 63 additions and 55 deletions

View File

@ -4,15 +4,6 @@
#include <librr/types.h>
typedef struct rr_arena rr_arena_s;
struct rr_arena
{
usz_t capacity;
usz_t offset;
char *allocation;
};
/// @brief Tests if two memory regions overlap partially or completely.
/// @param block1 Block to check for overlapping with block2.
// If the address of this block is higher than that of block2, the blocks will be swapped internally.
@ -52,10 +43,4 @@ usz_t rr_memrep(void *destination, usz_t num_bytes, void *sequence, usz_t len_se
/// @param num_bytes Number of bytes to copy from the source to the destination.
void rr_memcopy(void *destination, void *source, usz_t num_bytes);
rr_arena_s rr_new_arena(usz_t capacity);
void rr_delete_arena(rr_arena_s *arena);
void * rr_arena_alloc(rr_arena_s *arena, usz_t length);
char * rr_arena_clone_string(rr_arena_s *arena, char *string);
#endif // RR_MEMORY_H

View File

@ -62,43 +62,3 @@ void rr_memcopy(void *destination, void *source, usz_t num_bytes)
for(usz_t index = 0; index < num_bytes; ++index)
destination_8[index] = source_8[index];
}
rr_arena_s rr_new_arena(usz_t capacity)
{
rr_arena_s arena;
arena.capacity = capacity;
arena.offset = 0;
arena.allocation = malloc(capacity);
return arena;
}
void rr_delete_arena(rr_arena_s *arena)
{
if(arena->allocation == NULL) return;
free(arena->allocation);
arena->allocation = NULL;
}
void * rr_arena_alloc(rr_arena_s *arena, usz_t length)
{
if((arena->offset + length) > arena->capacity)
return NULL;
void *block = &arena->allocation[arena->offset];
arena->offset += length;
return block;
}
char * rr_arena_clone_string(rr_arena_s *arena, char *string)
{
usz_t len_string = rr_measure_string(string);
char *cloned_string = rr_arena_alloc(arena, len_string+1);
rr_memcopy(cloned_string, string, len_string);
cloned_string[len_string] = 0x00;
return cloned_string;
}

View File

@ -0,0 +1,22 @@
#ifndef RR_ARENA_ALLOCATOR_H
#define RR_ARENA_ALLOCATOR_H
#include <librr/types.h>
typedef struct rr_arena rr_arena_s;
struct rr_arena
{
usz_t capacity;
usz_t offset;
char *allocation;
};
rr_arena_s rr_new_arena(usz_t capacity);
void rr_delete_arena(rr_arena_s *arena);
void * rr_arena_alloc(rr_arena_s *arena, usz_t length);
char * rr_arena_clone_string(rr_arena_s *arena, const char *string);
#endif // RR_ARENA_ALLOCATOR_H

View File

@ -0,0 +1,41 @@
#include <alloc/arena.h>
rr_arena_s rr_new_arena(usz_t capacity)
{
rr_arena_s arena;
arena.capacity = capacity;
arena.offset = 0;
arena.allocation = malloc(capacity);
return arena;
}
void rr_delete_arena(rr_arena_s *arena)
{
if(arena->allocation == NULL) return;
free(arena->allocation);
arena->allocation = NULL;
}
void * rr_arena_alloc(rr_arena_s *arena, usz_t length)
{
if((arena->offset + length) > arena->capacity)
return NULL;
void *block = &arena->allocation[arena->offset];
arena->offset += length;
return block;
}
char * rr_arena_clone_string(rr_arena_s *arena, char *string)
{
usz_t len_string = rr_measure_string(string);
char *cloned_string = rr_arena_alloc(arena, len_string+1);
if(cloned_string == NULL)
return NULL;
rr_memcopy(cloned_string, string, len_string);
cloned_string[len_string] = 0x00;
return cloned_string;
}