Compare commits
2 Commits
dcbd9555d5
...
18a0832c5e
Author | SHA1 | Date |
---|---|---|
Eric-Paul Ickhorn | 18a0832c5e | |
Eric-Paul Ickhorn | 545295af9d |
|
@ -3,20 +3,12 @@
|
||||||
#define RR_ARENA_ALLOCATOR_H
|
#define RR_ARENA_ALLOCATOR_H
|
||||||
|
|
||||||
#include <librr/types.h>
|
#include <librr/types.h>
|
||||||
|
#include <librr/alloc/generic.h>
|
||||||
|
|
||||||
typedef struct rr_arena rr_arena_s;
|
rr_generic_arena_s rr_new_arena(usz_t capacity);
|
||||||
|
void rr_destruct_arena(rr_generic_arena_s *generic_arena);
|
||||||
|
|
||||||
struct rr_arena
|
void * rr_arena_alloc(rr_generic_arena_s *generic_arena, usz_t length);
|
||||||
{
|
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
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
|
||||||
|
#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
|
|
@ -1,25 +1,45 @@
|
||||||
#include <alloc/arena.h>
|
#include <librr/alloc/arena.h>
|
||||||
|
#include <librr/strutil.h>
|
||||||
|
#include <librr/memory.h>
|
||||||
|
|
||||||
rr_arena_s rr_new_arena(usz_t capacity)
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct rr_arena rr_arena_s;
|
||||||
|
|
||||||
|
struct rr_arena
|
||||||
{
|
{
|
||||||
rr_arena_s arena;
|
usz_t capacity;
|
||||||
arena.capacity = capacity;
|
usz_t offset;
|
||||||
arena.offset = 0;
|
char *allocation;
|
||||||
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_delete_arena(rr_arena_s *arena)
|
void rr_destruct_arena(rr_generic_arena_s *generic_arena)
|
||||||
{
|
{
|
||||||
if(arena->allocation == NULL) return;
|
free(generic_arena->specifics);
|
||||||
free(arena->allocation);
|
|
||||||
arena->allocation = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void * rr_arena_alloc(rr_arena_s *arena, usz_t length)
|
void * rr_arena_alloc(rr_generic_arena_s *generic_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;
|
||||||
|
|
||||||
|
@ -28,10 +48,10 @@ void * rr_arena_alloc(rr_arena_s *arena, usz_t length)
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
|
|
||||||
char * rr_arena_clone_string(rr_arena_s *arena, char *string)
|
char * rr_arena_clone_string(rr_generic_arena_s *generic_arena, const char *string)
|
||||||
{
|
{
|
||||||
usz_t len_string = rr_measure_string(string);
|
usz_t len_string = rr_measure_string(string);
|
||||||
char *cloned_string = rr_arena_alloc(arena, len_string+1);
|
char *cloned_string = rr_arena_alloc(generic_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);
|
||||||
|
|
Loading…
Reference in New Issue