WIP continue the work on arena allcoator implementation
This commit is contained in:
@@ -31,3 +31,8 @@ are deferred, and called in the reverse order. This way the first gui function
|
|||||||
that captrues input can signal to the other function that input is captured,
|
that captrues input can signal to the other function that input is captured,
|
||||||
and the drawing of the gui elements can reflect the priority of the gui
|
and the drawing of the gui elements can reflect the priority of the gui
|
||||||
functions. Overlapping gui elements in raygui is the main painpoint imo.
|
functions. Overlapping gui elements in raygui is the main painpoint imo.
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
|
||||||
|
If there is an allocation error it does not actually cause a crash...
|
||||||
|
FIX!!!
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#include "cig.h"
|
||||||
|
|
||||||
|
|
||||||
|
void *arena_allocator_alloc_func(
|
||||||
|
arena_allocator_t *this,
|
||||||
|
size_t bytes,
|
||||||
|
const char *file,
|
||||||
|
int line
|
||||||
|
) {
|
||||||
|
allocator_t std_alloc = allocator_stdlib();
|
||||||
|
buffer_allocator_t ba = (buffer_allocator_t) {
|
||||||
|
.size=dyn_array_length(this->bytes),
|
||||||
|
.capacity=dyn_array_capacity(this->bytes),
|
||||||
|
.data=this->bytes
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void arena_allocator_reset(arena_allocator_t *this) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void arena_allocator_destroy(arena_allocator_t *this) {
|
||||||
|
}
|
||||||
|
|
||||||
|
allocator_t arena_allocator_interface(arena_allocator_t *this) {
|
||||||
|
}
|
||||||
|
|
||||||
+4
-3
@@ -1,12 +1,13 @@
|
|||||||
#include "cig.h"
|
#include "cig.h"
|
||||||
|
|
||||||
void *buffer_allocator_alloc(buffer_allocator_t *this, size_t bytes) {
|
void *buffer_allocator_alloc(buffer_allocator_t *this, size_t bytes) {
|
||||||
// TODO: use max-align
|
size_t address_of_new_data = (size_t) &this->data[this->size];
|
||||||
// also make the default function crash if outOf memory?
|
size_t offset = address_of_new_data % MAX_ALIGN;
|
||||||
size_t new_size = this->size + bytes;
|
size_t new_size = this->size + offset + bytes;
|
||||||
if (new_size > this->capacity) {
|
if (new_size > this->capacity) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
this->size += offset;
|
||||||
void *ptr = &this->data[this->size];
|
void *ptr = &this->data[this->size];
|
||||||
this->size = new_size;
|
this->size = new_size;
|
||||||
return ptr;
|
return ptr;
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
typedef union any_align { char c; int i; long l; long long ll; float f; double d; void *p; long double ld; } any_align_t;
|
typedef union any_align { char c; int i; long l; long long ll; float f; double d; void *p; } any_align_t;
|
||||||
#define MAX_ALIGN (sizeof(any_align_t))
|
#define MAX_ALIGN ((size_t) sizeof(any_align_t))
|
||||||
#define KB (1024)
|
#define KB (1024)
|
||||||
#define MB (KB * KB)
|
#define MB (KB * KB)
|
||||||
#define GB (KB * KB * KB)
|
#define GB (KB * KB * KB)
|
||||||
@@ -112,18 +112,23 @@ allocator_t borrow_allocator_interface(borrow_allocator_t *this);
|
|||||||
// beforehand. Instead the number of bytes allocated will be recorded and
|
// beforehand. Instead the number of bytes allocated will be recorded and
|
||||||
// updated, so that the whole thing can be reallocated as a single chunk that is
|
// updated, so that the whole thing can be reallocated as a single chunk that is
|
||||||
// as large as it needs to be.
|
// as large as it needs to be.
|
||||||
|
//
|
||||||
|
// This basically exists for allocations that live for a single 'tick' of an
|
||||||
|
// application.
|
||||||
|
// It is intended to eventually stabilize and find the required size of the
|
||||||
|
// memory chunk that is needed for a single frame.
|
||||||
typedef struct arena_allocator {
|
typedef struct arena_allocator {
|
||||||
borrow_allocator_t borrow_allocator;
|
borrow_allocator_t borrow_allocator;
|
||||||
// Total size of bytes allocated. This allocator will only grow the
|
// Total size of bytes allocated. This allocator will only grow the
|
||||||
// allocated field as allocations are made, keeping track of the total
|
// allocated field as allocations are made, keeping track of the total
|
||||||
// memory allocated, and if the allocations have to be done in several
|
// memory allocated, and if the allocations have to be done in several
|
||||||
// chunks because your initial guess of how much memory was needed doesn't
|
// chunks because your initial guess of how much memory was needed doesn't
|
||||||
// meet reality, all chunks will be freed, and then for the next
|
// meet reality, all chunks will be freed, and then for the next
|
||||||
// initialization this total amount will be allocated as a single chunk.
|
// initialization this total amount will be allocated as a single chunk.
|
||||||
// This freeing of memory happens on reset, the next alloc allocates the
|
// This freeing of memory happens on reset, the next alloc allocates the
|
||||||
// whole chunk.
|
// whole chunk.
|
||||||
size_t total_allocated;
|
size_t total_allocated;
|
||||||
uint8_t *bytes;
|
uint8_t *bytes;
|
||||||
} arena_allocator_t;
|
} arena_allocator_t;
|
||||||
#define arena_allocator_create() \
|
#define arena_allocator_create() \
|
||||||
(arena_allocator_t) { \
|
(arena_allocator_t) { \
|
||||||
@@ -142,6 +147,7 @@ void *arena_allocator_alloc_func(
|
|||||||
|
|
||||||
void arena_allocator_reset(arena_allocator_t *this);
|
void arena_allocator_reset(arena_allocator_t *this);
|
||||||
void arena_allocator_destroy(arena_allocator_t *this);
|
void arena_allocator_destroy(arena_allocator_t *this);
|
||||||
|
allocator_t arena_allocator_interface(arena_allocator_t *this);
|
||||||
|
|
||||||
// dynamic arrays //////////////////////////////////////////////////////////////
|
// dynamic arrays //////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -234,6 +240,7 @@ void allocator_free_func(allocator_t this, void *ptr, const char *file, int line
|
|||||||
#include "buffer_allocator.c"
|
#include "buffer_allocator.c"
|
||||||
#include "borrow_allocator.c"
|
#include "borrow_allocator.c"
|
||||||
#include "dyn_array.c"
|
#include "dyn_array.c"
|
||||||
|
#include "arena_allocator.c"
|
||||||
|
|
||||||
#endif // ALLOCATOR_IMPLEMENTATION
|
#endif // ALLOCATOR_IMPLEMENTATION
|
||||||
#endif // ALLOCATOR_H
|
#endif // ALLOCATOR_H
|
||||||
|
|||||||
@@ -23,6 +23,4 @@ Test(macro_magic, default_values) {
|
|||||||
cr_assert_eq(a1.is_true, true);
|
cr_assert_eq(a1.is_true, true);
|
||||||
cr_assert_eq(a2.is_true, true);
|
cr_assert_eq(a2.is_true, true);
|
||||||
cr_assert_eq(a3.is_true, false);
|
cr_assert_eq(a3.is_true, false);
|
||||||
// TODO if there is an allocation error it does not actually cause a crash...
|
|
||||||
// FIX!!!
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user