summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/kit/allocator.c65
-rw-r--r--source/kit/allocator.h13
-rw-r--r--source/kit/input_buffer.c1
3 files changed, 65 insertions, 14 deletions
diff --git a/source/kit/allocator.c b/source/kit/allocator.c
index b07cca8..a761407 100644
--- a/source/kit/allocator.c
+++ b/source/kit/allocator.c
@@ -4,16 +4,30 @@
#ifndef KIT_DISABLE_SYSTEM_MALLOC
# include <stdlib.h>
+# include <string.h>
#endif
-static void *allocate(int request, void *state, ptrdiff_t size,
- ptrdiff_t previous_size, void *pointer) {
+enum {
+ KIT_ALLOC_TYPE_NONE,
+ KIT_ALLOC_TYPE_DEFAULT,
+ KIT_ALLOC_TYPE_BUMP,
+ KIT_ALLOC_TYPE_CUSTOM
+};
+
+static void *kit_allocate_default_(int request, ptrdiff_t size,
+ ptrdiff_t previous_size,
+ void *pointer) {
#ifndef KIT_DISABLE_SYSTEM_MALLOC
switch (request) {
case KIT_ALLOCATE:
+ case KIT_ALLOCATE_ZERO: {
assert(previous_size == 0);
assert(pointer == NULL);
- return malloc(size);
+ void *p = malloc(size);
+ if (request == KIT_ALLOCATE_ZERO && p != NULL)
+ memset(p, 0, size);
+ return p;
+ }
case KIT_DEALLOCATE:
assert(size == 0);
@@ -22,12 +36,18 @@ static void *allocate(int request, void *state, ptrdiff_t size,
return NULL;
case KIT_REALLOCATE:
- /* FIXME
- * Not implemented.
- */
+ case KIT_REALLOCATE_ZERO:
+ // FIXME
+ // Not implemented.
+ //
assert(0);
return NULL;
+ case KIT_DEALLOCATE_ALL:
+ // Do nothing.
+ //
+ return NULL;
+
default: assert(0);
}
#else
@@ -40,15 +60,36 @@ static void *allocate(int request, void *state, ptrdiff_t size,
void *kit_alloc_dispatch(kit_allocator_t alloc, int request,
ptrdiff_t size, ptrdiff_t previous_size,
void *pointer) {
- assert(alloc.allocate != NULL);
- if (alloc.allocate == NULL)
- return NULL;
- return alloc.allocate(request, alloc.state, size, previous_size,
- pointer);
+ switch (alloc.type) {
+ case KIT_ALLOC_TYPE_DEFAULT:
+ return kit_allocate_default_(request, size, previous_size,
+ pointer);
+
+ case KIT_ALLOC_TYPE_CUSTOM:
+ assert(alloc.allocate != NULL);
+ if (alloc.allocate != NULL)
+ return alloc.allocate(request, alloc.state, size,
+ previous_size, pointer);
+ break;
+
+ default:;
+ }
+
+ return NULL;
}
#endif
kit_allocator_t kit_alloc_default(void) {
- kit_allocator_t alloc = { .state = NULL, .allocate = allocate };
+ kit_allocator_t alloc = { .state = NULL,
+ .type = KIT_ALLOC_TYPE_DEFAULT,
+ .allocate = NULL };
return alloc;
}
+
+kit_allocator_t kit_alloc_bump(ptrdiff_t size, void *buffer,
+ kit_allocator_t upstream) {
+ // TODO
+ //
+
+ return kit_alloc_default();
+}
diff --git a/source/kit/allocator.h b/source/kit/allocator.h
index f6dc62d..f0f5c8d 100644
--- a/source/kit/allocator.h
+++ b/source/kit/allocator.h
@@ -11,7 +11,14 @@
extern "C" {
#endif
-enum { KIT_ALLOCATE, KIT_DEALLOCATE, KIT_REALLOCATE };
+enum {
+ KIT_ALLOCATE,
+ KIT_ALLOCATE_ZERO,
+ KIT_DEALLOCATE,
+ KIT_REALLOCATE,
+ KIT_REALLOCATE_ZERO,
+ KIT_DEALLOCATE_ALL
+};
typedef void *(*kit_allocate_fn)(int request, void *state,
ptrdiff_t size,
@@ -19,6 +26,7 @@ typedef void *(*kit_allocate_fn)(int request, void *state,
void *pointer);
typedef struct {
+ int type;
void *state;
kit_allocate_fn allocate;
} kit_allocator_t;
@@ -34,6 +42,9 @@ void *kit_alloc_dispatch(kit_allocator_t alloc, int request,
kit_allocator_t kit_alloc_default(void);
+kit_allocator_t kit_alloc_bump(ptrdiff_t size, void *buffer,
+ kit_allocator_t upstream);
+
#ifdef __cplusplus
}
#endif
diff --git a/source/kit/input_buffer.c b/source/kit/input_buffer.c
index 2ba3f4c..2e74cad 100644
--- a/source/kit/input_buffer.c
+++ b/source/kit/input_buffer.c
@@ -12,7 +12,6 @@ typedef struct {
static internal_buffer_t *buf_init(kit_is_handle_t upstream,
kit_allocator_t alloc) {
- assert(alloc.allocate != NULL);
internal_buffer_t *buf = kit_alloc_dispatch(alloc, KIT_ALLOCATE,
sizeof *buf, 0, NULL);