aboutsummaryrefslogtreecommitdiff
path: root/include/gpa.h
blob: 13642aa49c25e3f93f90d0761e1fe31f3a0d8af0 (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
200
201
202
203
204
205
206
207
208
209
210
// gpa.h --- General Purpose Allocator                                 -*- C -*-
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
// This file is part of COSMOS Codebase.
//
// Commentary:
//
//   To disable specific features, define:
//    - GPA_NO_FEAT_CTX
//    - GPA_NO_FEAT_SYS
//    - GPA_NO_FEAT_DBG       (auto-disabled with GPA_NO_FEAT_SYS or NDEBUG)
//    - GPA_NO_FEAT_UNW
//    - GPA_NO_FEAT_UNW_OOM   (auto-disabled with GPA_NO_FEAT_UNW)
//    - GPA_NO_FEAT_UNW_INVAL (auto-disabled with GPA_NO_FEAT_UNW)
//
//   Unwinding is handled centrally in `_gpa`, don't do unwinding in
//   actual Gaps.
//
//   TODO(cmmm): add tracing support
//   TODO(cmmm): save used gpa into gpa's ctx if there was none
//
// Code:

#ifndef INCLUDED_COSMOS_GPA_H
#define INCLUDED_COSMOS_GPA_H

#include "types.h"
#include "macros.h"

// TEMP(cmmm): actually implement FEAT_DBG
#undef  GPA_NO_FEAT_DBG
#define GPA_NO_FEAT_DBG

#if defined (NDEBUG) || defined (GPA_NO_FEAT_SYS)
#undef  GPA_NO_FEAT_DBG
#define GPA_NO_FEAT_DBG
#endif // defined (NDEBUG) || defined (GPA_NO_FEAT_SYS)

#ifdef GPA_NO_FEAT_UNW
#undef  GPA_NO_FEAT_UNW_OOM
#define GPA_NO_FEAT_UNW_OOM
#undef  GPA_NO_FEAT_UNW_INVAL
#define GPA_NO_FEAT_UNW_INVAL
#endif // GPA_NO_FEAT_UNW

typedef void *(*gpa_fn) (void *ctx, void *ptr, usz old_sz, usz new_sz, usz spec);

struct gpa {
  gpa_fn _fun;
  void  *_ctx;
};

#define GPA_NULL ((struct gpa) { ._fun = NULL, ._ctx = NULL })

struct gpa gpa_from (gpa_fn fun, void *ctx);

#define GPA_GRAB(    gpa,                   cnt, esz     )                 _gpa (gpa, NULL,       0,     cnt,           esz,   0                                     )
#define GPA_GRAB_X(  gpa,                   cnt, esz, ext)                 _gpa (gpa, NULL,       0,     cnt,           esz,                                    (ext))
#define GPA_GRAB_0(  gpa,                   cnt, esz     )                 _gpa (gpa, NULL,       0,     cnt,           esz,                           GPA_ZI        )
#define GPA_GRAB_0X( gpa,                   cnt, esz, ext)                 _gpa (gpa, NULL,       0,     cnt,           esz,                           GPA_ZI | (ext))
#define GPA_GRAB_T(  gpa,                          T     ) ((typeof  (T)*) _gpa (gpa, NULL,       0,       1, sizeof (   T  ), ALIGN_OF (   T  ) - 1                 ))
#define GPA_GRAB_TX( gpa,                          T, ext) ((typeof  (T)*) _gpa (gpa, NULL,       0,       1, sizeof (   T  ), ALIGN_OF (   T  ) - 1          | (ext)))
#define GPA_GRAB_T0( gpa,                          T     ) ((typeof  (T)*) _gpa (gpa, NULL,       0,       1, sizeof (   T  ), ALIGN_OF (   T  ) - 1 | GPA_ZI        ))
#define GPA_GRAB_T0X(gpa,                          T, ext) ((typeof  (T)*) _gpa (gpa, NULL,       0,       1, sizeof (   T  ), ALIGN_OF (   T  ) - 1 | GPA_ZI | (ext)))
#define GPA_GRAB_N(  gpa,                   cnt,   T     ) ((typeof  (T)*) _gpa (gpa, NULL,       0,     cnt, sizeof (   T  ), ALIGN_OF (   T  ) - 1                 ))
#define GPA_GRAB_NX( gpa,                   cnt,   T, ext) ((typeof  (T)*) _gpa (gpa, NULL,       0,     cnt, sizeof (   T  ), ALIGN_OF (   T  ) - 1          | (ext)))
#define GPA_GRAB_N0( gpa,                   cnt,   T     ) ((typeof  (T)*) _gpa (gpa, NULL,       0,     cnt, sizeof (   T  ), ALIGN_OF (   T  ) - 1 | GPA_ZI        ))
#define GPA_GRAB_N0X(gpa,                   cnt,   T, ext) ((typeof  (T)*) _gpa (gpa, NULL,       0,     cnt, sizeof (   T  ), ALIGN_OF (   T  ) - 1 | GPA_ZI | (ext)))
#define GPA_RESZ(    gpa, ptr, old_cnt, new_cnt, esz     )                 _gpa (gpa,  ptr, old_cnt, new_cnt,           esz,   0                                     )
#define GPA_RESZ_X(  gpa, ptr, old_cnt, new_cnt, esz, ext)                 _gpa (gpa,  ptr, old_cnt, new_cnt,           esz,                                    (ext))
#define GPA_RESZ_0(  gpa, ptr, old_cnt, new_cnt, esz     )                 _gpa (gpa,  ptr, old_cnt, new_cnt,           esz,                           GPA_ZI        )
#define GPA_RESZ_0X( gpa, ptr, old_cnt, new_cnt, esz, ext)                 _gpa (gpa,  ptr, old_cnt, new_cnt,           esz,                           GPA_ZI | (ext))
#define GPA_RESZ_N(  gpa, ptr, old_cnt, new_cnt          ) ((typeof (ptr)) _gpa (gpa,  ptr, old_cnt, new_cnt, sizeof (*(ptr)), ALIGN_OF (*(ptr)) - 1                 ))
#define GPA_RESZ_NX( gpa, ptr, old_cnt, new_cnt,      ext) ((typeof (ptr)) _gpa (gpa,  ptr, old_cnt, new_cnt, sizeof (*(ptr)), ALIGN_OF (*(ptr)) - 1          | (ext)))
#define GPA_RESZ_N0( gpa, ptr, old_cnt, new_cnt          ) ((typeof (ptr)) _gpa (gpa,  ptr, old_cnt, new_cnt, sizeof (*(ptr)), ALIGN_OF (*(ptr)) - 1 | GPA_ZI        ))
#define GPA_RESZ_N0X(gpa, ptr, old_cnt, new_cnt,      ext) ((typeof (ptr)) _gpa (gpa,  ptr, old_cnt, new_cnt, sizeof (*(ptr)), ALIGN_OF (*(ptr)) - 1 | GPA_ZI | (ext)))
#define GPA_DROP(    gpa, ptr,     cnt,          esz     ) ((void        ) _gpa (gpa,  ptr,     cnt,       0,           esz,   0                                     ))
#define GPA_DROP_X(  gpa, ptr,     cnt,          esz, ext) ((void        ) _gpa (gpa,  ptr,     cnt,       0,           esz,                                    (ext)))
#define GPA_DROP_T(  gpa, ptr                            ) ((void        ) _gpa (gpa,  ptr,       1,       0, sizeof (*(ptr)), 0                                     ))
#define GPA_DROP_TX( gpa, ptr,                        ext) ((void        ) _gpa (gpa,  ptr,       1,       0, sizeof (*(ptr)),                                  (ext)))
#define GPA_DROP_N(  gpa, ptr,     cnt                   ) ((void        ) _gpa (gpa,  ptr,     cnt,       0, sizeof (*(ptr)), 0                                     ))
#define GPA_DROP_NX( gpa, ptr,     cnt,               ext) ((void        ) _gpa (gpa,  ptr,     cnt,       0, sizeof (*(ptr)),                                  (ext)))

#define GPA(gpa, ptr, old_cnt, new_cnt, esz, spec)      \
  _gpa (gpa, ptr, old_cnt, new_cnt, esz, spec)

void *_gpa (struct gpa, void *ptr, usz old_cnt, usz new_cnt, usz esz, usz spec);

#define GPA_ZI ((usz) 1 << (sizeof (usz)*8 - 1))

#ifndef GPA_NO_FEAT_UNW
#include "unw.h"
UNW_DECLARE_REASON (OOM);
#define GPA_NO_UNW ((usz) 1 << (sizeof (usz)*8 - 2))
#else
#define GPA_NO_UNW 0
#endif // GPA_NO_FEAT_UNW

#define GPA_ALIGN_MASK ~(GPA_ZI | GPA_NO_UNW)

#ifndef GPA_NO_FEAT_SYS

#define GPA_SYS ((struct gpa) { ._fun = gpa_fun_sys, ._ctx = NULL })
void *gpa_fun_sys (void *ctx, void *ptr, usz old_sz, usz new_sz, usz spec);

#else // GPA_NO_FEAT_SYS

#define GPA_SYS GPA_NULL

#endif // GPA_NO_FEAT_SYS

#ifndef GPA_NO_FEAT_DBG

// TODO(cmmm): move to CMK_META section
#define DBG_GPA_WRAP_LIBC_LDFLAGS       \
  MV0 ("-Wl,--wrap=malloc"        ),    \
  MV0 ("-Wl,--wrap=calloc"        ),    \
  MV0 ("-Wl,--wrap=realloc"       ),    \
  MV0 ("-Wl,--wrap=reallocarray"  ),    \
  MV0 ("-Wl,--wrap=free"          ),    \
  MV0 ("-Wl,--wrap=aligned_alloc" ),    \
  MV0 ("-Wl,--wrap=memalign"      ),    \
  MV0 ("-Wl,--wrap=posix_memalign"),    \
  MV0 ("-Wl,--wrap=valloc"        )

struct dbg_gpa_call {
  struct dbg_gpa_call *next_in_ctx;
  struct dbg_gpa_call *next_temporal;
  struct dbg_loc       loc; // TODO(cmmm): replace with backtrace field
  /*  */ void         *old_ptr;
  /*  */ void         *new_ptr;
  /*  */ usz           old_sz;
  /*  */ usz           new_sz;
  /*  */ usz           spec;
};

struct dbg_gpa_ctx {
  struct dbg_gpa_ctx  *next;
  struct dbg_gpa_call *calls;
  struct     gpa       orig;
  struct dbg_loc       loc; // TODO(cmmm): replace with backtrace field
  const      char     *label;
  /* */      usz       active_allocs;
  /* */      usz       active_bytes;
  /* */      usz       peak_bytes;
  /* */      usz       total_allocs;
};

extern struct dbg_gpa_ctx *dbg_gpa_ctx_top;

#define DBG_GPA(lbl, gpa) _dbg_gpa (lbl, gpa, DBG_LOC)
struct gpa _dbg_gpa (const char *label, struct gpa, struct dbg_loc);

#else // GPA_NO_FEAT_DBG

#define DBG_GPA(sym, gpa) gpa

#endif // GPA_NO_FEAT_DBG

#ifndef GPA_NO_FEAT_CTX

struct gpa_ctx_frame {
  struct gpa_ctx_frame *next;
  struct gpa            gpa;
};
extern THREAD_LOCAL struct gpa_ctx_frame *_gpa_ctx_top;

struct gpa_ctx_frame *gpa_ctx_beg (struct gpa);
struct gpa gpa_ctx_end (void);
void gpa_ctx_rollback (struct gpa_ctx_frame *);

#define CTX_GRAB(    ...) GPA_GRAB(    _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_X(  ...) GPA_GRAB_X(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_0(  ...) GPA_GRAB_0(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_0X( ...) GPA_GRAB_0X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_T(  ...) GPA_GRAB_T(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_TX( ...) GPA_GRAB_TX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_T0( ...) GPA_GRAB_T0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_T0X(...) GPA_GRAB_T0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_N(  ...) GPA_GRAB_N(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_NX( ...) GPA_GRAB_NX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_N0( ...) GPA_GRAB_N0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_GRAB_N0X(...) GPA_GRAB_N0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ(    ...) GPA_RESZ(    _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_X(  ...) GPA_RESZ_X(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_0(  ...) GPA_RESZ_0(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_0X( ...) GPA_RESZ_0X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_T(  ...) GPA_RESZ_T(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_TX( ...) GPA_RESZ_TX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_T0( ...) GPA_RESZ_T0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_T0X(...) GPA_RESZ_T0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_N(  ...) GPA_RESZ_N(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_NX( ...) GPA_RESZ_NX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_N0( ...) GPA_RESZ_N0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_RESZ_N0X(...) GPA_RESZ_N0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_DROP(    ...) GPA_DROP(    _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_DROP_X(  ...) GPA_DROP_X(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_DROP_T(  ...) GPA_DROP_T(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_DROP_TX( ...) GPA_DROP_TX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_DROP_N(  ...) GPA_DROP_N(  _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
#define CTX_DROP_NX( ...) GPA_DROP_NX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)

#endif // GPA_NO_FEAT_CTX

#endif // INCLUDED_COSMOS_GPA_H

// gpa.h ends here