aboutsummaryrefslogtreecommitdiff
path: root/noleak.c
diff options
context:
space:
mode:
Diffstat (limited to 'noleak.c')
-rw-r--r--noleak.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/noleak.c b/noleak.c
new file mode 100644
index 0000000..eb676ca
--- /dev/null
+++ b/noleak.c
@@ -0,0 +1,34 @@
+#include "noleak.h"
+#include <stdlib.h>
+
+static void *___noleak_data = NULL;
+static size_t ___noleak_capacity = 0;
+static size_t ___noleak_used = 0;
+
+int noleak_init(size_t capacity)
+{
+ ___noleak_data = calloc(1, capacity);
+ if (___noleak_data == NULL)
+ return 1;
+ ___noleak_capacity = capacity;
+ return 0;
+}
+
+void noleak_deinit(void)
+{
+ if (___noleak_data != NULL) {
+ free(___noleak_data);
+ ___noleak_data = NULL;
+ }
+}
+
+void *noleak_alloc(size_t size)
+{
+ if (___noleak_data == NULL)
+ return NULL;
+ if (___noleak_used + size >= ___noleak_capacity)
+ return NULL;
+ void *const ptr = ___noleak_data + ___noleak_used;
+ ___noleak_used += size;
+ return ptr;
+}