-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmgallocator.c
More file actions
197 lines (170 loc) · 6.35 KB
/
mgallocator.c
File metadata and controls
197 lines (170 loc) · 6.35 KB
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
// Copyright (c) 2016-2020 Memgraph Ltd. [https://memgraph.com]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "mgallocator.h"
#include <assert.h>
// Somehow stdalign.h is not there (VS Build Tools 2019, Windows 11)
// https://docs.microsoft.com/en-us/cpp/cpp/alignment-cpp-declarations?view=msvc-170
// EVERYWHERE EXCEPT MSVC
#if !defined(_WIN32) || !defined(_MSC_VER)
#include <stdalign.h>
#endif
// ONLY ON MSVC
#ifdef _MSC_VER
#define alignof __alignof
typedef double max_align_t;
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *mg_system_realloc(struct mg_allocator *self, void *buf, size_t size) {
(void)self;
return realloc(buf, size);
}
void *mg_system_malloc(struct mg_allocator *self, size_t size) {
(void)self;
return malloc(size);
}
void mg_system_free(struct mg_allocator *self, void *buf) {
(void)self;
free(buf);
}
void *mg_allocator_malloc(struct mg_allocator *allocator, size_t size) {
return allocator->malloc(allocator, size);
}
void *mg_allocator_realloc(struct mg_allocator *allocator, void *buf,
size_t size) {
return allocator->realloc(allocator, buf, size);
}
void mg_allocator_free(struct mg_allocator *allocator, void *buf) {
allocator->free(allocator, buf);
}
struct mg_allocator mg_system_allocator = {mg_system_malloc, mg_system_realloc,
mg_system_free};
typedef struct mg_memory_block {
char *buffer;
struct mg_memory_block *next;
} mg_memory_block;
mg_memory_block *mg_memory_block_alloc(mg_allocator *allocator, size_t size) {
/// Ensure that the memory is properly aligned.
_Static_assert(
sizeof(mg_memory_block) % alignof(max_align_t) == 0,
"Size of mg_memory_block doesn't satisfy alignment requirements");
mg_memory_block *block =
mg_allocator_malloc(allocator, sizeof(mg_memory_block) + size);
if (!block) {
return NULL;
}
block->next = NULL;
block->buffer = (char *)block + sizeof(mg_memory_block);
return block;
}
typedef struct mg_linear_allocator {
void *(*malloc)(struct mg_allocator *self, size_t size);
void *(*realloc)(struct mg_allocator *self, void *buf, size_t size);
void (*free)(struct mg_allocator *self, void *buf);
mg_memory_block *current_block;
size_t current_offset;
const size_t block_size;
const size_t sep_alloc_threshold;
mg_allocator *underlying_allocator;
} mg_linear_allocator;
void *mg_linear_allocator_malloc(struct mg_allocator *allocator, size_t size) {
mg_linear_allocator *self = (mg_linear_allocator *)allocator;
if (size >= self->sep_alloc_threshold) {
// Make a new block, but put it below the first block so we don't waste
// bytes in it.
mg_memory_block *new_block =
mg_memory_block_alloc(self->underlying_allocator, size);
new_block->next = self->current_block->next;
self->current_block->next = new_block;
return new_block->buffer;
}
if (self->current_offset + size > self->block_size) {
// Create a new block and put it at the beginning of the list.
mg_memory_block *new_block =
mg_memory_block_alloc(self->underlying_allocator, self->block_size);
new_block->next = self->current_block;
self->current_block = new_block;
self->current_offset = 0;
}
assert(self->current_offset + size <= self->block_size);
assert(self->current_offset % alignof(max_align_t) == 0);
void *ret = self->current_block->buffer + self->current_offset;
self->current_offset += size;
if (self->current_offset % alignof(max_align_t) != 0) {
self->current_offset +=
alignof(max_align_t) - (self->current_offset % alignof(max_align_t));
}
return ret;
}
void *mg_linear_allocator_realloc(struct mg_allocator *allocator, void *buf,
size_t size) {
(void)allocator;
(void)buf;
(void)size;
fprintf(stderr, "mg_linear_allocator doesn't support realloc\n");
return NULL;
}
void mg_linear_allocator_free(struct mg_allocator *allocator, void *buf) {
(void)allocator;
(void)buf;
return;
}
mg_linear_allocator *mg_linear_allocator_init(mg_allocator *allocator,
size_t block_size,
size_t sep_alloc_threshold) {
mg_memory_block *first_block = mg_memory_block_alloc(allocator, block_size);
if (!first_block) {
return NULL;
}
mg_linear_allocator tmp_alloc = {mg_linear_allocator_malloc,
mg_linear_allocator_realloc,
mg_linear_allocator_free,
first_block,
0,
block_size,
sep_alloc_threshold,
allocator};
mg_linear_allocator *alloc =
mg_allocator_malloc(allocator, sizeof(mg_linear_allocator));
if (!alloc) {
mg_allocator_free(allocator, first_block);
return NULL;
}
memcpy(alloc, &tmp_alloc, sizeof(mg_linear_allocator));
return alloc;
}
void mg_linear_allocator_destroy(mg_linear_allocator *allocator) {
if (allocator == NULL) {
return;
}
while (allocator->current_block) {
mg_memory_block *next_block = allocator->current_block->next;
mg_allocator_free(allocator->underlying_allocator,
allocator->current_block);
allocator->current_block = next_block;
}
mg_allocator_free(allocator->underlying_allocator, allocator);
}
void mg_linear_allocator_reset(mg_linear_allocator *allocator) {
// The first block is always of size allocator->block_size. We will keep that
// one and free the others.
mg_memory_block *first_block = allocator->current_block;
while (first_block->next) {
mg_memory_block *new_next = first_block->next->next;
mg_allocator_free(allocator->underlying_allocator, first_block->next);
first_block->next = new_next;
}
allocator->current_offset = 0;
}