balloc 1.0.1 → 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.
- package/README.MD +37 -14
- package/README.ar.md +25 -2
- package/README.en.md +25 -2
- package/README.es.md +25 -2
- package/README.jp.md +25 -2
- package/README.pt.md +24 -5
- package/README.zh.md +25 -2
- package/change.log.md +7 -0
- package/docs/API /342/200/224 balloc.ar.md" +36 -0
- package/docs/API /342/200/224 balloc.en.md" +36 -0
- package/docs/API /342/200/224 balloc.es.md" +36 -0
- package/docs/API /342/200/224 balloc.jp.md" +36 -0
- package/docs/API /342/200/224 balloc.pt.md" +171 -12
- package/docs/API /342/200/224 balloc.zh.md" +36 -0
- package/package.json +22 -8
- package/src/ar/balloc.cjs +474 -309
- package/src/ar/balloc.js +1 -1
- package/src/ar/balloc.mjs +566 -383
- package/src/en/balloc.cjs +387 -221
- package/src/en/balloc.js +1 -1
- package/src/en/balloc.mjs +477 -293
- package/src/es/balloc.cjs +383 -234
- package/src/es/balloc.js +1 -1
- package/src/es/balloc.mjs +485 -319
- package/src/jp/balloc.cjs +409 -277
- package/src/jp/balloc.js +1 -1
- package/src/jp/balloc.mjs +501 -351
- package/src/pt/balloc.cjs +212 -62
- package/src/pt/balloc.js +1 -1
- package/src/pt/balloc.mjs +287 -119
- package/src/zh/balloc.cjs +328 -197
- package/src/zh/balloc.js +1 -1
- package/src/zh/balloc.mjs +423 -275
- package/types/ar/balloc.d.mts +390 -278
- package/types/en/balloc.d.mts +319 -206
- package/types/es/balloc.d.mts +306 -210
- package/types/jp/balloc.d.mts +332 -253
- package/types/pt/balloc.d.mts +143 -46
- package/types/zh/balloc.d.mts +262 -184
- package/img/pix.png +0 -0
package/src/en/balloc.cjs
CHANGED
|
@@ -43,6 +43,8 @@ var ADDRESS_OFFSET = 0 >>> 0;
|
|
|
43
43
|
var OCCUPIED_OFFSET = 4 >>> 0;
|
|
44
44
|
var CAPACITY_OFFSET = 8 >>> 0;
|
|
45
45
|
var COOKIE_OFFSET = 9 >>> 0;
|
|
46
|
+
var FLAGS_OFFSET = 10 >>> 0;
|
|
47
|
+
var VIEW_LOCK = 1 >>> 0;
|
|
46
48
|
var ROTATE_32BITS = 4294967296;
|
|
47
49
|
var CLAMP_11BITS = 2047 >>> 0;
|
|
48
50
|
var isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataView);
|
|
@@ -54,24 +56,24 @@ var Balloc = class {
|
|
|
54
56
|
* - `dataBus`: stores the data of allocated blocks;
|
|
55
57
|
* - `controlBus`: stores headers and other control metadata.
|
|
56
58
|
*
|
|
57
|
-
* Each block has a fixed-size header (`HEADER_SIZE`) and is
|
|
58
|
-
* identified by a 43-bit logical handle, composed
|
|
59
|
-
* and
|
|
59
|
+
* Each block has a fixed-size header (`HEADER_SIZE`) and is
|
|
60
|
+
* identified externally by a 43-bit logical handle, composed
|
|
61
|
+
* of an 11-bit generation and its 32-bit header address.
|
|
60
62
|
*
|
|
61
|
-
* The capacity of each block is rounded up to the next power of
|
|
62
|
-
* in elements, respecting the configured bucket
|
|
63
|
+
* The capacity of each block is rounded up to the next power of
|
|
64
|
+
* two in elements, respecting the width of the configured bucket.
|
|
63
65
|
*
|
|
64
66
|
* @param {number} [size=65535] - Initial capacity of the `dataBus`, in bytes.
|
|
65
67
|
* Must be a safe integer in the range [2, 0x80000000].
|
|
66
|
-
* @param {BucketType} [bucketSize="32u"] -
|
|
68
|
+
* @param {BucketType} [bucketSize="32u"] - Identifier for the bucket type and size
|
|
67
69
|
* (e.g., '32u' for Uint32, '64f' for Float64, '8i' for Int8).
|
|
68
70
|
* @param {number} [metadataSize=1024] - Initial capacity of the `controlBus`,
|
|
69
71
|
* in bytes. Must be a safe integer in the range
|
|
70
72
|
* [HEADER_SIZE, 0x80000000].
|
|
71
73
|
*
|
|
72
|
-
* @throws {RangeError} If `size` is
|
|
74
|
+
* @throws {RangeError} If `size` is outside the allowed range.
|
|
73
75
|
* @throws {RangeError} If `bucketSize` is not a valid bucket identifier.
|
|
74
|
-
* @throws {RangeError} If `metadataSize` is
|
|
76
|
+
* @throws {RangeError} If `metadataSize` is outside the allowed range.
|
|
75
77
|
*/
|
|
76
78
|
constructor(size = 65535, bucketSize = "32u", metadataSize = 1024) {
|
|
77
79
|
if (!Number.isSafeInteger(size) || size < 2 || size > 2147483648) throw new RangeError(`Parameter size must be in range [2, 0x80000000] but size is ${size}`);
|
|
@@ -85,8 +87,8 @@ var Balloc = class {
|
|
|
85
87
|
this.bucketArray = globalThis[`${this.bucketType}Array`];
|
|
86
88
|
this.wordSize = this.bucketSize >>> 3;
|
|
87
89
|
this.wordShift = this.bucketSize >>> 4;
|
|
88
|
-
this.size = Math.max(1 << 32 - Math.clz32(size) >>> 0, this.wordSize);
|
|
89
|
-
this.headerSize = 1 << 32 - Math.clz32(metadataSize) >>> 0;
|
|
90
|
+
this.size = Math.max(1 << 32 - Math.clz32(size - 1) >>> 0, this.wordSize);
|
|
91
|
+
this.headerSize = 1 << 32 - Math.clz32(metadataSize - 1) >>> 0;
|
|
90
92
|
this.dataPointer = 0 >>> 0;
|
|
91
93
|
this.controlPointer = 0 >>> 0;
|
|
92
94
|
this.echoPointer = 0 >>> 0;
|
|
@@ -113,24 +115,26 @@ var Balloc = class {
|
|
|
113
115
|
* Allocates a new memory block.
|
|
114
116
|
*
|
|
115
117
|
* The requested capacity is converted to the smallest power of two that
|
|
116
|
-
*
|
|
117
|
-
* of recyclable regions with the corresponding capacity; if
|
|
118
|
-
* region
|
|
118
|
+
* can accommodate `size` elements. The block is obtained preferentially from the list
|
|
119
|
+
* of recyclable regions with the corresponding capacity; if a compatible
|
|
120
|
+
* region does not exist, the block is reserved at the top of the `dataBus`.
|
|
119
121
|
*
|
|
120
122
|
* Next, a header is obtained from the control space. Previously freed headers
|
|
121
|
-
* can be reused via the internal recycling stack;
|
|
122
|
-
* is expanded when necessary.
|
|
123
|
+
* can be reused via the internal recycling stack;
|
|
124
|
+
* otherwise, the `controlBus` is expanded when necessary.
|
|
123
125
|
*
|
|
124
126
|
* The header receives the address, capacity, initial occupancy (zero), and
|
|
125
127
|
* a validation cookie. The returned value encodes the allocation generation
|
|
126
128
|
* and the header address:
|
|
127
129
|
*
|
|
128
|
-
* [11 bits
|
|
130
|
+
* [11 generation bits][32 header address bits]
|
|
129
131
|
*
|
|
130
132
|
* @param {number} [size=16] - Minimum number of elements the block must
|
|
131
133
|
* accommodate. Must be a safe integer in the range [2, 0x80000000].
|
|
132
134
|
* @returns {number} Handle identifying the allocated block.
|
|
133
|
-
* @throws {RangeError} If `size` is
|
|
135
|
+
* @throws {RangeError} If `size` is outside the allowed range.
|
|
136
|
+
* @throws {Error} If it is necessary to expand the `dataBus` while any
|
|
137
|
+
* block is borrowed via withView().
|
|
134
138
|
*/
|
|
135
139
|
alloc(size = 16) {
|
|
136
140
|
if (!Number.isSafeInteger(size) || size < 2 || size > 2147483648) throw new RangeError(`Parameter size must be in range [2, 0x80000000] but size is ${size}`);
|
|
@@ -156,31 +160,35 @@ var Balloc = class {
|
|
|
156
160
|
this.controlAdapter.setUint32(headerAddress + OCCUPIED_OFFSET, 0, IS_LITTLE_ENDIAN);
|
|
157
161
|
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, capacity);
|
|
158
162
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, this.bakeCookie(headerAddress, ++this.generation));
|
|
163
|
+
this.controlBus[headerAddress + FLAGS_OFFSET] = 0;
|
|
159
164
|
return (this.generation & CLAMP_11BITS) * ROTATE_32BITS + headerAddress;
|
|
160
165
|
}
|
|
161
166
|
/**
|
|
162
167
|
* Expands a block's capacity to the next power of two.
|
|
163
168
|
*
|
|
164
169
|
* When the block is located at the current top of the `dataBus`, expansion
|
|
165
|
-
* occurs in-place: only its capacity and `dataPointer` are
|
|
170
|
+
* occurs in-place: only its capacity and `dataPointer` are
|
|
171
|
+
* updated.
|
|
166
172
|
*
|
|
167
173
|
* If another region exists after the block, a new region with double the
|
|
168
|
-
* capacity is obtained from the recyclable space or allocated at the top of
|
|
169
|
-
*
|
|
174
|
+
* capacity is obtained from the recyclable space or allocated at the top of the `dataBus`.
|
|
175
|
+
* The currently occupied elements are then copied to the new region,
|
|
170
176
|
* and the previous region is returned to the recycling list.
|
|
171
177
|
*
|
|
172
|
-
* The handle remains valid because its header is preserved
|
|
173
|
-
* address and capacity recorded
|
|
178
|
+
* The handle remains valid because its header is preserved and only the
|
|
179
|
+
* address and capacity recorded in it are updated.
|
|
174
180
|
*
|
|
175
|
-
* @param {number} handler - Handle of the block to
|
|
181
|
+
* @param {number} handler - Handle of the block to be expanded.
|
|
176
182
|
* @returns {void}
|
|
177
|
-
* @throws {Error} If the handle is invalid or
|
|
178
|
-
*
|
|
183
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block
|
|
184
|
+
* is borrowed via withView(). Also occurs if expansion requires
|
|
185
|
+
* dataBus remapping while another block is borrowed.
|
|
186
|
+
* @throws {AggregateError} If the capacity has reached the maximum
|
|
179
187
|
* supported limit.
|
|
180
188
|
*/
|
|
181
189
|
realloc(handler) {
|
|
182
190
|
const generation = handler / ROTATE_32BITS & CLAMP_11BITS;
|
|
183
|
-
|
|
191
|
+
const headerAddress = handler >>> 0;
|
|
184
192
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
185
193
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
186
194
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
@@ -188,49 +196,53 @@ var Balloc = class {
|
|
|
188
196
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
189
197
|
throw new Error("Invalid or corrupted handle");
|
|
190
198
|
}
|
|
199
|
+
this.assertNotBorrowed(headerAddress);
|
|
191
200
|
if (capacity >= 31) throw new AggregateError([new Error("Can\xB4t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
192
201
|
const newCapacity = capacity + 1;
|
|
193
202
|
const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
|
|
194
203
|
const oldBlockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
195
204
|
if (dataAddress + oldBlockSize === this.dataPointer) {
|
|
196
205
|
while (dataAddress + newBlockSize > this.size) this.grow();
|
|
197
|
-
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity
|
|
206
|
+
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
198
207
|
this.dataPointer = dataAddress + newBlockSize;
|
|
199
208
|
return;
|
|
200
209
|
}
|
|
201
|
-
this.recyclePush(oldBlockSize, dataAddress);
|
|
202
210
|
let newAddress = this.recyclePop(newBlockSize);
|
|
203
211
|
if (newAddress === UNKNOWN) {
|
|
204
212
|
while (this.dataPointer + newBlockSize > this.size) this.grow();
|
|
205
213
|
newAddress = this.dataPointer;
|
|
206
214
|
this.dataPointer += newBlockSize;
|
|
207
215
|
}
|
|
208
|
-
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
209
216
|
this.dataBus.copyWithin(newAddress, dataAddress, dataAddress + occupied * this.wordSize);
|
|
210
217
|
this.controlAdapter.setUint32(headerAddress + ADDRESS_OFFSET, newAddress, IS_LITTLE_ENDIAN);
|
|
218
|
+
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
219
|
+
this.recyclePush(oldBlockSize, dataAddress);
|
|
211
220
|
}
|
|
212
221
|
/**
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
222
|
+
* Frees a memory block.
|
|
223
|
+
*
|
|
224
|
+
* The block's header is invalidated by zeroing its cookie, and its
|
|
225
|
+
* address is registered for later reuse. The data region
|
|
226
|
+
* is also returned to the recycling system, except when the block is
|
|
227
|
+
* exactly at the top of the `dataBus`; in that case, the `dataPointer` is simply
|
|
228
|
+
* rolled back, eliminating the region without creating an entry in the recycling
|
|
229
|
+
* list.
|
|
230
|
+
*
|
|
231
|
+
* @param {number} handler - Handle of the block to be freed.
|
|
232
|
+
* @returns {void}
|
|
233
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block
|
|
234
|
+
* is borrowed via withView().
|
|
235
|
+
*/
|
|
225
236
|
free(handler) {
|
|
226
237
|
const generation = handler / ROTATE_32BITS & CLAMP_11BITS;
|
|
227
|
-
|
|
238
|
+
const headerAddress = handler >>> 0;
|
|
228
239
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
229
240
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
230
241
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
231
242
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
232
243
|
throw new Error("Invalid or corrupted handle");
|
|
233
244
|
}
|
|
245
|
+
this.assertNotBorrowed(headerAddress);
|
|
234
246
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
235
247
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
236
248
|
if (this.echoPointer + 4 <= this.controlBus.byteLength) {
|
|
@@ -249,13 +261,13 @@ var Balloc = class {
|
|
|
249
261
|
* Inserts a free region into the recycling list.
|
|
250
262
|
*
|
|
251
263
|
* The list is organized by capacity in bytes. For each capacity, the
|
|
252
|
-
* stored value is a `Uint32Array` whose zero position contains the
|
|
253
|
-
* of registered addresses
|
|
254
|
-
*
|
|
264
|
+
* stored value is a `Uint32Array` whose zero position contains the quantity
|
|
265
|
+
* of registered addresses and subsequent positions contain the addresses of
|
|
266
|
+
* the free regions:
|
|
255
267
|
*
|
|
256
268
|
* [count, address1, address2, ...]
|
|
257
269
|
*
|
|
258
|
-
* When the page becomes full, its buffer is expanded to double
|
|
270
|
+
* When the page becomes full, its buffer is expanded to double its current
|
|
259
271
|
* capacity.
|
|
260
272
|
*
|
|
261
273
|
* @param {number} capacity - Capacity of the free region, in bytes.
|
|
@@ -288,7 +300,7 @@ var Balloc = class {
|
|
|
288
300
|
*
|
|
289
301
|
* @param {number} capacity - Desired capacity, in bytes.
|
|
290
302
|
* @returns {number} Initial address of a recyclable region or `UNKNOWN`
|
|
291
|
-
* when
|
|
303
|
+
* when a region with the requested capacity does not exist.
|
|
292
304
|
* @private
|
|
293
305
|
*/
|
|
294
306
|
recyclePop(capacity) {
|
|
@@ -305,25 +317,28 @@ var Balloc = class {
|
|
|
305
317
|
* Compacts the `dataBus` using the ReBump algorithm.
|
|
306
318
|
*
|
|
307
319
|
* Sequentially traverses the headers registered in the `controlBus` and copies
|
|
308
|
-
* still-valid regions to a new `dataBus`, eliminating free spaces
|
|
309
|
-
*
|
|
320
|
+
* still-valid regions to a new `dataBus`, eliminating free spaces
|
|
321
|
+
* between them. Each surviving block's address is updated in its
|
|
322
|
+
* respective header.
|
|
310
323
|
*
|
|
311
|
-
* The order of blocks in the `controlBus` is preserved.
|
|
312
|
-
* block is also fully preserved, including its reserved capacity
|
|
313
|
-
* occupancy.
|
|
324
|
+
* The order of blocks in the `controlBus` is preserved. The internal data of
|
|
325
|
+
* each block is also fully preserved, including its reserved capacity
|
|
326
|
+
* and current occupancy.
|
|
314
327
|
*
|
|
315
|
-
*
|
|
328
|
+
* Upon completion of compaction:
|
|
316
329
|
* - all recyclable regions are eliminated;
|
|
317
330
|
* - `dataPointer` points to the first byte after the last live block;
|
|
318
|
-
* - `dataBus` represents a contiguous heap for active blocks.
|
|
331
|
+
* - the `dataBus` represents a contiguous heap for active blocks.
|
|
319
332
|
*
|
|
320
|
-
* Control complexity is O(n), where `n` is the number of
|
|
321
|
-
* The effective
|
|
322
|
-
* belonging to live blocks.
|
|
333
|
+
* Control complexity is O(n), where `n` is the number of traversed
|
|
334
|
+
* headers. The effective memory movement cost also depends on the volume
|
|
335
|
+
* of bytes belonging to live blocks.
|
|
323
336
|
*
|
|
324
337
|
* @returns {void}
|
|
338
|
+
* @throws {Error} If any block is borrowed via withView().
|
|
325
339
|
*/
|
|
326
340
|
gc() {
|
|
341
|
+
this.assertNoActiveViews();
|
|
327
342
|
const newDataBus = new Uint8Array(this.dataBus.buffer.byteLength);
|
|
328
343
|
this.recycle.clear();
|
|
329
344
|
this.dataPointer = 0 >>> 0;
|
|
@@ -348,15 +363,17 @@ var Balloc = class {
|
|
|
348
363
|
* Expands the `dataBus` to double its current capacity.
|
|
349
364
|
*
|
|
350
365
|
* The underlying buffer is transferred to a larger `ArrayBuffer`, preserving
|
|
351
|
-
* existing data. All views associated with the buffer are recreated
|
|
352
|
-
* the new storage.
|
|
366
|
+
* existing data. All views associated with the buffer are recreated
|
|
367
|
+
* to reference the new storage.
|
|
353
368
|
*
|
|
354
369
|
* @returns {void}
|
|
355
|
-
* @throws {
|
|
356
|
-
*
|
|
370
|
+
* @throws {Error} If any block is borrowed via withView().
|
|
371
|
+
* @throws {RangeError} If the `dataBus` capacity has already exceeded the
|
|
372
|
+
* maximum supported limit.
|
|
357
373
|
* @private
|
|
358
374
|
*/
|
|
359
375
|
grow() {
|
|
376
|
+
this.assertNoActiveViews();
|
|
360
377
|
if (this.size > 2147483648) throw new RangeError("Data bus capacity exceeded");
|
|
361
378
|
const newSize = this.size * 2 >>> 0;
|
|
362
379
|
const newBuffer = this.dataBus.buffer.transfer(newSize);
|
|
@@ -372,12 +389,12 @@ var Balloc = class {
|
|
|
372
389
|
* Expands the `controlBus` to double its current capacity.
|
|
373
390
|
*
|
|
374
391
|
* The underlying buffer is transferred to a larger `ArrayBuffer`, preserving
|
|
375
|
-
* existing headers. The `controlAdapter` is recreated to reference
|
|
376
|
-
* storage.
|
|
392
|
+
* existing headers. The `controlAdapter` is recreated to reference
|
|
393
|
+
* the new storage.
|
|
377
394
|
*
|
|
378
395
|
* @returns {void}
|
|
379
|
-
* @throws {RangeError} If the current `controlBus` capacity has
|
|
380
|
-
* the maximum supported limit.
|
|
396
|
+
* @throws {RangeError} If the current `controlBus` capacity has already
|
|
397
|
+
* exceeded the maximum supported limit.
|
|
381
398
|
* @private
|
|
382
399
|
*/
|
|
383
400
|
headerGrow() {
|
|
@@ -389,39 +406,42 @@ var Balloc = class {
|
|
|
389
406
|
}
|
|
390
407
|
// ========== STACK OPERATIONS ==========
|
|
391
408
|
/**
|
|
392
|
-
* Appends an element to the end of the
|
|
409
|
+
* Appends an element to the end of the sequence occupied by the block.
|
|
393
410
|
*
|
|
394
411
|
* The element is written at the first free position after the last occupied
|
|
395
|
-
* element. Block occupancy is incremented only when space is available
|
|
396
|
-
* its capacity.
|
|
412
|
+
* element. Block occupancy is incremented only when space is available
|
|
413
|
+
* within its capacity.
|
|
397
414
|
*
|
|
398
415
|
* @param {number} handler - Block handle.
|
|
399
|
-
* @param {number} value - Value to
|
|
416
|
+
* @param {number} value - Value to be inserted.
|
|
400
417
|
* @returns {boolean} `true` if the value was inserted; `false` if the block
|
|
401
|
-
*
|
|
402
|
-
* @throws {Error} If the handle is invalid or
|
|
418
|
+
* is already out of available space.
|
|
419
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
420
|
+
*
|
|
403
421
|
*/
|
|
404
422
|
push(handler, value) {
|
|
405
423
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
424
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
406
425
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
407
426
|
this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
408
427
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
|
|
409
428
|
return true;
|
|
410
429
|
}
|
|
411
430
|
/**
|
|
412
|
-
* Removes
|
|
431
|
+
* Removes the last occupied element from the block and returns it.
|
|
413
432
|
*
|
|
414
|
-
* Occupancy is decremented before reading the removed element.
|
|
433
|
+
* Occupancy is decremented before reading the removed element. The corresponding
|
|
415
434
|
* bytes are not cleared and remain in the allocated region, but cease to be
|
|
416
435
|
* part of the occupied sequence.
|
|
417
436
|
*
|
|
418
437
|
* @param {number} handler - Block handle.
|
|
419
438
|
* @returns {number|undefined} Removed value or `undefined` if the block
|
|
420
439
|
* is empty.
|
|
421
|
-
* @throws {Error} If the handle is invalid or
|
|
440
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
422
441
|
*/
|
|
423
442
|
pop(handler) {
|
|
424
443
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
444
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
425
445
|
if (meta.occupied === 0) return void 0;
|
|
426
446
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
427
447
|
return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
|
|
@@ -443,21 +463,22 @@ var Balloc = class {
|
|
|
443
463
|
/**
|
|
444
464
|
* Inserts an element at the beginning of the occupied sequence.
|
|
445
465
|
*
|
|
446
|
-
* Currently occupied elements are shifted one position
|
|
447
|
-
* is written
|
|
448
|
-
* within the block's capacity.
|
|
466
|
+
* Currently occupied elements are shifted forward by one position and
|
|
467
|
+
* `value` is written at the first position. The operation requires a free
|
|
468
|
+
* position within the block's capacity.
|
|
449
469
|
*
|
|
450
|
-
* The operation
|
|
470
|
+
* The operation cost is proportional to the number of occupied elements,
|
|
451
471
|
* as data needs to be shifted.
|
|
452
472
|
*
|
|
453
473
|
* @param {number} handler - Block handle.
|
|
454
|
-
* @param {number} value - Value to
|
|
474
|
+
* @param {number} value - Value to be inserted.
|
|
455
475
|
* @returns {boolean} `true` if the value was inserted; `false` if the block
|
|
456
476
|
* is full.
|
|
457
|
-
* @throws {Error} If the handle is invalid or
|
|
477
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
458
478
|
*/
|
|
459
479
|
nudge(handler, value) {
|
|
460
480
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
481
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
461
482
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
462
483
|
this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
|
|
463
484
|
this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
|
|
@@ -465,20 +486,21 @@ var Balloc = class {
|
|
|
465
486
|
return true;
|
|
466
487
|
}
|
|
467
488
|
/**
|
|
468
|
-
* Removes
|
|
489
|
+
* Removes the first occupied element from the block and returns it.
|
|
469
490
|
*
|
|
470
|
-
* Remaining elements are shifted one position toward the beginning
|
|
471
|
-
* occupied region. The
|
|
491
|
+
* Remaining elements are shifted one position toward the beginning
|
|
492
|
+
* of the occupied region. The physical capacity of the block remains unchanged.
|
|
472
493
|
*
|
|
473
|
-
* The operation
|
|
494
|
+
* The operation cost is proportional to the number of shifted elements.
|
|
474
495
|
*
|
|
475
496
|
* @param {number} handler - Block handle.
|
|
476
497
|
* @returns {number|undefined} First removed value or `undefined` if the
|
|
477
498
|
* block is empty.
|
|
478
|
-
* @throws {Error} If the handle is invalid or
|
|
499
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
479
500
|
*/
|
|
480
501
|
pull(handler) {
|
|
481
502
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
503
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
482
504
|
if (meta.occupied === 0) return void 0;
|
|
483
505
|
const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
484
506
|
this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
|
|
@@ -500,15 +522,15 @@ var Balloc = class {
|
|
|
500
522
|
}
|
|
501
523
|
// ========== RANDOM ACCESS ==========
|
|
502
524
|
/**
|
|
503
|
-
* Checks if a value is present among the
|
|
525
|
+
* Checks if a value is present among the occupied elements of the block.
|
|
504
526
|
*
|
|
505
527
|
* The search is performed directly on the auxiliary `TypedArray` and considers
|
|
506
528
|
* only the currently occupied range, not the entire reserved physical capacity.
|
|
507
529
|
*
|
|
508
530
|
* @param {number} handler - Block handle.
|
|
509
531
|
* @param {number} value - Value to search for.
|
|
510
|
-
* @returns {boolean} `true` if `value` is present among the occupied
|
|
511
|
-
* otherwise, `false`.
|
|
532
|
+
* @returns {boolean} `true` if `value` is present among the occupied
|
|
533
|
+
* elements; otherwise, `false`.
|
|
512
534
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
513
535
|
*/
|
|
514
536
|
has(handler, value) {
|
|
@@ -517,6 +539,53 @@ var Balloc = class {
|
|
|
517
539
|
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
518
540
|
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).includes(value);
|
|
519
541
|
}
|
|
542
|
+
/**
|
|
543
|
+
* Locates the first occurrence of an element in the block's occupied sequence.
|
|
544
|
+
*
|
|
545
|
+
* The search is performed in ascending order of indices, using the `indexOf`
|
|
546
|
+
* operation of the underlying TypedArray. The search exclusively considers
|
|
547
|
+
* occupied elements, ignoring the block's unused capacity.
|
|
548
|
+
*
|
|
549
|
+
* The starting search position can be specified by `fromIndex`,
|
|
550
|
+
* following TypedArray indexing rules.
|
|
551
|
+
*
|
|
552
|
+
* @param {number} handler - Block handle.
|
|
553
|
+
* @param {number|bigint} searchElement - Element to locate.
|
|
554
|
+
* @param {number} [fromIndex=0] - Index from which to start the search.
|
|
555
|
+
* @returns {number} Index of the first occurrence or `-1` if the
|
|
556
|
+
* element is not found or if the block is empty.
|
|
557
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
558
|
+
*/
|
|
559
|
+
indexOf(handler, searchElement, fromIndex = 0) {
|
|
560
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
561
|
+
if (meta.occupied === 0) return -1;
|
|
562
|
+
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
563
|
+
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).indexOf(searchElement, fromIndex);
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Locates the last occurrence of an element in the block's occupied sequence.
|
|
567
|
+
*
|
|
568
|
+
* The search is performed in descending order of indices, using the `lastIndexOf`
|
|
569
|
+
* operation of the underlying TypedArray. The search exclusively considers
|
|
570
|
+
* occupied elements, ignoring the block's unused capacity.
|
|
571
|
+
*
|
|
572
|
+
* The starting search position can be specified by `fromIndex`,
|
|
573
|
+
* following TypedArray indexing rules. By default, the search starts
|
|
574
|
+
* at the last occupied element.
|
|
575
|
+
*
|
|
576
|
+
* @param {number} handler - Block handle.
|
|
577
|
+
* @param {number|bigint} searchElement - Element to locate.
|
|
578
|
+
* @param {number} [fromIndex=-1] - Index from which to start the search.
|
|
579
|
+
* @returns {number} Index of the last occurrence or `-1` if the
|
|
580
|
+
* element is not found or if the block is empty.
|
|
581
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
582
|
+
*/
|
|
583
|
+
lastIndexOf(handler, searchElement, fromIndex = -1) {
|
|
584
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
585
|
+
if (meta.occupied === 0) return -1;
|
|
586
|
+
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
587
|
+
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).lastIndexOf(searchElement, fromIndex);
|
|
588
|
+
}
|
|
520
589
|
/**
|
|
521
590
|
* Returns the occupied element at a zero-based index.
|
|
522
591
|
*
|
|
@@ -545,29 +614,32 @@ var Balloc = class {
|
|
|
545
614
|
* @param {number} value - New value.
|
|
546
615
|
* @returns {boolean} `true` if the value was written; `false` if the index
|
|
547
616
|
* is outside the occupied region.
|
|
548
|
-
* @throws {Error} If the handle is invalid or
|
|
617
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
549
618
|
*/
|
|
550
619
|
set(handler, index, value) {
|
|
551
620
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
621
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
552
622
|
if (index < 0 || index >= meta.occupied) return false;
|
|
553
623
|
this.writer(meta.dataAddress + index * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
554
624
|
return true;
|
|
555
625
|
}
|
|
556
626
|
// ============= MISCELLANEOUS ===============
|
|
557
627
|
/**
|
|
558
|
-
*
|
|
628
|
+
* Zeroes block occupancy and fills its entire physical capacity with
|
|
629
|
+
* a single value.
|
|
559
630
|
*
|
|
560
|
-
*
|
|
561
|
-
* After the operation, the block is considered
|
|
562
|
-
* used for filling.
|
|
631
|
+
* The fill covers all available slots, not just currently
|
|
632
|
+
* occupied elements. After the operation, the block is considered
|
|
633
|
+
* empty regardless of the value used for filling.
|
|
563
634
|
*
|
|
564
635
|
* @param {number} handler - Block handle.
|
|
565
636
|
* @param {number} [value=0] - Value used to fill all slots.
|
|
566
637
|
* @returns {void}
|
|
567
|
-
* @throws {Error} If the handle is invalid or
|
|
638
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
568
639
|
*/
|
|
569
640
|
clear(handler, value = 0) {
|
|
570
641
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
642
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
571
643
|
const slots = meta.byteCapacity / this.wordSize;
|
|
572
644
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
573
645
|
this.auxiliary.fill(value, rawAddress, rawAddress + slots);
|
|
@@ -576,25 +648,27 @@ var Balloc = class {
|
|
|
576
648
|
/**
|
|
577
649
|
* Fills a range of block slots with a value.
|
|
578
650
|
*
|
|
579
|
-
* The range uses zero-based indices and follows the convention
|
|
580
|
-
*
|
|
581
|
-
* filling extends to the end of the block's
|
|
651
|
+
* The range uses zero-based indices and follows the convention
|
|
652
|
+
* `[start, end)`, therefore `start` is inclusive and `end` is exclusive.
|
|
653
|
+
* When `end` is not provided, filling extends to the end of the block's
|
|
654
|
+
* physical capacity.
|
|
582
655
|
*
|
|
583
|
-
* Block occupancy becomes `end`, including when the filled range
|
|
584
|
-
*
|
|
656
|
+
* Block occupancy becomes `end`, including when the filled range
|
|
657
|
+
* extends beyond previous occupancy.
|
|
585
658
|
*
|
|
586
659
|
* @param {number} handler - Block handle.
|
|
587
660
|
* @param {number} value - Value used for filling.
|
|
588
661
|
* @param {number} [start=0] - Initial index, inclusive.
|
|
589
|
-
* @param {number|null} [end=null] - Final index, exclusive. When
|
|
590
|
-
* `null`, uses the total
|
|
662
|
+
* @param {number|null} [end=null] - Final index, exclusive. When
|
|
663
|
+
* omitted or `null`, uses the block's total capacity.
|
|
591
664
|
* @returns {void}
|
|
592
|
-
* @throws {Error} If the handle is invalid or
|
|
593
|
-
* @throws {RangeError} If `start` or `end` is
|
|
594
|
-
* `start > end`.
|
|
665
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
666
|
+
* @throws {RangeError} If `start` or `end` is outside capacity limits,
|
|
667
|
+
* or if `start > end`.
|
|
595
668
|
*/
|
|
596
669
|
fill(handler, value, start = 0, end = null) {
|
|
597
670
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
671
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
598
672
|
const slots = meta.byteCapacity / this.wordSize;
|
|
599
673
|
const lastIndex = end ?? slots;
|
|
600
674
|
if (start > lastIndex) {
|
|
@@ -614,15 +688,16 @@ var Balloc = class {
|
|
|
614
688
|
* Fills all physical slots of the block with a value and marks the entire
|
|
615
689
|
* capacity as occupied.
|
|
616
690
|
*
|
|
617
|
-
* Unlike `clear()`,
|
|
691
|
+
* Unlike `clear()`, this operation results in a fully occupied block.
|
|
618
692
|
*
|
|
619
693
|
* @param {number} handler - Block handle.
|
|
620
694
|
* @param {number} value - Value used for filling.
|
|
621
695
|
* @returns {void}
|
|
622
|
-
* @throws {Error} If the handle is invalid or
|
|
696
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
623
697
|
*/
|
|
624
698
|
fillAll(handler, value) {
|
|
625
699
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
700
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
626
701
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
627
702
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
628
703
|
this.auxiliary.fill(value, rawAddress, rawAddress + capacityElements);
|
|
@@ -631,25 +706,25 @@ var Balloc = class {
|
|
|
631
706
|
/**
|
|
632
707
|
* Copies elements from an `Array` or `TypedArray` to the beginning of the block.
|
|
633
708
|
*
|
|
634
|
-
* The copied quantity is limited by the block's capacity. Occupancy is
|
|
635
|
-
* to the greater value between
|
|
636
|
-
* copied; therefore, a call to `put()` does not reduce existing
|
|
637
|
-
* receiving a smaller sequence.
|
|
709
|
+
* The copied quantity is limited by the block's capacity. Occupancy is
|
|
710
|
+
* updated to the greater value between previous occupancy and the quantity
|
|
711
|
+
* effectively copied; therefore, a call to `put()` does not reduce existing
|
|
712
|
+
* occupancy when receiving a smaller sequence.
|
|
638
713
|
*
|
|
639
|
-
* For `Array`, only the first
|
|
640
|
-
* a source subarray is used without intermediate copying.
|
|
714
|
+
* For `Array`, only the first elements that fit are copied.
|
|
715
|
+
* For `TypedArray`, a source subarray is used without intermediate copying.
|
|
641
716
|
*
|
|
642
717
|
* @param {number} handler - Block handle.
|
|
643
718
|
* @param {Array|TypedArray} data - Sequence of values to copy.
|
|
644
719
|
* @returns {void}
|
|
645
|
-
* @throws {Error} If the handle is invalid or
|
|
720
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
646
721
|
* @throws {TypeError} If `data` is neither an `Array` nor a `TypedArray`.
|
|
647
722
|
*/
|
|
648
723
|
put(handler, data) {
|
|
649
724
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
725
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
650
726
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
651
727
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
652
|
-
meta.occupied = Math.max(length, meta.occupied);
|
|
653
728
|
if (Array.isArray(data)) {
|
|
654
729
|
this.auxiliary.set(data.slice(0, length), rawAddress);
|
|
655
730
|
} else if (isTypedArray(data)) {
|
|
@@ -658,20 +733,21 @@ var Balloc = class {
|
|
|
658
733
|
const actual = data?.constructor?.name ?? (data === null ? "null" : typeof data);
|
|
659
734
|
throw new TypeError(`data must be Array or TypedArray, but ${actual} was given.`);
|
|
660
735
|
}
|
|
661
|
-
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied, IS_LITTLE_ENDIAN);
|
|
736
|
+
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, Math.max(length, meta.occupied), IS_LITTLE_ENDIAN);
|
|
662
737
|
}
|
|
663
738
|
/**
|
|
664
739
|
* Returns a copy of the elements currently occupied by the block.
|
|
665
740
|
*
|
|
666
|
-
* The result is a `TypedArray` of the same type configured by
|
|
667
|
-
* Unoccupied physical capacity is not included in the result.
|
|
741
|
+
* The result is a `TypedArray` of the same type configured by
|
|
742
|
+
* `bucketSize`. Unoccupied physical capacity is not included in the result.
|
|
668
743
|
*
|
|
669
|
-
* The operation does not directly expose internal storage: the method
|
|
670
|
-
* `TypedArray.prototype.slice()`, producing a new view with
|
|
744
|
+
* The operation does not directly expose internal storage: the method
|
|
745
|
+
* uses `TypedArray.prototype.slice()`, producing a new view with
|
|
746
|
+
* independent storage.
|
|
671
747
|
*
|
|
672
748
|
* @param {number} handler - Block handle.
|
|
673
|
-
* @returns {TypedArray} Copy of
|
|
674
|
-
* block is empty, returns an empty `TypedArray` of the configured type.
|
|
749
|
+
* @returns {TypedArray} Copy of elements occupied by the block. When
|
|
750
|
+
* the block is empty, returns an empty `TypedArray` of the configured type.
|
|
675
751
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
676
752
|
*/
|
|
677
753
|
pick(handler) {
|
|
@@ -682,12 +758,42 @@ var Balloc = class {
|
|
|
682
758
|
}
|
|
683
759
|
return this.auxiliary.slice(rawAddress, rawAddress + meta.occupied);
|
|
684
760
|
}
|
|
761
|
+
/**
|
|
762
|
+
* Executes a callback over a direct view of the block's occupied region.
|
|
763
|
+
*
|
|
764
|
+
* The view shares Balloc's internal storage and performs no copying.
|
|
765
|
+
* The block is protected against incompatible operations during the callback.
|
|
766
|
+
*
|
|
767
|
+
* The callback must be synchronous. The received view must not be stored,
|
|
768
|
+
* returned, or used after the callback terminates. This restriction
|
|
769
|
+
* also applies to subviews derived from the original view.
|
|
770
|
+
*
|
|
771
|
+
* @param {number} handler - Block handle.
|
|
772
|
+
* @param {Function} callback - Function receiving the shared TypedArray.
|
|
773
|
+
* @returns {*} Return value produced by the callback.
|
|
774
|
+
* @throws {TypeError} If callback is not a function.
|
|
775
|
+
* @throws {Error} If the handle is invalid or already borrowed.
|
|
776
|
+
*/
|
|
777
|
+
withView(handler, callback) {
|
|
778
|
+
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
779
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
780
|
+
const flagsAddress = meta.headerAddress + FLAGS_OFFSET;
|
|
781
|
+
const flags = this.controlBus[flagsAddress];
|
|
782
|
+
if (flags & VIEW_LOCK) throw new Error("Block is already borrowed");
|
|
783
|
+
this.controlBus[flagsAddress] = flags | VIEW_LOCK;
|
|
784
|
+
try {
|
|
785
|
+
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
786
|
+
return callback(this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied));
|
|
787
|
+
} finally {
|
|
788
|
+
this.controlBus[flagsAddress] &= ~VIEW_LOCK;
|
|
789
|
+
}
|
|
790
|
+
}
|
|
685
791
|
// ======== CAPACITY IN SLOTS ==========
|
|
686
792
|
/**
|
|
687
793
|
* Returns the physical capacity of the block in bytes.
|
|
688
794
|
*
|
|
689
|
-
* Capacity corresponds to the space reserved for all slots in the
|
|
690
|
-
* regardless of how many elements are currently occupied.
|
|
795
|
+
* Capacity corresponds to the space reserved for all slots in the
|
|
796
|
+
* block, regardless of how many elements are currently occupied.
|
|
691
797
|
*
|
|
692
798
|
* @param {number} handler - Block handle.
|
|
693
799
|
* @returns {number} Physical capacity of the block, in bytes.
|
|
@@ -709,9 +815,9 @@ var Balloc = class {
|
|
|
709
815
|
return meta.occupied;
|
|
710
816
|
}
|
|
711
817
|
/**
|
|
712
|
-
* Returns the capacity
|
|
818
|
+
* Returns the block's capacity in number of elements.
|
|
713
819
|
*
|
|
714
|
-
* The value corresponds to the total
|
|
820
|
+
* The value corresponds to the total number of physically reserved slots,
|
|
715
821
|
* not the currently occupied quantity.
|
|
716
822
|
*
|
|
717
823
|
* @param {number} handler - Block handle.
|
|
@@ -722,33 +828,51 @@ var Balloc = class {
|
|
|
722
828
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
723
829
|
return 1 << meta.capacityExponent >>> 0;
|
|
724
830
|
}
|
|
725
|
-
// ========== Inter-block Operations =============
|
|
726
831
|
/**
|
|
727
|
-
*
|
|
728
|
-
*
|
|
729
|
-
* Copying begins at the first element of `source` and the first slot of
|
|
730
|
-
* `destiny`. The effectively copied quantity is limited by the number of occupied
|
|
731
|
-
* elements in the source, the destination's physical capacity, and the limit
|
|
732
|
-
* specified in `slots`.
|
|
832
|
+
* Checks if the memory block has reached its allocation capacity.
|
|
733
833
|
*
|
|
734
|
-
*
|
|
735
|
-
*
|
|
834
|
+
* A block is considered full when the number of occupied elements
|
|
835
|
+
* equals its total allocated capacity, leaving no positions available
|
|
836
|
+
* to store new elements.
|
|
736
837
|
*
|
|
737
|
-
*
|
|
738
|
-
*
|
|
739
|
-
*
|
|
740
|
-
* @param {number} source - Source block handle.
|
|
741
|
-
* @param {number} destiny - Destination block handle.
|
|
742
|
-
* @param {number} [slots=UNKNOWN] - Maximum number of elements to copy.
|
|
743
|
-
* `UNKNOWN` represents the sentinel value used not to impose an additional limit.
|
|
744
|
-
* @returns {Uint8Array} The `dataBus` after the operation.
|
|
745
|
-
* @throws {Error} If `source` or `destiny` is invalid or corrupted.
|
|
838
|
+
* @param {number} handler - Block handle.
|
|
839
|
+
* @returns {boolean} `true` if the block is full; otherwise, `false`.
|
|
840
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
746
841
|
*/
|
|
747
|
-
|
|
842
|
+
isFull(handler) {
|
|
843
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
844
|
+
return (1 << meta.capacityExponent >>> 0) - meta.occupied === 0;
|
|
845
|
+
}
|
|
846
|
+
// ========== Operations between blocks=============
|
|
847
|
+
/**
|
|
848
|
+
* Copies occupied elements from one block to another.
|
|
849
|
+
*
|
|
850
|
+
* Copying starts at the first element of `source` and the first slot of
|
|
851
|
+
* `destination`. The quantity effectively copied is limited by the number of
|
|
852
|
+
* occupied elements in the source, the physical capacity of the destination, and the
|
|
853
|
+
* limit specified in `slots`.
|
|
854
|
+
*
|
|
855
|
+
* Destination occupancy is increased, when necessary, to cover the
|
|
856
|
+
* copied elements. Destination capacity is unchanged.
|
|
857
|
+
*
|
|
858
|
+
* The operation uses `Uint8Array.prototype.copyWithin()` on the `dataBus`,
|
|
859
|
+
* therefore the return value is the `Uint8Array` representing the data bus.
|
|
860
|
+
*
|
|
861
|
+
* @param {number} source - Source block handle.
|
|
862
|
+
* @param {number} destination - Destination block handle.
|
|
863
|
+
* @param {number} [slots=UNKNOWN] - Maximum number of elements to copy.
|
|
864
|
+
* `UNKNOWN` represents the sentinel value used not to impose an
|
|
865
|
+
* additional limit.
|
|
866
|
+
* @returns {Uint8Array} The `dataBus` after the operation.
|
|
867
|
+
* @throws {Error} If handles are invalid, corrupted,
|
|
868
|
+
* or the destination block is borrowed via withView().
|
|
869
|
+
*/
|
|
870
|
+
copy(source, destination, slots = UNKNOWN) {
|
|
748
871
|
const metaA = {};
|
|
749
872
|
const metaB = {};
|
|
750
873
|
this.extractMetadata(source, metaA);
|
|
751
|
-
this.extractMetadata(
|
|
874
|
+
this.extractMetadata(destination, metaB);
|
|
875
|
+
this.assertNotBorrowed(metaB.headerAddress);
|
|
752
876
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
753
877
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
754
878
|
const newOccupied = Math.max(metaB.occupied, slotsToCopy);
|
|
@@ -758,26 +882,29 @@ var Balloc = class {
|
|
|
758
882
|
/**
|
|
759
883
|
* Copies a region of elements within the same block.
|
|
760
884
|
*
|
|
761
|
-
* Indices are zero-based and follow the semantics of
|
|
762
|
-
* `target` identifies the destination position,
|
|
763
|
-
* position, and `length` the maximum
|
|
885
|
+
* Indices are zero-based and follow the semantics of
|
|
886
|
+
* `Array.prototype.copyWithin()`: `target` identifies the destination position,
|
|
887
|
+
* `start` the starting source position, and `length` the maximum quantity of
|
|
888
|
+
* elements to copy.
|
|
764
889
|
*
|
|
765
|
-
* Negative indices are interpreted from the end of the block's capacity.
|
|
766
|
-
* this normalization, indices are clamped to physical capacity.
|
|
767
|
-
* is also limited by effectively occupied elements
|
|
890
|
+
* Negative indices are interpreted from the end of the block's capacity.
|
|
891
|
+
* After this normalization, indices are clamped to physical capacity.
|
|
892
|
+
* The copied quantity is also limited by effectively occupied elements
|
|
893
|
+
* and available space starting from `target`.
|
|
768
894
|
*
|
|
769
895
|
* The operation supports overlapping regions and can increase block occupancy
|
|
770
|
-
* when the
|
|
896
|
+
* when the destination range exceeds previous occupancy.
|
|
771
897
|
*
|
|
772
898
|
* @param {number} handler - Block handle.
|
|
773
899
|
* @param {number} target - Initial destination index.
|
|
774
900
|
* @param {number} start - Initial source index.
|
|
775
901
|
* @param {number} length - Maximum number of elements to copy.
|
|
776
902
|
* @returns {Uint8Array} The `dataBus` after the operation.
|
|
777
|
-
* @throws {Error} If the handle is invalid or
|
|
903
|
+
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
778
904
|
*/
|
|
779
905
|
copyWithin(handler, target, start, length) {
|
|
780
906
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
907
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
781
908
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
782
909
|
target = Math.trunc(target);
|
|
783
910
|
start = Math.trunc(start);
|
|
@@ -800,46 +927,45 @@ var Balloc = class {
|
|
|
800
927
|
* Appends occupied elements of `source` to the end of `target`.
|
|
801
928
|
*
|
|
802
929
|
* The copied quantity is limited by the number of occupied elements in the
|
|
803
|
-
* source and
|
|
930
|
+
* source and remaining free space in the destination's capacity.
|
|
804
931
|
*
|
|
805
932
|
* The operation supports `target` and `source` referencing the same block.
|
|
806
|
-
* In
|
|
807
|
-
* `copyWithin()` overlap semantics.
|
|
933
|
+
* In this case, copying is performed over the block's own region and
|
|
934
|
+
* preserves `copyWithin()` overlap semantics.
|
|
808
935
|
*
|
|
809
|
-
* @param {number} target - Handle of the block that will receive
|
|
810
|
-
* @param {number} source - Handle of the block whose elements will be
|
|
936
|
+
* @param {number} target - Handle of the block that will receive elements.
|
|
937
|
+
* @param {number} source - Handle of the block whose elements will be
|
|
938
|
+
* appended.
|
|
811
939
|
* @returns {number} Number of elements effectively appended.
|
|
812
|
-
* @throws {Error} If
|
|
940
|
+
* @throws {Error} If handles are invalid, corrupted,
|
|
941
|
+
* or the destination block is borrowed via withView().
|
|
813
942
|
*/
|
|
814
943
|
append(target, source) {
|
|
815
944
|
const metaTarget = {};
|
|
816
945
|
const metaSource = {};
|
|
817
946
|
this.extractMetadata(target, metaTarget);
|
|
818
947
|
this.extractMetadata(source, metaSource);
|
|
948
|
+
this.assertNotBorrowed(metaTarget.headerAddress);
|
|
819
949
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
820
950
|
const count = Math.min(available, metaSource.occupied);
|
|
821
951
|
if (count === 0) return 0;
|
|
822
952
|
const targetAddress = metaTarget.dataAddress + metaTarget.occupied * this.wordSize;
|
|
823
953
|
const sourceAddress = metaSource.dataAddress;
|
|
824
|
-
|
|
825
|
-
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
826
|
-
} else {
|
|
827
|
-
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
828
|
-
}
|
|
954
|
+
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
829
955
|
this.controlAdapter.setUint32(metaTarget.headerAddress + OCCUPIED_OFFSET, metaTarget.occupied + count, IS_LITTLE_ENDIAN);
|
|
830
956
|
return count;
|
|
831
957
|
}
|
|
832
958
|
/**
|
|
833
959
|
* Creates an independent copy of a block.
|
|
834
960
|
*
|
|
835
|
-
* The new block has the same physical capacity and occupancy as the
|
|
836
|
-
* All bytes in the reserved region, including currently
|
|
837
|
-
* are copied.
|
|
961
|
+
* The new block has the same physical capacity and occupancy as the
|
|
962
|
+
* original block. All bytes in the reserved region, including currently
|
|
963
|
+
* unoccupied positions, are copied.
|
|
838
964
|
*
|
|
839
|
-
* The new block receives a new handle and remains subject to normal
|
|
840
|
-
* and recycling rules.
|
|
965
|
+
* The new block receives a new handle and remains subject to normal
|
|
966
|
+
* allocation and recycling rules.
|
|
841
967
|
*
|
|
842
|
-
* @param {number} handler - Handle of the block to
|
|
968
|
+
* @param {number} handler - Handle of the block to be cloned.
|
|
843
969
|
* @returns {number} Handle of the new block.
|
|
844
970
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
845
971
|
*/
|
|
@@ -855,24 +981,28 @@ var Balloc = class {
|
|
|
855
981
|
/**
|
|
856
982
|
* Swaps the data regions associated with two blocks.
|
|
857
983
|
*
|
|
858
|
-
* The operation swaps region addresses, capacities, and occupancies
|
|
859
|
-
* Handles remain unchanged:
|
|
860
|
-
*
|
|
861
|
-
*
|
|
984
|
+
* The operation swaps region addresses, capacities, and occupancies
|
|
985
|
+
* between headers. Handles remain unchanged:
|
|
986
|
+
* after the operation, each handle continues identifying its own
|
|
987
|
+
* header, but that header now describes the region previously
|
|
988
|
+
* belonging to the other block.
|
|
862
989
|
*
|
|
863
|
-
* No data is copied in the `dataBus`; only the metadata of the two
|
|
864
|
-
* is modified.
|
|
990
|
+
* No data is copied in the `dataBus`; only the metadata of the two
|
|
991
|
+
* headers is modified.
|
|
865
992
|
*
|
|
866
993
|
* @param {number} source - Handle of the first block.
|
|
867
994
|
* @param {number} target - Handle of the second block.
|
|
868
995
|
* @returns {void}
|
|
869
|
-
* @throws {Error} If
|
|
996
|
+
* @throws {Error} If handles are invalid, corrupted,
|
|
997
|
+
* or either block is borrowed via withView().
|
|
870
998
|
*/
|
|
871
999
|
swap(source, target) {
|
|
872
1000
|
const metaA = {};
|
|
873
1001
|
const metaB = {};
|
|
874
1002
|
this.extractMetadata(source, metaA);
|
|
875
1003
|
this.extractMetadata(target, metaB);
|
|
1004
|
+
this.assertNotBorrowed(metaA.headerAddress);
|
|
1005
|
+
this.assertNotBorrowed(metaB.headerAddress);
|
|
876
1006
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
877
1007
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
878
1008
|
this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
|
|
@@ -884,11 +1014,11 @@ var Balloc = class {
|
|
|
884
1014
|
/**
|
|
885
1015
|
* Serializes the content and metadata of a memory block.
|
|
886
1016
|
*
|
|
887
|
-
* The method extracts occupied data
|
|
888
|
-
*
|
|
889
|
-
*
|
|
1017
|
+
* The method extracts occupied block data via `pick()`,
|
|
1018
|
+
* groups it alongside capacity and word type information
|
|
1019
|
+
* into a data envelope, and encodes it using the CBOR format.
|
|
890
1020
|
*
|
|
891
|
-
* @param {number} handler - Handle of the block to
|
|
1021
|
+
* @param {number} handler - Handle of the block to be serialized.
|
|
892
1022
|
* @returns {Uint8Array} A buffer containing the serialized block data.
|
|
893
1023
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
894
1024
|
*/
|
|
@@ -916,13 +1046,13 @@ var Balloc = class {
|
|
|
916
1046
|
* Restores a memory block from serialized data.
|
|
917
1047
|
*
|
|
918
1048
|
* Decodes the CBOR envelope, validates whether the word type of the serialized
|
|
919
|
-
* data is compatible with the current allocator instance, and allocates
|
|
920
|
-
* block with the
|
|
921
|
-
* data and occupancy are restored in the new block.
|
|
1049
|
+
* data is compatible with the current allocator instance, and allocates
|
|
1050
|
+
* a new block with the capacity required to accommodate the content.
|
|
1051
|
+
* Then, original data and occupancy are restored in the new block.
|
|
922
1052
|
*
|
|
923
1053
|
* @param {Uint8Array|ArrayBuffer} serialized - Buffer containing serialized data.
|
|
924
1054
|
* @returns {number} Handle of the newly recreated block.
|
|
925
|
-
* @throws {Error} If the decoded data's word type differs from the
|
|
1055
|
+
* @throws {Error} If the decoded data's word type differs from the allocator's configuration.
|
|
926
1056
|
*/
|
|
927
1057
|
load(serialized) {
|
|
928
1058
|
if (!(serialized instanceof ArrayBuffer) && !(serialized instanceof Uint8Array)) {
|
|
@@ -962,22 +1092,54 @@ var Balloc = class {
|
|
|
962
1092
|
return handler;
|
|
963
1093
|
}
|
|
964
1094
|
// ============= UTILITIES ==============
|
|
1095
|
+
/**
|
|
1096
|
+
* Prevents modification operations on a borrowed block.
|
|
1097
|
+
*
|
|
1098
|
+
* Checks the VIEW_LOCK flag in the header associated with the provided address.
|
|
1099
|
+
*
|
|
1100
|
+
* @param {number} headerAddress - Address of the header in controlBus.
|
|
1101
|
+
* @returns {void}
|
|
1102
|
+
* @throws {Error} If the block is borrowed via withView().
|
|
1103
|
+
* @private
|
|
1104
|
+
*/
|
|
1105
|
+
assertNotBorrowed(headerAddress) {
|
|
1106
|
+
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1107
|
+
throw new Error("Block is borrowed");
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* Prevents operations that replace or remap the dataBus
|
|
1112
|
+
* while any segment is borrowed.
|
|
1113
|
+
*
|
|
1114
|
+
* @returns {void}
|
|
1115
|
+
* @throws {Error} If any block is borrowed via withView().
|
|
1116
|
+
* @private
|
|
1117
|
+
*/
|
|
1118
|
+
assertNoActiveViews() {
|
|
1119
|
+
for (let i = 0; i < this.controlPointer; i += HEADER_SIZE) {
|
|
1120
|
+
if (this.controlBus[i + COOKIE_OFFSET] === 0) continue;
|
|
1121
|
+
if (this.controlBus[i + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1122
|
+
throw new Error("Cannot remap memory while a block is borrowed");
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
965
1126
|
/**
|
|
966
1127
|
* [INTERNAL USE]
|
|
967
|
-
* Decodes
|
|
1128
|
+
* Decodes metadata associated with a handle.
|
|
968
1129
|
*
|
|
969
1130
|
* The handle is split into generation and header address. The cookie stored
|
|
970
1131
|
* in the header is then recalculated to validate the combination; only after
|
|
971
|
-
* this validation
|
|
1132
|
+
* this validation are metadata read.
|
|
972
1133
|
*
|
|
973
|
-
* Decoded fields are written to the `out` object, which is returned
|
|
974
|
-
* itself. The object can be reused
|
|
1134
|
+
* Decoded fields are written to the `out` object, which is returned
|
|
1135
|
+
* by the function itself. The object can be reused between calls to avoid
|
|
1136
|
+
* new allocations.
|
|
975
1137
|
*
|
|
976
|
-
* @param {number} handler - Handle to
|
|
977
|
-
* @param {Metadata} out - Object that will receive
|
|
1138
|
+
* @param {number} handler - Handle to be decoded and validated.
|
|
1139
|
+
* @param {Metadata} out - Object that will receive decoded metadata.
|
|
978
1140
|
* @returns {Metadata} The same reference provided in `out`.
|
|
979
|
-
* @throws {Error} If the header cookie does not match the handle or
|
|
980
|
-
* handle does not identify a valid header.
|
|
1141
|
+
* @throws {Error} If the header cookie does not match the handle or
|
|
1142
|
+
* if the handle does not identify a valid header.
|
|
981
1143
|
* @private
|
|
982
1144
|
*/
|
|
983
1145
|
extractMetadata(handler, out) {
|
|
@@ -995,20 +1157,22 @@ var Balloc = class {
|
|
|
995
1157
|
}
|
|
996
1158
|
/**
|
|
997
1159
|
* [INTERNAL USE]
|
|
998
|
-
* Calculates
|
|
1160
|
+
* Calculates a header's validation cookie.
|
|
999
1161
|
*
|
|
1000
|
-
* The cookie is derived from the header address and the 11
|
|
1001
|
-
* fast hash.
|
|
1162
|
+
* The cookie is derived from the header address and the 11 generation bits via
|
|
1163
|
+
* fast hash.
|
|
1164
|
+
* The result is reduced to 8 bits.
|
|
1002
1165
|
*
|
|
1003
|
-
* The value zero is reserved as an invalid header indicator; therefore,
|
|
1004
|
-
* the calculation
|
|
1166
|
+
* The value zero is reserved as an invalid header indicator; therefore,
|
|
1167
|
+
* when the calculation produces zero, the method returns `1`.
|
|
1005
1168
|
*
|
|
1006
|
-
* The cookie
|
|
1007
|
-
* stale, or corrupted handles within the allocator's
|
|
1169
|
+
* The cookie does not constitute a cryptographic mechanism. Its purpose is
|
|
1170
|
+
* to detect invalid, stale, or corrupted handles within the allocator's
|
|
1171
|
+
* memory management model.
|
|
1008
1172
|
*
|
|
1009
1173
|
* @param {number} addr - Header address.
|
|
1010
|
-
* @param {number} gen - Generation associated with the handle. Only the 11
|
|
1011
|
-
* significant bits are considered.
|
|
1174
|
+
* @param {number} gen - Generation associated with the handle. Only the 11
|
|
1175
|
+
* least significant bits are considered.
|
|
1012
1176
|
* @returns {number} 8-bit cookie in the range [1, 255].
|
|
1013
1177
|
* @private
|
|
1014
1178
|
*/
|
|
@@ -1018,11 +1182,11 @@ var Balloc = class {
|
|
|
1018
1182
|
/**
|
|
1019
1183
|
* Returns the number of regions currently stored in the recycling list.
|
|
1020
1184
|
*
|
|
1021
|
-
* Each region corresponds to a freed block that could not be returned
|
|
1022
|
-
* to the top of the `dataBus` and therefore remains available for
|
|
1023
|
-
* future allocation of identical capacity.
|
|
1185
|
+
* Each region corresponds to a freed block that could not be returned
|
|
1186
|
+
* directly to the top of the `dataBus` and, therefore, remains available for
|
|
1187
|
+
* reuse by a future allocation of identical capacity.
|
|
1024
1188
|
*
|
|
1025
|
-
* @returns {number} Number of free regions
|
|
1189
|
+
* @returns {number} Number of free regions kept in `recycle`.
|
|
1026
1190
|
*/
|
|
1027
1191
|
recycleCount() {
|
|
1028
1192
|
let count = 0;
|
|
@@ -1032,9 +1196,10 @@ var Balloc = class {
|
|
|
1032
1196
|
return count;
|
|
1033
1197
|
}
|
|
1034
1198
|
/**
|
|
1035
|
-
* Returns the total bytes belonging to regions currently stored in the
|
|
1199
|
+
* Returns the total bytes belonging to regions currently stored in the
|
|
1200
|
+
* recycling list.
|
|
1036
1201
|
*
|
|
1037
|
-
* The value is calculated by summing
|
|
1202
|
+
* The value is calculated by summing each free region's capacity multiplied
|
|
1038
1203
|
* by the number of regions registered for that capacity.
|
|
1039
1204
|
*
|
|
1040
1205
|
* @returns {number} Number of bytes occupied by recyclable regions.
|
|
@@ -1047,24 +1212,25 @@ var Balloc = class {
|
|
|
1047
1212
|
return total;
|
|
1048
1213
|
}
|
|
1049
1214
|
/**
|
|
1050
|
-
* Returns the
|
|
1215
|
+
* Returns the quantity of allocated bytes that do not belong to currently
|
|
1216
|
+
* recyclable regions.
|
|
1051
1217
|
*
|
|
1052
|
-
* The value is obtained by subtracting the bytes belonging to free regions
|
|
1053
|
-
* in `recycle` from the total advance of `dataPointer`.
|
|
1218
|
+
* The value is obtained by subtracting the bytes belonging to free regions
|
|
1219
|
+
* kept in `recycle` from the total advance of `dataPointer`.
|
|
1054
1220
|
*
|
|
1055
|
-
* @returns {number} Number of bytes associated with non-recyclable regions
|
|
1056
|
-
* the area already used by
|
|
1221
|
+
* @returns {number} Number of bytes associated with non-recyclable regions
|
|
1222
|
+
* within the area already used by `dataBus`.
|
|
1057
1223
|
*/
|
|
1058
1224
|
get allocatedBytes() {
|
|
1059
1225
|
return this.dataPointer - this.recycleBytes;
|
|
1060
1226
|
}
|
|
1061
1227
|
/**
|
|
1062
|
-
* Returns the
|
|
1228
|
+
* Returns the quantity of bytes not yet used at the top of the `dataBus`.
|
|
1063
1229
|
*
|
|
1064
1230
|
* This space corresponds to the region between `dataPointer` and the end of the
|
|
1065
1231
|
* capacity currently reserved for the buffer.
|
|
1066
1232
|
*
|
|
1067
|
-
* @returns {number} Number of available
|
|
1233
|
+
* @returns {number} Number of bytes available after `dataPointer`.
|
|
1068
1234
|
*/
|
|
1069
1235
|
get unallocatedBytes() {
|
|
1070
1236
|
return this.size - this.dataPointer;
|
|
@@ -1072,13 +1238,13 @@ var Balloc = class {
|
|
|
1072
1238
|
/**
|
|
1073
1239
|
* Returns the ratio between recyclable memory and the used heap area.
|
|
1074
1240
|
*
|
|
1075
|
-
* The metric considers free regions
|
|
1076
|
-
* and uses `dataPointer` as the denominator. When no bytes have been used
|
|
1077
|
-
* the result is zero.
|
|
1241
|
+
* The metric considers free regions kept in `recycle` as fragmentation
|
|
1242
|
+
* and uses `dataPointer` as the denominator. When no bytes have been used
|
|
1243
|
+
* by the heap, the result is zero.
|
|
1078
1244
|
*
|
|
1079
|
-
* The value does not represent the fraction of the entire `dataBus` capacity,
|
|
1080
|
-
*
|
|
1081
|
-
*
|
|
1245
|
+
* The value does not represent the fraction of the entire `dataBus` capacity, but
|
|
1246
|
+
* specifically the proportion of recyclable bytes within the area already traversed
|
|
1247
|
+
* by the bump pointer.
|
|
1082
1248
|
*
|
|
1083
1249
|
* @returns {number} Fragmentation ratio in the range [0, 1] under normal
|
|
1084
1250
|
* operating conditions.
|