Compare commits

..

No commits in common. "18a0832c5e78a53c8b69acaefc7dbfb7aa62fa11" and "dcbd9555d537b79107c352deed3cd3bb2dde5bc0" have entirely different histories.

3 changed files with 26 additions and 76 deletions

View File

@ -3,12 +3,20 @@
#define RR_ARENA_ALLOCATOR_H #define RR_ARENA_ALLOCATOR_H
#include <librr/types.h> #include <librr/types.h>
#include <librr/alloc/generic.h>
rr_generic_arena_s rr_new_arena(usz_t capacity); typedef struct rr_arena rr_arena_s;
void rr_destruct_arena(rr_generic_arena_s *generic_arena);
void * rr_arena_alloc(rr_generic_arena_s *generic_arena, usz_t length); struct rr_arena
char * rr_arena_clone_string(rr_generic_arena_s *generic_arena, const char *string); {
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 #endif // RR_ARENA_ALLOCATOR_H

View File

@ -1,38 +0,0 @@
#ifndef RR_GENERIC_ALLOCATOR_H
#define RR_GENERIC_ALLOCATOR_H
#include <librr/types.h>
typedef struct rr_generic_allocator rr_generic_allocator_s;
typedef struct rr_generic_pool rr_generic_pool_s;
typedef struct rr_generic_arena rr_generic_arena_s;
typedef rr_generic_arena_s (*rr_create_arena_fn) (usz_t capacity);
typedef rr_generic_pool_s (*rr_create_pool_fn) (usz_t item_size, usz_t num_items);
struct rr_generic_allocator
{
void * (*fn_alloc) (rr_generic_allocator_s *allocator, usz_t num_bytes);
void (*fn_free) (rr_generic_allocator_s *allocator, void *block);
void (*fn_destruct) (rr_generic_allocator_s *allocator);
void *specifics;
};
struct rr_generic_pool
{
void * (*fn_alloc) (rr_generic_pool_s *pool);
void (*fn_free) (rr_generic_pool_s *pool, void *block);
void (*fn_destruct) (rr_generic_pool_s *pool);
usz_t (*fn_get_unit_size) (rr_generic_pool_s *pool);
void *specifics;
};
struct rr_generic_arena
{
void * (*fn_alloc) (rr_generic_arena_s *arena, usz_t num_bytes);
void (*fn_destruct) (rr_generic_arena_s *arena);
void *specifics;
};
#endif // RR_GENERIC_ALLOCATOR_H

View File

@ -1,45 +1,25 @@
#include <librr/alloc/arena.h> #include <alloc/arena.h>
#include <librr/strutil.h>
#include <librr/memory.h>
#include <stdlib.h> rr_arena_s rr_new_arena(usz_t capacity)
typedef struct rr_arena rr_arena_s;
struct rr_arena
{ {
usz_t capacity; rr_arena_s arena;
usz_t offset; arena.capacity = capacity;
char *allocation; arena.offset = 0;
}; arena.allocation = malloc(capacity);
rr_generic_arena_s rr_new_arena(usz_t capacity)
{
rr_arena_s *specifics = malloc(sizeof(rr_arena_s) + capacity);
specifics->capacity = capacity;
specifics->offset = 0;
// The arena allocation is right after the head structure.
specifics->allocation = (void *) (specifics + sizeof(rr_arena_s));
rr_generic_arena_s arena;
arena.fn_alloc = &rr_arena_alloc;
arena.fn_destruct = &rr_destruct_arena;
arena.specifics = specifics;
return arena; return arena;
} }
void rr_destruct_arena(rr_generic_arena_s *generic_arena) void rr_delete_arena(rr_arena_s *arena)
{ {
free(generic_arena->specifics); if(arena->allocation == NULL) return;
free(arena->allocation);
arena->allocation = NULL;
} }
void * rr_arena_alloc(rr_generic_arena_s *generic_arena, usz_t length) void * rr_arena_alloc(rr_arena_s *arena, usz_t length)
{ {
rr_arena_s *arena = generic_arena->specifics;
if((arena->offset + length) > arena->capacity) if((arena->offset + length) > arena->capacity)
return NULL; return NULL;
@ -48,10 +28,10 @@ void * rr_arena_alloc(rr_generic_arena_s *generic_arena, usz_t length)
return block; return block;
} }
char * rr_arena_clone_string(rr_generic_arena_s *generic_arena, const char *string) char * rr_arena_clone_string(rr_arena_s *arena, char *string)
{ {
usz_t len_string = rr_measure_string(string); usz_t len_string = rr_measure_string(string);
char *cloned_string = rr_arena_alloc(generic_arena, len_string+1); char *cloned_string = rr_arena_alloc(arena, len_string+1);
if(cloned_string == NULL) if(cloned_string == NULL)
return NULL; return NULL;
rr_memcopy(cloned_string, string, len_string); rr_memcopy(cloned_string, string, len_string);