retab everything to use tabs
This commit is contained in:
+1
-1
@@ -48,7 +48,7 @@ static const allocator_vtbl_t buffer_vtbl = {
|
|||||||
.reset = buffer_reset_impl,
|
.reset = buffer_reset_impl,
|
||||||
};
|
};
|
||||||
|
|
||||||
allocator_t buffer_allocator(buffer_allocator_t *this) {
|
allocator_t allocator_from_buffer(buffer_allocator_t *this) {
|
||||||
return (allocator_t) {
|
return (allocator_t) {
|
||||||
.this=this,
|
.this=this,
|
||||||
.vtbl=&buffer_vtbl,
|
.vtbl=&buffer_vtbl,
|
||||||
|
|||||||
@@ -37,19 +37,21 @@ void allocator_reset(allocator_t this);
|
|||||||
#define allocator_alloc(this, bytes) allocator_alloc_func(this, bytes, __FILE__, __LINE__)
|
#define allocator_alloc(this, bytes) allocator_alloc_func(this, bytes, __FILE__, __LINE__)
|
||||||
#define allocator_resize(this, old_ptr, bytes) allocator_resize_func(this, old_ptr, bytes, __FILE__, __LINE__)
|
#define allocator_resize(this, old_ptr, bytes) allocator_resize_func(this, old_ptr, bytes, __FILE__, __LINE__)
|
||||||
|
|
||||||
// std_allocator ///////////////////////////////////////////////////////////////
|
// forever_allocator ///////////////////////////////////////////////////////////
|
||||||
allocator_t forever_allocator();
|
allocator_t forever_allocator();
|
||||||
|
|
||||||
// buffer_allocator ////////////////////////////////////////////////////////////
|
// buffer_allocator ////////////////////////////////////////////////////////////
|
||||||
typedef struct buffer_allocator {
|
typedef struct buffer_allocator {
|
||||||
size_t size, capacity;
|
size_t size, capacity;
|
||||||
uint8_t *data;
|
uint8_t *data;
|
||||||
} buffer_allocator_t;
|
} buffer_allocator_t;
|
||||||
|
|
||||||
#define buffer_allocator_create(CAPACITY) \
|
// TODO: name stack value or something to signal what is happening here!
|
||||||
|
#define buffer_allocator_value(CAPACITY) \
|
||||||
((buffer_allocator_t){ \
|
((buffer_allocator_t){ \
|
||||||
.size = 0, .capacity = CAPACITY, .data = (uint8_t[CAPACITY]){0}})
|
.size = 0, .capacity = CAPACITY, .data = (uint8_t[CAPACITY]){0}})
|
||||||
|
|
||||||
allocator_t buffer_allocator(buffer_allocator_t *this);
|
allocator_t allocator_from_buffer(buffer_allocator_t *this);
|
||||||
|
|
||||||
// borrow_allocator ////////////////////////////////////////////////////////////
|
// borrow_allocator ////////////////////////////////////////////////////////////
|
||||||
typedef struct linked_allocation_node {
|
typedef struct linked_allocation_node {
|
||||||
@@ -81,6 +83,33 @@ allocator_t allocator_from_borrow(borrow_allocator_t *this);
|
|||||||
NAME.this = (allocator_reset(NAME), NULL)) \
|
NAME.this = (allocator_reset(NAME), NULL)) \
|
||||||
for (int UNIQUE = 0; UNIQUE < 1; UNIQUE++)
|
for (int UNIQUE = 0; UNIQUE < 1; UNIQUE++)
|
||||||
|
|
||||||
|
// arena allocator /////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
typedef struct arena_allocator {
|
||||||
|
allocator_t allocator;
|
||||||
|
size_t size;
|
||||||
|
size_t capacity;
|
||||||
|
// this may grow outside the allocated data. When data is not large enough,
|
||||||
|
// the backing allocator will be used to allocate exactly what the user
|
||||||
|
// asked for, and the total_allocated will grow for each such allocation.
|
||||||
|
// On the next reset if total_allocated is larger than capacity, we know
|
||||||
|
// that there are allocations outside the large buffer, so we reset the
|
||||||
|
// allocator, and allocate a new buffer that is the size of
|
||||||
|
// total_allocated, and set the new size of the capacity. Otherwise the
|
||||||
|
// size is just reset to 0. plus something else I think I forgot.
|
||||||
|
size_t total_allocated_bytes;
|
||||||
|
uint8_t *data;
|
||||||
|
} arena_allocator_t;
|
||||||
|
|
||||||
|
#define arena_allocator_value(INITIAL_CAPACITY) ((arena_allocator_t) { \
|
||||||
|
.allocator=borrow_allocator_create(), \
|
||||||
|
.size=0, \
|
||||||
|
.capacity=INITIAL_CAPACITY, \
|
||||||
|
.data=NULL \
|
||||||
|
})
|
||||||
|
|
||||||
|
allocator_t allocator_from_arena(arena_allocator_t *this);
|
||||||
|
|
||||||
// dynamic arrays //////////////////////////////////////////////////////////////
|
// dynamic arrays //////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
typedef struct dyn_array_header {
|
typedef struct dyn_array_header {
|
||||||
@@ -180,6 +209,7 @@ void allocator_reset(allocator_t this) {
|
|||||||
#include "forever_allocator.c"
|
#include "forever_allocator.c"
|
||||||
#include "buffer_allocator.c"
|
#include "buffer_allocator.c"
|
||||||
#include "borrow_allocator.c"
|
#include "borrow_allocator.c"
|
||||||
|
#include "arena_allocator.c"
|
||||||
#include "dyn_array.c"
|
#include "dyn_array.c"
|
||||||
|
|
||||||
#endif // CIG_IMPL
|
#endif // CIG_IMPL
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#include "cig.h"
|
#include "cig.h"
|
||||||
|
|
||||||
static void test_buffer_alloc(buffer_allocator_t impl) {
|
static void test_buffer_alloc(buffer_allocator_t impl) {
|
||||||
allocator_t inter = buffer_allocator(&impl);
|
allocator_t inter = allocator_from_buffer(&impl);
|
||||||
const int n_ints = 100;
|
const int n_ints = 100;
|
||||||
int *ints = allocator_alloc(inter, sizeof(int) * n_ints);
|
int *ints = allocator_alloc(inter, sizeof(int) * n_ints);
|
||||||
for (int i = 0; i < n_ints; i++) {
|
for (int i = 0; i < n_ints; i++) {
|
||||||
@@ -27,7 +27,7 @@ static void test_buffer_alloc(buffer_allocator_t impl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Test(buffer_allocator, test) {
|
Test(buffer_allocator, test) {
|
||||||
buffer_allocator_t impl = buffer_allocator_create(MB);
|
buffer_allocator_t impl = buffer_allocator_value(MB);
|
||||||
test_buffer_alloc(impl);
|
test_buffer_alloc(impl);
|
||||||
cr_assert_eq(impl.size, 0);
|
cr_assert_eq(impl.size, 0);
|
||||||
test_buffer_alloc(impl);
|
test_buffer_alloc(impl);
|
||||||
|
|||||||
Reference in New Issue
Block a user