balloc 1.3.0 → 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 +18 -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 +63 -35
- package/src/ar/balloc.js +1 -1
- package/src/ar/balloc.mjs +78 -39
- package/src/en/balloc.cjs +65 -35
- package/src/en/balloc.js +1 -1
- package/src/en/balloc.mjs +80 -39
- package/src/es/balloc.cjs +65 -35
- package/src/es/balloc.js +1 -1
- package/src/es/balloc.mjs +80 -39
- package/src/jp/balloc.cjs +65 -35
- package/src/jp/balloc.js +1 -1
- package/src/jp/balloc.mjs +80 -39
- package/src/pt/balloc.cjs +67 -35
- package/src/pt/balloc.js +1 -1
- package/src/pt/balloc.mjs +86 -38
- package/src/zh/balloc.cjs +63 -35
- package/src/zh/balloc.js +1 -1
- package/src/zh/balloc.mjs +78 -39
- package/types/ar/balloc.d.mts +36 -1
- package/types/en/balloc.d.mts +38 -1
- package/types/es/balloc.d.mts +38 -1
- package/types/jp/balloc.d.mts +38 -1
- package/types/pt/balloc.d.mts +41 -0
- package/types/zh/balloc.d.mts +36 -1
package/src/en/balloc.mjs
CHANGED
|
@@ -90,6 +90,7 @@ const CLAMP_11BITS = 0x7FF >>> 0;
|
|
|
90
90
|
* @property {number} generation - Generation to which the handle belongs.
|
|
91
91
|
* @property {number} headerAddress - Address of the header within the controlBus.
|
|
92
92
|
* @property {number} occupied - Number of occupied elements in the block.
|
|
93
|
+
* @property {number} borrowed - Flag indicating whether the block has been borrowed by withView().
|
|
93
94
|
*/
|
|
94
95
|
|
|
95
96
|
/**
|
|
@@ -103,6 +104,15 @@ const isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataVi
|
|
|
103
104
|
/**
|
|
104
105
|
* Echo Stack Dual Bus Bump Allocator with ReBump GC.
|
|
105
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
|
+
|
|
106
116
|
export default class Balloc {
|
|
107
117
|
|
|
108
118
|
/**
|
|
@@ -267,12 +277,15 @@ export default class Balloc {
|
|
|
267
277
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
268
278
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
269
279
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
280
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
270
281
|
|
|
271
282
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
272
283
|
throw new Error("Invalid or corrupted handle");
|
|
273
284
|
}
|
|
274
285
|
|
|
275
|
-
|
|
286
|
+
if (borrowed) {
|
|
287
|
+
throw new Error("Block is borrowed");
|
|
288
|
+
}
|
|
276
289
|
|
|
277
290
|
if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
278
291
|
|
|
@@ -319,7 +332,7 @@ export default class Balloc {
|
|
|
319
332
|
*/
|
|
320
333
|
shrink(handler) {
|
|
321
334
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
322
|
-
|
|
335
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
323
336
|
|
|
324
337
|
const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
|
|
325
338
|
if (newCapacity >= meta.capacityExponent) return;
|
|
@@ -365,12 +378,15 @@ export default class Balloc {
|
|
|
365
378
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
366
379
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
367
380
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
381
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
368
382
|
|
|
369
383
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
370
384
|
throw new Error("Invalid or corrupted handle");
|
|
371
385
|
}
|
|
372
386
|
|
|
373
|
-
|
|
387
|
+
if (borrowed) {
|
|
388
|
+
throw new Error("Block is borrowed");
|
|
389
|
+
}
|
|
374
390
|
|
|
375
391
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
376
392
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
@@ -575,7 +591,7 @@ export default class Balloc {
|
|
|
575
591
|
*/
|
|
576
592
|
push(handler, value) {
|
|
577
593
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
578
|
-
|
|
594
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
579
595
|
|
|
580
596
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
581
597
|
|
|
@@ -599,7 +615,7 @@ export default class Balloc {
|
|
|
599
615
|
*/
|
|
600
616
|
pop(handler) {
|
|
601
617
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
602
|
-
|
|
618
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
603
619
|
|
|
604
620
|
if (meta.occupied === 0) return undefined;
|
|
605
621
|
|
|
@@ -643,7 +659,7 @@ export default class Balloc {
|
|
|
643
659
|
*/
|
|
644
660
|
nudge(handler, value) {
|
|
645
661
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
646
|
-
|
|
662
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
647
663
|
|
|
648
664
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
649
665
|
|
|
@@ -669,7 +685,7 @@ export default class Balloc {
|
|
|
669
685
|
*/
|
|
670
686
|
pull(handler) {
|
|
671
687
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
672
|
-
|
|
688
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
673
689
|
|
|
674
690
|
if (meta.occupied === 0) return undefined;
|
|
675
691
|
|
|
@@ -812,7 +828,7 @@ export default class Balloc {
|
|
|
812
828
|
*/
|
|
813
829
|
set(handler, index, value) {
|
|
814
830
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
815
|
-
|
|
831
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
816
832
|
|
|
817
833
|
if (index < 0 || index >= meta.occupied) return false;
|
|
818
834
|
|
|
@@ -837,7 +853,7 @@ export default class Balloc {
|
|
|
837
853
|
*/
|
|
838
854
|
clear(handler, value = 0) {
|
|
839
855
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
840
|
-
|
|
856
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
841
857
|
|
|
842
858
|
const slots = meta.byteCapacity / this.wordSize;
|
|
843
859
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
@@ -870,7 +886,7 @@ export default class Balloc {
|
|
|
870
886
|
*/
|
|
871
887
|
fill(handler, value, start = 0, end = null) {
|
|
872
888
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
873
|
-
|
|
889
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
874
890
|
|
|
875
891
|
const slots = meta.byteCapacity / this.wordSize;
|
|
876
892
|
const lastIndex = end ?? slots;
|
|
@@ -903,7 +919,7 @@ export default class Balloc {
|
|
|
903
919
|
*/
|
|
904
920
|
fillAll(handler, value) {
|
|
905
921
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
906
|
-
|
|
922
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
907
923
|
|
|
908
924
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
909
925
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
@@ -931,7 +947,7 @@ export default class Balloc {
|
|
|
931
947
|
*/
|
|
932
948
|
put(handler, data) {
|
|
933
949
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
934
|
-
|
|
950
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
935
951
|
|
|
936
952
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
937
953
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
@@ -993,37 +1009,35 @@ export default class Balloc {
|
|
|
993
1009
|
*/
|
|
994
1010
|
withView(...args) {
|
|
995
1011
|
if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
|
|
996
|
-
|
|
997
|
-
const callback = args
|
|
1012
|
+
const lastIndex = args.length - 1;
|
|
1013
|
+
const callback = args[lastIndex];
|
|
998
1014
|
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
999
1015
|
|
|
1000
|
-
const
|
|
1001
|
-
const
|
|
1002
|
-
const
|
|
1016
|
+
const lookup = Object.create(null);
|
|
1017
|
+
const acquired = [];
|
|
1018
|
+
const views = [];
|
|
1003
1019
|
|
|
1004
|
-
|
|
1020
|
+
try {
|
|
1021
|
+
for (let i = 0; i < lastIndex; i++) {
|
|
1022
|
+
const metadata = this.extractMetadata(args[i], this.metadata);
|
|
1023
|
+
const headerAddr = metadata.headerAddress;
|
|
1005
1024
|
|
|
1006
|
-
const flags = headers.map(header => this.controlBus[header + FLAGS_OFFSET]);
|
|
1007
1025
|
|
|
1008
|
-
|
|
1026
|
+
if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
|
|
1027
|
+
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
1009
1028
|
|
|
1010
|
-
|
|
1029
|
+
this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
1011
1030
|
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
acquiredCount++;
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
const views = metadata.map(meta => {
|
|
1019
|
-
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
1020
|
-
return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
|
|
1021
|
-
});
|
|
1031
|
+
const rawAddress = metadata.dataAddress >>> this.wordShift;
|
|
1032
|
+
lookup[headerAddr] = true;
|
|
1033
|
+
acquired.push(headerAddr);
|
|
1022
1034
|
|
|
1035
|
+
views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied))
|
|
1036
|
+
}
|
|
1023
1037
|
return callback(...views);
|
|
1024
1038
|
} finally {
|
|
1025
|
-
for (let i = 0; i <
|
|
1026
|
-
this.controlBus[
|
|
1039
|
+
for (let i = 0; i < acquired.length; i++) {
|
|
1040
|
+
this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
1027
1041
|
}
|
|
1028
1042
|
}
|
|
1029
1043
|
}
|
|
@@ -1122,7 +1136,7 @@ export default class Balloc {
|
|
|
1122
1136
|
|
|
1123
1137
|
this.extractMetadata(source, metaA);
|
|
1124
1138
|
this.extractMetadata(destination, metaB);
|
|
1125
|
-
|
|
1139
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1126
1140
|
|
|
1127
1141
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
1128
1142
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
@@ -1157,7 +1171,7 @@ export default class Balloc {
|
|
|
1157
1171
|
*/
|
|
1158
1172
|
copyWithin(handler, target, start, length) {
|
|
1159
1173
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
1160
|
-
|
|
1174
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
1161
1175
|
|
|
1162
1176
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
1163
1177
|
target = Math.trunc(target);
|
|
@@ -1208,7 +1222,8 @@ export default class Balloc {
|
|
|
1208
1222
|
|
|
1209
1223
|
this.extractMetadata(target, metaTarget);
|
|
1210
1224
|
this.extractMetadata(source, metaSource);
|
|
1211
|
-
|
|
1225
|
+
if (metaTarget.borrowed) throw new Error("Block is borrowed");
|
|
1226
|
+
|
|
1212
1227
|
|
|
1213
1228
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
1214
1229
|
const count = Math.min(available, metaSource.occupied);
|
|
@@ -1279,8 +1294,9 @@ export default class Balloc {
|
|
|
1279
1294
|
this.extractMetadata(source, metaA);
|
|
1280
1295
|
this.extractMetadata(target, metaB);
|
|
1281
1296
|
|
|
1282
|
-
|
|
1283
|
-
|
|
1297
|
+
if (metaA.borrowed) throw new Error("Block is borrowed");
|
|
1298
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1299
|
+
|
|
1284
1300
|
|
|
1285
1301
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
1286
1302
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
@@ -1394,6 +1410,29 @@ export default class Balloc {
|
|
|
1394
1410
|
return handler;
|
|
1395
1411
|
}
|
|
1396
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
|
+
}
|
|
1397
1436
|
// ============= UTILITIES ==============
|
|
1398
1437
|
|
|
1399
1438
|
/**
|
|
@@ -1405,7 +1444,8 @@ export default class Balloc {
|
|
|
1405
1444
|
* @returns {void}
|
|
1406
1445
|
* @throws {Error} If the block is borrowed via withView().
|
|
1407
1446
|
* @private
|
|
1408
|
-
|
|
1447
|
+
* @deprecated
|
|
1448
|
+
*/
|
|
1409
1449
|
assertNotBorrowed(headerAddress) {
|
|
1410
1450
|
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1411
1451
|
throw new Error("Block is borrowed");
|
|
@@ -1463,6 +1503,7 @@ export default class Balloc {
|
|
|
1463
1503
|
out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
|
|
1464
1504
|
out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
|
|
1465
1505
|
out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
1506
|
+
out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
|
|
1466
1507
|
|
|
1467
1508
|
return out;
|
|
1468
1509
|
}
|
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)) {
|
|
@@ -795,28 +801,28 @@ var Balloc = class {
|
|
|
795
801
|
*/
|
|
796
802
|
withView(...args) {
|
|
797
803
|
if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
|
|
798
|
-
const
|
|
804
|
+
const lastIndex = args.length - 1;
|
|
805
|
+
const callback = args[lastIndex];
|
|
799
806
|
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
800
|
-
const
|
|
801
|
-
const
|
|
802
|
-
const
|
|
803
|
-
if (new Set(headers).size !== headers.length) throw new Error("Duplicate handler");
|
|
804
|
-
const flags = headers.map((header) => this.controlBus[header + FLAGS_OFFSET]);
|
|
805
|
-
if (flags.some((flag) => flag & VIEW_LOCK)) throw new Error("Block is already borrowed");
|
|
806
|
-
let acquiredCount = 0;
|
|
807
|
+
const lookup = /* @__PURE__ */ Object.create(null);
|
|
808
|
+
const acquired = [];
|
|
809
|
+
const views = [];
|
|
807
810
|
try {
|
|
808
|
-
for (
|
|
809
|
-
this.
|
|
810
|
-
|
|
811
|
+
for (let i = 0; i < lastIndex; i++) {
|
|
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");
|
|
815
|
+
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
816
|
+
this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
817
|
+
const rawAddress = metadata.dataAddress >>> this.wordShift;
|
|
818
|
+
lookup[headerAddr] = true;
|
|
819
|
+
acquired.push(headerAddr);
|
|
820
|
+
views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
|
|
811
821
|
}
|
|
812
|
-
const views = metadata.map((meta) => {
|
|
813
|
-
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
814
|
-
return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
|
|
815
|
-
});
|
|
816
822
|
return callback(...views);
|
|
817
823
|
} finally {
|
|
818
|
-
for (let i = 0; i <
|
|
819
|
-
this.controlBus[
|
|
824
|
+
for (let i = 0; i < acquired.length; i++) {
|
|
825
|
+
this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
820
826
|
}
|
|
821
827
|
}
|
|
822
828
|
}
|
|
@@ -904,7 +910,7 @@ var Balloc = class {
|
|
|
904
910
|
const metaB = {};
|
|
905
911
|
this.extractMetadata(source, metaA);
|
|
906
912
|
this.extractMetadata(destination, metaB);
|
|
907
|
-
|
|
913
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
908
914
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
909
915
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
910
916
|
const newOccupied = Math.max(metaB.occupied, slotsToCopy);
|
|
@@ -936,7 +942,7 @@ var Balloc = class {
|
|
|
936
942
|
*/
|
|
937
943
|
copyWithin(handler, target, start, length) {
|
|
938
944
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
939
|
-
|
|
945
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
940
946
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
941
947
|
target = Math.trunc(target);
|
|
942
948
|
start = Math.trunc(start);
|
|
@@ -977,7 +983,7 @@ var Balloc = class {
|
|
|
977
983
|
const metaSource = {};
|
|
978
984
|
this.extractMetadata(target, metaTarget);
|
|
979
985
|
this.extractMetadata(source, metaSource);
|
|
980
|
-
|
|
986
|
+
if (metaTarget.borrowed) throw new Error("Block is borrowed");
|
|
981
987
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
982
988
|
const count = Math.min(available, metaSource.occupied);
|
|
983
989
|
if (count === 0) return 0;
|
|
@@ -1033,8 +1039,8 @@ var Balloc = class {
|
|
|
1033
1039
|
const metaB = {};
|
|
1034
1040
|
this.extractMetadata(source, metaA);
|
|
1035
1041
|
this.extractMetadata(target, metaB);
|
|
1036
|
-
|
|
1037
|
-
|
|
1042
|
+
if (metaA.borrowed) throw new Error("Block is borrowed");
|
|
1043
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1038
1044
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
1039
1045
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
1040
1046
|
this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
|
|
@@ -1123,6 +1129,28 @@ var Balloc = class {
|
|
|
1123
1129
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
|
|
1124
1130
|
return handler;
|
|
1125
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
|
+
}
|
|
1126
1154
|
// ============= UTILIDADES (UTILITIES) ==============
|
|
1127
1155
|
/**
|
|
1128
1156
|
* Impide operaciones de modificación sobre un bloque prestado.
|
|
@@ -1133,7 +1161,8 @@ var Balloc = class {
|
|
|
1133
1161
|
* @returns {void}
|
|
1134
1162
|
* @throws {Error} Si el bloque está prestado mediante `withView()`.
|
|
1135
1163
|
* @private
|
|
1136
|
-
|
|
1164
|
+
* @deprecated
|
|
1165
|
+
*/
|
|
1137
1166
|
assertNotBorrowed(headerAddress) {
|
|
1138
1167
|
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1139
1168
|
throw new Error("Block is borrowed");
|
|
@@ -1185,6 +1214,7 @@ var Balloc = class {
|
|
|
1185
1214
|
out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
|
|
1186
1215
|
out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
|
|
1187
1216
|
out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
1217
|
+
out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
|
|
1188
1218
|
return out;
|
|
1189
1219
|
}
|
|
1190
1220
|
/**
|