summaryrefslogtreecommitdiff
path: root/allocator.c
diff options
context:
space:
mode:
Diffstat (limited to 'allocator.c')
-rw-r--r--allocator.c34
1 files changed, 5 insertions, 29 deletions
diff --git a/allocator.c b/allocator.c
index 6add3cf..4dc05c8 100644
--- a/allocator.c
+++ b/allocator.c
@@ -4,8 +4,6 @@
static void allocr_create(Allocator *a) {
a->first = a->last = NULL;
- a->dyn = NULL;
- a->dyn_len = a->dyn_cap = 0;
}
static void *allocr_malloc(Allocator *a, size_t bytes) {
@@ -39,29 +37,11 @@ static void *allocr_calloc(Allocator *a, size_t n, size_t sz) {
return data;
}
-/* IMPORTANT: this can only be called with data which was originally allocated with allocr_realloc(a, NULL, x)
- */
-static void *allocr_realloc(Allocator *a, void *data, size_t new_size) {
- if (data) {
- DynPage *page = (DynPage *)((char *)data - offsetof(DynPage, data));
- page = err_realloc(page, new_size + sizeof(DynPage));
- *page->self = page;
- return page->data;
- } else {
- if (a->dyn_len >= a->dyn_cap) {
- a->dyn_cap = 2 * (a->dyn_len + 1);
- a->dyn = realloc(a->dyn, a->dyn_cap * sizeof(DynPage *));
- for (size_t i = 0; i < a->dyn_len; i++) {
- a->dyn[i]->self = &a->dyn[i];
- }
- }
- DynPage *page = err_malloc(sizeof(DynPage) + new_size);
- page->self = &a->dyn[a->dyn_len];
- *page->self = page;
- a->dyn_len++;
- return page->data;
- }
- return NULL;
+/* OPTIM */
+static void *allocr_realloc(Allocator *a, void *data, size_t old_size, size_t new_size) {
+ void *ret = allocr_malloc(a, new_size);
+ memcpy(ret, data, old_size);
+ return ret;
}
static void allocr_free_all(Allocator *a) {
@@ -70,8 +50,4 @@ static void allocr_free_all(Allocator *a) {
free(page);
page = next;
}
- for (size_t i = 0; i < a->dyn_len; i++) {
- free(a->dyn[i]);
- }
- free(a->dyn);
}