balloc 1.0.1 → 1.3.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.
@@ -1,7 +1,7 @@
1
1
  export type BucketType = '8u' | '16u' | '32u' | '64u' | '8i' | '16i' | '32i' | '64i' | '16f' | '32f' | '64f';
2
2
  export type Metadata = {
3
3
  /**
4
- * - The capacity of the memory block in bytes.
4
+ * - The byte capacity of the memory block.
5
5
  */
6
6
  byteCapacity: number;
7
7
  /**
@@ -58,97 +58,117 @@ export default class Balloc {
58
58
  * - `dataBus`: stores the data of allocated blocks;
59
59
  * - `controlBus`: stores headers and other control metadata.
60
60
  *
61
- * Each block has a fixed-size header (`HEADER_SIZE`) and is externally
62
- * identified by a 43-bit logical handle, composed of an 11-bit generation
63
- * and a 32-bit header address.
61
+ * Each block has a fixed-size header (`HEADER_SIZE`) and is
62
+ * identified externally by a 43-bit logical handle, composed
63
+ * of an 11-bit generation and its 32-bit header address.
64
64
  *
65
- * The capacity of each block is rounded up to the next power of two
66
- * in elements, respecting the configured bucket width.
65
+ * The capacity of each block is rounded up to the next power of
66
+ * two in elements, respecting the width of the configured bucket.
67
67
  *
68
68
  * @param {number} [size=65535] - Initial capacity of the `dataBus`, in bytes.
69
69
  * Must be a safe integer in the range [2, 0x80000000].
70
- * @param {BucketType} [bucketSize="32u"] - Bucket type and size identifier
70
+ * @param {BucketType} [bucketSize="32u"] - Identifier for the bucket type and size
71
71
  * (e.g., '32u' for Uint32, '64f' for Float64, '8i' for Int8).
72
72
  * @param {number} [metadataSize=1024] - Initial capacity of the `controlBus`,
73
73
  * in bytes. Must be a safe integer in the range
74
74
  * [HEADER_SIZE, 0x80000000].
75
75
  *
76
- * @throws {RangeError} If `size` is out of the allowed range.
76
+ * @throws {RangeError} If `size` is outside the allowed range.
77
77
  * @throws {RangeError} If `bucketSize` is not a valid bucket identifier.
78
- * @throws {RangeError} If `metadataSize` is out of the allowed range.
78
+ * @throws {RangeError} If `metadataSize` is outside the allowed range.
79
79
  */
80
80
  constructor(size?: number, bucketSize?: BucketType, metadataSize?: number);
81
81
  /**
82
82
  * Allocates a new memory block.
83
83
  *
84
84
  * The requested capacity is converted to the smallest power of two that
85
- * accommodates `size` elements. The block is preferably obtained from the list
86
- * of recyclable regions with the corresponding capacity; if no compatible
87
- * region exists, the block is reserved at the top of the `dataBus`.
85
+ * can accommodate `size` elements. The block is obtained preferentially from the list
86
+ * of recyclable regions with the corresponding capacity; if a compatible
87
+ * region does not exist, the block is reserved at the top of the `dataBus`.
88
88
  *
89
89
  * Next, a header is obtained from the control space. Previously freed headers
90
- * can be reused via the internal recycling stack; otherwise, the `controlBus`
91
- * is expanded when necessary.
90
+ * can be reused via the internal recycling stack;
91
+ * otherwise, the `controlBus` is expanded when necessary.
92
92
  *
93
93
  * The header receives the address, capacity, initial occupancy (zero), and
94
94
  * a validation cookie. The returned value encodes the allocation generation
95
95
  * and the header address:
96
96
  *
97
- * [11 bits generation][32 bits header address]
97
+ * [11 generation bits][32 header address bits]
98
98
  *
99
99
  * @param {number} [size=16] - Minimum number of elements the block must
100
100
  * accommodate. Must be a safe integer in the range [2, 0x80000000].
101
101
  * @returns {number} Handle identifying the allocated block.
102
- * @throws {RangeError} If `size` is out of the allowed range.
102
+ * @throws {RangeError} If `size` is outside the allowed range.
103
+ * @throws {Error} If it is necessary to expand the `dataBus` while any
104
+ * block is borrowed via withView().
103
105
  */
104
106
  alloc(size?: number): number;
105
107
  /**
106
108
  * Expands a block's capacity to the next power of two.
107
109
  *
108
110
  * When the block is located at the current top of the `dataBus`, expansion
109
- * occurs in-place: only its capacity and `dataPointer` are updated.
111
+ * occurs in-place: only its capacity and `dataPointer` are
112
+ * updated.
110
113
  *
111
114
  * If another region exists after the block, a new region with double the
112
- * capacity is obtained from the recyclable space or allocated at the top of
113
- * the `dataBus`. Currently occupied elements are then copied to the new region,
115
+ * capacity is obtained from the recyclable space or allocated at the top of the `dataBus`.
116
+ * The currently occupied elements are then copied to the new region,
114
117
  * and the previous region is returned to the recycling list.
115
118
  *
116
- * The handle remains valid because its header is preserved, and only the
117
- * address and capacity recorded within it are updated.
119
+ * The handle remains valid because its header is preserved and only the
120
+ * address and capacity recorded in it are updated.
118
121
  *
119
- * @param {number} handler - Handle of the block to expand.
122
+ * @param {number} handler - Handle of the block to be expanded.
120
123
  * @returns {void}
121
- * @throws {Error} If the handle is invalid or corrupted.
122
- * @throws {AggregateError} If the capacity has reached its maximum
124
+ * @throws {Error} If the handle is invalid, corrupted, or the block
125
+ * is borrowed via withView(). Also occurs if expansion requires
126
+ * dataBus remapping while another block is borrowed.
127
+ * @throws {AggregateError} If the capacity has reached the maximum
123
128
  * supported limit.
124
129
  */
125
130
  realloc(handler: number): void;
126
131
  /**
127
- * Frees a memory block.
132
+ * Reduces a block to the smallest power-of-two capacity that can contain
133
+ * its occupied elements, while preserving its address and handler.
128
134
  *
129
- * The block's header is invalidated by zeroing its cookie, and its address
130
- * is registered for later reuse. The data region is also returned to the
131
- * recycling system, except when the block is exactly at the top of the
132
- * `dataBus`; in that case, `dataPointer` is simply rolled back, eliminating
133
- * the region without creating an entry in the recycling list.
135
+ * If the block is at the top of the dataBus, dataPointer is moved backward.
136
+ * Otherwise, the released tail is split into power-of-two regions and each
137
+ * region is returned to the recycling list.
134
138
  *
135
- * @param {number} handler - Handle of the block to free.
139
+ * @param {number} handler - Handler of the block to shrink.
136
140
  * @returns {void}
137
- * @throws {Error} If the handle is invalid or corrupted.
141
+ * @throws {Error} If the handler is invalid, corrupted, or borrowed by withView().
138
142
  */
143
+ shrink(handler: number): void;
144
+ /**
145
+ * Frees a memory block.
146
+ *
147
+ * The block's header is invalidated by zeroing its cookie, and its
148
+ * address is registered for later reuse. The data region
149
+ * is also returned to the recycling system, except when the block is
150
+ * exactly at the top of the `dataBus`; in that case, the `dataPointer` is simply
151
+ * rolled back, eliminating the region without creating an entry in the recycling
152
+ * list.
153
+ *
154
+ * @param {number} handler - Handle of the block to be freed.
155
+ * @returns {void}
156
+ * @throws {Error} If the handle is invalid, corrupted, or the block
157
+ * is borrowed via withView().
158
+ */
139
159
  free(handler: number): void;
140
160
  /**
141
161
  * [INTERNAL USE]
142
162
  * Inserts a free region into the recycling list.
143
163
  *
144
164
  * The list is organized by capacity in bytes. For each capacity, the
145
- * stored value is a `Uint32Array` whose zero position contains the number
146
- * of registered addresses, and subsequent positions contain the addresses
147
- * of the free regions:
165
+ * stored value is a `Uint32Array` whose zero position contains the quantity
166
+ * of registered addresses and subsequent positions contain the addresses of
167
+ * the free regions:
148
168
  *
149
169
  * [count, address1, address2, ...]
150
170
  *
151
- * When the page becomes full, its buffer is expanded to double the current
171
+ * When the page becomes full, its buffer is expanded to double its current
152
172
  * capacity.
153
173
  *
154
174
  * @param {number} capacity - Capacity of the free region, in bytes.
@@ -167,7 +187,7 @@ export default class Balloc {
167
187
  *
168
188
  * @param {number} capacity - Desired capacity, in bytes.
169
189
  * @returns {number} Initial address of a recyclable region or `UNKNOWN`
170
- * when no region exists with the requested capacity.
190
+ * when a region with the requested capacity does not exist.
171
191
  * @private
172
192
  */
173
193
  private recyclePop;
@@ -175,23 +195,25 @@ export default class Balloc {
175
195
  * Compacts the `dataBus` using the ReBump algorithm.
176
196
  *
177
197
  * Sequentially traverses the headers registered in the `controlBus` and copies
178
- * still-valid regions to a new `dataBus`, eliminating free spaces between them.
179
- * The address of each surviving block is updated in its respective header.
198
+ * still-valid regions to a new `dataBus`, eliminating free spaces
199
+ * between them. Each surviving block's address is updated in its
200
+ * respective header.
180
201
  *
181
- * The order of blocks in the `controlBus` is preserved. Internal data of each
182
- * block is also fully preserved, including its reserved capacity and current
183
- * occupancy.
202
+ * The order of blocks in the `controlBus` is preserved. The internal data of
203
+ * each block is also fully preserved, including its reserved capacity
204
+ * and current occupancy.
184
205
  *
185
- * At the end of compaction:
206
+ * Upon completion of compaction:
186
207
  * - all recyclable regions are eliminated;
187
208
  * - `dataPointer` points to the first byte after the last live block;
188
- * - `dataBus` represents a contiguous heap for active blocks.
209
+ * - the `dataBus` represents a contiguous heap for active blocks.
189
210
  *
190
- * Control complexity is O(n), where `n` is the number of headers traversed.
191
- * The effective cost of memory movement also depends on the volume of bytes
192
- * belonging to live blocks.
211
+ * Control complexity is O(n), where `n` is the number of traversed
212
+ * headers. The effective memory movement cost also depends on the volume
213
+ * of bytes belonging to live blocks.
193
214
  *
194
215
  * @returns {void}
216
+ * @throws {Error} If any block is borrowed via withView().
195
217
  */
196
218
  gc(): void;
197
219
  /**
@@ -199,12 +221,13 @@ export default class Balloc {
199
221
  * Expands the `dataBus` to double its current capacity.
200
222
  *
201
223
  * The underlying buffer is transferred to a larger `ArrayBuffer`, preserving
202
- * existing data. All views associated with the buffer are recreated to reference
203
- * the new storage.
224
+ * existing data. All views associated with the buffer are recreated
225
+ * to reference the new storage.
204
226
  *
205
227
  * @returns {void}
206
- * @throws {RangeError} If the `dataBus` capacity has exceeded the maximum
207
- * supported limit.
228
+ * @throws {Error} If any block is borrowed via withView().
229
+ * @throws {RangeError} If the `dataBus` capacity has already exceeded the
230
+ * maximum supported limit.
208
231
  * @private
209
232
  */
210
233
  private grow;
@@ -213,40 +236,40 @@ export default class Balloc {
213
236
  * Expands the `controlBus` to double its current capacity.
214
237
  *
215
238
  * The underlying buffer is transferred to a larger `ArrayBuffer`, preserving
216
- * existing headers. The `controlAdapter` is recreated to reference the new
217
- * storage.
239
+ * existing headers. The `controlAdapter` is recreated to reference
240
+ * the new storage.
218
241
  *
219
242
  * @returns {void}
220
- * @throws {RangeError} If the current `controlBus` capacity has exceeded
221
- * the maximum supported limit.
243
+ * @throws {RangeError} If the current `controlBus` capacity has already
244
+ * exceeded the maximum supported limit.
222
245
  * @private
223
246
  */
224
247
  private headerGrow;
225
248
  /**
226
- * Appends an element to the end of the block's occupied sequence.
249
+ * Appends an element to the end of the sequence occupied by the block.
227
250
  *
228
251
  * The element is written at the first free position after the last occupied
229
- * element. Block occupancy is incremented only when space is available within
230
- * its capacity.
252
+ * element. Block occupancy is incremented only when space is available
253
+ * within its capacity.
231
254
  *
232
- * @param {number} handler - Block handle.
233
- * @param {number} value - Value to insert.
255
+ * @param {number} value - Value to be inserted.
234
256
  * @returns {boolean} `true` if the value was inserted; `false` if the block
235
- * has no available space.
236
- * @throws {Error} If the handle is invalid or corrupted.
257
+ * is already out of available space.
258
+ * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
259
+ *
237
260
  */
238
- push(handler: number, value: number): boolean;
261
+ push(handler: any, value: number): boolean;
239
262
  /**
240
- * Removes and returns the last occupied element from the block.
263
+ * Removes the last occupied element from the block and returns it.
241
264
  *
242
- * Occupancy is decremented before reading the removed element. Corresponding
265
+ * Occupancy is decremented before reading the removed element. The corresponding
243
266
  * bytes are not cleared and remain in the allocated region, but cease to be
244
267
  * part of the occupied sequence.
245
268
  *
246
269
  * @param {number} handler - Block handle.
247
270
  * @returns {number|undefined} Removed value or `undefined` if the block
248
271
  * is empty.
249
- * @throws {Error} If the handle is invalid or corrupted.
272
+ * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
250
273
  */
251
274
  pop(handler: number): number | undefined;
252
275
  /**
@@ -261,32 +284,32 @@ export default class Balloc {
261
284
  /**
262
285
  * Inserts an element at the beginning of the occupied sequence.
263
286
  *
264
- * Currently occupied elements are shifted one position forward and `value`
265
- * is written to the first position. The operation requires a free position
266
- * within the block's capacity.
287
+ * Currently occupied elements are shifted forward by one position and
288
+ * `value` is written at the first position. The operation requires a free
289
+ * position within the block's capacity.
267
290
  *
268
- * The operation's cost is proportional to the number of occupied elements,
291
+ * The operation cost is proportional to the number of occupied elements,
269
292
  * as data needs to be shifted.
270
293
  *
271
294
  * @param {number} handler - Block handle.
272
- * @param {number} value - Value to insert.
295
+ * @param {number} value - Value to be inserted.
273
296
  * @returns {boolean} `true` if the value was inserted; `false` if the block
274
297
  * is full.
275
- * @throws {Error} If the handle is invalid or corrupted.
298
+ * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
276
299
  */
277
300
  nudge(handler: number, value: number): boolean;
278
301
  /**
279
- * Removes and returns the first occupied element from the block.
302
+ * Removes the first occupied element from the block and returns it.
280
303
  *
281
- * Remaining elements are shifted one position toward the beginning of the
282
- * occupied region. The block's physical capacity remains unchanged.
304
+ * Remaining elements are shifted one position toward the beginning
305
+ * of the occupied region. The physical capacity of the block remains unchanged.
283
306
  *
284
- * The operation's cost is proportional to the number of shifted elements.
307
+ * The operation cost is proportional to the number of shifted elements.
285
308
  *
286
309
  * @param {number} handler - Block handle.
287
310
  * @returns {number|undefined} First removed value or `undefined` if the
288
311
  * block is empty.
289
- * @throws {Error} If the handle is invalid or corrupted.
312
+ * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
290
313
  */
291
314
  pull(handler: number): number | undefined;
292
315
  /**
@@ -299,18 +322,55 @@ export default class Balloc {
299
322
  */
300
323
  head(handler: number): number | undefined;
301
324
  /**
302
- * Checks if a value is present among the block's occupied elements.
325
+ * Checks if a value is present among the occupied elements of the block.
303
326
  *
304
327
  * The search is performed directly on the auxiliary `TypedArray` and considers
305
328
  * only the currently occupied range, not the entire reserved physical capacity.
306
329
  *
307
330
  * @param {number} handler - Block handle.
308
331
  * @param {number} value - Value to search for.
309
- * @returns {boolean} `true` if `value` is present among the occupied elements;
310
- * otherwise, `false`.
332
+ * @returns {boolean} `true` if `value` is present among the occupied
333
+ * elements; otherwise, `false`.
311
334
  * @throws {Error} If the handle is invalid or corrupted.
312
335
  */
313
336
  has(handler: number, value: number): boolean;
337
+ /**
338
+ * Locates the first occurrence of an element in the block's occupied sequence.
339
+ *
340
+ * The search is performed in ascending order of indices, using the `indexOf`
341
+ * operation of the underlying TypedArray. The search exclusively considers
342
+ * occupied elements, ignoring the block's unused capacity.
343
+ *
344
+ * The starting search position can be specified by `fromIndex`,
345
+ * following TypedArray indexing rules.
346
+ *
347
+ * @param {number} handler - Block handle.
348
+ * @param {number|bigint} searchElement - Element to locate.
349
+ * @param {number} [fromIndex=0] - Index from which to start the search.
350
+ * @returns {number} Index of the first occurrence or `-1` if the
351
+ * element is not found or if the block is empty.
352
+ * @throws {Error} If the handle is invalid or corrupted.
353
+ */
354
+ indexOf(handler: number, searchElement: number | bigint, fromIndex?: number): number;
355
+ /**
356
+ * Locates the last occurrence of an element in the block's occupied sequence.
357
+ *
358
+ * The search is performed in descending order of indices, using the `lastIndexOf`
359
+ * operation of the underlying TypedArray. The search exclusively considers
360
+ * occupied elements, ignoring the block's unused capacity.
361
+ *
362
+ * The starting search position can be specified by `fromIndex`,
363
+ * following TypedArray indexing rules. By default, the search starts
364
+ * at the last occupied element.
365
+ *
366
+ * @param {number} handler - Block handle.
367
+ * @param {number|bigint} searchElement - Element to locate.
368
+ * @param {number} [fromIndex=-1] - Index from which to start the search.
369
+ * @returns {number} Index of the last occurrence or `-1` if the
370
+ * element is not found or if the block is empty.
371
+ * @throws {Error} If the handle is invalid or corrupted.
372
+ */
373
+ lastIndexOf(handler: number, searchElement: number | bigint, fromIndex?: number): number;
314
374
  /**
315
375
  * Returns the occupied element at a zero-based index.
316
376
  *
@@ -335,93 +395,113 @@ export default class Balloc {
335
395
  * @param {number} value - New value.
336
396
  * @returns {boolean} `true` if the value was written; `false` if the index
337
397
  * is outside the occupied region.
338
- * @throws {Error} If the handle is invalid or corrupted.
398
+ * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
339
399
  */
340
400
  set(handler: number, index: number, value: number): boolean;
341
401
  /**
342
- * Clears block occupancy and fills its entire physical capacity with a single value.
402
+ * Zeroes block occupancy and fills its entire physical capacity with
403
+ * a single value.
343
404
  *
344
- * Filling covers all available slots, not just currently occupied elements.
345
- * After the operation, the block is considered empty regardless of the value
346
- * used for filling.
405
+ * The fill covers all available slots, not just currently
406
+ * occupied elements. After the operation, the block is considered
407
+ * empty regardless of the value used for filling.
347
408
  *
348
409
  * @param {number} handler - Block handle.
349
410
  * @param {number} [value=0] - Value used to fill all slots.
350
411
  * @returns {void}
351
- * @throws {Error} If the handle is invalid or corrupted.
412
+ * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
352
413
  */
353
414
  clear(handler: number, value?: number): void;
354
415
  /**
355
416
  * Fills a range of block slots with a value.
356
417
  *
357
- * The range uses zero-based indices and follows the convention `[start, end)`,
358
- * meaning `start` is inclusive and `end` is exclusive. When `end` is not provided,
359
- * filling extends to the end of the block's physical capacity.
418
+ * The range uses zero-based indices and follows the convention
419
+ * `[start, end)`, therefore `start` is inclusive and `end` is exclusive.
420
+ * When `end` is not provided, filling extends to the end of the block's
421
+ * physical capacity.
360
422
  *
361
- * Block occupancy becomes `end`, including when the filled range extends beyond
362
- * the previous occupancy.
423
+ * Block occupancy becomes `end`, including when the filled range
424
+ * extends beyond previous occupancy.
363
425
  *
364
426
  * @param {number} handler - Block handle.
365
427
  * @param {number} value - Value used for filling.
366
428
  * @param {number} [start=0] - Initial index, inclusive.
367
- * @param {number|null} [end=null] - Final index, exclusive. When omitted or
368
- * `null`, uses the total block capacity.
429
+ * @param {number|null} [end=null] - Final index, exclusive. When
430
+ * omitted or `null`, uses the block's total capacity.
369
431
  * @returns {void}
370
- * @throws {Error} If the handle is invalid or corrupted.
371
- * @throws {RangeError} If `start` or `end` is out of capacity bounds, or if
372
- * `start > end`.
432
+ * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
433
+ * @throws {RangeError} If `start` or `end` is outside capacity limits,
434
+ * or if `start > end`.
373
435
  */
374
436
  fill(handler: number, value: number, start?: number, end?: number | null): void;
375
437
  /**
376
438
  * Fills all physical slots of the block with a value and marks the entire
377
439
  * capacity as occupied.
378
440
  *
379
- * Unlike `clear()`, the operation results in a fully occupied block.
441
+ * Unlike `clear()`, this operation results in a fully occupied block.
380
442
  *
381
443
  * @param {number} handler - Block handle.
382
444
  * @param {number} value - Value used for filling.
383
445
  * @returns {void}
384
- * @throws {Error} If the handle is invalid or corrupted.
446
+ * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
385
447
  */
386
448
  fillAll(handler: number, value: number): void;
387
449
  /**
388
450
  * Copies elements from an `Array` or `TypedArray` to the beginning of the block.
389
451
  *
390
- * The copied quantity is limited by the block's capacity. Occupancy is updated
391
- * to the greater value between the previous occupancy and the quantity effectively
392
- * copied; therefore, a call to `put()` does not reduce existing occupancy when
393
- * receiving a smaller sequence.
452
+ * The copied quantity is limited by the block's capacity. Occupancy is
453
+ * updated to the greater value between previous occupancy and the quantity
454
+ * effectively copied; therefore, a call to `put()` does not reduce existing
455
+ * occupancy when receiving a smaller sequence.
394
456
  *
395
- * For `Array`, only the first fitting elements are copied. For `TypedArray`,
396
- * a source subarray is used without intermediate copying.
457
+ * For `Array`, only the first elements that fit are copied.
458
+ * For `TypedArray`, a source subarray is used without intermediate copying.
397
459
  *
398
460
  * @param {number} handler - Block handle.
399
461
  * @param {Array|TypedArray} data - Sequence of values to copy.
400
462
  * @returns {void}
401
- * @throws {Error} If the handle is invalid or corrupted.
463
+ * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
402
464
  * @throws {TypeError} If `data` is neither an `Array` nor a `TypedArray`.
403
465
  */
404
466
  put(handler: number, data: any[] | TypedArray): void;
405
467
  /**
406
468
  * Returns a copy of the elements currently occupied by the block.
407
469
  *
408
- * The result is a `TypedArray` of the same type configured by `bucketSize`.
409
- * Unoccupied physical capacity is not included in the result.
470
+ * The result is a `TypedArray` of the same type configured by
471
+ * `bucketSize`. Unoccupied physical capacity is not included in the result.
410
472
  *
411
- * The operation does not directly expose internal storage: the method uses
412
- * `TypedArray.prototype.slice()`, producing a new view with independent storage.
473
+ * The operation does not directly expose internal storage: the method
474
+ * uses `TypedArray.prototype.slice()`, producing a new view with
475
+ * independent storage.
413
476
  *
414
477
  * @param {number} handler - Block handle.
415
- * @returns {TypedArray} Copy of the elements occupied by the block. When the
416
- * block is empty, returns an empty `TypedArray` of the configured type.
478
+ * @returns {TypedArray} Copy of elements occupied by the block. When
479
+ * the block is empty, returns an empty `TypedArray` of the configured type.
417
480
  * @throws {Error} If the handle is invalid or corrupted.
418
481
  */
419
482
  pick(handler: number): TypedArray;
483
+ /**
484
+ * Executes a callback over a direct view of the block's occupied region.
485
+ *
486
+ * The view shares Balloc's internal storage and performs no copying.
487
+ * The block is protected against incompatible operations during the callback.
488
+ *
489
+ * The callback must be synchronous. The received view must not be stored,
490
+ * returned, or used after the callback terminates. This restriction
491
+ * also applies to subviews derived from the original view.
492
+ *
493
+ * @param {number} handler - Block handle.
494
+ * @param {...(number|Function)} args - One or more block handles followed by the callback.
495
+ * @returns {*} Return value produced by the callback.
496
+ * @throws {TypeError} If no handle is provided or the callback is not a function.
497
+ * @throws {Error} If a handle is invalid, duplicated, or already borrowed.
498
+ */
499
+ withView(...args: (number | Function)[]): any;
420
500
  /**
421
501
  * Returns the physical capacity of the block in bytes.
422
502
  *
423
- * Capacity corresponds to the space reserved for all slots in the block,
424
- * regardless of how many elements are currently occupied.
503
+ * Capacity corresponds to the space reserved for all slots in the
504
+ * block, regardless of how many elements are currently occupied.
425
505
  *
426
506
  * @param {number} handler - Block handle.
427
507
  * @returns {number} Physical capacity of the block, in bytes.
@@ -437,9 +517,9 @@ export default class Balloc {
437
517
  */
438
518
  occupied(handler: number): number;
439
519
  /**
440
- * Returns the capacity of the block in number of elements.
520
+ * Returns the block's capacity in number of elements.
441
521
  *
442
- * The value corresponds to the total quantity of physically reserved slots,
522
+ * The value corresponds to the total number of physically reserved slots,
443
523
  * not the currently occupied quantity.
444
524
  *
445
525
  * @param {number} handler - Block handle.
@@ -448,76 +528,94 @@ export default class Balloc {
448
528
  */
449
529
  length(handler: number): number;
450
530
  /**
451
- * Copies occupied elements from one block to another.
531
+ * Checks if the memory block has reached its allocation capacity.
452
532
  *
453
- * Copying begins at the first element of `source` and the first slot of
454
- * `destiny`. The effectively copied quantity is limited by the number of occupied
455
- * elements in the source, the destination's physical capacity, and the limit
456
- * specified in `slots`.
533
+ * A block is considered full when the number of occupied elements
534
+ * equals its total allocated capacity, leaving no positions available
535
+ * to store new elements.
457
536
  *
458
- * Destination occupancy is increased, when necessary, to cover the copied
459
- * elements. Destination capacity is not changed.
460
- *
461
- * The operation uses `Uint8Array.prototype.copyWithin()` on the `dataBus`,
462
- * therefore the return value is the `Uint8Array` representing the data bus.
463
- *
464
- * @param {number} source - Source block handle.
465
- * @param {number} destiny - Destination block handle.
466
- * @param {number} [slots=UNKNOWN] - Maximum number of elements to copy.
467
- * `UNKNOWN` represents the sentinel value used not to impose an additional limit.
468
- * @returns {Uint8Array} The `dataBus` after the operation.
469
- * @throws {Error} If `source` or `destiny` is invalid or corrupted.
537
+ * @param {number} handler - Block handle.
538
+ * @returns {boolean} `true` if the block is full; otherwise, `false`.
539
+ * @throws {Error} If the handle is invalid or corrupted.
470
540
  */
471
- copy(source: number, destiny: number, slots?: number): Uint8Array;
541
+ isFull(handler: number): boolean;
542
+ /**
543
+ * Copies occupied elements from one block to another.
544
+ *
545
+ * Copying starts at the first element of `source` and the first slot of
546
+ * `destination`. The quantity effectively copied is limited by the number of
547
+ * occupied elements in the source, the physical capacity of the destination, and the
548
+ * limit specified in `slots`.
549
+ *
550
+ * Destination occupancy is increased, when necessary, to cover the
551
+ * copied elements. Destination capacity is unchanged.
552
+ *
553
+ * The operation uses `Uint8Array.prototype.copyWithin()` on the `dataBus`,
554
+ * therefore the return value is the `Uint8Array` representing the data bus.
555
+ *
556
+ * @param {number} source - Source block handle.
557
+ * @param {number} destination - Destination block handle.
558
+ * @param {number} [slots=UNKNOWN] - Maximum number of elements to copy.
559
+ * `UNKNOWN` represents the sentinel value used not to impose an
560
+ * additional limit.
561
+ * @returns {Uint8Array} The `dataBus` after the operation.
562
+ * @throws {Error} If handles are invalid, corrupted,
563
+ * or the destination block is borrowed via withView().
564
+ */
565
+ copy(source: number, destination: number, slots?: number): Uint8Array;
472
566
  /**
473
567
  * Copies a region of elements within the same block.
474
568
  *
475
- * Indices are zero-based and follow the semantics of `Array.prototype.copyWithin()`:
476
- * `target` identifies the destination position, `start` the source starting
477
- * position, and `length` the maximum number of elements to copy.
569
+ * Indices are zero-based and follow the semantics of
570
+ * `Array.prototype.copyWithin()`: `target` identifies the destination position,
571
+ * `start` the starting source position, and `length` the maximum quantity of
572
+ * elements to copy.
478
573
  *
479
- * Negative indices are interpreted from the end of the block's capacity. After
480
- * this normalization, indices are clamped to physical capacity. The copied quantity
481
- * is also limited by effectively occupied elements and available space from `target`.
574
+ * Negative indices are interpreted from the end of the block's capacity.
575
+ * After this normalization, indices are clamped to physical capacity.
576
+ * The copied quantity is also limited by effectively occupied elements
577
+ * and available space starting from `target`.
482
578
  *
483
579
  * The operation supports overlapping regions and can increase block occupancy
484
- * when the target range exceeds current occupancy.
580
+ * when the destination range exceeds previous occupancy.
485
581
  *
486
582
  * @param {number} handler - Block handle.
487
583
  * @param {number} target - Initial destination index.
488
584
  * @param {number} start - Initial source index.
489
585
  * @param {number} length - Maximum number of elements to copy.
490
586
  * @returns {Uint8Array} The `dataBus` after the operation.
491
- * @throws {Error} If the handle is invalid or corrupted.
587
+ * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
492
588
  */
493
589
  copyWithin(handler: number, target: number, start: number, length: number): Uint8Array;
494
590
  /**
495
591
  * Appends occupied elements of `source` to the end of `target`.
496
592
  *
497
593
  * The copied quantity is limited by the number of occupied elements in the
498
- * source and the remaining free space in the destination's capacity.
594
+ * source and remaining free space in the destination's capacity.
499
595
  *
500
596
  * The operation supports `target` and `source` referencing the same block.
501
- * In that case, the copy is performed on the block's own region and preserves
502
- * `copyWithin()` overlap semantics.
597
+ * In this case, copying is performed over the block's own region and
598
+ * preserves `copyWithin()` overlap semantics.
503
599
  *
504
- * @param {number} target - Handle of the block that will receive the elements.
505
- * @param {number} source - Handle of the block whose elements will be appended.
600
+ * @param {number} target - Handle of the block that will receive elements.
601
+ * @param {number} source - Handle of the block whose elements will be
602
+ * appended.
506
603
  * @returns {number} Number of elements effectively appended.
507
- * @throws {Error} If `target` or `source` is invalid or corrupted.
604
+ * @throws {Error} If handles are invalid, corrupted,
605
+ * or the destination block is borrowed via withView().
508
606
  */
509
607
  append(target: number, source: number): number;
510
608
  /**
511
609
  * Creates an independent copy of a block.
512
610
  *
513
- * The new block has the same physical capacity and occupancy as the original block.
514
- * All bytes in the reserved region, including currently unoccupied positions,
515
- * are copied.
611
+ * The new block has the same physical capacity and occupancy as the
612
+ * original block. All bytes in the reserved region, including currently
613
+ * unoccupied positions, are copied.
516
614
  *
517
- * The new block receives a new handle and remains subject to normal allocation
518
- * and recycling rules.
615
+ * The new block receives a new handle and remains subject to normal
616
+ * allocation and recycling rules.
519
617
  *
520
- * @param {number} handler - Handle of the block to clone.
618
+ * @param {number} handler - Handle of the block to be cloned.
521
619
  * @returns {number} Handle of the new block.
522
620
  * @throws {Error} If the handle is invalid or corrupted.
523
621
  */
@@ -525,28 +623,30 @@ export default class Balloc {
525
623
  /**
526
624
  * Swaps the data regions associated with two blocks.
527
625
  *
528
- * The operation swaps region addresses, capacities, and occupancies between headers.
529
- * Handles remain unchanged: after the operation, each handle continues identifying
530
- * its own header, but that header now describes the region previously belonging
531
- * to the other block.
626
+ * The operation swaps region addresses, capacities, and occupancies
627
+ * between headers. Handles remain unchanged:
628
+ * after the operation, each handle continues identifying its own
629
+ * header, but that header now describes the region previously
630
+ * belonging to the other block.
532
631
  *
533
- * No data is copied in the `dataBus`; only the metadata of the two headers
534
- * is modified.
632
+ * No data is copied in the `dataBus`; only the metadata of the two
633
+ * headers is modified.
535
634
  *
536
635
  * @param {number} source - Handle of the first block.
537
636
  * @param {number} target - Handle of the second block.
538
637
  * @returns {void}
539
- * @throws {Error} If `source` or `target` is invalid or corrupted.
638
+ * @throws {Error} If handles are invalid, corrupted,
639
+ * or either block is borrowed via withView().
540
640
  */
541
641
  swap(source: number, target: number): void;
542
642
  /**
543
643
  * Serializes the content and metadata of a memory block.
544
644
  *
545
- * The method extracts occupied data from the block via `pick()`, groups them
546
- * along with capacity and word type information into a data envelope, and encodes
547
- * them using the CBOR format.
645
+ * The method extracts occupied block data via `pick()`,
646
+ * groups it alongside capacity and word type information
647
+ * into a data envelope, and encodes it using the CBOR format.
548
648
  *
549
- * @param {number} handler - Handle of the block to serialize.
649
+ * @param {number} handler - Handle of the block to be serialized.
550
650
  * @returns {Uint8Array} A buffer containing the serialized block data.
551
651
  * @throws {Error} If the handle is invalid or corrupted.
552
652
  */
@@ -555,50 +655,73 @@ export default class Balloc {
555
655
  * Restores a memory block from serialized data.
556
656
  *
557
657
  * Decodes the CBOR envelope, validates whether the word type of the serialized
558
- * data is compatible with the current allocator instance, and allocates a new
559
- * block with the necessary capacity to accommodate the content. Then, the original
560
- * data and occupancy are restored in the new block.
658
+ * data is compatible with the current allocator instance, and allocates
659
+ * a new block with the capacity required to accommodate the content.
660
+ * Then, original data and occupancy are restored in the new block.
561
661
  *
562
662
  * @param {Uint8Array|ArrayBuffer} serialized - Buffer containing serialized data.
563
663
  * @returns {number} Handle of the newly recreated block.
564
- * @throws {Error} If the decoded data's word type differs from the one configured in the allocator.
664
+ * @throws {Error} If the decoded data's word type differs from the allocator's configuration.
565
665
  */
566
666
  load(serialized: Uint8Array | ArrayBuffer): number;
667
+ /**
668
+ * Prevents modification operations on a borrowed block.
669
+ *
670
+ * Checks the VIEW_LOCK flag in the header associated with the provided address.
671
+ *
672
+ * @param {number} headerAddress - Address of the header in controlBus.
673
+ * @returns {void}
674
+ * @throws {Error} If the block is borrowed via withView().
675
+ * @private
676
+ */
677
+ private assertNotBorrowed;
678
+ /**
679
+ * Prevents operations that replace or remap the dataBus
680
+ * while any segment is borrowed.
681
+ *
682
+ * @returns {void}
683
+ * @throws {Error} If any block is borrowed via withView().
684
+ * @private
685
+ */
686
+ private assertNoActiveViews;
567
687
  /**
568
688
  * [INTERNAL USE]
569
- * Decodes the metadata associated with a handle.
689
+ * Decodes metadata associated with a handle.
570
690
  *
571
691
  * The handle is split into generation and header address. The cookie stored
572
692
  * in the header is then recalculated to validate the combination; only after
573
- * this validation is metadata read.
693
+ * this validation are metadata read.
574
694
  *
575
- * Decoded fields are written to the `out` object, which is returned by the function
576
- * itself. The object can be reused across calls to avoid new allocations.
695
+ * Decoded fields are written to the `out` object, which is returned
696
+ * by the function itself. The object can be reused between calls to avoid
697
+ * new allocations.
577
698
  *
578
- * @param {number} handler - Handle to decode and validate.
579
- * @param {Metadata} out - Object that will receive the decoded metadata.
699
+ * @param {number} handler - Handle to be decoded and validated.
700
+ * @param {Metadata} out - Object that will receive decoded metadata.
580
701
  * @returns {Metadata} The same reference provided in `out`.
581
- * @throws {Error} If the header cookie does not match the handle or if the
582
- * handle does not identify a valid header.
702
+ * @throws {Error} If the header cookie does not match the handle or
703
+ * if the handle does not identify a valid header.
583
704
  * @private
584
705
  */
585
706
  private extractMetadata;
586
707
  /**
587
708
  * [INTERNAL USE]
588
- * Calculates the validation cookie of a header.
709
+ * Calculates a header's validation cookie.
589
710
  *
590
- * The cookie is derived from the header address and the 11-bit generation via
591
- * fast hash. The result is reduced to 8 bits.
711
+ * The cookie is derived from the header address and the 11 generation bits via
712
+ * fast hash.
713
+ * The result is reduced to 8 bits.
592
714
  *
593
- * The value zero is reserved as an invalid header indicator; therefore, when
594
- * the calculation yields zero, the method returns `1`.
715
+ * The value zero is reserved as an invalid header indicator; therefore,
716
+ * when the calculation produces zero, the method returns `1`.
595
717
  *
596
- * The cookie is not a cryptographic mechanism. Its purpose is to detect invalid,
597
- * stale, or corrupted handles within the allocator's memory management model.
718
+ * The cookie does not constitute a cryptographic mechanism. Its purpose is
719
+ * to detect invalid, stale, or corrupted handles within the allocator's
720
+ * memory management model.
598
721
  *
599
722
  * @param {number} addr - Header address.
600
- * @param {number} gen - Generation associated with the handle. Only the 11 least
601
- * significant bits are considered.
723
+ * @param {number} gen - Generation associated with the handle. Only the 11
724
+ * least significant bits are considered.
602
725
  * @returns {number} 8-bit cookie in the range [1, 255].
603
726
  * @private
604
727
  */
@@ -606,51 +729,53 @@ export default class Balloc {
606
729
  /**
607
730
  * Returns the number of regions currently stored in the recycling list.
608
731
  *
609
- * Each region corresponds to a freed block that could not be returned directly
610
- * to the top of the `dataBus` and therefore remains available for reuse by a
611
- * future allocation of identical capacity.
732
+ * Each region corresponds to a freed block that could not be returned
733
+ * directly to the top of the `dataBus` and, therefore, remains available for
734
+ * reuse by a future allocation of identical capacity.
612
735
  *
613
- * @returns {number} Number of free regions maintained in `recycle`.
736
+ * @returns {number} Number of free regions kept in `recycle`.
614
737
  */
615
738
  recycleCount(): number;
616
739
  /**
617
- * Returns the total bytes belonging to regions currently stored in the recycling list.
740
+ * Returns the total bytes belonging to regions currently stored in the
741
+ * recycling list.
618
742
  *
619
- * The value is calculated by summing the capacity of each free region multiplied
743
+ * The value is calculated by summing each free region's capacity multiplied
620
744
  * by the number of regions registered for that capacity.
621
745
  *
622
746
  * @returns {number} Number of bytes occupied by recyclable regions.
623
747
  */
624
748
  get recycleBytes(): number;
625
749
  /**
626
- * Returns the amount of allocated bytes that do not belong to currently recyclable regions.
750
+ * Returns the quantity of allocated bytes that do not belong to currently
751
+ * recyclable regions.
627
752
  *
628
- * The value is obtained by subtracting the bytes belonging to free regions maintained
629
- * in `recycle` from the total advance of `dataPointer`.
753
+ * The value is obtained by subtracting the bytes belonging to free regions
754
+ * kept in `recycle` from the total advance of `dataPointer`.
630
755
  *
631
- * @returns {number} Number of bytes associated with non-recyclable regions within
632
- * the area already used by the `dataBus`.
756
+ * @returns {number} Number of bytes associated with non-recyclable regions
757
+ * within the area already used by `dataBus`.
633
758
  */
634
759
  get allocatedBytes(): number;
635
760
  /**
636
- * Returns the amount of bytes not yet used at the top of the `dataBus`.
761
+ * Returns the quantity of bytes not yet used at the top of the `dataBus`.
637
762
  *
638
763
  * This space corresponds to the region between `dataPointer` and the end of the
639
764
  * capacity currently reserved for the buffer.
640
765
  *
641
- * @returns {number} Number of available bytes after `dataPointer`.
766
+ * @returns {number} Number of bytes available after `dataPointer`.
642
767
  */
643
768
  get unallocatedBytes(): number;
644
769
  /**
645
770
  * Returns the ratio between recyclable memory and the used heap area.
646
771
  *
647
- * The metric considers free regions maintained in `recycle` as fragmentation
648
- * and uses `dataPointer` as the denominator. When no bytes have been used by the heap,
649
- * the result is zero.
772
+ * The metric considers free regions kept in `recycle` as fragmentation
773
+ * and uses `dataPointer` as the denominator. When no bytes have been used
774
+ * by the heap, the result is zero.
650
775
  *
651
- * The value does not represent the fraction of the entire `dataBus` capacity,
652
- * but specifically the proportion of recyclable bytes within the area already
653
- * traversed by the bump pointer.
776
+ * The value does not represent the fraction of the entire `dataBus` capacity, but
777
+ * specifically the proportion of recyclable bytes within the area already traversed
778
+ * by the bump pointer.
654
779
  *
655
780
  * @returns {number} Fragmentation ratio in the range [0, 1] under normal
656
781
  * operating conditions.