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
|
#include "std_allocator.h"
#include <stdlib.h>
static void *allocate(size_t size, size_t alignment, void *user_data) {
(void)user_data;
// `aligned_alloc(3)` requires that the requested size be a multiple of the requested alignment.
// this is safe to do, because we ignore `old_size` everywhere else.
size_t actual_size = (size + alignment - 1) & ~(alignment - 1);
return aligned_alloc(alignment, actual_size);
}
static void deallocate(void *old_ptr, size_t old_size, void *user_data) {
(void)old_size;
(void)user_data;
free(old_ptr);
}
static void *reallocate(void *old_ptr, size_t old_size, size_t new_size, size_t alignment, void *user_data) {
(void)old_size;
(void)alignment;
(void)user_data;
// fixme: does `realloc(3)` preserve alignment?
return realloc(old_ptr, new_size);
}
SandAllocator sand_get_std_allocator(void) {
return (SandAllocator) {
.allocate = allocate,
.deallocate = deallocate,
.reallocate = reallocate,
.user_data = NULL,
};
}
|