balloc 0.0.0-stage → 1.0.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.
@@ -0,0 +1,1264 @@
1
+ You can also get this documentation translated into other languages:
2
+ | Language | Link |
3
+ |---|---|
4
+ | Arabic | [API — balloc.ar.md](./docs/API%20—%20balloc.ar.md) |
5
+ | Chinese | [API — balloc.zh.md](./docs/API%20—%20balloc.zh.md) |
6
+ | Spanish | [API — balloc.es.md](./docs/API%20—%20balloc.es.md) |
7
+ | English | [API — balloc.en.md](./docs/API%20—%20balloc.en.md) |
8
+ | Japanese | [API — balloc.jp.md](./docs/API%20—%20balloc.jp.md) |
9
+ | Portuguese | [API — balloc.pt.md](./docs/API%20—%20balloc.pt.md) |
10
+
11
+ # Balloc.js
12
+
13
+ `Balloc` is a memory allocator based on **bump allocation**, featuring two independent buses:
14
+
15
+ * `dataBus` — storage for block data.
16
+ * `controlBus` — storage for headers and metadata.
17
+
18
+ Blocks are externally identified by a **43-bit logical handler**, composed of an 11-bit generation and a 32-bit header address. The physical capacity of the blocks is always a power of two in number of elements.
19
+
20
+ ## API Ontology
21
+
22
+ ```text
23
+ Balloc
24
+ ├── Instantiation
25
+ │ └── constructor()
26
+ │
27
+ ├── Block Lifecycle
28
+ │ ├── alloc()
29
+ │ ├── realloc()
30
+ │ ├── free()
31
+ │ └── gc()
32
+ │
33
+ ├── Linear Structures
34
+ │ ├── Stack
35
+ │ │ ├── push()
36
+ │ │ ├── pop()
37
+ │ │ └── peek()
38
+ │ │
39
+ │ └── Queue
40
+ │ ├── nudge()
41
+ │ ├── pull()
42
+ │ └── head()
43
+ │
44
+ ├── Content Access
45
+ │ ├── has()
46
+ │ ├── get()
47
+ │ ├── set()
48
+ │ ├── clear()
49
+ │ ├── fill()
50
+ │ ├── fillAll()
51
+ │ ├── put()
52
+ │ └── pick()
53
+ │
54
+ ├── Capacity and Occupancy
55
+ │ ├── byteSize()
56
+ │ ├── occupied()
57
+ │ └── length()
58
+ │
59
+ ├── Inter-block Operations
60
+ │ ├── copy()
61
+ │ ├── copyWithin()
62
+ │ ├── append()
63
+ │ └── clone()
64
+ │ └── swap()
65
+ │
66
+ ├── Serialization
67
+ │ ├── dump()
68
+ │ └── load()
69
+ │
70
+ ├── Diagnostics and Utilization
71
+ │ ├── recycleCount()
72
+ │ ├── recycleBytes
73
+ │ ├── allocatedBytes
74
+ │ ├── unallocatedBytes
75
+ │ └── fragmentationRatio
76
+ │
77
+ └── Internal Usage
78
+ ├── Recycling
79
+ │ ├── recyclePush()
80
+ │ └── recyclePop()
81
+ │
82
+ ├── Scaling
83
+ │ ├── grow()
84
+ │ └── headerGrow()
85
+ │
86
+ └── Utilities
87
+ ├── extractMetadata()
88
+ └── bakeCookie()
89
+
90
+ ```
91
+
92
+ ---
93
+
94
+ ## Capacity Semantics
95
+
96
+ The logical capacity unit depends on `bucketSize`.
97
+
98
+ ```text
99
+ bucketSize = "8i" or bucketSize = "8u" → 1 byte per element
100
+ bucketSize = "16i" or bucketSize = "16u" or bucketSize = "16f" → 2 bytes per element
101
+ bucketSize = "32i" or bucketSize = "32u" or bucketSize = "32f" → 4 bytes per element
102
+ bucketSize = "64i" or bucketSize = "64u" or bucketSize = "64f" → 8 bytes per element
103
+
104
+ ```
105
+
106
+ The allocator computes capacity in **elements** as a power of two and subsequently converts this capacity into bytes.
107
+
108
+ For example, with `bucketSize = "32u"`:
109
+
110
+ ```text
111
+ capacity = 8 elements
112
+ wordSize = 4 bytes
113
+
114
+ byteCapacity = 8 × 4 = 32 bytes
115
+
116
+ ```
117
+
118
+ Physical capacity and occupancy are distinct concepts:
119
+
120
+ ```text
121
+ length(handler) → physical slots
122
+ occupied(handler) → occupied slots
123
+ byteSize(handler) → reserved physical bytes
124
+
125
+ ```
126
+
127
+ ---
128
+
129
+ # 1. Instantiation
130
+
131
+ ## `new Balloc(size, bucketSize, metadataSize)`
132
+
133
+ Creates a new allocator.
134
+
135
+ Storage is split between `dataBus`, intended for block data, and `controlBus`, intended for headers. The initial capacity of both is rounded according to the internal growth strategy.
136
+
137
+ ### Parameters
138
+
139
+ | Parameter | Type | Default | Description |
140
+ | --- | --- | --- | --- |
141
+ | `size` | `number` | `65535` | Initial capacity of `dataBus`, in bytes. |
142
+ | `bucketSize` | `BucketType` | Size of each element. | |
143
+ | `metadataSize` | `number` | `1024` | Initial capacity of `controlBus`, in bytes. |
144
+
145
+ `size` must be a safe integer within the range `[2, 0x80000000]`.
146
+
147
+ `bucketSize` accepts a `BucketType` value which is a string in the format `[Size in bytes][data type]`, accepting only values from the table:
148
+
149
+ | Signed Integer | Unsigned Integer | Float |
150
+ | --- | --- | --- |
151
+ | `8i` | `8u` | |
152
+ | `16i` | `16u` | `16f` |
153
+ | `32i` | `32u` | `32f` |
154
+ | `64i` | `64u` | `64f` |
155
+
156
+ `metadataSize` must be a safe integer within the range `[16, 0x80000000]`.
157
+
158
+ ### Exceptions
159
+
160
+ * `RangeError` — `size` out of range.
161
+ * `RangeError` — `bucketSize` different from those defined by `BucketType`.
162
+ * `RangeError` — `metadataSize` out of the permitted range.
163
+
164
+ ### Example
165
+
166
+ ```js
167
+ const { Balloc } = require("Balloc");
168
+
169
+ const memory = new Balloc();
170
+
171
+ ```
172
+
173
+ To use 32-bit unsigned elements:
174
+
175
+ ```js
176
+ const memory = new Balloc(65535, "32u");
177
+
178
+ ```
179
+
180
+ The constructor internally instantiates the `TypedArray` corresponding to `bucketSize`, alongside the `DataView`s used for reading and writing.
181
+
182
+ ---
183
+
184
+ # 2. Block Lifecycle
185
+
186
+ The fundamental cycle of a block is:
187
+
188
+ ```text
189
+ alloc()
190
+ │
191
+ ├── block operations
192
+ │
193
+ ├── realloc()
194
+ │
195
+ └── free()
196
+ │
197
+ └── recycling
198
+
199
+ ```
200
+
201
+ Recycling allows reusing freed regions with a capacity that exactly matches the new allocation.
202
+
203
+ ## `alloc(size = 16)`
204
+
205
+ Allocates a new block and returns its handler.
206
+
207
+ Capacity is rounded up to the smallest power of two that can accommodate `size` elements. The block is searched first in the list of recyclable regions; if no compatible region exists, space is obtained from the top of the `dataBus`.
208
+
209
+ ### Parameters
210
+
211
+ | Parameter | Type | Default | Description |
212
+ | --- | --- | --- | --- |
213
+ | `size` | `number` | `16` | Minimum desired number of elements. |
214
+
215
+ `size` must be within the range `[2, 0x80000000]`.
216
+
217
+ ### Return
218
+
219
+ ```text
220
+ number
221
+
222
+ ```
223
+
224
+ Block handler.
225
+
226
+ The handler has the logical form:
227
+
228
+ ```text
229
+ [ 11 generation bits ][ 32 header address bits ]
230
+
231
+ ```
232
+
233
+ ### Example
234
+
235
+ ```js
236
+ const handler = memory.alloc(100);
237
+
238
+ memory.push(handler, 42);
239
+
240
+ ```
241
+
242
+ A request for 100 elements does not reserve exactly 100 slots: capacity will be the next power of two.
243
+
244
+ ---
245
+
246
+ ## `realloc(handler)`
247
+
248
+ Increases block capacity to the next power of two.
249
+
250
+ If the block is located at the current top of the `dataBus`, expansion occurs **in-place**. Otherwise, a new region is obtained from the recycling system or the heap top, occupied elements are copied, and the previous region is recycled.
251
+
252
+ The handler does not change, as its header is preserved.
253
+
254
+ ### Parameters
255
+
256
+ | Parameter | Type | Description |
257
+ | --- | --- | --- |
258
+ | `handler` | `number` | Block handler. |
259
+
260
+ ### Return
261
+
262
+ ```text
263
+ void
264
+
265
+ ```
266
+
267
+ ### Exceptions
268
+
269
+ * `Error` — invalid or corrupted handler.
270
+ * `AggregateError` — capacity has reached the maximum limit.
271
+
272
+ ### Example
273
+
274
+ ```js
275
+ const handler = memory.alloc(8);
276
+
277
+ memory.realloc(handler);
278
+
279
+ ```
280
+
281
+ The operation doubles physical capacity, moving to the next power of two.
282
+
283
+ ---
284
+
285
+ ## `free(handler)`
286
+
287
+ Frees a block.
288
+
289
+ The header is invalidated by zeroing its cookie. The data region is:
290
+
291
+ * returned to recycling when it is not at the top of the `dataBus`;
292
+ * removed directly from the top when it is the most recent region on the heap.
293
+
294
+ ### Parameters
295
+
296
+ | Parameter | Type | Description |
297
+ | --- | --- | --- |
298
+ | `handler` | `number` | Block handler. |
299
+
300
+ ### Return
301
+
302
+ ```text
303
+ void
304
+
305
+ ```
306
+
307
+ ### Exceptions
308
+
309
+ `Error` when the handler is invalid or corrupted.
310
+
311
+ ```js
312
+ memory.free(handler);
313
+
314
+ ```
315
+
316
+ A freed handler is no longer valid.
317
+
318
+ ---
319
+
320
+ ## `gc()`
321
+
322
+ Compacts the `dataBus` via the **ReBump** algorithm.
323
+
324
+ The operation traverses existing headers on the `controlBus`, copies only still-valid blocks to a new `dataBus`, and updates their addresses in the headers. Free spaces between blocks are eliminated.
325
+
326
+ ### Effects
327
+
328
+ * eliminates all regions present in `recycle`;
329
+ * compacts live blocks;
330
+ * preserves header order;
331
+ * preserves physical block capacity;
332
+ * preserves occupancy;
333
+ * updates block addresses;
334
+ * repositions `dataPointer` after the last live block.
335
+
336
+ ### Return
337
+
338
+ ```text
339
+ void
340
+
341
+ ```
342
+
343
+ ### Complexity
344
+
345
+ Header traversal is `O(n)`, where `n` is the number of headers examined. Total cost also depends on the volume of data moved.
346
+
347
+ ```js
348
+ memory.gc();
349
+
350
+ ```
351
+
352
+ `gc()` does not alter handlers, as headers remain at the same addresses; only data regions are compacted.
353
+
354
+ ---
355
+
356
+ # 3. Linear Structures
357
+
358
+ Blocks can be used as linear structures accessed from the extremities.
359
+
360
+ ## 3.1 Stack
361
+
362
+ ### `push(handler, value)`
363
+
364
+ Adds `value` to the end of the occupied sequence.
365
+
366
+ If space is unavailable, no element is inserted and the method returns `false`.
367
+
368
+ ```js
369
+ memory.push(handler, 10);
370
+ memory.push(handler, 20);
371
+ memory.push(handler, 30);
372
+
373
+ ```
374
+
375
+ Return:
376
+
377
+ ```text
378
+ boolean
379
+
380
+ ```
381
+
382
+ * `true` — element inserted.
383
+ * `false` — block full.
384
+
385
+ ---
386
+
387
+ ### `pop(handler)`
388
+
389
+ Removes and returns the last occupied element.
390
+
391
+ Occupancy is decremented before element reading. Old bytes are not cleared.
392
+
393
+ Return:
394
+
395
+ ```text
396
+ number | undefined
397
+
398
+ ```
399
+
400
+ `undefined` indicates an empty block.
401
+
402
+ ```js
403
+ const value = memory.pop(handler);
404
+
405
+ ```
406
+
407
+ ---
408
+
409
+ ### `peek(handler)`
410
+
411
+ Returns the last occupied element without removing it.
412
+
413
+ Return:
414
+
415
+ ```text
416
+ number | undefined
417
+
418
+ ```
419
+
420
+ ---
421
+
422
+ ## 3.2 Queue
423
+
424
+ ### `nudge(handler, value)`
425
+
426
+ Inserts an element at the beginning of the occupied sequence.
427
+
428
+ Existing elements are shifted forward by one position. The operation is proportional to the number of occupied elements.
429
+
430
+ Return:
431
+
432
+ ```text
433
+ boolean
434
+
435
+ ```
436
+
437
+ * `true` — element inserted.
438
+ * `false` — block full.
439
+
440
+ ---
441
+
442
+ ### `pull(handler)`
443
+
444
+ Removes and returns the first element.
445
+
446
+ Remaining elements are shifted one position toward the beginning.
447
+
448
+ Return:
449
+
450
+ ```text
451
+ number | undefined
452
+
453
+ ```
454
+
455
+ ---
456
+
457
+ ### `head(handler)`
458
+
459
+ Returns the first element without removing it.
460
+
461
+ Return:
462
+
463
+ ```text
464
+ number | undefined
465
+
466
+ ```
467
+
468
+ ---
469
+
470
+ # 4. Content Access
471
+
472
+ ## `has(handler, value)`
473
+
474
+ Checks whether `value` is among the occupied elements.
475
+
476
+ The search uses the auxiliary `TypedArray` and considers only `[0, occupied)`, ignoring unoccupied physical capacity.
477
+
478
+ ```js
479
+ if (memory.has(handler, 42)) {
480
+ // ...
481
+ }
482
+
483
+ ```
484
+
485
+ Return:
486
+
487
+ ```text
488
+ boolean
489
+
490
+ ```
491
+
492
+ ---
493
+
494
+ ## `get(handler, index)`
495
+
496
+ Gets the element at the specified index.
497
+
498
+ Zero-based index. Indices outside `[0, occupied)` return `undefined`.
499
+
500
+ ```js
501
+ const value = memory.get(handler, 3);
502
+
503
+ ```
504
+
505
+ Return:
506
+
507
+ ```text
508
+ number | undefined
509
+
510
+ ```
511
+
512
+ ---
513
+
514
+ ## `set(handler, index, value)`
515
+
516
+ Replaces the element at an occupied index.
517
+
518
+ Occupancy is not modified.
519
+
520
+ Indices outside `[0, occupied)` are ignored and cause the method to return `false`.
521
+
522
+ ```js
523
+ memory.set(handler, 3, 99);
524
+
525
+ ```
526
+
527
+ Return:
528
+
529
+ ```text
530
+ boolean
531
+
532
+ ```
533
+
534
+ * `true` — value written.
535
+ * `false` — invalid index for the occupied region.
536
+
537
+ ---
538
+
539
+ ## `clear(handler, value = 0)`
540
+
541
+ Zeros occupancy and fills **all physical slots** of the block with `value`.
542
+
543
+ This includes slots that were not part of the occupied sequence.
544
+
545
+ ```js
546
+ memory.clear(handler);
547
+
548
+ ```
549
+
550
+ After the operation:
551
+
552
+ ```text
553
+ occupied = 0
554
+
555
+ ```
556
+
557
+ Physical capacity remains unchanged.
558
+
559
+ ---
560
+
561
+ ## `fill(handler, value, start = 0, end = null)`
562
+
563
+ Fills a range of slots with `value`.
564
+
565
+ The range follows the convention:
566
+
567
+ ```text
568
+ [start, end)
569
+
570
+ ```
571
+
572
+ `start` is inclusive and `end` is exclusive. When `end` is `null`, the limit is the total physical capacity. Occupancy becomes `end`.
573
+
574
+ ```js
575
+ memory.fill(handler, 0, 2, 6);
576
+
577
+ ```
578
+
579
+ Fills:
580
+
581
+ ```text
582
+ 2, 3, 4, 5
583
+
584
+ ```
585
+
586
+ ### Exceptions
587
+
588
+ `RangeError` when:
589
+
590
+ * `start < 0`;
591
+ * `start > capacity`;
592
+ * `end < 0`;
593
+ * `end > capacity`;
594
+ * `start > end`.
595
+
596
+ ---
597
+
598
+ ## `fillAll(handler, value)`
599
+
600
+ Fills all physical slots with `value` and marks the entire capacity as occupied.
601
+
602
+ Equivalent to filling the entire capacity followed by setting:
603
+
604
+ ```text
605
+ occupied = capacity
606
+
607
+ ```
608
+
609
+ Unlike `clear()`, the block ends up fully occupied.
610
+
611
+ ```js
612
+ memory.fillAll(handler, 0);
613
+
614
+ ```
615
+
616
+ ---
617
+
618
+ ## `put(handler, data)`
619
+
620
+ Copies an `Array` or `TypedArray` to the beginning of the block.
621
+
622
+ The amount copied is limited to block capacity. Occupancy becomes the greater value between prior occupancy and the amount actually copied. Therefore, `put()` does not reduce existing occupancy when receiving a smaller sequence.
623
+
624
+ ```js
625
+ memory.put(handler, [10, 20, 30, 40]);
626
+
627
+ ```
628
+
629
+ Also accepts `TypedArray`:
630
+
631
+ ```js
632
+ memory.put(handler, new Uint32Array([10, 20, 30]));
633
+
634
+ ```
635
+
636
+ ### Accepted Types
637
+
638
+ ```text
639
+ Array
640
+ TypedArray
641
+
642
+ ```
643
+
644
+ ### Exceptions
645
+
646
+ `TypeError` when `data` is neither `Array` nor `TypedArray`.
647
+
648
+ ---
649
+
650
+ ## `pick(handler)`
651
+
652
+ Returns a copy of currently occupied elements.
653
+
654
+ The type of the returned `TypedArray` matches the `bucketSize` configured in the allocator. Unoccupied physical capacity is not included.
655
+
656
+ ```js
657
+ const values = memory.pick(handler);
658
+
659
+ ```
660
+
661
+ The result has storage independent of `dataBus`.
662
+
663
+ For a `bucketSize = "32u"` allocator, for instance, the `Uint32Array` type will store the result.
664
+
665
+ ---
666
+
667
+ # 5. Capacity and Occupancy
668
+
669
+ ## `byteSize(handler)`
670
+
671
+ Returns the physical capacity of the block in bytes.
672
+
673
+ Does not represent the number of bytes currently occupied by elements; it represents the space reserved for all slots in the block.
674
+
675
+ ```js
676
+ const bytes = memory.byteSize(handler);
677
+
678
+ ```
679
+
680
+ ---
681
+
682
+ ## `occupied(handler)`
683
+
684
+ Returns the number of currently occupied elements.
685
+
686
+ ```js
687
+ const count = memory.occupied(handler);
688
+
689
+ ```
690
+
691
+ ---
692
+
693
+ ## `length(handler)`
694
+
695
+ Returns the total quantity of available physical slots.
696
+
697
+ Capacity is independent of occupancy:
698
+
699
+ ```text
700
+ length >= occupied
701
+
702
+ ```
703
+
704
+ ```js
705
+ const capacity = memory.length(handler);
706
+
707
+ ```
708
+
709
+ ---
710
+
711
+ # 6. Inter-block Operations
712
+
713
+ ## `copy(source, destiny, slots = UNKNOWN)`
714
+
715
+ Copies occupied elements from `source` to the beginning of `destiny`.
716
+
717
+ The amount copied is limited by:
718
+
719
+ 1. `source` occupancy;
720
+ 2. `destiny` capacity;
721
+ 3. `slots`.
722
+
723
+ `destiny` occupancy is increased when necessary, but its capacity is not altered.
724
+
725
+ ```js
726
+ memory.copy(source, destiny);
727
+
728
+ ```
729
+
730
+ Explicit limitation:
731
+
732
+ ```js
733
+ memory.copy(source, destiny, 8);
734
+
735
+ ```
736
+
737
+ ### Return
738
+
739
+ ```text
740
+ Uint8Array
741
+
742
+ ```
743
+
744
+ The `dataBus` itself after the operation.
745
+
746
+ ---
747
+
748
+ ## `copyWithin(handler, target, start, length)`
749
+
750
+ Copies a region within the block itself.
751
+
752
+ Semantics follow the concept of `Array.prototype.copyWithin()`, but applied over physical block capacity and limited by source occupancy available.
753
+
754
+ ```js
755
+ memory.copyWithin(handler, 4, 0, 4);
756
+
757
+ ```
758
+
759
+ Negative indices are interpreted relative to capacity end.
760
+
761
+ The operation:
762
+
763
+ * supports overlapping regions;
764
+ * limits indices to capacity;
765
+ * limits reading to the occupied region;
766
+ * limits writing to capacity;
767
+ * may increase `occupied`.
768
+
769
+ Return:
770
+
771
+ ```text
772
+ Uint8Array
773
+
774
+ ```
775
+
776
+ The `dataBus`.
777
+
778
+ ---
779
+
780
+ ## `append(target, source)`
781
+
782
+ Appends occupied elements of `source` to the end of occupied elements of `target`.
783
+
784
+ The amount is limited by source occupancy and available space in destination.
785
+
786
+ ```js
787
+ const count = memory.append(target, source);
788
+
789
+ ```
790
+
791
+ ### Return
792
+
793
+ ```text
794
+ number
795
+
796
+ ```
797
+
798
+ Number of elements effectively appended.
799
+
800
+ The operation also accepts:
801
+
802
+ ```js
803
+ memory.append(handler, handler);
804
+
805
+ ```
806
+
807
+ when `target` and `source` are the same block.
808
+
809
+ ---
810
+
811
+ ## `clone(handler)`
812
+
813
+ Creates an independent copy of the block.
814
+
815
+ The clone possesses:
816
+
817
+ * same physical capacity;
818
+ * same occupancy;
819
+ * same bytes across the entire reserved region;
820
+ * new handler.
821
+
822
+ ```js
823
+ const copy = memory.clone(handler);
824
+
825
+ ```
826
+
827
+ Unoccupied content is also copied.
828
+
829
+ ---
830
+
831
+ ## `swap(source, target)`
832
+
833
+ Swaps the data regions associated with two handlers.
834
+
835
+ The operation swaps in headers:
836
+
837
+ * address;
838
+ * capacity;
839
+ * occupancy.
840
+
841
+ No bytes are copied in `dataBus`. Handlers continue pointing to their respective headers.
842
+
843
+ ```js
844
+ memory.swap(a, b);
845
+
846
+ ```
847
+
848
+ Conceptually:
849
+
850
+ ```text
851
+ before:
852
+
853
+ handler A → region A
854
+ handler B → region B
855
+
856
+ after:
857
+
858
+ handler A → region B
859
+ handler B → region A
860
+
861
+ ```
862
+
863
+ ---
864
+
865
+ # 7. Serialization
866
+
867
+ Methods specialized in supporting block state export and import using the CBOR format.
868
+
869
+ ## `dump(handler)`
870
+
871
+ Serializes the contents of a memory block identified by `handler`.
872
+
873
+ The method extracts the occupied data from the block, groups it alongside capacity and word type information into a data envelope, and encodes it using the CBOR format.
874
+
875
+ ### Return
876
+
877
+ ```text
878
+ Uint8Array
879
+
880
+
881
+ ```
882
+
883
+ A buffer containing the serialized block data.
884
+
885
+ ### Exceptions
886
+
887
+ `Error` — if the handler is invalid or corrupted.
888
+
889
+ ---
890
+
891
+ ## `load(serialized)`
892
+
893
+ Restores a memory block from serialized data.
894
+
895
+ Decodes the CBOR envelope passed in `serialized`, validates whether the word type of the serialized data is compatible with the current allocator instance, and allocates a new block with the necessary capacity to accommodate the content. Then, original data and occupancy are restored in the new block.
896
+
897
+ ### Return
898
+
899
+ ```text
900
+ number
901
+
902
+
903
+ ```
904
+
905
+ Handler of the newly recreated block.
906
+
907
+ ### Exceptions
908
+
909
+ `Error` — if the word type of the decoded data diverges from the one configured in the allocator.
910
+
911
+ ---
912
+
913
+ # 8. Diagnostics and Utilization
914
+
915
+ ## `recycleCount()`
916
+
917
+ Returns the quantity of regions currently stored in the recycling system.
918
+
919
+ A region corresponds to a freed area that could not simply be removed from the top of the `dataBus`.
920
+
921
+ ```js
922
+ const count = memory.recycleCount();
923
+
924
+ ```
925
+
926
+ ---
927
+
928
+ ## `recycleBytes`
929
+
930
+ Getter that returns the total bytes belonging to currently recyclable regions.
931
+
932
+ ```js
933
+ const bytes = memory.recycleBytes;
934
+
935
+ ```
936
+
937
+ The value is calculated as:
938
+
939
+ ```text
940
+ Σ(capacity × region_quantity)
941
+
942
+ ```
943
+
944
+ ---
945
+
946
+ ## `allocatedBytes`
947
+
948
+ Getter that returns bytes associated with regions not currently in the recycling list.
949
+
950
+ Calculated by:
951
+
952
+ ```text
953
+ dataPointer - recycleBytes
954
+
955
+ ```
956
+
957
+ ```js
958
+ const bytes = memory.allocatedBytes;
959
+
960
+ ```
961
+
962
+ ---
963
+
964
+ ## `unallocatedBytes`
965
+
966
+ Getter that returns space still available at the top of the `dataBus`.
967
+
968
+ This is the region between `dataPointer` and the end of reserved capacity:
969
+
970
+ ```text
971
+ size - dataPointer
972
+
973
+ ```
974
+
975
+ ```js
976
+ const bytes = memory.unallocatedBytes;
977
+
978
+ ```
979
+
980
+ ---
981
+
982
+ ## `fragmentationRatio`
983
+
984
+ Getter that calculates the ratio between recyclable bytes and the heap area already traversed by the bump pointer.
985
+
986
+ ```text
987
+ recycleBytes / dataPointer
988
+
989
+ ```
990
+
991
+ When `dataPointer` is zero, the result is `0`.
992
+
993
+ ```js
994
+ const ratio = memory.fragmentationRatio;
995
+
996
+ ```
997
+
998
+ The value measures fragmentation **within the already used area of the heap**, rather than relative to total `dataBus` capacity.
999
+
1000
+ ---
1001
+
1002
+ # 9. Internal Usage
1003
+
1004
+ Methods in this section are part of `Balloc` implementation and do not constitute the interface intended for consumer code.
1005
+
1006
+ ```text
1007
+ Internal usage
1008
+ ├── Recycling
1009
+ │ ├── recyclePush()
1010
+ │ └── recyclePop()
1011
+ │
1012
+ ├── Scaling
1013
+ │ ├── grow()
1014
+ │ └── headerGrow()
1015
+ │
1016
+ └── Validation / metadata
1017
+ ├── extractMetadata()
1018
+ └── bakeCookie()
1019
+
1020
+ ```
1021
+
1022
+ ## 9.1 Recycling
1023
+
1024
+ ### `recyclePush(capacity, address)`
1025
+
1026
+ **Private.**
1027
+
1028
+ Inserts a free region into the recycling list associated with its capacity in bytes.
1029
+
1030
+ Each capacity has a page represented by `Uint32Array`:
1031
+
1032
+ ```text
1033
+ [count, address1, address2, ...]
1034
+
1035
+ ```
1036
+
1037
+ The structure operates as a LIFO stack. When the page becomes full, its buffer expands to double.
1038
+
1039
+ ---
1040
+
1041
+ ### `recyclePop(capacity)`
1042
+
1043
+ **Private.**
1044
+
1045
+ Removes and returns a recyclable region of exact capacity.
1046
+
1047
+ Removal occurs from the end of the page, characterizing LIFO behavior.
1048
+
1049
+ Returns:
1050
+
1051
+ ```text
1052
+ address
1053
+
1054
+ ```
1055
+
1056
+ or:
1057
+
1058
+ ```js
1059
+ UNKNOWN // 0xFFFFFFFF
1060
+
1061
+ ```
1062
+
1063
+ when no region exists available for that capacity.
1064
+
1065
+ ---
1066
+
1067
+ ## 9.2 Scaling
1068
+
1069
+ ### `grow()`
1070
+
1071
+ **Private. Do not use directly.**
1072
+
1073
+ Doubles `dataBus` capacity.
1074
+
1075
+ The underlying `ArrayBuffer` is transferred to a larger buffer and associated views are recreated.
1076
+
1077
+ Triggered automatically when a new region does not fit in remaining `dataBus` space.
1078
+
1079
+ ---
1080
+
1081
+ ### `headerGrow()`
1082
+
1083
+ **Private. Do not use directly.**
1084
+
1085
+ Doubles `controlBus` capacity.
1086
+
1087
+ Existing headers are preserved during `ArrayBuffer` transfer, and `controlAdapter` is recreated over the new storage.
1088
+
1089
+ Triggered automatically when there is no space for a new header.
1090
+
1091
+ ---
1092
+
1093
+ ## 9.3 Validation and Metadata
1094
+
1095
+ ### `extractMetadata(handler, out)`
1096
+
1097
+ **Private.**
1098
+
1099
+ Decodes and validates a handler.
1100
+
1101
+ The method extracts:
1102
+
1103
+ ```text
1104
+ generation
1105
+ headerAddress
1106
+ dataAddress
1107
+ capacityExponent
1108
+ byteCapacity
1109
+ occupied
1110
+
1111
+ ```
1112
+
1113
+ Handler validity is verified by recalculating the cookie stored in the header. The `out` object is reusable, avoiding new allocations.
1114
+
1115
+ Signature:
1116
+
1117
+ ```js
1118
+ extractMetadata(handler, out)
1119
+
1120
+ ```
1121
+
1122
+ Return:
1123
+
1124
+ ```text
1125
+ Metadata
1126
+
1127
+ ```
1128
+
1129
+ The return is the reference provided in `out` itself.
1130
+
1131
+ ---
1132
+
1133
+ ### `bakeCookie(addr, gen)`
1134
+
1135
+ **Private.**
1136
+
1137
+ Calculates the validation cookie used to verify handlers.
1138
+
1139
+ The cookie combines:
1140
+
1141
+ * header address;
1142
+ * lower 11 bits of generation.
1143
+
1144
+ The result is reduced to 8 bits. The value `0` is reserved to indicate an invalid header, so the method replaces `0` with `1`.
1145
+
1146
+ The cookie is not cryptographic. Its function is to detect invalid, stale, or corrupted handlers within the allocator management model.
1147
+
1148
+ ---
1149
+
1150
+ # 10. Storage Model
1151
+
1152
+ Each block features a fixed **16-byte** header:
1153
+
1154
+ ```text
1155
+ offset size field
1156
+ ────── ─────── ─────────────
1157
+ 0 4 bytes address
1158
+ 4 4 bytes occupied
1159
+ 8 1 byte capacity
1160
+ 9 1 byte cookie
1161
+ 10 6 bytes reserved
1162
+
1163
+ ```
1164
+
1165
+ Relevant fields are:
1166
+
1167
+ ### `address`
1168
+
1169
+ Initial address of the data region inside `dataBus`.
1170
+
1171
+ ### `occupied`
1172
+
1173
+ Number of currently occupied elements.
1174
+
1175
+ ### `capacity`
1176
+
1177
+ Power of two exponent representing physical capacity:
1178
+
1179
+ ```text
1180
+ capacityElements = 2 ** capacity
1181
+
1182
+ ```
1183
+
1184
+ Capacity in bytes is:
1185
+
1186
+ ```text
1187
+ capacityBytes = 2 ** capacity × wordSize
1188
+
1189
+ ```
1190
+
1191
+ ### `cookie`
1192
+
1193
+ Validation value associated with the header address and handler generation.
1194
+
1195
+ ---
1196
+
1197
+ # 11. Handlers
1198
+
1199
+ The handler is the external identifier used by practically all block operations.
1200
+
1201
+ Its logical composition is:
1202
+
1203
+ ```text
1204
+ ┌───────────────────────┬───────────────────────────────┐
1205
+ │ generation — 11 bits │ header address — 32 bits │
1206
+ └───────────────────────┴───────────────────────────────┘
1207
+
1208
+ ```
1209
+
1210
+ The lower part corresponds directly to the header address; the upper part represents generation.
1211
+
1212
+ Generation participates in cookie validation. Consequently, an invalid handler or one whose header has been invalidated causes an error in operations using it.
1213
+
1214
+ ---
1215
+
1216
+ # 12. Public API Summary
1217
+
1218
+ | Category | Method / property | Return |
1219
+ | --- | --- | --- |
1220
+ | Instantiation | `constructor()` | `Balloc`|
1221
+ | Lifecycle | `alloc()` | `number`|
1222
+ | Lifecycle | `realloc()` | `void`|
1223
+ | Lifecycle | `free()` | `void`|
1224
+ | Lifecycle | `gc()` | `void`|
1225
+ | Stack | `push()` | `boolean`|
1226
+ | Stack | `pop()` | `number` |
1227
+ | Stack | `peek()` | `number` |
1228
+ | Queue | `nudge()` | `boolean`|
1229
+ | Queue | `pull()` | `number` |
1230
+ | Queue | `head()` | `number` |
1231
+ | Access | `has()` | `boolean`|
1232
+ | Access | `get()` | `number` |
1233
+ | Access | `set()` | `boolean`|
1234
+ | Access | `clear()` | `void`|
1235
+ | Access | `fill()` | `void`|
1236
+ | Access | `fillAll()` | `void`|
1237
+ | Access | `put()` | `void`|
1238
+ | Access | `pick()` | `TypedArray`|
1239
+ | Capacity | `byteSize()` | `number`|
1240
+ | Capacity | `occupied()` | `number`|
1241
+ | Capacity | `length()` | `number`|
1242
+ | Blocks | `copy()` | `Uint8Array`|
1243
+ | Blocks | `copyWithin()` | `Uint8Array`|
1244
+ | Blocks | `append()` | `number`|
1245
+ | Blocks | `clone()` | `number`|
1246
+ | Blocks | `swap()` | `void`|
1247
+ | Serialization | `dump()` | `Uint8Array`|
1248
+ | Serialization | `load()` | `number`|
1249
+ | Diagnostics | `recycleCount()` | `number`|
1250
+ | Diagnostics | `recycleBytes` | `number`|
1251
+ | Diagnostics | `allocatedBytes` | `number`|
1252
+ | Diagnostics | `unallocatedBytes` | `number`|
1253
+ | Diagnostics | `fragmentationRatio` | `number`|
1254
+
1255
+ ## Internal Methods
1256
+
1257
+ | Group | Method | Visibility |
1258
+ | --- | --- | --- |
1259
+ | Recycling | `recyclePush()` | private |
1260
+ | Recycling | `recyclePop()` | private |
1261
+ | Scaling | `grow()` | private |
1262
+ | Scaling | `headerGrow()` | private |
1263
+ | Metadata | `extractMetadata()` | private |
1264
+ | Validation | `bakeCookie()` | private |