summaryrefslogtreecommitdiff
path: root/kit/allocator.c
blob: 9fd7bad97fb05d208a368939d78ecc48ac9193d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "allocator.h"

#include <assert.h>
#include <string.h>

#ifndef KIT_DISABLE_SYSTEM_MALLOC
#  include <stdlib.h>
#endif

static void *kit_allocate_default_(i32 request, i64 size,
                                   i64 previous_size, void *pointer) {
#ifndef KIT_DISABLE_SYSTEM_MALLOC
  switch (request) {
    case KIT_ALLOCATE:
    case KIT_ALLOCATE_ZERO: {
      assert(size >= 0);
      assert(previous_size == 0);
      assert(pointer == NULL);

      if (size <= 0)
        return NULL;

      void *p = malloc(size);

      if (p != NULL && request == KIT_ALLOCATE_ZERO)
        memset(p, 0, size);

      return p;
    }

    case KIT_REALLOCATE:
    case KIT_REALLOCATE_ZERO: {
      assert(size >= 0);
      assert(previous_size != 0 || pointer == NULL);
      assert(previous_size == 0 || pointer != NULL);

      if (previous_size == 0 && pointer != NULL)
        return NULL;
      if (previous_size != 0 && pointer == NULL)
        return NULL;
      if (size == previous_size)
        return pointer;

      u8 *p = NULL;

      if (size > 0) {
        p = (u8 *) malloc(size);

        if (p != NULL) {
          if (size > 0 && previous_size > 0)
            memcpy(p, pointer,
                   size < previous_size ? size : previous_size);
          if (request == KIT_REALLOCATE_ZERO && size > previous_size)
            memset(p + previous_size, 0, size - previous_size);
        }
      }

      free(pointer);

      return p;
    }

    case KIT_DEALLOCATE:
      assert(size == 0);
      assert(pointer != NULL);
      if (pointer != NULL)
        free(pointer);
      return NULL;

    case KIT_DEALLOCATE_ALL:
      //  Do nothing.
      //
      return NULL;

    default:;
  }
#endif

  assert(0);
  return NULL;
}

static void *kit_allocate_from_buffer_(kit_allocator_t *alloc,
                                       i32 request, i64 size,
                                       i64   previous_size,
                                       void *pointer) {
  assert(alloc != NULL);
  assert(pointer == NULL || pointer < alloc->data);

  if (alloc == NULL)
    return NULL;

  switch (request) {
    case KIT_ALLOCATE:
    case KIT_ALLOCATE_ZERO: {
      assert(size >= 0);
      assert(previous_size == 0);
      assert(pointer == NULL);

      if (size <= 0)
        return NULL;

      if (alloc->size < size)
        return NULL;

      void *p = alloc->data;
      alloc->bytes += size;
      alloc->size -= size;

      if (request == KIT_ALLOCATE_ZERO)
        memset(p, 0, size);

      return p;
    }

    case KIT_REALLOCATE:
    case KIT_REALLOCATE_ZERO: {
      assert(size >= 0);
      assert(previous_size != 0 || pointer == NULL);
      assert(previous_size == 0 || pointer != NULL);

      if (size <= 0)
        return NULL;
      if (size <= previous_size)
        return pointer;
      if (previous_size == 0 && pointer != NULL)
        return NULL;
      if (previous_size != 0 && pointer == NULL)
        return NULL;

      if ((u8 *) pointer + previous_size == alloc->data) {
        if (alloc->size < size - previous_size)
          return NULL;
        alloc->bytes += size - previous_size;
        alloc->size -= size - previous_size;
        return pointer;
      }

      if (alloc->size < size)
        return NULL;

      u8 *p = alloc->bytes;
      alloc->bytes += size;
      alloc->size -= size;

      if (previous_size > 0)
        memcpy(p, pointer, previous_size);
      if (request == KIT_REALLOCATE_ZERO)
        memset(p + previous_size, 0, size - previous_size);

      return p;
    }

    case KIT_DEALLOCATE:
    case KIT_DEALLOCATE_ALL: return NULL;

    default:;
  }

  assert(0);
  return NULL;
}

#ifndef KIT_ENABLE_CUSTOM_ALLOC_DISPATCH
void *kit_alloc_dispatch(kit_allocator_t *alloc, i32 request,
                         i64 size, i64 previous_size, void *pointer) {
  if (alloc == NULL)
    return kit_allocate_default_(request, 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_BUFFER:
      return kit_allocate_from_buffer_(alloc, request,
                                       //  alignment
                                       ((size + 7) / 8) * 8,
                                       previous_size, pointer);

    default:;
  }

  return NULL;
}
#endif

kit_allocator_t kit_alloc_default(void) {
  return (kit_allocator_t) { .type = KIT_ALLOC_TYPE_DEFAULT,
                             .size = 0,
                             .data = NULL };
}

kit_allocator_t kit_alloc_buffer(i64 size, void *buffer) {
  return (kit_allocator_t) { .type = KIT_ALLOC_TYPE_BUFFER,
                             .size = size,
                             .data = buffer };
}