diff --git a/borrow_allocator.c b/borrow_allocator.c index ca66841..82cd081 100644 --- a/borrow_allocator.c +++ b/borrow_allocator.c @@ -82,7 +82,7 @@ static const allocator_vtbl_t borrow_vtbl = { .reset = borrow_reset, }; -allocator_t borrow_allocator(borrow_allocator_t *this) { +allocator_t allocator_from_borrow(borrow_allocator_t *this) { return (allocator_t) { .this=this, .vtbl=&borrow_vtbl, diff --git a/cig.h b/cig.h index 9572b0f..c488330 100644 --- a/cig.h +++ b/cig.h @@ -62,9 +62,9 @@ typedef struct borrow_allocator { linked_allocation_node_t *head; } borrow_allocator_t; -#define borrow_allocator_create() ((borrow_allocator_t){.head=NULL}) - -allocator_t borrow_allocator(borrow_allocator_t *this); +#define borrow_allocator_value() ((borrow_allocator_t){.head=NULL}) +allocator_t allocator_from_borrow(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 // 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. // TODO: simplify! #define with_borrow(NAME) \ - for (allocator_t NAME = \ - borrow_allocator(&borrow_allocator_create()); \ - !((borrow_allocator_t *)NAME.this)->head; \ - ((borrow_allocator_t *)NAME.this)->head = \ - (allocator_reset(NAME), \ - (linked_allocation_node_t *)1)) \ + for (allocator_t NAME = borrow_allocator_create(); NAME.this != NULL; \ + NAME.this = (allocator_reset(NAME), NULL)) \ for (int UNIQUE = 0; UNIQUE < 1; UNIQUE++) // dynamic arrays ////////////////////////////////////////////////////////////// @@ -182,8 +178,7 @@ void allocator_reset(allocator_t this) { this.vtbl->reset(this.this); } -// TODO: rename std_allocator.c to forever_allocator.c -#include "std_allocator.c" +#include "forever_allocator.c" #include "buffer_allocator.c" #include "borrow_allocator.c" #include "dyn_array.c" diff --git a/std_allocator.c b/forever_allocator.c similarity index 100% rename from std_allocator.c rename to forever_allocator.c diff --git a/test_borrow_allocator.c b/test_borrow_allocator.c index f08b526..bb06d5a 100644 --- a/test_borrow_allocator.c +++ b/test_borrow_allocator.c @@ -3,7 +3,7 @@ #define EXPECT 5 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)); *ptr = EXPECT; cr_assert_eq(*ptr, EXPECT);