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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
// gpa.c --- General Purpose Allocator -*- C -*-
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
// This file is part of COSMOS Codebase.
//
// Commentary:
//
// Code:
#include "gpa.h"
#include "dbg.h"
struct gpa
gpa_from (gpa_fn fun, void *ctx)
{
DBG_ASSERT_NN (fun);
return (struct gpa) {
._fun = fun,
._ctx = ctx,
};
}
void *
_gpa (struct gpa gpa, void *ptr, usz old_cnt, usz new_cnt, usz esz, usz spec)
{
usz old_sz, new_sz;
if (usz_mul_overflow (old_cnt, esz, &old_sz)) {
#ifndef GPA_NO_FEAT_UNW_INVAL
if (~spec & GPA_NO_UNW)
UNW (INVAL, 1, "old_cnt*esz overflowed!");
#endif // GPA_NO_FEAT_UNW_INVAL
return NULL;
}
if (usz_mul_overflow (new_cnt, esz, &new_sz)) {
#ifndef GPA_NO_FEAT_UNW_INVAL
if (~spec & GPA_NO_UNW)
UNW (INVAL, 2, "new_cnt*esz overflowed!");
#endif // GPA_NO_FEAT_UNW_INVAL
return NULL;
}
if (!gpa._fun) {
#ifndef GPA_NO_FEAT_CTX
if (_gpa_ctx_top) {
gpa = _gpa_ctx_top->gpa;
// DBG_BACKTRACE ("warning", "defaulted to gpa_ctx!");
} else
#endif // GPA_NO_FEAT_CTX
#ifndef GPA_NO_FEAT_SYS
if (1) {
gpa = GPA_SYS;
// DBG_BACKTRACE ("warning", "defaulted to gpa_sys!");
} else
#endif // GPA_NO_FEAT_SYS
#ifndef GPA_NO_FEAT_UNW_INVAL
if (~spec & GPA_NO_UNW) {
UNW (INVAL, 3, "no gpa to default to!");
} else
#endif // GPA_NO_FEAT_UNW_INVAL
return NULL;
}
void *new_ptr = gpa._fun (gpa._ctx, ptr, old_sz, new_sz, spec);
#ifndef GPA_NO_FEAT_UNW_OOM
if ((spec & GPA_NO_UNW) == 0 && new_sz && !new_ptr)
UNW (OOM, 3, "Allocation request couldn't be fulfilled!");
#endif // GPA_NO_FEAT_UNW_OOM
return new_ptr;
}
#ifndef GPA_NO_FEAT_UNW
UNW_DEFINE_REASON (OOM, "Out Of Memory");
#endif // GPA_NO_FEAT_UNW
#ifndef GPA_NO_FEAT_SYS
#include <string.h> // TODO(cmmm): remove dep <string.h> (memset)
#ifndef GPA_NO_FEAT_DBG
void *__real_calloc (usz cnt, usz esz);
void *__real_realloc (void *ptr, usz sz);
void __real_free (void *ptr);
#else // GPA_NO_FEAT_DBG
#include <stdlib.h>
#define __real_calloc calloc
#define __real_realloc realloc
#define __real_free free
#endif // GPA_NO_FEAT_DBG
// TODO(cmmm): support alignment (unw/return null if can't be met)
void *
gpa_fun_sys (void *ctx, void *ptr, usz old_sz, usz new_sz, usz spec)
{
UNUSED (ctx);
if (new_sz == 0) {
__real_free (ptr);
return NULL;
}
if (!ptr && (spec & GPA_ZI))
return __real_calloc (new_sz, 1);
void *ret = __real_realloc (ptr, new_sz);
if (new_sz > old_sz && (spec & GPA_ZI))
memset (ret + old_sz, 0, new_sz - old_sz);
return ret;
}
#endif // GPA_NO_FEAT_SYS
#ifndef GPA_NO_FEAT_DBG
static void *
dbg_gpa_fun (void *_ctx, void *ptr, usz old_sz, usz new_sz, usz spec)
{
struct dbg_gpa_ctx *ctx = _ctx;
// TODO(cmmm): save backtrace
// struct dbg_gpa_call *call = DA_GRAB (&ctx->calls, 1);
// *call = (struct dbg_gpa_call) {
// .old_ptr = ptr,
// .new_ptr = ctx->orig._fun (ctx->orig._ctx, ptr, old_sz, new_sz, spec),
// .old_sz = old_sz,
// .new_sz = new_sz,
// .spec = spec,
// };
// return call->new_ptr;
return NULL;
}
struct dbg_gpa_ctx *dbg_gpa_ctx_top = NULL;
struct gpa
_dbg_gpa (const char *label, struct gpa orig, struct dbg_loc loc)
{
struct dbg_gpa_ctx *ctx = GPA_GRAB_T (GPA_SYS, *ctx);
*ctx = (struct dbg_gpa_ctx) {
.next = dbg_gpa_ctx_top,
.loc = loc, // TODO(cmmm): save backtrace instead of loc
.label = label,
.orig = orig,
};
dbg_gpa_ctx_top = ctx;
return (struct gpa) {
._ctx = ctx,
._fun = dbg_gpa_fun,
};
}
static struct gpa bare;
BEFORE_MAIN static void
dbg_gpa_bare_init (void)
{
bare = DBG_GPA ("bare", GPA_SYS);
// atexit (gpa_dbg_report_all);
}
// TODO(cmmm): split this of into a sub feature: DBG_BARE
// TODO(cmmm): redo this with __real_*, just add logging and stuff...
// HOLD(cmmm): wrap mmap and munmap
// void *
// __wrap_malloc (usz sz)
// {
// return GPA_GRAB_X (bare, 1, sz, ALIGN_OF (void *) - 1 | GPA_NO_UNW);
// }
//
// void *
// __wrap_calloc (usz sz)
// {
// return GPA_GRAB_0X (bare, 1, sz, ALIGN_OF (void *) - 1 | GPA_NO_UNW);
// }
//
// void *
// __wrap_realloc (void *ptr, usz sz)
// {
// return GPA_RESZ_X (bare, ptr, 0, 1, sz, ALIGN_OF (void *) - 1 | GPA_NO_UNW);
// }
//
// void *
// __wrap_reallocarray (void *ptr, usz cnt, usz esz)
// {
// return GPA_RESZ_X (bare, ptr, 0, cnt, esz, ALIGN_OF (void *) - 1 | GPA_NO_UNW);
// }
//
// void
// __wrap_free (void *ptr)
// {
// GPA_DROP (bare, ptr, 0, GPA_NO_UNW);
// }
//
// void *
// __wrap_aligned_alloc (usz align, usz sz)
// {
// DBG_WARNING ("TODO(cmmm): make sure it works");
// return GPA_GRAB (bare, 1, sz, boundary - 1 | GPA_NO_UNW);
// }
//
// void *
// __wrap_memalign (usz boundary, usz sz)
// {
// DBG_WARNING ("TODO(cmmm): make sure it works");
// return GPA_GRAB (bare, 1, sz, boundary - 1 | GPA_NO_UNW);
// }
//
// int
// __wrap_posix_memalign (void **memptr, usz align, usz sz)
// {
// DBG_WARNING ("TODO(cmmm): make sure it works");
// void *ptr = GPA_GRAB (bare, 1, sz, boundary - 1 | GPA_NO_UNW);
// if (ptr)
// *memptr = ptr;
// }
//
// void *
// __wrap_valloc (usz sz)
// {
// DBG_WARNING ("TODO(cmmm): make sure it works");
// return GPA_GRAB (bare, 1, sz, getpagesize () - 1 | GPA_NO_UNW);
// }
#endif // GPA_NO_FEAT_DBG
#ifndef GPA_NO_FEAT_CTX
#include "arena.h"
// TODO(cmmm): this is exactly the same as lgr's ctx, extract it into ctx.h
THREAD_LOCAL struct gpa_ctx_frame *_gpa_ctx_top = NULL;
struct gpa_ctx_frame *
gpa_ctx_beg (struct gpa gpa)
{
struct gpa_ctx_frame *new = ARENA_GRAB_T (&scr, *new);
new->gpa = gpa;
new->next = _gpa_ctx_top;
_gpa_ctx_top = new;
return new;
}
struct gpa
gpa_ctx_end (void)
{
struct gpa gpa = _gpa_ctx_top->gpa;
_gpa_ctx_top = _gpa_ctx_top->next;
return gpa;
}
void
gpa_ctx_rollback (struct gpa_ctx_frame *to)
{
while (_gpa_ctx_top != to) {
DBG_ASSERT_NN (_gpa_ctx_top);
_gpa_ctx_top = _gpa_ctx_top->next;
}
}
#endif // GPA_NO_FEAT_CTX
// gpa.c ends here
|