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/es/balloc.mjs
CHANGED
|
@@ -104,6 +104,15 @@ const isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataVi
|
|
|
104
104
|
/**
|
|
105
105
|
* Asignador tipo Bump de doble bus con pila de eco y recolección de basura ReBump.
|
|
106
106
|
*/
|
|
107
|
+
/**
|
|
108
|
+
* Contrato de un adaptador síncrono para la ingesta de datos.
|
|
109
|
+
*
|
|
110
|
+
* El adaptador recibe la instancia de Balloc y utiliza su API pública para almacenar los datos, devolviendo el handler que identifica el bloque principal.
|
|
111
|
+
*
|
|
112
|
+
* @typedef {Object} FormatAdapter
|
|
113
|
+
* @property {(allocator: Balloc) => number} load - Carga los datos en el asignador y devuelve el handler correspondiente.
|
|
114
|
+
*/
|
|
115
|
+
|
|
107
116
|
export default class Balloc {
|
|
108
117
|
|
|
109
118
|
/**
|
|
@@ -267,12 +276,15 @@ export default class Balloc {
|
|
|
267
276
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
268
277
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
269
278
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
279
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
270
280
|
|
|
271
281
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
272
282
|
throw new Error("Invalid or corrupted handle");
|
|
273
283
|
}
|
|
274
284
|
|
|
275
|
-
|
|
285
|
+
if (borrowed) {
|
|
286
|
+
throw new Error("Block is borrowed");
|
|
287
|
+
}
|
|
276
288
|
|
|
277
289
|
if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
278
290
|
|
|
@@ -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;
|
|
@@ -351,12 +363,15 @@ export default class Balloc {
|
|
|
351
363
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
352
364
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
353
365
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
366
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
354
367
|
|
|
355
368
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
356
369
|
throw new Error("Invalid or corrupted handle");
|
|
357
370
|
}
|
|
358
371
|
|
|
359
|
-
|
|
372
|
+
if (borrowed) {
|
|
373
|
+
throw new Error("Block is borrowed");
|
|
374
|
+
}
|
|
360
375
|
|
|
361
376
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
362
377
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
@@ -562,7 +577,7 @@ export default class Balloc {
|
|
|
562
577
|
*/
|
|
563
578
|
push(handler, value) {
|
|
564
579
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
565
|
-
|
|
580
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
566
581
|
|
|
567
582
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
568
583
|
|
|
@@ -586,7 +601,7 @@ export default class Balloc {
|
|
|
586
601
|
*/
|
|
587
602
|
pop(handler) {
|
|
588
603
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
589
|
-
|
|
604
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
590
605
|
|
|
591
606
|
if (meta.occupied === 0) return undefined;
|
|
592
607
|
|
|
@@ -630,7 +645,7 @@ export default class Balloc {
|
|
|
630
645
|
*/
|
|
631
646
|
nudge(handler, value) {
|
|
632
647
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
633
|
-
|
|
648
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
634
649
|
|
|
635
650
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
636
651
|
|
|
@@ -656,7 +671,7 @@ export default class Balloc {
|
|
|
656
671
|
*/
|
|
657
672
|
pull(handler) {
|
|
658
673
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
659
|
-
|
|
674
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
660
675
|
|
|
661
676
|
if (meta.occupied === 0) return undefined;
|
|
662
677
|
|
|
@@ -799,7 +814,7 @@ export default class Balloc {
|
|
|
799
814
|
*/
|
|
800
815
|
set(handler, index, value) {
|
|
801
816
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
802
|
-
|
|
817
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
803
818
|
|
|
804
819
|
if (index < 0 || index >= meta.occupied) return false;
|
|
805
820
|
|
|
@@ -824,7 +839,7 @@ export default class Balloc {
|
|
|
824
839
|
*/
|
|
825
840
|
clear(handler, value = 0) {
|
|
826
841
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
827
|
-
|
|
842
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
828
843
|
|
|
829
844
|
const slots = meta.byteCapacity / this.wordSize;
|
|
830
845
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
@@ -857,7 +872,7 @@ export default class Balloc {
|
|
|
857
872
|
*/
|
|
858
873
|
fill(handler, value, start = 0, end = null) {
|
|
859
874
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
860
|
-
|
|
875
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
861
876
|
|
|
862
877
|
const slots = meta.byteCapacity / this.wordSize;
|
|
863
878
|
const lastIndex = end ?? slots;
|
|
@@ -891,7 +906,7 @@ export default class Balloc {
|
|
|
891
906
|
*/
|
|
892
907
|
fillAll(handler, value) {
|
|
893
908
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
894
|
-
|
|
909
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
895
910
|
|
|
896
911
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
897
912
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
@@ -920,7 +935,7 @@ export default class Balloc {
|
|
|
920
935
|
*/
|
|
921
936
|
put(handler, data) {
|
|
922
937
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
923
|
-
|
|
938
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
924
939
|
|
|
925
940
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
926
941
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
@@ -987,26 +1002,30 @@ export default class Balloc {
|
|
|
987
1002
|
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
988
1003
|
|
|
989
1004
|
const lookup = Object.create(null);
|
|
1005
|
+
const acquired = [];
|
|
990
1006
|
const views = [];
|
|
991
1007
|
|
|
992
1008
|
try {
|
|
993
|
-
for(let i = 0; i < lastIndex; i++) {
|
|
994
|
-
const metadata = this.extractMetadata(args[i],
|
|
1009
|
+
for (let i = 0; i < lastIndex; i++) {
|
|
1010
|
+
const metadata = this.extractMetadata(args[i], this.metadata);
|
|
1011
|
+
const headerAddr = metadata.headerAddress;
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
|
|
1015
|
+
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
995
1016
|
|
|
996
|
-
|
|
997
|
-
if (Object.hasOwn(lookup, metadata.headerAddress)) throw new Error("Duplicate handler");
|
|
998
|
-
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
999
|
-
|
|
1000
|
-
this.controlBus[metadata.headerAddress + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
1017
|
+
this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
1001
1018
|
|
|
1002
1019
|
const rawAddress = metadata.dataAddress >>> this.wordShift;
|
|
1003
|
-
lookup[
|
|
1020
|
+
lookup[headerAddr] = true;
|
|
1021
|
+
acquired.push(headerAddr);
|
|
1022
|
+
|
|
1004
1023
|
views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied))
|
|
1005
1024
|
}
|
|
1006
1025
|
return callback(...views);
|
|
1007
1026
|
} finally {
|
|
1008
|
-
for (
|
|
1009
|
-
this.controlBus[
|
|
1027
|
+
for (let i = 0; i < acquired.length; i++) {
|
|
1028
|
+
this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
1010
1029
|
}
|
|
1011
1030
|
}
|
|
1012
1031
|
}
|
|
@@ -1105,7 +1124,7 @@ export default class Balloc {
|
|
|
1105
1124
|
|
|
1106
1125
|
this.extractMetadata(source, metaA);
|
|
1107
1126
|
this.extractMetadata(destination, metaB);
|
|
1108
|
-
|
|
1127
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1109
1128
|
|
|
1110
1129
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
1111
1130
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
@@ -1140,7 +1159,7 @@ export default class Balloc {
|
|
|
1140
1159
|
*/
|
|
1141
1160
|
copyWithin(handler, target, start, length) {
|
|
1142
1161
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
1143
|
-
|
|
1162
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
1144
1163
|
|
|
1145
1164
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
1146
1165
|
target = Math.trunc(target);
|
|
@@ -1191,7 +1210,8 @@ export default class Balloc {
|
|
|
1191
1210
|
|
|
1192
1211
|
this.extractMetadata(target, metaTarget);
|
|
1193
1212
|
this.extractMetadata(source, metaSource);
|
|
1194
|
-
|
|
1213
|
+
if (metaTarget.borrowed) throw new Error("Block is borrowed");
|
|
1214
|
+
|
|
1195
1215
|
|
|
1196
1216
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
1197
1217
|
const count = Math.min(available, metaSource.occupied);
|
|
@@ -1262,8 +1282,9 @@ export default class Balloc {
|
|
|
1262
1282
|
this.extractMetadata(source, metaA);
|
|
1263
1283
|
this.extractMetadata(target, metaB);
|
|
1264
1284
|
|
|
1265
|
-
|
|
1266
|
-
|
|
1285
|
+
if (metaA.borrowed) throw new Error("Block is borrowed");
|
|
1286
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1287
|
+
|
|
1267
1288
|
|
|
1268
1289
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
1269
1290
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
@@ -1377,6 +1398,29 @@ export default class Balloc {
|
|
|
1377
1398
|
return handler;
|
|
1378
1399
|
}
|
|
1379
1400
|
|
|
1401
|
+
// ========== INTEGRACIÓN E INGESTA DE DATOS ==========
|
|
1402
|
+
|
|
1403
|
+
/**
|
|
1404
|
+
* Carga datos mediante un adaptador especializado.
|
|
1405
|
+
*
|
|
1406
|
+
* El adaptador recibe esta instancia y utiliza su API pública para asignar
|
|
1407
|
+
* y completar los bloques necesarios. La organización y la interpretación
|
|
1408
|
+
* de los datos son responsabilidad exclusiva del adaptador; Balloc solo
|
|
1409
|
+
* delega la operación y devuelve el handler resultante.
|
|
1410
|
+
*
|
|
1411
|
+
* La operación es síncrona. El adaptador debe completar la ingesta antes
|
|
1412
|
+
* de retornar y es responsable de revertir sus propias asignaciones si
|
|
1413
|
+
* ocurre un error durante la carga.
|
|
1414
|
+
*
|
|
1415
|
+
* @param {FormatAdapter} adapter - Adaptador que implementa load(allocator).
|
|
1416
|
+
* @returns {number} Handler devuelto por el adaptador.
|
|
1417
|
+
* @throws {TypeError} Si el adaptador no implementa el método load().
|
|
1418
|
+
* @throws {Error} Si el adaptador rechaza la infraestructura suscrita.
|
|
1419
|
+
*/
|
|
1420
|
+
loadData(adapter) {
|
|
1421
|
+
if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
|
|
1422
|
+
return adapter.load(this);
|
|
1423
|
+
}
|
|
1380
1424
|
// ============= UTILIDADES (UTILITIES) ==============
|
|
1381
1425
|
|
|
1382
1426
|
/**
|
|
@@ -1388,7 +1432,8 @@ export default class Balloc {
|
|
|
1388
1432
|
* @returns {void}
|
|
1389
1433
|
* @throws {Error} Si el bloque está prestado mediante `withView()`.
|
|
1390
1434
|
* @private
|
|
1391
|
-
|
|
1435
|
+
* @deprecated
|
|
1436
|
+
*/
|
|
1392
1437
|
assertNotBorrowed(headerAddress) {
|
|
1393
1438
|
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1394
1439
|
throw new Error("Block is borrowed");
|
package/src/jp/balloc.cjs
CHANGED
|
@@ -187,10 +187,13 @@ var Balloc = class {
|
|
|
187
187
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
188
188
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
189
189
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
190
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
190
191
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
191
192
|
throw new Error("Invalid or corrupted handle");
|
|
192
193
|
}
|
|
193
|
-
|
|
194
|
+
if (borrowed) {
|
|
195
|
+
throw new Error("Block is borrowed");
|
|
196
|
+
}
|
|
194
197
|
if (capacity >= 31) throw new AggregateError([new Error("Can\xB4t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
195
198
|
const newCapacity = capacity + 1;
|
|
196
199
|
const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
|
|
@@ -227,7 +230,7 @@ var Balloc = class {
|
|
|
227
230
|
*/
|
|
228
231
|
shrink(handler) {
|
|
229
232
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
230
|
-
|
|
233
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
231
234
|
const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
|
|
232
235
|
if (newCapacity >= meta.capacityExponent) return;
|
|
233
236
|
const oldBlockSize = meta.byteCapacity;
|
|
@@ -251,10 +254,13 @@ var Balloc = class {
|
|
|
251
254
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
252
255
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
253
256
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
257
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
254
258
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
255
259
|
throw new Error("Invalid or corrupted handle");
|
|
256
260
|
}
|
|
257
|
-
|
|
261
|
+
if (borrowed) {
|
|
262
|
+
throw new Error("Block is borrowed");
|
|
263
|
+
}
|
|
258
264
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
259
265
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
260
266
|
if (this.echoPointer + 4 <= this.controlBus.byteLength) {
|
|
@@ -423,7 +429,7 @@ var Balloc = class {
|
|
|
423
429
|
*/
|
|
424
430
|
push(handler, value) {
|
|
425
431
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
426
|
-
|
|
432
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
427
433
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
428
434
|
this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
429
435
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
|
|
@@ -441,7 +447,7 @@ var Balloc = class {
|
|
|
441
447
|
*/
|
|
442
448
|
pop(handler) {
|
|
443
449
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
444
|
-
|
|
450
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
445
451
|
if (meta.occupied === 0) return void 0;
|
|
446
452
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
447
453
|
return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
|
|
@@ -474,7 +480,7 @@ var Balloc = class {
|
|
|
474
480
|
*/
|
|
475
481
|
nudge(handler, value) {
|
|
476
482
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
477
|
-
|
|
483
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
478
484
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
479
485
|
this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
|
|
480
486
|
this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
|
|
@@ -495,7 +501,7 @@ var Balloc = class {
|
|
|
495
501
|
*/
|
|
496
502
|
pull(handler) {
|
|
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 === 0) return void 0;
|
|
500
506
|
const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
501
507
|
this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
|
|
@@ -606,7 +612,7 @@ var Balloc = class {
|
|
|
606
612
|
*/
|
|
607
613
|
set(handler, index, value) {
|
|
608
614
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
609
|
-
|
|
615
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
610
616
|
if (index < 0 || index >= meta.occupied) return false;
|
|
611
617
|
this.writer(meta.dataAddress + index * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
612
618
|
return true;
|
|
@@ -625,7 +631,7 @@ var Balloc = class {
|
|
|
625
631
|
*/
|
|
626
632
|
clear(handler, value = 0) {
|
|
627
633
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
628
|
-
|
|
634
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
629
635
|
const slots = meta.byteCapacity / this.wordSize;
|
|
630
636
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
631
637
|
this.auxiliary.fill(value, rawAddress, rawAddress + slots);
|
|
@@ -652,7 +658,7 @@ var Balloc = class {
|
|
|
652
658
|
*/
|
|
653
659
|
fill(handler, value, start = 0, end = null) {
|
|
654
660
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
655
|
-
|
|
661
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
656
662
|
const slots = meta.byteCapacity / this.wordSize;
|
|
657
663
|
const lastIndex = end ?? slots;
|
|
658
664
|
if (start > lastIndex) {
|
|
@@ -680,7 +686,7 @@ var Balloc = class {
|
|
|
680
686
|
*/
|
|
681
687
|
fillAll(handler, value) {
|
|
682
688
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
683
|
-
|
|
689
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
684
690
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
685
691
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
686
692
|
this.auxiliary.fill(value, rawAddress, rawAddress + capacityElements);
|
|
@@ -704,7 +710,7 @@ var Balloc = class {
|
|
|
704
710
|
*/
|
|
705
711
|
put(handler, data) {
|
|
706
712
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
707
|
-
|
|
713
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
708
714
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
709
715
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
710
716
|
if (Array.isArray(data)) {
|
|
@@ -761,21 +767,24 @@ var Balloc = class {
|
|
|
761
767
|
const callback = args[lastIndex];
|
|
762
768
|
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
763
769
|
const lookup = /* @__PURE__ */ Object.create(null);
|
|
770
|
+
const acquired = [];
|
|
764
771
|
const views = [];
|
|
765
772
|
try {
|
|
766
773
|
for (let i = 0; i < lastIndex; i++) {
|
|
767
|
-
const metadata = this.extractMetadata(args[i],
|
|
768
|
-
|
|
774
|
+
const metadata = this.extractMetadata(args[i], this.metadata);
|
|
775
|
+
const headerAddr = metadata.headerAddress;
|
|
776
|
+
if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
|
|
769
777
|
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
770
|
-
this.controlBus[
|
|
778
|
+
this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
771
779
|
const rawAddress = metadata.dataAddress >>> this.wordShift;
|
|
772
|
-
lookup[
|
|
780
|
+
lookup[headerAddr] = true;
|
|
781
|
+
acquired.push(headerAddr);
|
|
773
782
|
views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
|
|
774
783
|
}
|
|
775
784
|
return callback(...views);
|
|
776
785
|
} finally {
|
|
777
|
-
for (
|
|
778
|
-
this.controlBus[
|
|
786
|
+
for (let i = 0; i < acquired.length; i++) {
|
|
787
|
+
this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
779
788
|
}
|
|
780
789
|
}
|
|
781
790
|
}
|
|
@@ -859,7 +868,7 @@ var Balloc = class {
|
|
|
859
868
|
const metaB = {};
|
|
860
869
|
this.extractMetadata(source, metaA);
|
|
861
870
|
this.extractMetadata(destination, metaB);
|
|
862
|
-
|
|
871
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
863
872
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
864
873
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
865
874
|
const newOccupied = Math.max(metaB.occupied, slotsToCopy);
|
|
@@ -889,7 +898,7 @@ var Balloc = class {
|
|
|
889
898
|
*/
|
|
890
899
|
copyWithin(handler, target, start, length) {
|
|
891
900
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
892
|
-
|
|
901
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
893
902
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
894
903
|
target = Math.trunc(target);
|
|
895
904
|
start = Math.trunc(start);
|
|
@@ -927,7 +936,7 @@ var Balloc = class {
|
|
|
927
936
|
const metaSource = {};
|
|
928
937
|
this.extractMetadata(target, metaTarget);
|
|
929
938
|
this.extractMetadata(source, metaSource);
|
|
930
|
-
|
|
939
|
+
if (metaTarget.borrowed) throw new Error("Block is borrowed");
|
|
931
940
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
932
941
|
const count = Math.min(available, metaSource.occupied);
|
|
933
942
|
if (count === 0) return 0;
|
|
@@ -979,8 +988,8 @@ var Balloc = class {
|
|
|
979
988
|
const metaB = {};
|
|
980
989
|
this.extractMetadata(source, metaA);
|
|
981
990
|
this.extractMetadata(target, metaB);
|
|
982
|
-
|
|
983
|
-
|
|
991
|
+
if (metaA.borrowed) throw new Error("Block is borrowed");
|
|
992
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
984
993
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
985
994
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
986
995
|
this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
|
|
@@ -1069,6 +1078,28 @@ var Balloc = class {
|
|
|
1069
1078
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
|
|
1070
1079
|
return handler;
|
|
1071
1080
|
}
|
|
1081
|
+
// ========== データ統合と取り込み ==========
|
|
1082
|
+
/**
|
|
1083
|
+
* 専用アダプターを通じてデータを読み込みます。
|
|
1084
|
+
*
|
|
1085
|
+
* アダプターはこのインスタンスを受け取り、その公開 API を使用して
|
|
1086
|
+
* 必要なブロックを割り当て、データを書き込みます。データの構成と
|
|
1087
|
+
* 解釈はアダプターのみが担当し、Balloc は処理を委譲して生成された
|
|
1088
|
+
* ハンドラーを返します。
|
|
1089
|
+
*
|
|
1090
|
+
* この操作は同期的に実行されます。アダプターは戻る前にデータの
|
|
1091
|
+
* 取り込みを完了し、読み込みに失敗した場合は自身の割り当てを
|
|
1092
|
+
* 元に戻す責任を負います。
|
|
1093
|
+
*
|
|
1094
|
+
* @param {FormatAdapter} adapter - load(allocator) を実装するアダプター。
|
|
1095
|
+
* @returns {number} アダプターが返すハンドラー。
|
|
1096
|
+
* @throws {TypeError} アダプターが load() メソッドを実装していない場合。
|
|
1097
|
+
* @throws {Error} アダプターが指定されたインフラストラクチャを拒否した場合。
|
|
1098
|
+
*/
|
|
1099
|
+
loadData(adapter) {
|
|
1100
|
+
if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
|
|
1101
|
+
return adapter.load(this);
|
|
1102
|
+
}
|
|
1072
1103
|
// ============= ユーティリティ (UTILITIES) ==============
|
|
1073
1104
|
/**
|
|
1074
1105
|
* 借用中のブロックに対する変更操作を禁止します。
|
|
@@ -1079,7 +1110,8 @@ var Balloc = class {
|
|
|
1079
1110
|
* @returns {void}
|
|
1080
1111
|
* @throws {Error} ブロックが withView() によって借用されている場合。
|
|
1081
1112
|
* @private
|
|
1082
|
-
|
|
1113
|
+
* @deprecated
|
|
1114
|
+
*/
|
|
1083
1115
|
assertNotBorrowed(headerAddress) {
|
|
1084
1116
|
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1085
1117
|
throw new Error("Block is borrowed");
|