This commit is contained in:
2025-12-03 18:26:32 +01:00
parent 5d22f8c47a
commit 83a8526a78
4 changed files with 8 additions and 13 deletions
+1 -1
View File
@@ -82,7 +82,7 @@ static const allocator_vtbl_t borrow_vtbl = {
.reset = borrow_reset, .reset = borrow_reset,
}; };
allocator_t borrow_allocator(borrow_allocator_t *this) { allocator_t allocator_from_borrow(borrow_allocator_t *this) {
return (allocator_t) { return (allocator_t) {
.this=this, .this=this,
.vtbl=&borrow_vtbl, .vtbl=&borrow_vtbl,
+6 -11
View File
@@ -62,9 +62,9 @@ typedef struct borrow_allocator {
linked_allocation_node_t *head; linked_allocation_node_t *head;
} borrow_allocator_t; } borrow_allocator_t;
#define borrow_allocator_create() ((borrow_allocator_t){.head=NULL}) #define borrow_allocator_value() ((borrow_allocator_t){.head=NULL})
allocator_t allocator_from_borrow(borrow_allocator_t *this);
allocator_t borrow_allocator(borrow_allocator_t *this); #define borrow_allocator_create() allocator_from_borrow(&borrow_allocator_value())
// Some text that can be used as an identifier (no, not by you), so that I can // Some text that can be used as an identifier (no, not by you), so that I can
// use a variable that won't collide with yours inside macros. // use a variable that won't collide with yours inside macros.
@@ -78,12 +78,8 @@ allocator_t borrow_allocator(borrow_allocator_t *this);
// guaranteed memory leak. // guaranteed memory leak.
// TODO: simplify! // TODO: simplify!
#define with_borrow(NAME) \ #define with_borrow(NAME) \
for (allocator_t NAME = \ for (allocator_t NAME = borrow_allocator_create(); NAME.this != NULL; \
borrow_allocator(&borrow_allocator_create()); \ NAME.this = (allocator_reset(NAME), NULL)) \
!((borrow_allocator_t *)NAME.this)->head; \
((borrow_allocator_t *)NAME.this)->head = \
(allocator_reset(NAME), \
(linked_allocation_node_t *)1)) \
for (int UNIQUE = 0; UNIQUE < 1; UNIQUE++) for (int UNIQUE = 0; UNIQUE < 1; UNIQUE++)
// dynamic arrays ////////////////////////////////////////////////////////////// // dynamic arrays //////////////////////////////////////////////////////////////
@@ -182,8 +178,7 @@ void allocator_reset(allocator_t this) {
this.vtbl->reset(this.this); this.vtbl->reset(this.this);
} }
// TODO: rename std_allocator.c to forever_allocator.c #include "forever_allocator.c"
#include "std_allocator.c"
#include "buffer_allocator.c" #include "buffer_allocator.c"
#include "borrow_allocator.c" #include "borrow_allocator.c"
#include "dyn_array.c" #include "dyn_array.c"
+1 -1
View File
@@ -3,7 +3,7 @@
#define EXPECT 5 #define EXPECT 5
Test(borrow_allocator, test) { Test(borrow_allocator, test) {
allocator_t balloc = borrow_allocator(&borrow_allocator_create()); allocator_t balloc = allocator_from_borrow(&borrow_allocator_value());
int *ptr = allocator_alloc(balloc, sizeof(int)); int *ptr = allocator_alloc(balloc, sizeof(int));
*ptr = EXPECT; *ptr = EXPECT;
cr_assert_eq(*ptr, EXPECT); cr_assert_eq(*ptr, EXPECT);