Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* @title: Generic Arena APIs */
2#pragma once
3#include <mem/arena_provider.h>
4
5#define ARN_TYPE(n) struct arn_##n
6#define ARN_CAST(id, a) ((ARN_TYPE(id) *) (uintptr_t) (a))
7#define ARN_RAW(n) (struct arena *) n
8#define ARN_CAP_OP_BIT(op) (1ULL << (uint64_t) ARENA_OP_OFFSET(op))
9
10/* signatures:
11 *
12 * 5 member tuple format:
13 *
14 * 1. return type
15 * 2. attributes
16 * 3. return keyword (where relevant)
17 * 4. parameter list (excluding arena handle)
18 * 5. argument list forwarded to arena_<op>_full
19 *
20 */
21
22/* Alloc/free core */
23#define ARN_SIG_alloc \
24 void *, cc_warn_unused_result, return, \
25 (size_t size, struct alloc_params ap), (size, ap)
26#define ARN_SIG_realloc \
27 void *, cc_warn_unused_result, return, \
28 (void *ptr, size_t size, struct alloc_params ap), (ptr, size, ap)
29#define ARN_SIG_alloc_special \
30 void *, cc_warn_unused_result, return, \
31 (struct arena_params * p, struct alloc_params ap), (p, ap)
32#define ARN_SIG_alloc_aligned \
33 void *, cc_warn_unused_result, return, \
34 (size_t size, size_t align, struct alloc_params ap), (size, align, ap)
35#define ARN_SIG_free \
36 enum err, cc_warn_unused_result, return, \
37 (void *ptr, struct alloc_params ap), (ptr, ap)
38#define ARN_SIG_free_aligned \
39 enum err, cc_warn_unused_result, return, \
40 (void *ptr, size_t align, struct alloc_params ap), (ptr, align, ap)
41
42/* Lifecycle, cleanup */
43#define ARN_SIG_clear \
44 enum err, cc_warn_unused_result, return, (struct arena_params * p), (p)
45#define ARN_SIG_destroy void, , , (struct arena_params * p), (p)
46#define ARN_SIG_reset void, , , (struct arena_params * p), (p)
47
48/* Tag, budget, query */
49#define ARN_SIG_set_tag \
50 enum err, cc_warn_unused_result, return, (void *ptr, arena_tag_t tag), \
51 (ptr, tag)
52#define ARN_SIG_set_budget \
53 enum err, cc_warn_unused_result, return, (struct arena_budget b), (b)
54#define ARN_SIG_qry_size size_t, , return, (void *p), (p)
55#define ARN_SIG_qry_tag arena_tag_t, , return, (void *p), (p)
56#define ARN_SIG_qry_trace stack_handle_t, , return, (void *p), (p)
57#define ARN_SIG_qry_owns bool, , return, (const void *p), (p)
58
59/* Arena metadata */
60#define ARN_SIG_get_log_site \
61 struct log_site *, , return, (struct arena_params * p), (p)
62#define ARN_SIG_get_budget \
63 struct arena_budget, , return, (struct arena_params * p), (p)
64#define ARN_SIG_get_dumpster \
65 struct arena_dumpster *, , return, (struct arena_params * p), (p)
66#define ARN_SIG_get_identity \
67 arena_identity_t, , return, (struct arena_params * p), (p)
68#define ARN_SIG_identity_eq bool, , return, (struct arena * b), (b)
69
70/* Other misc. things */
71#define ARN_SIG_on_hook \
72 enum err, cc_warn_unused_result, return, (enum arena_hook_type hook), (hook)
73#define ARN_SIG_is_empty bool, , return, (), ()
74#define ARN_SIG_getref bool, , return, (), ()
75#define ARN_SIG_putref void, , , (), ()
76
77#define ARN_UNPAREN_(...) __VA_ARGS__
78#define ARN_APPLY_(m, ...) m(__VA_ARGS__)
79#define ARN_EACH_(m, c, list) ARN_EACH_I_(m, c, ARN_UNPAREN_ list)
80#define ARN_EACH_I_(m, c, ...) \
81 PP_DISPATCH(ARN_EACH, PP_NARG(__VA_ARGS__))(m, c, __VA_ARGS__)
82
83#define ARN_EACH_1(m, c, x) m(c, x)
84#define ARN_EACH_2(m, c, x, ...) m(c, x) ARN_EACH_1(m, c, __VA_ARGS__)
85#define ARN_EACH_3(m, c, x, ...) m(c, x) ARN_EACH_2(m, c, __VA_ARGS__)
86#define ARN_EACH_4(m, c, x, ...) m(c, x) ARN_EACH_3(m, c, __VA_ARGS__)
87#define ARN_EACH_5(m, c, x, ...) m(c, x) ARN_EACH_4(m, c, __VA_ARGS__)
88#define ARN_EACH_6(m, c, x, ...) m(c, x) ARN_EACH_5(m, c, __VA_ARGS__)
89#define ARN_EACH_7(m, c, x, ...) m(c, x) ARN_EACH_6(m, c, __VA_ARGS__)
90#define ARN_EACH_8(m, c, x, ...) m(c, x) ARN_EACH_7(m, c, __VA_ARGS__)
91#define ARN_EACH_9(m, c, x, ...) m(c, x) ARN_EACH_8(m, c, __VA_ARGS__)
92#define ARN_EACH_10(m, c, x, ...) m(c, x) ARN_EACH_9(m, c, __VA_ARGS__)
93#define ARN_EACH_11(m, c, x, ...) m(c, x) ARN_EACH_10(m, c, __VA_ARGS__)
94#define ARN_EACH_12(m, c, x, ...) m(c, x) ARN_EACH_11(m, c, __VA_ARGS__)
95#define ARN_EACH_13(m, c, x, ...) m(c, x) ARN_EACH_12(m, c, __VA_ARGS__)
96#define ARN_EACH_14(m, c, x, ...) m(c, x) ARN_EACH_13(m, c, __VA_ARGS__)
97#define ARN_EACH_15(m, c, x, ...) m(c, x) ARN_EACH_14(m, c, __VA_ARGS__)
98#define ARN_EACH_16(m, c, x, ...) m(c, x) ARN_EACH_15(m, c, __VA_ARGS__)
99#define ARN_EACH_17(m, c, x, ...) m(c, x) ARN_EACH_16(m, c, __VA_ARGS__)
100#define ARN_EACH_18(m, c, x, ...) m(c, x) ARN_EACH_17(m, c, __VA_ARGS__)
101#define ARN_EACH_19(m, c, x, ...) m(c, x) ARN_EACH_18(m, c, __VA_ARGS__)
102#define ARN_EACH_20(m, c, x, ...) m(c, x) ARN_EACH_19(m, c, __VA_ARGS__)
103#define ARN_EACH_21(m, c, x, ...) m(c, x) ARN_EACH_20(m, c, __VA_ARGS__)
104#define ARN_EACH_22(m, c, x, ...) m(c, x) ARN_EACH_21(m, c, __VA_ARGS__)
105#define ARN_EACH_23(m, c, x, ...) m(c, x) ARN_EACH_22(m, c, __VA_ARGS__)
106#define ARN_EACH_24(m, c, x, ...) m(c, x) ARN_EACH_23(m, c, __VA_ARGS__)
107#define ARN_EACH_25(m, c, x, ...) m(c, x) ARN_EACH_24(m, c, __VA_ARGS__)
108#define ARN_EACH_26(m, c, x, ...) m(c, x) ARN_EACH_25(m, c, __VA_ARGS__)
109#define ARN_EACH_27(m, c, x, ...) m(c, x) ARN_EACH_26(m, c, __VA_ARGS__)
110#define ARN_EACH_28(m, c, x, ...) m(c, x) ARN_EACH_27(m, c, __VA_ARGS__)
111#define ARN_EACH_29(m, c, x, ...) m(c, x) ARN_EACH_28(m, c, __VA_ARGS__)
112#define ARN_EACH_30(m, c, x, ...) m(c, x) ARN_EACH_29(m, c, __VA_ARGS__)
113#define ARN_EACH_31(m, c, x, ...) m(c, x) ARN_EACH_30(m, c, __VA_ARGS__)
114#define ARN_EACH_32(m, c, x, ...) m(c, x) ARN_EACH_31(m, c, __VA_ARGS__)
115
116#define ARN_FWD_I_(id, op, ret, attr, rk, params, args) \
117 static inline cc_always_inline attr ret arn_##id##_##op##_( \
118 ARN_TYPE(id) * h, ARN_UNPAREN_ params) { \
119 rk arena_##op##_full(ARN_RAW(h), ARN_UNPAREN_ args); \
120 }
121#define ARN_FWD_(id, op) ARN_APPLY_(ARN_FWD_I_, id, op, ARN_SIG_##op)
122
123#define ARN_MEMB_I_(id, op, ret, attr, rk, params, args) \
124 attr ret (*op)(ARN_TYPE(id) *, ARN_UNPAREN_ params);
125#define ARN_MEMB_(id, op) ARN_APPLY_(ARN_MEMB_I_, id, op, ARN_SIG_##op)
126#define ARN_INIT_(id, op) .op = arn_##id##_##op##_,
127
128#define ARN_CAPS_OF_(id, op) | ARN_CAP_OP_BIT(op)
129#define ARN_CAPS_DECLARE_(id, ops_) \
130 struct arn_##id; \
131 enum : uint64_t { \
132 arn_##id##_caps = 0 ARN_EACH_(ARN_CAPS_OF_, id, ops_), \
133 };
134
135#define ARN_NAMESPACE_(id, ops_, extra_fwd, extra_memb, extra_init) \
136 ARN_TYPE(id); \
137 ARN_EACH_(ARN_FWD_, id, ops_) \
138 ARN_UNPAREN_ extra_fwd static const struct arn_##id##_ns { \
139 ARN_UNPAREN_ extra_memb ARN_EACH_(ARN_MEMB_, id, ops_) \
140 } arn_##id \
141 cc_unused = {ARN_UNPAREN_ extra_init ARN_EACH_(ARN_INIT_, id, ops_)}
142
143#define ARN_PROVIDER_DECLARE(id, ops_) \
144 ARN_CAPS_DECLARE_(id, ops_); \
145 extern struct arena_desc arena_##id##_desc; \
146 ARN_NAMESPACE_(id, ops_, \
147 (static inline cc_always_inline ARN_TYPE(id) * \
148 arn_##id##_create_(struct arena_params * p) { \
149 return ARN_CAST( \
150 id, arena_create_via(&arena_##id##_desc, p)); \
151 }), \
152 (ARN_TYPE(id) * (*create)(struct arena_params * p);), \
153 (.create = arn_##id##_create_, ))
154
155#define ARN_IMPL_(id, op) .op = arena_##id##_##op,
156#define ARN_PROVIDER_DEFINE(id, ops_, strategy_, description_) \
157 static struct arena_ops arena_##id##_ops = { \
158 .create = arena_##id##_create, ARN_EACH_(ARN_IMPL_, id, ops_)}; \
159 struct arena_desc arena_##id##_desc = { \
160 .name = #id, \
161 .description = (description_), \
162 .strategy = (strategy_), \
163 .ops = &arena_##id##_ops, \
164 .promised_caps = arn_##id##_caps & ARENA_CAP_PROMISES, \
165 .static_caps = arn_##id##_caps, \
166 }
167
168/* Nice wrappers */
169#define arn_new(ns, h, T, ...) \
170 ((T *) (ns).alloc((h), sizeof(T), ARN_PARAMS(__VA_ARGS__)))
171
172#define arn_new_array(ns, h, T, n, ...) \
173 ({ \
174 size_t __an_bytes; \
175 __builtin_mul_overflow((size_t) (n), sizeof(T), &__an_bytes) \
176 ? (T *) NULL \
177 : (T *) (ns).alloc((h), __an_bytes, ARN_PARAMS(__VA_ARGS__)); \
178 })
179
180/* TODO: cc_cleanup style scoped arena allocations with
181 * mark and rewind on supported arena types (bump) */
182

Warning: That file was not part of the compilation database. It may have many parsing errors.