fixed it somehow, still WIP
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
#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
|
||||||
|
// also make the default function crash if outOf memory?
|
||||||
size_t new_size = this->size + bytes;
|
size_t new_size = this->size + bytes;
|
||||||
if (new_size > this->capacity) {
|
if (new_size > this->capacity) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -122,14 +122,26 @@ typedef struct arena_allocator {
|
|||||||
// 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 allocated;
|
size_t total_allocated;
|
||||||
// Initial chunk of memory to allocate.
|
uint8_t *bytes;
|
||||||
size_t chunk_size;
|
|
||||||
} arena_allocator_t;
|
} arena_allocator_t;
|
||||||
void *arena_allocator_alloc_func(arena_allocator_t *this, size_t bytes, const char *file, int line);
|
#define arena_allocator_create() \
|
||||||
|
(arena_allocator_t) { \
|
||||||
|
.borrow_allocator = borrow_allocator_create(), .total_allocated = 0, \
|
||||||
|
.bytes = NULL \
|
||||||
|
}
|
||||||
|
|
||||||
|
void *arena_allocator_alloc_func(
|
||||||
|
arena_allocator_t *this,
|
||||||
|
size_t bytes,
|
||||||
|
const char *file,
|
||||||
|
int line
|
||||||
|
);
|
||||||
#define arena_allocator_alloc(THIS, BYTES) \
|
#define arena_allocator_alloc(THIS, BYTES) \
|
||||||
arena_allocator_alloc_func(THIS, BYTES, __FILE__, __LINE__)
|
arena_allocator_alloc_func(THIS, BYTES, __FILE__, __LINE__)
|
||||||
// TODO finish this shite
|
|
||||||
|
void arena_allocator_reset(arena_allocator_t *this);
|
||||||
|
void arena_allocator_destroy(arena_allocator_t *this);
|
||||||
|
|
||||||
// dynamic arrays //////////////////////////////////////////////////////////////
|
// dynamic arrays //////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|||||||
+5
-1
@@ -32,6 +32,7 @@ void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int
|
|||||||
file,
|
file,
|
||||||
line
|
line
|
||||||
);
|
);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
@@ -70,8 +71,8 @@ void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items, const cha
|
|||||||
);
|
);
|
||||||
if (header == NULL) { return NULL; }
|
if (header == NULL) { return NULL; }
|
||||||
header->capacity = new_capacity;
|
header->capacity = new_capacity;
|
||||||
header->size = new_size;
|
|
||||||
}
|
}
|
||||||
|
header->size = new_size;
|
||||||
return &header->bytes;
|
return &header->bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,17 +91,20 @@ void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *fil
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t dyn_array_length(void *this) {
|
size_t dyn_array_length(void *this) {
|
||||||
|
if (this == NULL) { return 0; }
|
||||||
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
||||||
return header->size;
|
return header->size;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t dyn_array_capacity(void *this) {
|
size_t dyn_array_capacity(void *this) {
|
||||||
|
if (this == NULL) { return 0; }
|
||||||
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
||||||
return header->capacity;
|
return header->capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dyn_array_destroy(void *this) {
|
void dyn_array_destroy(void *this) {
|
||||||
|
if (this == NULL) { return; }
|
||||||
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
||||||
allocator_free(header->allocator, header);
|
allocator_free(header->allocator, header);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,13 @@ TESTBIN := /tmp/all_tests
|
|||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
|
|
||||||
|
run_test: test
|
||||||
|
$(TESTBIN)
|
||||||
|
|
||||||
|
run_test_memcheck: test
|
||||||
|
valgrind --leak-check=full --show-leak-kinds=all --trace-children=yes --error-exitcode=1 $(TESTBIN); \
|
||||||
|
|
||||||
|
|
||||||
test:
|
test:
|
||||||
@echo "Discovering test files..."
|
@echo "Discovering test files..."
|
||||||
@files=$$(find . -type f -name 'test*.c'); \
|
@files=$$(find . -type f -name 'test*.c'); \
|
||||||
@@ -15,8 +22,6 @@ test:
|
|||||||
else \
|
else \
|
||||||
echo "Compiling all test files into $(TESTBIN)..."; \
|
echo "Compiling all test files into $(TESTBIN)..."; \
|
||||||
$(CC) $(CFLAGS) _allocator_impl.c $$files -o $(TESTBIN) $(LDFLAGS) || exit 1; \
|
$(CC) $(CFLAGS) _allocator_impl.c $$files -o $(TESTBIN) $(LDFLAGS) || exit 1; \
|
||||||
echo "Running tests..."; \
|
|
||||||
valgrind --leak-check=full --show-leak-kinds=all --trace-children=yes --error-exitcode=1 $(TESTBIN); \
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
PROJECT_ROOT := $(shell pwd)
|
PROJECT_ROOT := $(shell pwd)
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ Test(dynamic_arrays, pop) {
|
|||||||
dyn_array_append(numbers, 48);
|
dyn_array_append(numbers, 48);
|
||||||
dyn_array_append(numbers, 49);
|
dyn_array_append(numbers, 49);
|
||||||
dyn_array_append(numbers, 50);
|
dyn_array_append(numbers, 50);
|
||||||
|
cr_assert_eq(dyn_array_length(numbers), 11);
|
||||||
// NOTE: you can stack for loops to have scoped variables you can abuse
|
// NOTE: you can stack for loops to have scoped variables you can abuse
|
||||||
// in macros. e.g.
|
// in macros. e.g.
|
||||||
// for (TYPE UNIQUE = (int)dyn_array_length(numbers); UNIQUE != 0; UNIQUE = 0;)
|
// for (TYPE UNIQUE = (int)dyn_array_length(numbers); UNIQUE != 0; UNIQUE = 0;)
|
||||||
|
|||||||
Reference in New Issue
Block a user