include/context_vars/context_vars_ctx.h

filter: Statementstmt 98.0% 98 / 100 Branchbran 86.3% 19 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.9% 117 / 122 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/context_vars/context_vars_var.h ↑ Summary blib/lib/Context/Vars.pm »
1 #ifndef CONTEXT_VARS_CTX_H
2 #define CONTEXT_VARS_CTX_H
3  
4 /* The context core: what a context is, which one is current, and how one is
5 * made current for a stretch of code and put back afterwards.
6 *
7 * A context is a HOLDER: one HAMT map struct that the holder owns, and a
8 * count. HAMT's map struct is a small thing onto a tree of shared nodes; a
9 * second struct onto the same nodes costs one small allocation, and the
10 * first store through either copies the few nodes it touches and leaves the
11 * other exactly as it was. That is the whole of why this is cheap: making a
12 * context for a sub call, or keeping one for a callback, is map_copy.
13 *
14 * Two holders never share a struct. They share nodes.
15 *
16 * ---- the current context ------------------------------------------------
17 *
18 * One pointer for each interpreter, in MY_CXT. It is changed in exactly two
19 * ways: something is entered, which puts the restore on the save stack, and
20 * that restore runs. Nothing sets it and walks away. A croak between the two
21 * runs the restore as a return does, and what goes back is the PREVIOUS
22 * context, not the empty one, so that nesting works.
23 *
24 * ---- the empty context --------------------------------------------------
25 *
26 * One holder for each interpreter, current when nothing has been entered. It
27 * is what every piece of code with no context of its own sees, all at once,
28 * so NOTHING IS EVER STORED INTO IT: a value there would be read by whatever
29 * ran next, which is the mistake this module exists to make impossible.
30 * context_vars_store and context_vars_remove croak when it is current. */
31  
32 struct context_vars_ctx {
33 hamt_map *map;
34 UV refcnt;
35 struct context_vars_ctx *next; /* on the freelist */
36 };
37 /* Also declared, as an opaque type, by the public context_vars_abi.h. */
38 #ifndef CONTEXT_VARS_CTX_FWD
39 #define CONTEXT_VARS_CTX_FWD
40 typedef struct context_vars_ctx context_vars_ctx;
41 #endif
42  
43 /* Holders kept for reuse, for each interpreter. A scoped sub call takes one
44 * and gives it back, so past the first few calls nothing is allocated. */
45 #define CONTEXT_VARS_FREE_MAX 64
46  
47 typedef struct {
48 context_vars_ctx *cur;
49 context_vars_ctx *empty;
50 context_vars_ctx *free_list;
51 IV free_n;
52 IV live; /* holders handed out and not given back */
53 IV depth; /* enters whose restore has not run */
54 IV scopes; /* scopes opened, for the tests */
55 context_vars_ctx *program; /* see context_vars_writable */
56 AV *locals; /* what `local` displaced, newest last */
57 } my_cxt_t;
58  
59 START_MY_CXT
60  
61 /* ---- holders ------------------------------------------------------------ */
62  
63 /* A new holder that owns `map`, with a count of one. Takes over the struct,
64 * including when it fails: the caller never releases `map` itself. */
65 12127 static context_vars_ctx *context_vars_ctx_new(pTHX_ hamt_map *map) {
66 dMY_CXT;
67 context_vars_ctx *c;
68  
69 12127 ▶ if (!map) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!map) croak("Context::Vars: out of memory"); 0 12127 12127
70  
71 12127 ▶ if (MY_CXT.free_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.free_list) { 6583 5544 12127
72 6583 c = MY_CXT.free_list;
73 6583 MY_CXT.free_list = c->next;
74 6583 MY_CXT.free_n--;
75 }
76 else {
77 5544 Newx(c, 1, context_vars_ctx);
78 }
79 12127 c->map = map;
80 12127 c->refcnt = 1;
81 12127 c->next = NULL;
82 12127 MY_CXT.live++;
83 12127 return c;
84 }
85  
86 1341 static context_vars_ctx *context_vars_retain(pTHX_ context_vars_ctx *c) {
87 PERL_UNUSED_CONTEXT;
88 1341 c->refcnt++;
89 1341 return c;
90 }
91  
92 12147 static void context_vars_release(pTHX_ context_vars_ctx *c) {
93 dMY_CXT;
94 12147 ▶ if (--c->refcnt) return;
Branch: 1 branch
BranchTrueFalseTotal
if (--c->refcnt) return; 75 12072 12147
95  
96 12072 (CONTEXT_VARS_HAMT->map_release)(aTHX_ c->map);
97 12072 c->map = NULL;
98 12072 MY_CXT.live--;
99 12072 ▶ if (MY_CXT.free_n < CONTEXT_VARS_FREE_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.free_n < CONTEXT_VARS_FREE_MAX) { 6862 5210 12072
100 6862 c->next = MY_CXT.free_list;
101 6862 MY_CXT.free_list = c;
102 6862 MY_CXT.free_n++;
103 }
104 else {
105 5210 Safefree(c);
106 }
107 }
108  
109 /* ---- the current context ------------------------------------------------ */
110  
111 /* Borrowed. Never NULL. */
112 48 static context_vars_ctx *context_vars_current(pTHX) {
113 dMY_CXT;
114 48 return MY_CXT.cur;
115 }
116  
117 4 static int context_vars_is_empty(pTHX_ const context_vars_ctx *c) {
118 dMY_CXT;
119 4 return c == MY_CXT.empty;
120 }
121  
122 /* What the current context holds, kept for later: a new holder onto the same
123 * nodes, owned by the caller. With nothing entered it is the empty context
124 * itself with its count raised, and nothing is allocated. */
125 5268 static context_vars_ctx *context_vars_capture(pTHX) {
126 dMY_CXT;
127 5268 ▶ if (MY_CXT.cur == MY_CXT.empty)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.cur == MY_CXT.empty) 7 5261 5268
128 7 return context_vars_retain(aTHX_ MY_CXT.empty);
129 5261 return context_vars_ctx_new(aTHX_
130 5261 (CONTEXT_VARS_HAMT->map_copy)(aTHX_ MY_CXT.cur->map));
131 }
132  
133 /* The save-stack destructor. `p` is the context to put back. */
134 6872 static void context_vars_restore(pTHX_ void *p) {
135 dMY_CXT;
136 6872 context_vars_ctx *leaving = MY_CXT.cur;
137 6872 MY_CXT.cur = (context_vars_ctx *)p;
138 6872 MY_CXT.depth--;
139 6872 context_vars_release(aTHX_ leaving);
140 6872 }
141  
142 /* Makes `c` itself current until the caller's LEAVE. The caller must be
143 * inside an ENTER of its own. A store while it is current changes `c`, and
144 * the next enter of `c` sees it. */
145 1327 static void context_vars_enter(pTHX_ context_vars_ctx *c) {
146 dMY_CXT;
147 1327 context_vars_retain(aTHX_ c);
148 1327 SAVEDESTRUCTOR_X(context_vars_restore, (void *)MY_CXT.cur);
149 1327 MY_CXT.cur = c;
150 1327 MY_CXT.depth++;
151 1327 }
152  
153 /* Makes a NEW holder current, onto the nodes `c` holds, until the caller's
154 * LEAVE. A store while it is current changes nothing anybody else can see.
155 *
156 * A copy of the EMPTY context is the empty context itself, entered: nothing
157 * is allocated, which is what lets a loop capture and enter for every
158 * callback at almost no cost when no variable is ever assigned. The first
159 * store there gives the frame a holder of its own (context_vars_writable),
160 * so the empty context is still never written. */
161 6820 static void context_vars_enter_copy(pTHX_ const context_vars_ctx *c) {
162 dMY_CXT;
163 context_vars_ctx *fresh;
164 6820 ▶ if (c == MY_CXT.empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == MY_CXT.empty) { 1275 5545 6820
165 1275 context_vars_enter(aTHX_ MY_CXT.empty);
166 1275 return;
167 }
168 5545 fresh = context_vars_ctx_new(aTHX_
169 5545 (CONTEXT_VARS_HAMT->map_copy)(aTHX_ c->map));
170 5545 SAVEDESTRUCTOR_X(context_vars_restore, (void *)MY_CXT.cur);
171 5545 MY_CXT.cur = fresh;
172 5545 MY_CXT.depth++;
173 }
174  
175 /* A fresh context onto what is current now, until the caller's LEAVE. */
176 1569 static void context_vars_scope(pTHX) {
177 dMY_CXT;
178 1569 MY_CXT.scopes++;
179 1569 context_vars_enter_copy(aTHX_ MY_CXT.cur);
180 1569 }
181  
182 /* ---- variables ---------------------------------------------------------- */
183  
184 /* The stored scalar, borrowed and read-only, or NULL when the name is not
185 * set. It is valid until the current context is next stored into or left. */
186 10953 static SV *context_vars_fetch(pTHX_ const char *name, STRLEN len) {
187 dMY_CXT;
188 10953 return (CONTEXT_VARS_HAMT->map_fetch)(aTHX_ MY_CXT.cur->map, name, len, 0);
189 }
190  
191 /* Makes sure the current context is one that may be stored into, which the
192 * empty context never is.
193 *
194 * With nothing entered, the empty context is replaced as the current one,
195 * for the rest of the interpreter's life, by the PROGRAM CONTEXT: a holder
196 * that code outside any scope shares. It is made the first time such code
197 * assigns a variable, and a program that never does that never has one.
198 *
199 * With something entered and the empty context current - a capture taken
200 * with nothing set was entered - the entered frame gets a fresh holder of
201 * its own in the empty one's place, and its restore lets go of that. */
202 7345 static void context_vars_writable(pTHX) {
203 dMY_CXT;
204 context_vars_ctx *fresh;
205  
206 7345 ▶ if (MY_CXT.cur != MY_CXT.empty) return;
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.cur != MY_CXT.empty) return; 6061 1284 7345
207  
208 1284 fresh = context_vars_ctx_new(aTHX_
209 1284 (CONTEXT_VARS_HAMT->map_copy)(aTHX_ MY_CXT.empty->map));
210 1284 ▶ if (MY_CXT.depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.depth == 0) { 18 1266 1284
211 18 MY_CXT.program = fresh;
212 /* Not a holder a test counts: it is never given back. */
213 18 MY_CXT.live--;
214 }
215 else {
216 /* The enter that made the empty context current took a reference
217 * to it, which the restore would have given back. */
218 1266 MY_CXT.empty->refcnt--;
219 }
220 1284 MY_CXT.cur = fresh;
221 }
222  
223 7351 static void context_vars_refuse_empty(pTHX_ const char *what) {
224 dMY_CXT;
225 7351 ▶ if (MY_CXT.cur == MY_CXT.empty)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.cur == MY_CXT.empty) 2 7349 7351
226 2 croak("Context::Vars: internal error: %s with no context entered", what);
227 7349 }
228  
229 /* Stores a copy of `value`. The caller's scalar is not kept. */
230 7320 static void context_vars_store(pTHX_ const char *name, STRLEN len, SV *value) {
231 dMY_CXT;
232 7320 context_vars_refuse_empty(aTHX_ "a store");
233 7319 ▶ if ((CONTEXT_VARS_HAMT->map_store)(aTHX_ MY_CXT.cur->map, name, len, 0, value)
Branch: 1 branch
BranchTrueFalseTotal
if ((CONTEXT_VARS_HAMT->map_store)(aTHX_ MY_CXT.cur->map, name, len, 0, value) 0 7319 7319
234 != HAMT_ABI_OK)
235 0 croak("Context::Vars: out of memory");
236 7319 }
237  
238 /* Removing a name that is not set is not an error. */
239 31 static void context_vars_remove(pTHX_ const char *name, STRLEN len) {
240 dMY_CXT;
241 31 context_vars_refuse_empty(aTHX_ "a remove");
242 30 ▶ if ((CONTEXT_VARS_HAMT->map_remove)(aTHX_ MY_CXT.cur->map, name, len, 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((CONTEXT_VARS_HAMT->map_remove)(aTHX_ MY_CXT.cur->map, name, len, 0) 0 30 30
243 != HAMT_ABI_OK)
244 0 croak("Context::Vars: out of memory");
245 30 }
246  
247 19 static size_t context_vars_count(pTHX) {
248 dMY_CXT;
249 19 return (CONTEXT_VARS_HAMT->map_count)(aTHX_ MY_CXT.cur->map);
250 }
251  
252 /* ---- one interpreter's state --------------------------------------------- */
253  
254 /* Called with MY_CXT already made, at BOOT and in a new thread. A new thread
255 * starts with its own empty context and nothing else: a holder's nodes own
256 * scalars of the interpreter that made them. */
257 37 static void context_vars_ctx_init(pTHX) {
258 dMY_CXT;
259 37 MY_CXT.free_list = NULL;
260 37 MY_CXT.free_n = 0;
261 37 MY_CXT.live = 0;
262 37 MY_CXT.depth = 0;
263 37 MY_CXT.scopes = 0;
264 37 MY_CXT.empty = NULL;
265 37 MY_CXT.cur = NULL;
266 37 MY_CXT.program = NULL;
267 37 MY_CXT.locals = newAV();
268 37 MY_CXT.empty = context_vars_ctx_new(aTHX_ (CONTEXT_VARS_HAMT->map_new)(aTHX));
269 37 MY_CXT.cur = MY_CXT.empty;
270 /* The empty context is not one of the holders a test counts. */
271 37 MY_CXT.live = 0;
272 37 }
273  
274 #endif
« include/context_vars/context_vars_var.h ↑ Summary blib/lib/Context/Vars.pm »