balloc 1.3.1 → 1.4.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/CHANGELOG.md +9 -0
- package/README.MD +37 -7
- package/README.ar.md +31 -1
- package/README.en.md +37 -7
- package/README.es.md +31 -1
- package/README.jp.md +31 -1
- package/README.pt.md +36 -7
- package/README.zh.md +31 -1
- package/docs/API /342/200/224 balloc.ar.md" +56 -0
- package/docs/API /342/200/224 balloc.en.md" +55 -0
- package/docs/API /342/200/224 balloc.es.md" +56 -0
- package/docs/API /342/200/224 balloc.jp.md" +56 -0
- package/docs/API /342/200/224 balloc.pt.md" +56 -0
- package/docs/API /342/200/224 balloc.zh.md" +56 -0
- package/package.json +1 -1
- package/src/ar/balloc.cjs +54 -24
- package/src/ar/balloc.js +1 -1
- package/src/ar/balloc.mjs +71 -28
- package/src/en/balloc.cjs +56 -24
- package/src/en/balloc.js +1 -1
- package/src/en/balloc.mjs +73 -28
- package/src/es/balloc.cjs +56 -24
- package/src/es/balloc.js +1 -1
- package/src/es/balloc.mjs +73 -28
- package/src/jp/balloc.cjs +56 -24
- package/src/jp/balloc.js +1 -1
- package/src/jp/balloc.mjs +73 -28
- package/src/pt/balloc.cjs +56 -23
- package/src/pt/balloc.js +1 -1
- package/src/pt/balloc.mjs +79 -28
- package/src/zh/balloc.cjs +54 -24
- package/src/zh/balloc.js +1 -1
- package/src/zh/balloc.mjs +71 -28
- package/types/ar/balloc.d.mts +32 -1
- package/types/en/balloc.d.mts +34 -1
- package/types/es/balloc.d.mts +34 -1
- package/types/jp/balloc.d.mts +34 -1
- package/types/pt/balloc.d.mts +37 -0
- package/types/zh/balloc.d.mts +32 -1
package/src/en/balloc.mjs
CHANGED
|
@@ -104,6 +104,15 @@ const isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataVi
|
|
|
104
104
|
/**
|
|
105
105
|
* Echo Stack Dual Bus Bump Allocator with ReBump GC.
|
|
106
106
|
*/
|
|
107
|
+
/**
|
|
108
|
+
* Synchronous adapter contract for data ingestion.
|
|
109
|
+
*
|
|
110
|
+
* The adapter receives the Balloc instance and uses its public API to store data, returning the handler that identifies the primary block.
|
|
111
|
+
*
|
|
112
|
+
* @typedef {Object} FormatAdapter
|
|
113
|
+
* @property {(allocator: Balloc) => number} load - Loads data into the allocator and returns the corresponding handler.
|
|
114
|
+
*/
|
|
115
|
+
|
|
107
116
|
export default class Balloc {
|
|
108
117
|
|
|
109
118
|
/**
|
|
@@ -268,12 +277,15 @@ export default class Balloc {
|
|
|
268
277
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
269
278
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
270
279
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
280
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
271
281
|
|
|
272
282
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
273
283
|
throw new Error("Invalid or corrupted handle");
|
|
274
284
|
}
|
|
275
285
|
|
|
276
|
-
|
|
286
|
+
if (borrowed) {
|
|
287
|
+
throw new Error("Block is borrowed");
|
|
288
|
+
}
|
|
277
289
|
|
|
278
290
|
if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
279
291
|
|
|
@@ -320,7 +332,7 @@ export default class Balloc {
|
|
|
320
332
|
*/
|
|
321
333
|
shrink(handler) {
|
|
322
334
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
323
|
-
|
|
335
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
324
336
|
|
|
325
337
|
const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
|
|
326
338
|
if (newCapacity >= meta.capacityExponent) return;
|
|
@@ -366,12 +378,15 @@ export default class Balloc {
|
|
|
366
378
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
367
379
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
368
380
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
381
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
369
382
|
|
|
370
383
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
371
384
|
throw new Error("Invalid or corrupted handle");
|
|
372
385
|
}
|
|
373
386
|
|
|
374
|
-
|
|
387
|
+
if (borrowed) {
|
|
388
|
+
throw new Error("Block is borrowed");
|
|
389
|
+
}
|
|
375
390
|
|
|
376
391
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
377
392
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
@@ -576,7 +591,7 @@ export default class Balloc {
|
|
|
576
591
|
*/
|
|
577
592
|
push(handler, value) {
|
|
578
593
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
579
|
-
|
|
594
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
580
595
|
|
|
581
596
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
582
597
|
|
|
@@ -600,7 +615,7 @@ export default class Balloc {
|
|
|
600
615
|
*/
|
|
601
616
|
pop(handler) {
|
|
602
617
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
603
|
-
|
|
618
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
604
619
|
|
|
605
620
|
if (meta.occupied === 0) return undefined;
|
|
606
621
|
|
|
@@ -644,7 +659,7 @@ export default class Balloc {
|
|
|
644
659
|
*/
|
|
645
660
|
nudge(handler, value) {
|
|
646
661
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
647
|
-
|
|
662
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
648
663
|
|
|
649
664
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
650
665
|
|
|
@@ -670,7 +685,7 @@ export default class Balloc {
|
|
|
670
685
|
*/
|
|
671
686
|
pull(handler) {
|
|
672
687
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
673
|
-
|
|
688
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
674
689
|
|
|
675
690
|
if (meta.occupied === 0) return undefined;
|
|
676
691
|
|
|
@@ -813,7 +828,7 @@ export default class Balloc {
|
|
|
813
828
|
*/
|
|
814
829
|
set(handler, index, value) {
|
|
815
830
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
816
|
-
|
|
831
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
817
832
|
|
|
818
833
|
if (index < 0 || index >= meta.occupied) return false;
|
|
819
834
|
|
|
@@ -838,7 +853,7 @@ export default class Balloc {
|
|
|
838
853
|
*/
|
|
839
854
|
clear(handler, value = 0) {
|
|
840
855
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
841
|
-
|
|
856
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
842
857
|
|
|
843
858
|
const slots = meta.byteCapacity / this.wordSize;
|
|
844
859
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
@@ -871,7 +886,7 @@ export default class Balloc {
|
|
|
871
886
|
*/
|
|
872
887
|
fill(handler, value, start = 0, end = null) {
|
|
873
888
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
874
|
-
|
|
889
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
875
890
|
|
|
876
891
|
const slots = meta.byteCapacity / this.wordSize;
|
|
877
892
|
const lastIndex = end ?? slots;
|
|
@@ -904,7 +919,7 @@ export default class Balloc {
|
|
|
904
919
|
*/
|
|
905
920
|
fillAll(handler, value) {
|
|
906
921
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
907
|
-
|
|
922
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
908
923
|
|
|
909
924
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
910
925
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
@@ -932,7 +947,7 @@ export default class Balloc {
|
|
|
932
947
|
*/
|
|
933
948
|
put(handler, data) {
|
|
934
949
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
935
|
-
|
|
950
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
936
951
|
|
|
937
952
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
938
953
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
@@ -999,26 +1014,30 @@ export default class Balloc {
|
|
|
999
1014
|
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
1000
1015
|
|
|
1001
1016
|
const lookup = Object.create(null);
|
|
1017
|
+
const acquired = [];
|
|
1002
1018
|
const views = [];
|
|
1003
1019
|
|
|
1004
1020
|
try {
|
|
1005
|
-
for(let i = 0; i < lastIndex; i++) {
|
|
1006
|
-
const metadata = this.extractMetadata(args[i],
|
|
1021
|
+
for (let i = 0; i < lastIndex; i++) {
|
|
1022
|
+
const metadata = this.extractMetadata(args[i], this.metadata);
|
|
1023
|
+
const headerAddr = metadata.headerAddress;
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
|
|
1027
|
+
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
1007
1028
|
|
|
1008
|
-
|
|
1009
|
-
if (Object.hasOwn(lookup, metadata.headerAddress)) throw new Error("Duplicate handler");
|
|
1010
|
-
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
1011
|
-
|
|
1012
|
-
this.controlBus[metadata.headerAddress + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
1029
|
+
this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
1013
1030
|
|
|
1014
1031
|
const rawAddress = metadata.dataAddress >>> this.wordShift;
|
|
1015
|
-
lookup[
|
|
1032
|
+
lookup[headerAddr] = true;
|
|
1033
|
+
acquired.push(headerAddr);
|
|
1034
|
+
|
|
1016
1035
|
views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied))
|
|
1017
1036
|
}
|
|
1018
1037
|
return callback(...views);
|
|
1019
1038
|
} finally {
|
|
1020
|
-
for (
|
|
1021
|
-
this.controlBus[
|
|
1039
|
+
for (let i = 0; i < acquired.length; i++) {
|
|
1040
|
+
this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
1022
1041
|
}
|
|
1023
1042
|
}
|
|
1024
1043
|
}
|
|
@@ -1117,7 +1136,7 @@ export default class Balloc {
|
|
|
1117
1136
|
|
|
1118
1137
|
this.extractMetadata(source, metaA);
|
|
1119
1138
|
this.extractMetadata(destination, metaB);
|
|
1120
|
-
|
|
1139
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1121
1140
|
|
|
1122
1141
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
1123
1142
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
@@ -1152,7 +1171,7 @@ export default class Balloc {
|
|
|
1152
1171
|
*/
|
|
1153
1172
|
copyWithin(handler, target, start, length) {
|
|
1154
1173
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
1155
|
-
|
|
1174
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
1156
1175
|
|
|
1157
1176
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
1158
1177
|
target = Math.trunc(target);
|
|
@@ -1203,7 +1222,8 @@ export default class Balloc {
|
|
|
1203
1222
|
|
|
1204
1223
|
this.extractMetadata(target, metaTarget);
|
|
1205
1224
|
this.extractMetadata(source, metaSource);
|
|
1206
|
-
|
|
1225
|
+
if (metaTarget.borrowed) throw new Error("Block is borrowed");
|
|
1226
|
+
|
|
1207
1227
|
|
|
1208
1228
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
1209
1229
|
const count = Math.min(available, metaSource.occupied);
|
|
@@ -1274,8 +1294,9 @@ export default class Balloc {
|
|
|
1274
1294
|
this.extractMetadata(source, metaA);
|
|
1275
1295
|
this.extractMetadata(target, metaB);
|
|
1276
1296
|
|
|
1277
|
-
|
|
1278
|
-
|
|
1297
|
+
if (metaA.borrowed) throw new Error("Block is borrowed");
|
|
1298
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1299
|
+
|
|
1279
1300
|
|
|
1280
1301
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
1281
1302
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
@@ -1389,6 +1410,29 @@ export default class Balloc {
|
|
|
1389
1410
|
return handler;
|
|
1390
1411
|
}
|
|
1391
1412
|
|
|
1413
|
+
// ========== INTEGRATION AND DATA INGESTION ==========
|
|
1414
|
+
|
|
1415
|
+
/**
|
|
1416
|
+
* Loads data through a specialized adapter.
|
|
1417
|
+
*
|
|
1418
|
+
* The adapter receives this instance and uses its public API to allocate
|
|
1419
|
+
* and populate the required blocks. Data organization and interpretation
|
|
1420
|
+
* are the adapter's sole responsibility; Balloc only delegates the
|
|
1421
|
+
* operation and returns the resulting handler.
|
|
1422
|
+
*
|
|
1423
|
+
* The operation is synchronous. The adapter must complete ingestion before
|
|
1424
|
+
* returning and is responsible for reverting its own allocations if
|
|
1425
|
+
* loading fails.
|
|
1426
|
+
*
|
|
1427
|
+
* @param {FormatAdapter} adapter - Adapter implementing load(allocator).
|
|
1428
|
+
* @returns {number} Handler returned by the adapter.
|
|
1429
|
+
* @throws {TypeError} If the adapter does not implement load().
|
|
1430
|
+
* @throws {Error} If the adapter rejects the subscribed infrastructure.
|
|
1431
|
+
*/
|
|
1432
|
+
loadData(adapter) {
|
|
1433
|
+
if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
|
|
1434
|
+
return adapter.load(this);
|
|
1435
|
+
}
|
|
1392
1436
|
// ============= UTILITIES ==============
|
|
1393
1437
|
|
|
1394
1438
|
/**
|
|
@@ -1400,7 +1444,8 @@ export default class Balloc {
|
|
|
1400
1444
|
* @returns {void}
|
|
1401
1445
|
* @throws {Error} If the block is borrowed via withView().
|
|
1402
1446
|
* @private
|
|
1403
|
-
|
|
1447
|
+
* @deprecated
|
|
1448
|
+
*/
|
|
1404
1449
|
assertNotBorrowed(headerAddress) {
|
|
1405
1450
|
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1406
1451
|
throw new Error("Block is borrowed");
|
package/src/es/balloc.cjs
CHANGED
|
@@ -192,10 +192,13 @@ var Balloc = class {
|
|
|
192
192
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
193
193
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
194
194
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
195
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
195
196
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
196
197
|
throw new Error("Invalid or corrupted handle");
|
|
197
198
|
}
|
|
198
|
-
|
|
199
|
+
if (borrowed) {
|
|
200
|
+
throw new Error("Block is borrowed");
|
|
201
|
+
}
|
|
199
202
|
if (capacity >= 31) throw new AggregateError([new Error("Can\xB4t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
200
203
|
const newCapacity = capacity + 1;
|
|
201
204
|
const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
|
|
@@ -232,7 +235,7 @@ var Balloc = class {
|
|
|
232
235
|
*/
|
|
233
236
|
shrink(handler) {
|
|
234
237
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
235
|
-
|
|
238
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
236
239
|
const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
|
|
237
240
|
if (newCapacity >= meta.capacityExponent) return;
|
|
238
241
|
const oldBlockSize = meta.byteCapacity;
|
|
@@ -256,10 +259,13 @@ var Balloc = class {
|
|
|
256
259
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
257
260
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
258
261
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
262
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
259
263
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
260
264
|
throw new Error("Invalid or corrupted handle");
|
|
261
265
|
}
|
|
262
|
-
|
|
266
|
+
if (borrowed) {
|
|
267
|
+
throw new Error("Block is borrowed");
|
|
268
|
+
}
|
|
263
269
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
264
270
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
265
271
|
if (this.echoPointer + 4 <= this.controlBus.byteLength) {
|
|
@@ -438,7 +444,7 @@ var Balloc = class {
|
|
|
438
444
|
*/
|
|
439
445
|
push(handler, value) {
|
|
440
446
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
441
|
-
|
|
447
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
442
448
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
443
449
|
this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
444
450
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
|
|
@@ -458,7 +464,7 @@ var Balloc = class {
|
|
|
458
464
|
*/
|
|
459
465
|
pop(handler) {
|
|
460
466
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
461
|
-
|
|
467
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
462
468
|
if (meta.occupied === 0) return void 0;
|
|
463
469
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
464
470
|
return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
|
|
@@ -495,7 +501,7 @@ var Balloc = class {
|
|
|
495
501
|
*/
|
|
496
502
|
nudge(handler, value) {
|
|
497
503
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
498
|
-
|
|
504
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
499
505
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
500
506
|
this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
|
|
501
507
|
this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
|
|
@@ -517,7 +523,7 @@ var Balloc = class {
|
|
|
517
523
|
*/
|
|
518
524
|
pull(handler) {
|
|
519
525
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
520
|
-
|
|
526
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
521
527
|
if (meta.occupied === 0) return void 0;
|
|
522
528
|
const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
523
529
|
this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
|
|
@@ -635,7 +641,7 @@ var Balloc = class {
|
|
|
635
641
|
*/
|
|
636
642
|
set(handler, index, value) {
|
|
637
643
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
638
|
-
|
|
644
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
639
645
|
if (index < 0 || index >= meta.occupied) return false;
|
|
640
646
|
this.writer(meta.dataAddress + index * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
641
647
|
return true;
|
|
@@ -656,7 +662,7 @@ var Balloc = class {
|
|
|
656
662
|
*/
|
|
657
663
|
clear(handler, value = 0) {
|
|
658
664
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
659
|
-
|
|
665
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
660
666
|
const slots = meta.byteCapacity / this.wordSize;
|
|
661
667
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
662
668
|
this.auxiliary.fill(value, rawAddress, rawAddress + slots);
|
|
@@ -685,7 +691,7 @@ var Balloc = class {
|
|
|
685
691
|
*/
|
|
686
692
|
fill(handler, value, start = 0, end = null) {
|
|
687
693
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
688
|
-
|
|
694
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
689
695
|
const slots = meta.byteCapacity / this.wordSize;
|
|
690
696
|
const lastIndex = end ?? slots;
|
|
691
697
|
if (start > lastIndex) {
|
|
@@ -715,7 +721,7 @@ var Balloc = class {
|
|
|
715
721
|
*/
|
|
716
722
|
fillAll(handler, value) {
|
|
717
723
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
718
|
-
|
|
724
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
719
725
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
720
726
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
721
727
|
this.auxiliary.fill(value, rawAddress, rawAddress + capacityElements);
|
|
@@ -741,7 +747,7 @@ var Balloc = class {
|
|
|
741
747
|
*/
|
|
742
748
|
put(handler, data) {
|
|
743
749
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
744
|
-
|
|
750
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
745
751
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
746
752
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
747
753
|
if (Array.isArray(data)) {
|
|
@@ -799,21 +805,24 @@ var Balloc = class {
|
|
|
799
805
|
const callback = args[lastIndex];
|
|
800
806
|
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
801
807
|
const lookup = /* @__PURE__ */ Object.create(null);
|
|
808
|
+
const acquired = [];
|
|
802
809
|
const views = [];
|
|
803
810
|
try {
|
|
804
811
|
for (let i = 0; i < lastIndex; i++) {
|
|
805
|
-
const metadata = this.extractMetadata(args[i],
|
|
806
|
-
|
|
812
|
+
const metadata = this.extractMetadata(args[i], this.metadata);
|
|
813
|
+
const headerAddr = metadata.headerAddress;
|
|
814
|
+
if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
|
|
807
815
|
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
808
|
-
this.controlBus[
|
|
816
|
+
this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
809
817
|
const rawAddress = metadata.dataAddress >>> this.wordShift;
|
|
810
|
-
lookup[
|
|
818
|
+
lookup[headerAddr] = true;
|
|
819
|
+
acquired.push(headerAddr);
|
|
811
820
|
views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
|
|
812
821
|
}
|
|
813
822
|
return callback(...views);
|
|
814
823
|
} finally {
|
|
815
|
-
for (
|
|
816
|
-
this.controlBus[
|
|
824
|
+
for (let i = 0; i < acquired.length; i++) {
|
|
825
|
+
this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
817
826
|
}
|
|
818
827
|
}
|
|
819
828
|
}
|
|
@@ -901,7 +910,7 @@ var Balloc = class {
|
|
|
901
910
|
const metaB = {};
|
|
902
911
|
this.extractMetadata(source, metaA);
|
|
903
912
|
this.extractMetadata(destination, metaB);
|
|
904
|
-
|
|
913
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
905
914
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
906
915
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
907
916
|
const newOccupied = Math.max(metaB.occupied, slotsToCopy);
|
|
@@ -933,7 +942,7 @@ var Balloc = class {
|
|
|
933
942
|
*/
|
|
934
943
|
copyWithin(handler, target, start, length) {
|
|
935
944
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
936
|
-
|
|
945
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
937
946
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
938
947
|
target = Math.trunc(target);
|
|
939
948
|
start = Math.trunc(start);
|
|
@@ -974,7 +983,7 @@ var Balloc = class {
|
|
|
974
983
|
const metaSource = {};
|
|
975
984
|
this.extractMetadata(target, metaTarget);
|
|
976
985
|
this.extractMetadata(source, metaSource);
|
|
977
|
-
|
|
986
|
+
if (metaTarget.borrowed) throw new Error("Block is borrowed");
|
|
978
987
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
979
988
|
const count = Math.min(available, metaSource.occupied);
|
|
980
989
|
if (count === 0) return 0;
|
|
@@ -1030,8 +1039,8 @@ var Balloc = class {
|
|
|
1030
1039
|
const metaB = {};
|
|
1031
1040
|
this.extractMetadata(source, metaA);
|
|
1032
1041
|
this.extractMetadata(target, metaB);
|
|
1033
|
-
|
|
1034
|
-
|
|
1042
|
+
if (metaA.borrowed) throw new Error("Block is borrowed");
|
|
1043
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1035
1044
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
1036
1045
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
1037
1046
|
this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
|
|
@@ -1120,6 +1129,28 @@ var Balloc = class {
|
|
|
1120
1129
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
|
|
1121
1130
|
return handler;
|
|
1122
1131
|
}
|
|
1132
|
+
// ========== INTEGRACIÓN E INGESTA DE DATOS ==========
|
|
1133
|
+
/**
|
|
1134
|
+
* Carga datos mediante un adaptador especializado.
|
|
1135
|
+
*
|
|
1136
|
+
* El adaptador recibe esta instancia y utiliza su API pública para asignar
|
|
1137
|
+
* y completar los bloques necesarios. La organización y la interpretación
|
|
1138
|
+
* de los datos son responsabilidad exclusiva del adaptador; Balloc solo
|
|
1139
|
+
* delega la operación y devuelve el handler resultante.
|
|
1140
|
+
*
|
|
1141
|
+
* La operación es síncrona. El adaptador debe completar la ingesta antes
|
|
1142
|
+
* de retornar y es responsable de revertir sus propias asignaciones si
|
|
1143
|
+
* ocurre un error durante la carga.
|
|
1144
|
+
*
|
|
1145
|
+
* @param {FormatAdapter} adapter - Adaptador que implementa load(allocator).
|
|
1146
|
+
* @returns {number} Handler devuelto por el adaptador.
|
|
1147
|
+
* @throws {TypeError} Si el adaptador no implementa el método load().
|
|
1148
|
+
* @throws {Error} Si el adaptador rechaza la infraestructura suscrita.
|
|
1149
|
+
*/
|
|
1150
|
+
loadData(adapter) {
|
|
1151
|
+
if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
|
|
1152
|
+
return adapter.load(this);
|
|
1153
|
+
}
|
|
1123
1154
|
// ============= UTILIDADES (UTILITIES) ==============
|
|
1124
1155
|
/**
|
|
1125
1156
|
* Impide operaciones de modificación sobre un bloque prestado.
|
|
@@ -1130,7 +1161,8 @@ var Balloc = class {
|
|
|
1130
1161
|
* @returns {void}
|
|
1131
1162
|
* @throws {Error} Si el bloque está prestado mediante `withView()`.
|
|
1132
1163
|
* @private
|
|
1133
|
-
|
|
1164
|
+
* @deprecated
|
|
1165
|
+
*/
|
|
1134
1166
|
assertNotBorrowed(headerAddress) {
|
|
1135
1167
|
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1136
1168
|
throw new Error("Block is borrowed");
|