nish 0.37.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,6 +53,18 @@
53
53
  * allocates is given back when it ends. The driver enters and leaves around
54
54
  * each resume (`nish_async_enter`, `nish_async_leave`).
55
55
  *
56
+ * **Cancellation.** `nish_async_cancel` marks a task cancelled
57
+ * (`NISH_TASK_CANCELLED`, a bit beside the queued bit) and fails each wait it
58
+ * has pending, so every parked await answers false and the task is queued; a
59
+ * wait the task makes afterwards is made already failed. Nothing is unwound:
60
+ * the task runs on from each failed await to its end, its `using` bindings
61
+ * dispose as they do on any return, and `nish_async_done` frees its region.
62
+ * `nish_async_cancel_after` arms a deadline that cancels a task when it
63
+ * passes, a timer in the heap that `done` takes out, and
64
+ * `nish_async_wait_fd_within` a descriptor wait that fails when its own
65
+ * deadline passes first. A timer leaves the heap the moment its wait is
66
+ * answered either way (`heap_at`), so the heap holds only live timers.
67
+ *
56
68
  * The executor runs on the thread that runs `main`; its state is not shared
57
69
  * with the threads of `nish/threads`. */
58
70
 
@@ -96,9 +108,14 @@ typedef struct nish_task {
96
108
  uint32_t gen; /* moves on each time the node is reused */
97
109
  uint64_t region; /* the task's region; 0 for `main` and a free node */
98
110
  uint32_t index; /* the node's place in the task table, for ever */
99
- uint32_t queued; /* on the ready queue, so it is queued once */
111
+ uint32_t flags; /* NISH_TASK_QUEUED, NISH_TASK_CANCELLED */
100
112
  } nish_task;
101
113
 
114
+ /* On the ready queue, so it is queued once. */
115
+ #define NISH_TASK_QUEUED 1u
116
+ /* Cancelled: each wait it has, or makes, answers false at once. */
117
+ #define NISH_TASK_CANCELLED 2u
118
+
102
119
  _Static_assert(sizeof(nish_task) <= NISH_ASYNC_HEADER, "a node's header holds a task");
103
120
  _Static_assert(offsetof(nish_task, kind) == 8, "the driver reads the kind at byte 8");
104
121
 
@@ -119,6 +136,8 @@ typedef struct nish_wait {
119
136
  int32_t fd; /* the registered duplicate, or -1 for a timer */
120
137
  int64_t deadline; /* a timer's, in CLOCK_MONOTONIC nanoseconds */
121
138
  uint64_t seq; /* a timer's place among equal deadlines */
139
+ int32_t heap_at; /* its place in the timer heap, or -1 */
140
+ int32_t cancels; /* a task's deadline: firing cancels the task, waking nothing */
122
141
  } nish_wait;
123
142
 
124
143
  static nish_task nish_async_main_task = {NULL, -1, 1, 0, 0, 0};
@@ -145,6 +164,10 @@ static uint32_t nish_async_heap_len;
145
164
  static uint32_t nish_async_heap_cap;
146
165
  static uint64_t nish_async_seq;
147
166
 
167
+ /* Per task index, the slot + 1 of its armed deadline, or 0. */
168
+ static uint32_t *nish_async_deadlines;
169
+ static uint32_t nish_async_deadline_cap;
170
+
148
171
  static int nish_async_poller = -1;
149
172
  static uint32_t nish_async_fd_waits;
150
173
 
@@ -183,6 +206,11 @@ static void nish_async_table_add(nish_task *t) {
183
206
  }
184
207
  if (nish_async_task_count == nish_async_task_cap)
185
208
  nish_async_tasks = nish_async_grow(nish_async_tasks, &nish_async_task_cap, sizeof *nish_async_tasks);
209
+ while (nish_async_task_count >= nish_async_deadline_cap) {
210
+ uint32_t had = nish_async_deadline_cap;
211
+ nish_async_deadlines = nish_async_grow(nish_async_deadlines, &nish_async_deadline_cap, sizeof *nish_async_deadlines);
212
+ memset(nish_async_deadlines + had, 0, (size_t)(nish_async_deadline_cap - had) * sizeof *nish_async_deadlines);
213
+ }
186
214
  t->index = nish_async_task_count;
187
215
  nish_async_tasks[nish_async_task_count++] = t;
188
216
  }
@@ -194,8 +222,8 @@ static int64_t nish_async_now(void) {
194
222
  }
195
223
 
196
224
  static void nish_async_enqueue(nish_task *t) {
197
- if (t->queued) return;
198
- t->queued = 1;
225
+ if (t->flags & NISH_TASK_QUEUED) return;
226
+ t->flags |= NISH_TASK_QUEUED;
199
227
  t->next = NULL;
200
228
  if (nish_async_tail) nish_async_tail->next = t;
201
229
  else nish_async_head = t;
@@ -228,7 +256,7 @@ void *nish_async_spawn(int32_t kind, uint64_t bytes) {
228
256
  nish_task *t = (nish_task *)(chunk + i * slab->stride);
229
257
  t->gen = 1;
230
258
  t->region = 0;
231
- t->queued = 0;
259
+ t->flags = 0;
232
260
  nish_async_table_add(t);
233
261
  t->next = slab->free;
234
262
  slab->free = t;
@@ -239,7 +267,7 @@ void *nish_async_spawn(int32_t kind, uint64_t bytes) {
239
267
  slab->free = t->next;
240
268
  t->next = NULL;
241
269
  t->kind = kind;
242
- t->queued = 0;
270
+ t->flags = 0;
243
271
  t->region = nish_region_new();
244
272
  nish_async_live++;
245
273
  return t;
@@ -261,10 +289,30 @@ void nish_async_leave(void *node, void *prev) {
261
289
  nish_async_current = prev;
262
290
  }
263
291
 
264
- /* A task finished: free its region and put its node back, a generation on. */
292
+ static void nish_async_disarm(nish_task *t);
293
+
294
+ /* A task finished: take out its deadline, free its region and put its node
295
+ back, a generation on. */
265
296
  void nish_async_done(void *node) {
266
297
  nish_task *t = node;
267
298
  if (t->kind < 0) return;
299
+ nish_async_disarm(t);
300
+ /* A task that cancelled itself while awaitAll ran its calls was queued by
301
+ its own failed waits and may finish still on the ready queue: take it
302
+ off, so the queue never holds a node on the free list. */
303
+ if (t->flags & NISH_TASK_QUEUED) {
304
+ nish_task **at = &nish_async_head;
305
+ nish_task *before = NULL;
306
+ while (*at && *at != t) {
307
+ before = *at;
308
+ at = &(*at)->next;
309
+ }
310
+ if (*at) {
311
+ *at = t->next;
312
+ if (nish_async_tail == t) nish_async_tail = before;
313
+ }
314
+ t->flags &= ~NISH_TASK_QUEUED;
315
+ }
268
316
  nish_region_free(t->region);
269
317
  t->region = 0;
270
318
  nish_async_live--;
@@ -311,6 +359,8 @@ static uint32_t nish_async_wait_new(void) {
311
359
  w->fd = -1;
312
360
  w->deadline = 0;
313
361
  w->seq = 0;
362
+ w->heap_at = -1;
363
+ w->cancels = 0;
314
364
  return slot;
315
365
  }
316
366
 
@@ -328,11 +378,80 @@ static uint64_t nish_async_fired_now(uint32_t slot, int32_t answer) {
328
378
  return nish_async_token(slot);
329
379
  }
330
380
 
381
+ /* ---- The timer heap ---------------------------------------------------- */
382
+
383
+ static int nish_async_timer_before(uint32_t a, uint32_t b) {
384
+ nish_wait *x = &nish_async_waits[a];
385
+ nish_wait *y = &nish_async_waits[b];
386
+ return x->deadline < y->deadline || (x->deadline == y->deadline && x->seq < y->seq);
387
+ }
388
+
389
+ static void nish_async_heap_set(uint32_t at, uint32_t slot) {
390
+ nish_async_heap[at] = slot;
391
+ nish_async_waits[slot].heap_at = (int32_t)at;
392
+ }
393
+
394
+ static void nish_async_heap_up(uint32_t at) {
395
+ uint32_t slot = nish_async_heap[at];
396
+ while (at > 0) {
397
+ uint32_t up = (at - 1) / 2;
398
+ if (!nish_async_timer_before(slot, nish_async_heap[up])) break;
399
+ nish_async_heap_set(at, nish_async_heap[up]);
400
+ at = up;
401
+ }
402
+ nish_async_heap_set(at, slot);
403
+ }
404
+
405
+ static void nish_async_heap_down(uint32_t at) {
406
+ uint32_t slot = nish_async_heap[at];
407
+ for (;;) {
408
+ uint32_t kid = at * 2 + 1;
409
+ if (kid >= nish_async_heap_len) break;
410
+ if (kid + 1 < nish_async_heap_len && nish_async_timer_before(nish_async_heap[kid + 1], nish_async_heap[kid]))
411
+ kid++;
412
+ if (!nish_async_timer_before(nish_async_heap[kid], slot)) break;
413
+ nish_async_heap_set(at, nish_async_heap[kid]);
414
+ at = kid;
415
+ }
416
+ nish_async_heap_set(at, slot);
417
+ }
418
+
419
+ static void nish_async_heap_push(uint32_t slot) {
420
+ if (nish_async_heap_len == nish_async_heap_cap)
421
+ nish_async_heap = nish_async_grow(nish_async_heap, &nish_async_heap_cap, sizeof *nish_async_heap);
422
+ nish_async_heap_set(nish_async_heap_len++, slot);
423
+ nish_async_heap_up(nish_async_heap_len - 1);
424
+ }
425
+
426
+ /* Take the timer of `slot` out of the heap, wherever it is. */
427
+ static void nish_async_heap_remove(uint32_t slot) {
428
+ int32_t at = nish_async_waits[slot].heap_at;
429
+ if (at < 0) return;
430
+ nish_async_waits[slot].heap_at = -1;
431
+ uint32_t last = nish_async_heap[--nish_async_heap_len];
432
+ if ((uint32_t)at == nish_async_heap_len) return;
433
+ nish_async_heap_set((uint32_t)at, last);
434
+ nish_async_heap_up((uint32_t)at);
435
+ nish_async_heap_down((uint32_t)nish_async_waits[last].heap_at);
436
+ }
437
+
438
+ /* ---- Answering a wait ---------------------------------------------------- */
439
+
440
+ static nish_task *nish_async_task_at(uint32_t index, uint32_t gen) {
441
+ nish_task *t = index == 0 ? &nish_async_main_task
442
+ : index < nish_async_task_count ? nish_async_tasks[index] : NULL;
443
+ return t && t->gen == gen ? t : NULL;
444
+ }
445
+
446
+ static void nish_async_cancel_task(nish_task *t);
447
+
331
448
  /* The wait in `slot` completed with `answer`: queue its task, or drop it when
332
- the task it was made for has finished. */
449
+ the task it was made for has finished. A task's deadline cancels the task
450
+ instead. */
333
451
  static void nish_async_fire(uint32_t slot, int32_t answer) {
334
452
  nish_wait *w = &nish_async_waits[slot];
335
453
  if (w->state != NISH_WAIT_PENDING) return;
454
+ nish_async_heap_remove(slot);
336
455
  if (w->fd >= 0) {
337
456
  #if defined(__linux__)
338
457
  epoll_ctl(nish_async_poller, EPOLL_CTL_DEL, w->fd, NULL);
@@ -343,9 +462,14 @@ static void nish_async_fire(uint32_t slot, int32_t answer) {
343
462
  }
344
463
  w->state = NISH_WAIT_FIRED;
345
464
  w->answer = answer;
346
- nish_task *t = w->task == 0 ? &nish_async_main_task
347
- : w->task < nish_async_task_count ? nish_async_tasks[w->task] : NULL;
348
- if (!t || t->gen != w->task_gen) {
465
+ nish_task *t = nish_async_task_at(w->task, w->task_gen);
466
+ if (w->cancels) {
467
+ if (nish_async_deadlines[w->task] == slot + 1) nish_async_deadlines[w->task] = 0;
468
+ nish_async_wait_free_slot(slot);
469
+ if (t) nish_async_cancel_task(t);
470
+ return;
471
+ }
472
+ if (!t) {
349
473
  nish_async_late++;
350
474
  nish_async_wait_free_slot(slot);
351
475
  return;
@@ -353,46 +477,90 @@ static void nish_async_fire(uint32_t slot, int32_t answer) {
353
477
  nish_async_enqueue(t);
354
478
  }
355
479
 
356
- static int nish_async_timer_before(uint32_t a, uint32_t b) {
357
- nish_wait *x = &nish_async_waits[a];
358
- nish_wait *y = &nish_async_waits[b];
359
- return x->deadline < y->deadline || (x->deadline == y->deadline && x->seq < y->seq);
480
+ /* A timer came due: a sleep is over (true), a descriptor wait's deadline
481
+ passed first (false), a task's deadline cancels it. */
482
+ static void nish_async_expire(uint32_t slot) {
483
+ nish_async_fire(slot, nish_async_waits[slot].fd >= 0 ? 0 : 1);
360
484
  }
361
485
 
362
- static void nish_async_heap_push(uint32_t slot) {
363
- if (nish_async_heap_len == nish_async_heap_cap)
364
- nish_async_heap = nish_async_grow(nish_async_heap, &nish_async_heap_cap, sizeof *nish_async_heap);
365
- uint32_t at = nish_async_heap_len++;
366
- while (at > 0) {
367
- uint32_t up = (at - 1) / 2;
368
- if (!nish_async_timer_before(slot, nish_async_heap[up])) break;
369
- nish_async_heap[at] = nish_async_heap[up];
370
- at = up;
486
+ /* ---- Cancellation -------------------------------------------------------- */
487
+
488
+ /* Mark `t` cancelled and fail each wait it has pending. */
489
+ static void nish_async_cancel_task(nish_task *t) {
490
+ if (t->flags & NISH_TASK_CANCELLED) return;
491
+ t->flags |= NISH_TASK_CANCELLED;
492
+ nish_async_disarm(t);
493
+ for (uint32_t slot = 0; slot < nish_async_wait_count; slot++) {
494
+ nish_wait *w = &nish_async_waits[slot];
495
+ if (w->state == NISH_WAIT_PENDING && !w->cancels && w->task == t->index && w->task_gen == t->gen)
496
+ nish_async_fire(slot, 0);
371
497
  }
372
- nish_async_heap[at] = slot;
373
498
  }
374
499
 
375
- static uint32_t nish_async_heap_pop(void) {
376
- uint32_t top = nish_async_heap[0];
377
- uint32_t last = nish_async_heap[--nish_async_heap_len];
378
- uint32_t at = 0;
379
- for (;;) {
380
- uint32_t kid = at * 2 + 1;
381
- if (kid >= nish_async_heap_len) break;
382
- if (kid + 1 < nish_async_heap_len && nish_async_timer_before(nish_async_heap[kid + 1], nish_async_heap[kid]))
383
- kid++;
384
- if (!nish_async_timer_before(nish_async_heap[kid], last)) break;
385
- nish_async_heap[at] = nish_async_heap[kid];
386
- at = kid;
387
- }
388
- if (nish_async_heap_len) nish_async_heap[at] = last;
389
- return top;
500
+ /* Take out `t`'s armed deadline, if it has one. */
501
+ static void nish_async_disarm(nish_task *t) {
502
+ if (!nish_async_deadlines || t->index >= nish_async_deadline_cap) return;
503
+ uint32_t armed = nish_async_deadlines[t->index];
504
+ if (!armed) return;
505
+ nish_async_deadlines[t->index] = 0;
506
+ nish_async_heap_remove(armed - 1);
507
+ nish_async_wait_free_slot(armed - 1);
508
+ }
509
+
510
+ /* The task `handle` names (`nish_async_task_id`), or null for one that has
511
+ finished, `main`, or no task. */
512
+ static nish_task *nish_async_task_of(int64_t handle) {
513
+ uint32_t index = (uint32_t)((uint64_t)handle & 0xffffffffu);
514
+ if (index == 0) return NULL;
515
+ return nish_async_task_at(index, (uint32_t)((uint64_t)handle >> 32));
516
+ }
517
+
518
+ /* The running task's handle: its generation, then its index; 0 for `main`. */
519
+ int64_t nish_async_task_id(void) {
520
+ nish_task *t = nish_async_current;
521
+ if (t == &nish_async_main_task) return 0;
522
+ return (int64_t)(((uint64_t)t->gen << 32) | t->index);
523
+ }
524
+
525
+ /* Cancel the task `handle` names: true when it is still running (a second
526
+ cancel too), false for one that has finished, or `main`. */
527
+ int32_t nish_async_cancel(int64_t handle) {
528
+ nish_task *t = nish_async_task_of(handle);
529
+ if (!t) return 0;
530
+ nish_async_cancel_task(t);
531
+ return 1;
532
+ }
533
+
534
+ /* Whether the running task is cancelled. */
535
+ int32_t nish_async_cancelled(void) {
536
+ return (nish_async_current->flags & NISH_TASK_CANCELLED) != 0;
537
+ }
538
+
539
+ /* Cancel the task `handle` names `ms` milliseconds from now, in place of any
540
+ deadline it had; a negative `ms` only takes the old one out. False for a
541
+ task that has finished, or `main`. */
542
+ int32_t nish_async_cancel_after(int64_t handle, int32_t ms) {
543
+ nish_task *t = nish_async_task_of(handle);
544
+ if (!t) return 0;
545
+ nish_async_disarm(t);
546
+ if (ms < 0 || (t->flags & NISH_TASK_CANCELLED)) return 1;
547
+ nish_task *running = nish_async_current;
548
+ nish_async_current = t;
549
+ uint32_t slot = nish_async_wait_new();
550
+ nish_async_current = running;
551
+ nish_wait *w = &nish_async_waits[slot];
552
+ w->cancels = 1;
553
+ w->deadline = nish_async_now() + (int64_t)ms * 1000000;
554
+ w->seq = nish_async_seq++;
555
+ nish_async_heap_push(slot);
556
+ nish_async_deadlines[t->index] = slot + 1;
557
+ return 1;
390
558
  }
391
559
 
392
560
  /* A wait for `fd` to be readable (`dir` 0) or writable (`dir` 1). */
393
561
  uint64_t nish_async_wait_fd(int32_t fd, int32_t dir) {
394
562
  uint32_t slot = nish_async_wait_new();
395
- if (fd < 0) return nish_async_fired_now(slot, 0);
563
+ if (fd < 0 || (nish_async_current->flags & NISH_TASK_CANCELLED)) return nish_async_fired_now(slot, 0);
396
564
  if (nish_async_poller < 0) {
397
565
  #if defined(__linux__)
398
566
  nish_async_poller = epoll_create1(EPOLL_CLOEXEC);
@@ -438,10 +606,23 @@ uint64_t nish_async_wait_fd(int32_t fd, int32_t dir) {
438
606
  return token;
439
607
  }
440
608
 
441
- /* A wait for `ms` milliseconds; a negative `ms` answers false at once. */
609
+ /* A descriptor wait that fails (answers false) when `ms` milliseconds pass
610
+ before `fd` is ready; a negative `ms` waits as long as it takes. */
611
+ uint64_t nish_async_wait_fd_within(int32_t fd, int32_t dir, int32_t ms) {
612
+ uint64_t token = nish_async_wait_fd(fd, dir);
613
+ uint32_t slot = (uint32_t)(token & 0xffffffffu) - 1;
614
+ nish_wait *w = &nish_async_waits[slot];
615
+ if (ms < 0 || w->state != NISH_WAIT_PENDING) return token;
616
+ w->deadline = nish_async_now() + (int64_t)ms * 1000000;
617
+ w->seq = nish_async_seq++;
618
+ nish_async_heap_push(slot);
619
+ return token;
620
+ }
621
+
622
+ /* A wait for `ms` milliseconds; a negative `ms`, or a cancelled task, answers false at once. */
442
623
  uint64_t nish_async_sleep(int32_t ms) {
443
624
  uint32_t slot = nish_async_wait_new();
444
- if (ms < 0) return nish_async_fired_now(slot, 0);
625
+ if (ms < 0 || (nish_async_current->flags & NISH_TASK_CANCELLED)) return nish_async_fired_now(slot, 0);
445
626
  nish_async_waits[slot].deadline = nish_async_now() + (int64_t)ms * 1000000;
446
627
  nish_async_waits[slot].seq = nish_async_seq++;
447
628
  nish_async_heap_push(slot);
@@ -519,7 +700,7 @@ static void nish_async_block(void) {
519
700
  }
520
701
  int64_t now = nish_async_now();
521
702
  while (nish_async_heap_len && nish_async_waits[nish_async_heap[0]].deadline <= now)
522
- nish_async_fire(nish_async_heap_pop(), 1);
703
+ nish_async_expire(nish_async_heap[0]);
523
704
  }
524
705
 
525
706
  /* The next task to resume, waiting on the kernel for one when none is ready;
@@ -533,7 +714,7 @@ void *nish_async_next(void) {
533
714
  nish_async_head = t->next;
534
715
  if (!nish_async_head) nish_async_tail = NULL;
535
716
  t->next = NULL;
536
- t->queued = 0;
717
+ t->flags &= ~NISH_TASK_QUEUED;
537
718
  return t;
538
719
  }
539
720
 
@@ -546,11 +727,13 @@ void *nish_async_copy_str(const void *s) {
546
727
  }
547
728
 
548
729
  /* 0: nodes the slabs ever allocated; 1: completions dropped because their task
549
- had finished; 2: spawned tasks not yet finished, each of which holds a region. */
730
+ had finished; 2: spawned tasks not yet finished, each of which holds a region;
731
+ 3: timers in the heap (sleeps, bounded waits and armed deadlines). */
550
732
  int32_t nish_async_stat(int32_t which) {
551
733
  if (which == 0) return nish_async_allocated;
552
734
  if (which == 1) return nish_async_late;
553
735
  if (which == 2) return nish_async_live;
736
+ if (which == 3) return (int32_t)nish_async_heap_len;
554
737
  return -1;
555
738
  }
556
739
 
@@ -77,7 +77,8 @@ static void *nish_cross_block(uint64_t size) {
77
77
  * value. */
78
78
  static int nish_since(uint64_t mark, uintptr_t p) {
79
79
  for (const nish_xchunk *c = (const nish_xchunk *)nish_arena.chunks; c; c = c->next) {
80
- uintptr_t base = (uintptr_t)(c + 1), end = base + c->cap;
80
+ /* Bit 0 of `cap` marks a carved chunk (runtime.c's NISH_CHUNK_CARVED). */
81
+ uintptr_t base = (uintptr_t)(c + 1), end = base + (c->cap & ~(uint64_t)1);
81
82
  int marked = mark >= base && mark <= end;
82
83
  if (p >= (marked ? (uintptr_t)mark : base) && p < end) return 1;
83
84
  if (marked) return 0;
@@ -55,8 +55,15 @@
55
55
  *
56
56
  * Freeing a region frees its chunks through runtime.c's `nish_free_arena`,
57
57
  * with the region's arena swapped in for the call, so an `-DNISH_ASAN` build
58
- * forgets the freed blocks as it does for the thread's arena. */
58
+ * forgets the freed blocks as it does for the thread's arena.
59
+ *
60
+ * **Adopting** a region gives its chunks to the current arena instead of
61
+ * freeing them, then retires the handle as `free` does: `awaitAll` runs each
62
+ * call that waits in a region of its own, and adopts it when the call
63
+ * finishes, so the answer stays readable and is freed with the caller's
64
+ * memory (docs/security/async.md, ASYNC-14). */
59
65
 
66
+ #include <stddef.h>
60
67
  #include <stdint.h>
61
68
  #include <stdlib.h>
62
69
 
@@ -73,6 +80,20 @@ extern NISH_TLS uint64_t nish_region_chunk;
73
80
  #define NISH_REGION_GEN_END ((uint64_t)1 << 40)
74
81
  #define NISH_REGION_PAGE 1024
75
82
 
83
+ /* runtime.c's `nish_chunk`, which `nish_region_adopt` links through and
84
+ carves: the same three facts runtime.c asserts of it. Bit 0 of `cap` marks a
85
+ carved chunk, as runtime.c's NISH_CHUNK_CARVED. */
86
+ typedef struct nish_chunk_link { struct nish_chunk_link *next; uint64_t cap; } nish_chunk_link;
87
+ _Static_assert(sizeof(nish_chunk_link) == 16, "runtime.c's nish_chunk: { next, cap }, the bytes after it");
88
+ _Static_assert(offsetof(nish_chunk_link, next) == 0, "runtime.c's nish_chunk: next first");
89
+ _Static_assert(offsetof(nish_chunk_link, cap) == 8, "runtime.c's nish_chunk: cap second");
90
+ #define NISH_CHUNK_CARVED 1
91
+
92
+ /* The least a caller's chunk must have left for `nish_region_adopt` to carve
93
+ it into a chunk of its own: below it the bytes are given up, so an adopt
94
+ leaves at most this much behind. */
95
+ #define NISH_ADOPT_CARVE 256
96
+
76
97
  typedef struct nish_region_slot {
77
98
  struct nish_arena arena; /* the region's arena, or, while entered, the one it displaced */
78
99
  uint64_t gen; /* the live handle's generation; NISH_REGION_GEN_END once retired */
@@ -223,12 +244,10 @@ void nish_region_leave(uint64_t r, void *root) {
223
244
  s->root = root;
224
245
  }
225
246
 
226
- void nish_region_free(uint64_t r) {
227
- nish_region_slot *s = nish_region_slot_of(r);
228
- if (s->entered) nish_region_die(NISH_REGION_FREE_ENTERED);
229
- nish_region_swap(s);
230
- nish_free_arena();
231
- nish_region_swap(s);
247
+ /* Give the slot back: its generation moves on, so every copy of `r` is
248
+ stale, and the slot joins the free list unless that generation is the last.
249
+ The slot's arena is already empty. */
250
+ static void nish_region_retire(nish_region_slot *s, uint64_t r) {
232
251
  s->root = 0;
233
252
  uint64_t index = r & (NISH_REGION_SLOTS - 1);
234
253
  nish_region_lock();
@@ -241,3 +260,62 @@ void nish_region_free(uint64_t r) {
241
260
  }
242
261
  nish_region_unlock();
243
262
  }
263
+
264
+ void nish_region_free(uint64_t r) {
265
+ nish_region_slot *s = nish_region_slot_of(r);
266
+ if (s->entered) nish_region_die(NISH_REGION_FREE_ENTERED);
267
+ nish_region_swap(s);
268
+ nish_free_arena();
269
+ nish_region_swap(s);
270
+ nish_region_retire(s, r);
271
+ }
272
+
273
+ /* An `awaitAll` lane's region, once the lane has finished: its chunks go on
274
+ top of the arena in `nish_arena`, newest first as there, so what the lane
275
+ answered lives on in the arena that awaited it and is given back when that
276
+ arena next releases below them. What the caller's current chunk had left is
277
+ then carved into a chunk of its own, on top of the region's, and bumping
278
+ goes on there: the caller's chunk is not buried under the region's, which
279
+ would leave its rest unused and make the caller's next allocation grow a
280
+ fresh 64 KiB chunk -- a chunk per adopt in a loop that never releases
281
+ (docs/security/async.md, ASYNC-15). The carved chunk has no malloc of its
282
+ own, and bit 0 of its `cap` says so, so `nish_free_until` passes it by; it
283
+ always sits above the chunk it was carved from, so whatever frees that one
284
+ has dropped it first. Less than NISH_ADOPT_CARVE left is given up. The
285
+ next-chunk size (`nish_region_chunk`) is not touched: it is the caller's,
286
+ which a region only swaps while entered. A newest chunk the lane released
287
+ back to empty is freed, not moved, so a lane's temporaries do not outlive
288
+ it. A region that never allocated moves nothing. Like `free`, it panics on
289
+ a region that is entered. */
290
+ void nish_region_adopt(uint64_t r) {
291
+ nish_region_slot *s = nish_region_slot_of(r);
292
+ if (s->entered) nish_region_die(NISH_REGION_FREE_ENTERED);
293
+ nish_chunk_link *head = s->arena.chunks;
294
+ if (head && s->arena.off == 0) {
295
+ /* The lane rewound its newest chunk to empty: a release to the mark it
296
+ took on entry, 0 in an empty region, keeps that chunk for reuse
297
+ (`nish_reset_arena`), but nothing will bump it again once adopted. */
298
+ nish_chunk_link *next = head->next;
299
+ if (!(head->cap & NISH_CHUNK_CARVED)) free(head);
300
+ head = next;
301
+ uint64_t cap = head ? head->cap & ~(uint64_t)NISH_CHUNK_CARVED : 0;
302
+ s->arena = head ? (struct nish_arena){ (char *)(head + 1), cap, cap, (void *)head } : (struct nish_arena){ 0 };
303
+ }
304
+ if (head) {
305
+ nish_chunk_link *last = head;
306
+ while (last->next) last = last->next;
307
+ struct nish_arena caller = nish_arena;
308
+ uint64_t left = caller.buf ? caller.cap - caller.off : 0;
309
+ last->next = caller.chunks;
310
+ nish_arena = s->arena;
311
+ if (left >= NISH_ADOPT_CARVE) {
312
+ nish_chunk_link *c = (nish_chunk_link *)(caller.buf + caller.off);
313
+ uint64_t cap = left - sizeof *c;
314
+ c->next = nish_arena.chunks;
315
+ c->cap = cap | NISH_CHUNK_CARVED;
316
+ nish_arena = (struct nish_arena){ (char *)(c + 1), 0, cap, (void *)c };
317
+ }
318
+ }
319
+ s->arena = (struct nish_arena){ 0 };
320
+ nish_region_retire(s, r);
321
+ }