include/context_vars/context_vars_ctx.h

filter: Statementstmt 98.0% 102 / 104 Branchbran 86.3% 19 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 96.0% 121 / 126 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 HV *map_stash; /* Context::Vars::Data::Map: a hash in a context */
57 HV *vec_stash; /* Context::Vars::Data::Vector: an array in one */
58 HV *hash_stash; /* Context::Vars::Hash: a hash proxy */
59 HV *array_stash; /* Context::Vars::Array: an array proxy */
60 AV *locals; /* what `local` displaced, newest last */
61 } my_cxt_t;
62  
63 START_MY_CXT
64  
65 /* ---- holders ------------------------------------------------------------ */
66  
67 /* A new holder that owns `map`, with a count of one. Takes over the struct,
68 * including when it fails: the caller never releases `map` itself. */
69 16181 static context_vars_ctx *context_vars_ctx_new(pTHX_ hamt_map *map) {
70 dMY_CXT;
71 context_vars_ctx *c;
72  
73 16181 ▶ if (!map) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!map) croak("Context::Vars: out of memory"); 0 16181 16181
74  
75 16181 ▶ if (MY_CXT.free_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.free_list) { 10620 5561 16181
76 10620 c = MY_CXT.free_list;
77 10620 MY_CXT.free_list = c->next;
78 10620 MY_CXT.free_n--;
79 }
80 else {
81 5561 Newx(c, 1, context_vars_ctx);
82 }
83 16181 c->map = map;
84 16181 c->refcnt = 1;
85 16181 c->next = NULL;
86 16181 MY_CXT.live++;
87 16181 return c;
88 }
89  
90 1355 static context_vars_ctx *context_vars_retain(pTHX_ context_vars_ctx *c) {
91 PERL_UNUSED_CONTEXT;
92 1355 c->refcnt++;
93 1355 return c;
94 }
95  
96 16199 static void context_vars_release(pTHX_ context_vars_ctx *c) {
97 dMY_CXT;
98 16199 ▶ if (--c->refcnt) return;
Branch: 1 branch
BranchTrueFalseTotal
if (--c->refcnt) return; 82 16117 16199
99  
100 16117 (CONTEXT_VARS_HAMT->map_release)(aTHX_ c->map);
101 16117 c->map = NULL;
102 16117 MY_CXT.live--;
103 16117 ▶ if (MY_CXT.free_n < CONTEXT_VARS_FREE_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.free_n < CONTEXT_VARS_FREE_MAX) { 10907 5210 16117
104 10907 c->next = MY_CXT.free_list;
105 10907 MY_CXT.free_list = c;
106 10907 MY_CXT.free_n++;
107 }
108 else {
109 5210 Safefree(c);
110 }
111 }
112  
113 /* ---- the current context ------------------------------------------------ */
114  
115 /* Borrowed. Never NULL. */
116 48 static context_vars_ctx *context_vars_current(pTHX) {
117 dMY_CXT;
118 48 return MY_CXT.cur;
119 }
120  
121 4 static int context_vars_is_empty(pTHX_ const context_vars_ctx *c) {
122 dMY_CXT;
123 4 return c == MY_CXT.empty;
124 }
125  
126 /* What the current context holds, kept for later: a new holder onto the same
127 * nodes, owned by the caller. With nothing entered it is the empty context
128 * itself with its count raised, and nothing is allocated. */
129 5271 static context_vars_ctx *context_vars_capture(pTHX) {
130 dMY_CXT;
131 5271 ▶ if (MY_CXT.cur == MY_CXT.empty)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.cur == MY_CXT.empty) 7 5264 5271
132 7 return context_vars_retain(aTHX_ MY_CXT.empty);
133 5264 return context_vars_ctx_new(aTHX_
134 5264 (CONTEXT_VARS_HAMT->map_copy)(aTHX_ MY_CXT.cur->map));
135 }
136  
137 /* The save-stack destructor. `p` is the context to put back. */
138 10922 static void context_vars_restore(pTHX_ void *p) {
139 dMY_CXT;
140 10922 context_vars_ctx *leaving = MY_CXT.cur;
141 10922 MY_CXT.cur = (context_vars_ctx *)p;
142 10922 MY_CXT.depth--;
143 10922 context_vars_release(aTHX_ leaving);
144 10922 }
145  
146 /* Makes `c` itself current until the caller's LEAVE. The caller must be
147 * inside an ENTER of its own. A store while it is current changes `c`, and
148 * the next enter of `c` sees it. */
149 1341 static void context_vars_enter(pTHX_ context_vars_ctx *c) {
150 dMY_CXT;
151 1341 context_vars_retain(aTHX_ c);
152 1341 SAVEDESTRUCTOR_X(context_vars_restore, (void *)MY_CXT.cur);
153 1341 MY_CXT.cur = c;
154 1341 MY_CXT.depth++;
155 1341 }
156  
157 /* Makes a NEW holder current, onto the nodes `c` holds, until the caller's
158 * LEAVE. A store while it is current changes nothing anybody else can see.
159 *
160 * A copy of the EMPTY context is the empty context itself, entered: nothing
161 * is allocated, which is what lets a loop capture and enter for every
162 * callback at almost no cost when no variable is ever assigned. The first
163 * store there gives the frame a holder of its own (context_vars_writable),
164 * so the empty context is still never written. */
165 10868 static void context_vars_enter_copy(pTHX_ const context_vars_ctx *c) {
166 dMY_CXT;
167 context_vars_ctx *fresh;
168 10868 ▶ if (c == MY_CXT.empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == MY_CXT.empty) { 1287 9581 10868
169 1287 context_vars_enter(aTHX_ MY_CXT.empty);
170 1287 return;
171 }
172 9581 fresh = context_vars_ctx_new(aTHX_
173 9581 (CONTEXT_VARS_HAMT->map_copy)(aTHX_ c->map));
174 9581 SAVEDESTRUCTOR_X(context_vars_restore, (void *)MY_CXT.cur);
175 9581 MY_CXT.cur = fresh;
176 9581 MY_CXT.depth++;
177 }
178  
179 /* A fresh context onto what is current now, until the caller's LEAVE. */
180 5616 static void context_vars_scope(pTHX) {
181 dMY_CXT;
182 5616 MY_CXT.scopes++;
183 5616 context_vars_enter_copy(aTHX_ MY_CXT.cur);
184 5616 }
185  
186 /* ---- variables ---------------------------------------------------------- */
187  
188 /* The stored scalar, borrowed and read-only, or NULL when the name is not
189 * set. It is valid until the current context is next stored into or left. */
190 19600 static SV *context_vars_fetch(pTHX_ const char *name, STRLEN len) {
191 dMY_CXT;
192 19600 return (CONTEXT_VARS_HAMT->map_fetch)(aTHX_ MY_CXT.cur->map, name, len, 0);
193 }
194  
195 /* Makes sure the current context is one that may be stored into, which the
196 * empty context never is.
197 *
198 * With nothing entered, the empty context is replaced as the current one,
199 * for the rest of the interpreter's life, by the PROGRAM CONTEXT: a holder
200 * that code outside any scope shares. It is made the first time such code
201 * assigns a variable, and a program that never does that never has one.
202 *
203 * With something entered and the empty context current - a capture taken
204 * with nothing set was entered - the entered frame gets a fresh holder of
205 * its own in the empty one's place, and its restore lets go of that. */
206 11505 static void context_vars_writable(pTHX) {
207 dMY_CXT;
208 context_vars_ctx *fresh;
209  
210 11505 ▶ if (MY_CXT.cur != MY_CXT.empty) return;
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.cur != MY_CXT.empty) return; 10211 1294 11505
211  
212 1294 fresh = context_vars_ctx_new(aTHX_
213 1294 (CONTEXT_VARS_HAMT->map_copy)(aTHX_ MY_CXT.empty->map));
214 1294 ▶ if (MY_CXT.depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.depth == 0) { 21 1273 1294
215 21 MY_CXT.program = fresh;
216 /* Not a holder a test counts: it is never given back. */
217 21 MY_CXT.live--;
218 }
219 else {
220 /* The enter that made the empty context current took a reference
221 * to it, which the restore would have given back. */
222 1273 MY_CXT.empty->refcnt--;
223 }
224 1294 MY_CXT.cur = fresh;
225 }
226  
227 11511 static void context_vars_refuse_empty(pTHX_ const char *what) {
228 dMY_CXT;
229 11511 ▶ if (MY_CXT.cur == MY_CXT.empty)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.cur == MY_CXT.empty) 2 11509 11511
230 2 croak("Context::Vars: internal error: %s with no context entered", what);
231 11509 }
232  
233 /* Stores a copy of `value`. The caller's scalar is not kept. */
234 11480 static void context_vars_store(pTHX_ const char *name, STRLEN len, SV *value) {
235 dMY_CXT;
236 11480 context_vars_refuse_empty(aTHX_ "a store");
237 11479 ▶ 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 11479 11479
238 != HAMT_ABI_OK)
239 0 croak("Context::Vars: out of memory");
240 11479 }
241  
242 /* Removing a name that is not set is not an error. */
243 31 static void context_vars_remove(pTHX_ const char *name, STRLEN len) {
244 dMY_CXT;
245 31 context_vars_refuse_empty(aTHX_ "a remove");
246 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
247 != HAMT_ABI_OK)
248 0 croak("Context::Vars: out of memory");
249 30 }
250  
251 19 static size_t context_vars_count(pTHX) {
252 dMY_CXT;
253 19 return (CONTEXT_VARS_HAMT->map_count)(aTHX_ MY_CXT.cur->map);
254 }
255  
256 /* ---- one interpreter's state --------------------------------------------- */
257  
258 /* Called with MY_CXT already made, at BOOT and in a new thread. A new thread
259 * starts with its own empty context and nothing else: a holder's nodes own
260 * scalars of the interpreter that made them. */
261 42 static void context_vars_ctx_init(pTHX) {
262 dMY_CXT;
263 42 MY_CXT.free_list = NULL;
264 42 MY_CXT.free_n = 0;
265 42 MY_CXT.live = 0;
266 42 MY_CXT.depth = 0;
267 42 MY_CXT.scopes = 0;
268 42 MY_CXT.empty = NULL;
269 42 MY_CXT.cur = NULL;
270 42 MY_CXT.program = NULL;
271 42 MY_CXT.map_stash = gv_stashpvs("Context::Vars::Data::Map", GV_ADD);
272 42 MY_CXT.vec_stash = gv_stashpvs("Context::Vars::Data::Vector", GV_ADD);
273 42 MY_CXT.hash_stash = gv_stashpvs("Context::Vars::Hash", GV_ADD);
274 42 MY_CXT.array_stash = gv_stashpvs("Context::Vars::Array", GV_ADD);
275 42 MY_CXT.locals = newAV();
276 42 MY_CXT.empty = context_vars_ctx_new(aTHX_ (CONTEXT_VARS_HAMT->map_new)(aTHX));
277 42 MY_CXT.cur = MY_CXT.empty;
278 /* The empty context is not one of the holders a test counts. */
279 42 MY_CXT.live = 0;
280 42 }
281  
282 #endif
« include/context_vars/context_vars_var.h ↑ Summary blib/lib/Context/Vars.pm »