update
This commit is contained in:
+1
-1
@@ -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,
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user