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