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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! Memory management for zshrs
//!
//! Port from zsh/Src/mem.c
//!
//! In Rust, we don't need the complex heap management that zsh uses in C.
//! Instead, we provide a simpler arena-style allocator abstraction that
//! can be used for temporary allocations that all get freed at once.
use RefCell;
// list of zsh heaps // c:127
/// A memory arena for temporary allocations.
///
/// Port of the `heaps` linked-list arena C zsh maintains in
/// Src/mem.c (see `new_heaps()` line 194 / `old_heaps()` line 220).
/// The C source uses a hand-rolled bump allocator with `pushheap`/
/// `popheap` semantics for shell-lifetime allocations; in Rust we
/// stack `Vec<String>`/`Vec<Vec<u8>>` per generation and let normal
/// drop semantics handle the actual frees.
///
/// `heap_arena` is the Rust port's wrapper around what C tracks via
/// the module-static `Heap heaps` chain + `HeapStack heapstack` —
/// there is no `struct heap_arena` in zsh C. Canonical C `struct heap`
/// (the chunk header) is at `zsh_h.rs:1039`.
thread_local!
/// Push heap state.
// save states of zsh heaps // c:291
/// Port of `pushheap()` from Src/mem.c:291 — the global entry-point
/// version that operates on the thread-local arena.
// reset heap to previous state and destroy state information // c:443
/// Pop heap state and free allocations.
/// Port of `popheap()` from Src/mem.c:443.
// reset heaps to previous state // c:325
/// Free current heap allocations but keep state.
/// Port of `freeheap()` from Src/mem.c:325.
/// Allocate memory.
// allocate permanent memory // c:959
/// Port of `zalloc(size_t size)` from Src/mem.c:959. In Rust we use `Box`
/// rather than `malloc(3)`; the type-default initialization stands
/// in for the C source's uninitialized buffer.
/// WARNING: param names don't match C — Rust=() vs C=(size)
// allocate memory from the current memory pool and clear it // c:942
/// Allocate zeroed memory.
/// Port of `zshcalloc(size_t size)` from Src/mem.c:977 — the C source pairs
/// `zalloc()` with `memset(0)`; Rust's `Box::default()` handles
/// both.
/// WARNING: param names don't match C — Rust=() vs C=(size)
/// Reallocate memory.
/// Port of `zrealloc(void *ptr, size_t size)` from Src/mem.c:994 — Vec::resize fills the
/// gap with `T::default()`, mirroring the C source's "old contents
/// preserved, new bytes uninitialized" semantics.
/// WARNING: param names don't match C — Rust=() vs C=(ptr, size)
/// Free memory.
/// Port of `zfree(void *p, int sz)` from Src/mem.c:1433 (or :1869 in the
/// MALLOC_DEBUG build). Takes a `Box<T>` rather than `T` so the
/// C-port call sites read the same as the original `zfree(ptr)`
// right size of this block, freeing it will be faster, though; the value // c:1433
// 0 for this parameter means: `don't know' // c:1433
/// (an explicit allocator release on a heap pointer). Drop happens
/// automatically when the Box goes out of scope.
/// WARNING: param names don't match C — Rust=(_ptr) vs C=(p, sz)
/// Free a string.
/// Port of `zsfree(char *p)` from Src/mem.c:1641 — the C source's
/// `free(NULL)`-tolerant string-specific deallocator. In Rust the
/// Drop impl on `String` handles the actual free.
/// Duplicate a string into heap storage.
/// Port of `dupstring(const char *s)` from Src/string.c:33 — the heap-arena
/// variant of `ztrdup()`. In Rust both collapse to `String::clone`
/// since `String` always owns its allocation.
/// Duplicate a string with explicit length.
/// Port of `dupstring_wlen(const char *s, unsigned len)` from Src/string.c:48 — used when the
/// source isn't NUL-terminated (e.g. a slice of a larger buffer).
/// Check if a pointer is within the heap arena.
/// Port of `zheapptr(void *p)` from Src/mem.c:561 — the C source uses it
/// to tell heap-arena strings from permanent ones (the pastebuf code
/// has different freeing rules). Rust's borrow-checker subsumes
/// this distinction; the function is kept for call-site parity but
/// always returns true.
/// Reallocate heap memory.
/// Port of `hrealloc(char *p, size_t old, size_t new)` from Src/mem.c:687 — heap-arena
/// counterpart of `zrealloc()` (Src/mem.c:687).
/// WARNING: param names don't match C — Rust=(old, new_size) vs C=(p, old, new)
/// Duplicate an array of strings.
/// Port of `zarrdup(char **s)` from Src/utils.c:4532.
/// Duplicate an array up to a maximum length.
/// zshrs-original convenience — closest C analog is the bounded
/// loops Src/utils.c uses around `zarrdup` when the max is known.
/// Get array length.
/// Port of `arrlen(char **s)` from Src/utils.c:2357 — the C source's
/// canonical NULL-terminated `char**` length walker. Rust slices
/// already know their length, so this collapses to `arr.len()`.
/// Check if array length is less than n.
/// Port of `arrlen_lt(char **s, unsigned upper_bound)` from Src/utils.c:2400 — short-circuit
/// version that stops walking once the bound is exceeded.
/// Check if array length is less than or equal to n.
/// Port of `arrlen_le(char **s, unsigned upper_bound)` from Src/utils.c:2391.
/// Check if array length is greater than n.
/// Port of `arrlen_gt(char **s, unsigned lower_bound)` from Src/utils.c:2382.
/// Concatenate strings with separator.
/// Port of `sepjoin(char **s, char *sep, int heap)` from Src/utils.c:3928 — C source's `IFS`-
/// driven array→string join. Default separator is space, matching
/// the C source's `sep ? sep : " "` fallback.
/// WARNING: param names don't match C — Rust=(arr, sep) vs C=(s, sep, heap)
/// Split string by separator.
/// Port of `sepsplit(char *s, char *sep, int allownull, int heap)` from Src/utils.c:3962 — the C source's
/// `IFS`-driven splitter. `allow_empty` mirrors the `allownull`
/// argument the C function takes.
/// WARNING: param names don't match C — Rust=(s, sep, allow_empty) vs C=(s, sep, allownull, heap)
/// Duplicate a string to permanent storage.
/// Port of `ztrdup(const char *s)` from Src/string.c:62 — C zsh's canonical
/// `strdup(3)` analog tied to the zsh allocator. In Rust both heap
/// and permanent storage are the same (`String` owns its buffer).
/// Duplicate the first `n` characters of a string.
/// Port of `ztrduppfx(const char *s, int len)` from Src/string.c:172 — same role as
/// `dupstring_wlen` (Src/string.c:145) but allocated as permanent
/// rather than heap-arena. Rust collapses both to `String::clone`.
/// Concatenate two strings into a new permanent string.
/// Port of `bicat(const char *s1, const char *s2)` from Src/string.c:145.
// This version always uses permanently-allocated space. // c:98
/// Concatenate three strings into a new permanent string.
/// Port of `tricat(char const *s1, char const *s2, char const *s3)` from Src/string.c:98 — used heavily by the
/// completion machinery for "prefix + match + suffix" assembly.
// concatenate s1 and s2 in dynamically allocated buffer // c:131
// This version always uses space from the current heap. // c:131
/// Concatenate two strings into a new heap-arena string.
/// Port of `dyncat(const char *s1, const char *s2)` from Src/string.c:131 — heap-arena variant
/// of `bicat()`.
/// Get the last character of a string.
/// Port of `strend(char *str)` from Src/string.c:196 — C source returns the
/// pointer to the NUL terminator's predecessor; Rust returns the
/// char.
// Append a string to an allocated string, reallocating to make room. // c:186
/// Append a string in-place.
/// Port of `appstr(char *base, char const *append)` from Src/string.c:186 — the C source uses
/// `strcat(3)` with realloc; Rust's `String::push_str` does both.
/// Port of `bin_mem(char *name, char **argv, Options ops, int func)` from `Src/mem.c:1722`.
/// C body (gated on `#ifdef ZSH_MEM_DEBUG`) reads zsh's custom
/// malloc counters (`m_l`, `m_high`, `m_s`, `m_b`, `m_m[]`, `m_f[]`)
/// and prints them. zshrs uses the system allocator, so those
/// counters don't exist and the body emits a "not available"
/// notice matching `#else` defaults.
///
/// C signature: `int bin_mem(char *name, char **argv, Options ops,
/// int func)`.
/// WARNING: param names don't match C — Rust=(_argv, _ops, _func) vs C=(name, argv, ops, func)
// The canonical `zcontext_save()` / `zcontext_restore()` port lives
// in `crate::ported::context` (Src/context.c:80/117), NOT here. The
// previous Rust port had a `MemContext` aggregate + zero-arg
// `zcontext_save() -> MemContext` shim attributed to "Src/init.c"
// which is not where the C versions live — invented Rust-only
// duplicate name. Deleted per PORT.md Rule A (no fns/structs whose
// name doesn't exist in upstream C source at the cited location).
// No external callers used the mem.rs versions.
// queue_signals / unqueue_signals / QUEUEING_ENABLED / run_queued_signals
// live in `signals_h.rs` — that's the canonical Rust home for the
// `Src/signals.h:90/92/112/114/116` macros. mem.rs callers that need
// the same state must go through signals_h so the counter is shared
// across the whole tree (the prior parallel copies here split the
// queueing state, which was wrong).
pub use crate;
// ===========================================================
// Direct ports of arena/heap routines from Src/mem.c. Rust
// uses owned allocations + RAII, so the C heap-arena machinery
// (zalloc, zhalloc, switch_heaps, mmap_heap_alloc, etc.) is
// replaced by stdlib alloc + scoped owned strings. These free-
// fn entries satisfy ABI/name parity for the drift gate.
// ===========================================================
/// Port of `new_heap_id()` from Src/mem.c:182.
/// C: `static Heapid new_heap_id(void)` → `return next_heap_id++;`
// `next_heap_id` from Src/mem.c:178 — monotonically incrementing counter
// for heap-arena identification under ZSH_MEM_DEBUG.
pub static NEXT_HEAP_ID: AtomicU64 =
new;
/// Port of `mod_export Heapid last_heap_id` from `Src/mem.c:194`.
/// Tracks the most recently created heap arena id — used by
/// `memory_validate` (ZSH_MEM_DEBUG path) to recognize cross-arena
/// pointer use. Without ZSH_MEM_DEBUG this is set but never read.
pub static LAST_HEAP_ID: AtomicU64 =
new; // c:153
// Use new heaps from now on. This returns the old heap-list. // c:194
/// Port of `new_heaps()` from Src/mem.c:194.
/// C: `Heap new_heaps(void)` — save current `heaps`/`fheap` chain,
/// reset both to NULL, return the saved head for later restoration.
// Re-install the old heaps again, freeing the new ones. // c:220
/// Port of `old_heaps(Heap old)` from Src/mem.c:220.
/// C: `void old_heaps(Heap old)` — free the current heaps chain (each
/// `h->next`), then restore `heaps = old`.
// Temporarily switch to other heaps (or back again). // c:267
/// Port of `switch_heaps(Heap new)` from Src/mem.c:267.
/// C: `Heap switch_heaps(Heap new)` — return current `heaps`, install
/// `new` in its place. Used to enter a different heap-arena scope.
// `heaps` / `fheap` from Src/mem.c:526 — head of the current arena
// chain and free-list pointer respectively.
pub static HEAPS: AtomicPtr =
new;
pub static FHEAP: AtomicPtr =
new;
/// Port of `mmap_heap_alloc(size_t *n)` from Src/mem.c:526.
/// C: `static Heap mmap_heap_alloc(size_t *n)` — round `*n` up to the
/// page size, mmap an anonymous region of that size, write back the
/// actual allocation in `*n`. Returns the Heap header.
// allocate memory from the current memory pool // c:577
/// Port of `zhalloc(size_t size)` from Src/mem.c:577 — heap-arena `malloc`
/// (memory freed at the end of the current heap frame). Shim;
/// Rust callers use owned `Vec`/`String`.
// c:577
/// Port of `memory_validate(Heapid heap_id)` from Src/mem.c:896.
/// C: `int memory_validate(Heapid heap_id)` — under `ZSH_MEM_DEBUG`,
/// walk the heap chain to verify `heap_id` is still alive. Returns
/// 0 if found (valid), 1 otherwise.
/// Port of `hcalloc(size_t size)` from Src/mem.c:946 — heap-arena `calloc`
/// (zero-fill `zhalloc`). Shim.
// c:946
/// Port of `malloc(size_t size)` from Src/mem.c:1189 — wrapped `malloc`
/// for the legacy arena system. Shim.
/// Port of `free(void *p)` from Src/mem.c:1631.
/// C: `void free(void *p)` → `zfree(p, 0);` — Rust callers use Drop
/// to free owned allocations; this shim documents the C name parity.
/// Port of `realloc(void *p, size_t size)` from Src/mem.c:1648 — wrapped `realloc`.
/// Shim.
/// WARNING: param names don't match C — Rust=(_size) vs C=(p, size)
/// Port of `calloc(size_t n, size_t size)` from Src/mem.c:1697 — wrapped `calloc`.
/// Shim.