Subastra
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1#ifndef __H__MEMORY__
2#define __H__MEMORY__
3
4#include <alloca.h>
5#if defined(__SANITIZE_ADDRESS__)
6#include <sanitizer/asan_interface.h>
7#endif
8
9#include <string.h>
10
11#include "defs.h"
12
15typedef void *allocator_ptr;
16
18typedef void *(mem_alloc_f)(allocator_ptr allocator, sz size);
20// at least `size` bytes using the `allocator`.
21typedef void *(mem_realloc_f)(allocator_ptr allocator, void *memory,
22 sz new_size);
25typedef void(mem_free_f)(allocator_ptr allocator, void *memory);
26
37
40static inline void *allocator_alloc(allocator_t allocator, sz size) {
41 return allocator.alloc(allocator.allocator, size);
42}
43
46#define allocator_alloc_ty(ty, allocator, sz) \
47 (ty *)allocator_alloc(allocator, sizeof(ty) * sz)
48
52static inline void *allocator_alloc_copy(allocator_t allocator,
53 const void *data, sz size) {
54 void *ptr = allocator.alloc(allocator.allocator, size);
55 memcpy(ptr, data, size);
56 return ptr;
57}
58
62#define allocator_alloc_copy_ty(ty, allocator, data, sz) \
63 (ty *)allocator_alloc_copy(allocator, data, sizeof(ty) * sz)
64
68static inline void *allocator_realloc(allocator_t allocator, void *memory,
69 sz size) {
70 ASSERT_(allocator.realloc, "This allocator does not implement `realloc`");
71 return allocator.realloc(allocator.allocator, memory, size);
72}
73
76static inline void allocator_free(allocator_t allocator, void *memory) {
77 if (!allocator.free)
78 return;
79 allocator.free(allocator.allocator, memory);
80}
81
83static void *alloc_malloc__alloc(allocator_ptr allocator, sz new_size) {
84 return malloc(new_size);
85}
86
88static void *alloc_malloc__realloc(allocator_ptr allocator, void *memory,
89 sz new_size) {
90 return realloc(memory, new_size);
91}
92
94static void alloc_malloc__free(allocator_ptr allocator, void *memory) {
95 free(memory);
96}
97
100 allocator_t ret;
101 ret.allocator = 0;
102 ret.alloc = alloc_malloc__alloc;
103 ret.realloc = alloc_malloc__realloc;
104 ret.free = alloc_malloc__free;
105
106 return ret;
107}
108
109static void *alloc_stack_alloc__alloc(allocator_ptr allocator, sz new_size) {
110 return alloca(new_size);
111}
112
114 allocator_t ret;
116 ret.realloc = 0;
117 ret.free = 0;
118 return ret;
119}
120
125#if defined(__SANITIZE_ADDRESS__)
126#define poison_memory_region(ptr, sz) __asan_poison_memory_region(ptr, sz)
127#else
128#define poison_memory_region(ptr, sz)
129#endif
130
133#if defined(__SANITIZE_ADDRESS__)
134#define unpoison_memory_region(ptr, sz) __asan_unpoison_memory_region(ptr, sz)
135#else
136#define unpoison_memory_region(ptr, sz)
137#endif
138
139#endif
size_t sz
Definition defs.h:51
#define ASSERT_(expr, msg)
Definition defs.h:29
void() mem_free_f(allocator_ptr allocator, void *memory)
Give up the ownership of the memory within the allocator.
Definition memory.h:25
void *() mem_realloc_f(allocator_ptr allocator, void *memory, sz new_size)
Reallocate the memory at memory to a new location with.
Definition memory.h:21
static allocator_t allocator_new_malloc()
Wrap malloc into an generic allocator interface.
Definition memory.h:99
void * allocator_ptr
Opaque type-erased pointer to an allocator. Carries no value other than marking an argument to the fu...
Definition memory.h:15
static void * alloc_stack_alloc__alloc(allocator_ptr allocator, sz new_size)
Definition memory.h:109
void *() mem_alloc_f(allocator_ptr allocator, sz size)
Allocate size bytes using the allocator
Definition memory.h:18
static allocator_t allocator_new_stack_alloc()
Definition memory.h:113
A generic allocator type passed by value. Contains a fallback allocator and a set of function pointer...
Definition memory.h:30
static void * allocator_alloc_copy(allocator_t allocator, const void *data, sz size)
Copy size bytes of data from data into a freshly allocated slice using the allocator.
Definition memory.h:52
static void * allocator_alloc(allocator_t allocator, sz size)
Proxy the allocation to the actual allocator.
Definition memory.h:40
static void * allocator_realloc(allocator_t allocator, void *memory, sz size)
Move memory to a new memory region with at least size bytes. If an allocator does not implement reall...
Definition memory.h:68
mem_alloc_f * alloc
Definition memory.h:33
mem_free_f * free
Definition memory.h:35
static void allocator_free(allocator_t allocator, void *memory)
Release memory back to the allocator.
Definition memory.h:76
allocator_ptr allocator
A pointer to the actual underyling allocator, type-erased.
Definition memory.h:32
mem_realloc_f * realloc
Definition memory.h:34