aboutsummaryrefslogtreecommitdiff
path: root/allocator.c
diff options
context:
space:
mode:
Diffstat (limited to 'allocator.c')
-rw-r--r--allocator.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/allocator.c b/allocator.c
new file mode 100644
index 0000000..c498bc7
--- /dev/null
+++ b/allocator.c
@@ -0,0 +1,75 @@
1#include <stdlib.h>
2#include "allocator.h"
3
4/* Interface Code */
5static void *
6default_allocate(struct its1_allocator *allocator, size_t n)
7{
8 (void) allocator;
9 return malloc(n);
10}
11
12static void
13default_deallocate(struct its1_allocator *allocator, void *ptr)
14{
15 (void) allocator;
16 free(ptr);
17}
18
19struct its1_allocator its1_default_allocator = {
20 .allocate = &default_allocate,
21 .deallocate = &default_deallocate
22};
23
24/* Libary Code */
25#include <string.h>
26
27void *
28its1_malloc(struct its1_allocator *allocator, size_t n)
29{
30 if (allocator == NULL) {
31 allocator = &its1_default_allocator;
32 }
33
34 return allocator->allocate(allocator, n);
35}
36
37void
38its1_free(struct its1_allocator *allocator, void *ptr)
39{
40 if (allocator == NULL) {
41 allocator = &its1_default_allocator;
42 }
43
44 allocator->deallocate(allocator, ptr);
45}
46
47char *
48its1_strdup(struct its1_allocator *allocator, const char *str)
49{
50 size_t n;
51 char *dest;
52
53 n = strlen(str) + 1;
54 dest = its1_malloc(allocator, n);
55 if (dest != NULL) {
56 memcpy(dest, str, n);
57 }
58
59 return dest;
60}
61
62void *
63its1_malloc_zero(struct its1_allocator *allocator, size_t n)
64{
65 void *ptr;
66
67 ptr = its1_malloc(allocator, n);
68 if (ptr != NULL) {
69 memset(ptr, 0, n);
70 }
71
72 return ptr;
73}
74
75int main() {}