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/src/es/balloc.mjs CHANGED
@@ -90,6 +90,7 @@ const CLAMP_11BITS = 0x7FF >>> 0;
90
90
  * @property {number} generation - Generación a la que pertenece el descriptor.
91
91
  * @property {number} headerAddress - Dirección de la cabecera (header) dentro del bus de control (`controlBus`).
92
92
  * @property {number} occupied - Número de elementos ocupados en el bloque.
93
+ * @property {number} borrowed - Indicador de si el bloque ha sido prestado por withView().
93
94
  */
94
95
 
95
96
  /**
@@ -103,6 +104,15 @@ const isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataVi
103
104
  /**
104
105
  * Asignador tipo Bump de doble bus con pila de eco y recolección de basura ReBump.
105
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
+
106
116
  export default class Balloc {
107
117
 
108
118
  /**
@@ -266,12 +276,15 @@ export default class Balloc {
266
276
  const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
267
277
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
268
278
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
279
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
269
280
 
270
281
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
271
282
  throw new Error("Invalid or corrupted handle");
272
283
  }
273
284
 
274
- this.assertNotBorrowed(headerAddress);
285
+ if (borrowed) {
286
+ throw new Error("Block is borrowed");
287
+ }
275
288
 
276
289
  if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
277
290
 
@@ -319,7 +332,7 @@ export default class Balloc {
319
332
  */
320
333
  shrink(handler) {
321
334
  const meta = this.extractMetadata(handler, this.metadata);
322
- this.assertNotBorrowed(meta.headerAddress);
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;
@@ -350,12 +363,15 @@ export default class Balloc {
350
363
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
351
364
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
352
365
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
366
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
353
367
 
354
368
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
355
369
  throw new Error("Invalid or corrupted handle");
356
370
  }
357
371
 
358
- this.assertNotBorrowed(headerAddress);
372
+ if (borrowed) {
373
+ throw new Error("Block is borrowed");
374
+ }
359
375
 
360
376
  const blockSize = (1 << capacity >>> 0) * this.wordSize;
361
377
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
@@ -561,7 +577,7 @@ export default class Balloc {
561
577
  */
562
578
  push(handler, value) {
563
579
  const meta = this.extractMetadata(handler, this.metadata);
564
- this.assertNotBorrowed(meta.headerAddress);
580
+ if (meta.borrowed) throw new Error("Block is borrowed");
565
581
 
566
582
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
567
583
 
@@ -585,7 +601,7 @@ export default class Balloc {
585
601
  */
586
602
  pop(handler) {
587
603
  const meta = this.extractMetadata(handler, this.metadata);
588
- this.assertNotBorrowed(meta.headerAddress);
604
+ if (meta.borrowed) throw new Error("Block is borrowed");
589
605
 
590
606
  if (meta.occupied === 0) return undefined;
591
607
 
@@ -629,7 +645,7 @@ export default class Balloc {
629
645
  */
630
646
  nudge(handler, value) {
631
647
  const meta = this.extractMetadata(handler, this.metadata);
632
- this.assertNotBorrowed(meta.headerAddress);
648
+ if (meta.borrowed) throw new Error("Block is borrowed");
633
649
 
634
650
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
635
651
 
@@ -655,7 +671,7 @@ export default class Balloc {
655
671
  */
656
672
  pull(handler) {
657
673
  const meta = this.extractMetadata(handler, this.metadata);
658
- this.assertNotBorrowed(meta.headerAddress);
674
+ if (meta.borrowed) throw new Error("Block is borrowed");
659
675
 
660
676
  if (meta.occupied === 0) return undefined;
661
677
 
@@ -798,7 +814,7 @@ export default class Balloc {
798
814
  */
799
815
  set(handler, index, value) {
800
816
  const meta = this.extractMetadata(handler, this.metadata);
801
- this.assertNotBorrowed(meta.headerAddress);
817
+ if (meta.borrowed) throw new Error("Block is borrowed");
802
818
 
803
819
  if (index < 0 || index >= meta.occupied) return false;
804
820
 
@@ -823,7 +839,7 @@ export default class Balloc {
823
839
  */
824
840
  clear(handler, value = 0) {
825
841
  const meta = this.extractMetadata(handler, this.metadata);
826
- this.assertNotBorrowed(meta.headerAddress);
842
+ if (meta.borrowed) throw new Error("Block is borrowed");
827
843
 
828
844
  const slots = meta.byteCapacity / this.wordSize;
829
845
  const rawAddress = meta.dataAddress >>> this.wordShift;
@@ -856,7 +872,7 @@ export default class Balloc {
856
872
  */
857
873
  fill(handler, value, start = 0, end = null) {
858
874
  const meta = this.extractMetadata(handler, this.metadata);
859
- this.assertNotBorrowed(meta.headerAddress);
875
+ if (meta.borrowed) throw new Error("Block is borrowed");
860
876
 
861
877
  const slots = meta.byteCapacity / this.wordSize;
862
878
  const lastIndex = end ?? slots;
@@ -890,7 +906,7 @@ export default class Balloc {
890
906
  */
891
907
  fillAll(handler, value) {
892
908
  const meta = this.extractMetadata(handler, this.metadata);
893
- this.assertNotBorrowed(meta.headerAddress);
909
+ if (meta.borrowed) throw new Error("Block is borrowed");
894
910
 
895
911
  const rawAddress = meta.dataAddress >>> this.wordShift;
896
912
  const capacityElements = meta.byteCapacity / this.wordSize;
@@ -919,7 +935,7 @@ export default class Balloc {
919
935
  */
920
936
  put(handler, data) {
921
937
  const meta = this.extractMetadata(handler, this.metadata);
922
- this.assertNotBorrowed(meta.headerAddress);
938
+ if (meta.borrowed) throw new Error("Block is borrowed");
923
939
 
924
940
  const rawAddress = meta.dataAddress >>> this.wordShift;
925
941
  const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
@@ -981,37 +997,35 @@ export default class Balloc {
981
997
  */
982
998
  withView(...args) {
983
999
  if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
984
-
985
- const callback = args.pop();
1000
+ const lastIndex = args.length - 1;
1001
+ const callback = args[lastIndex];
986
1002
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
987
1003
 
988
- const handlers = args;
989
- const metadata = handlers.map(handler => this.extractMetadata(handler, {}));
990
- const headers = metadata.map(meta => meta.headerAddress);
1004
+ const lookup = Object.create(null);
1005
+ const acquired = [];
1006
+ const views = [];
991
1007
 
992
- if (new Set(headers).size !== headers.length) throw new Error("Duplicate handler");
1008
+ try {
1009
+ for (let i = 0; i < lastIndex; i++) {
1010
+ const metadata = this.extractMetadata(args[i], this.metadata);
1011
+ const headerAddr = metadata.headerAddress;
993
1012
 
994
- const flags = headers.map(header => this.controlBus[header + FLAGS_OFFSET]);
995
1013
 
996
- if (flags.some(flag => flag & VIEW_LOCK)) throw new Error("Block is already borrowed");
1014
+ if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
1015
+ if (metadata.borrowed) throw new Error("Block is already borrowed");
997
1016
 
998
- let acquiredCount = 0;
1017
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
999
1018
 
1000
- try {
1001
- for (const header of headers) {
1002
- this.controlBus[header + FLAGS_OFFSET] |= VIEW_LOCK;
1003
- acquiredCount++;
1004
- }
1005
-
1006
- const views = metadata.map(meta => {
1007
- const rawAddress = meta.dataAddress >>> this.wordShift;
1008
- return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
1009
- });
1019
+ const rawAddress = metadata.dataAddress >>> this.wordShift;
1020
+ lookup[headerAddr] = true;
1021
+ acquired.push(headerAddr);
1010
1022
 
1023
+ views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied))
1024
+ }
1011
1025
  return callback(...views);
1012
1026
  } finally {
1013
- for (let i = 0; i < acquiredCount; i++) {
1014
- this.controlBus[headers[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
1027
+ for (let i = 0; i < acquired.length; i++) {
1028
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
1015
1029
  }
1016
1030
  }
1017
1031
  }
@@ -1110,7 +1124,7 @@ export default class Balloc {
1110
1124
 
1111
1125
  this.extractMetadata(source, metaA);
1112
1126
  this.extractMetadata(destination, metaB);
1113
- this.assertNotBorrowed(metaB.headerAddress);
1127
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1114
1128
 
1115
1129
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
1116
1130
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
@@ -1145,7 +1159,7 @@ export default class Balloc {
1145
1159
  */
1146
1160
  copyWithin(handler, target, start, length) {
1147
1161
  const meta = this.extractMetadata(handler, this.metadata);
1148
- this.assertNotBorrowed(meta.headerAddress);
1162
+ if (meta.borrowed) throw new Error("Block is borrowed");
1149
1163
 
1150
1164
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
1151
1165
  target = Math.trunc(target);
@@ -1196,7 +1210,8 @@ export default class Balloc {
1196
1210
 
1197
1211
  this.extractMetadata(target, metaTarget);
1198
1212
  this.extractMetadata(source, metaSource);
1199
- this.assertNotBorrowed(metaTarget.headerAddress);
1213
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
1214
+
1200
1215
 
1201
1216
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
1202
1217
  const count = Math.min(available, metaSource.occupied);
@@ -1267,8 +1282,9 @@ export default class Balloc {
1267
1282
  this.extractMetadata(source, metaA);
1268
1283
  this.extractMetadata(target, metaB);
1269
1284
 
1270
- this.assertNotBorrowed(metaA.headerAddress);
1271
- this.assertNotBorrowed(metaB.headerAddress);
1285
+ if (metaA.borrowed) throw new Error("Block is borrowed");
1286
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1287
+
1272
1288
 
1273
1289
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
1274
1290
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
@@ -1382,6 +1398,29 @@ export default class Balloc {
1382
1398
  return handler;
1383
1399
  }
1384
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
+ }
1385
1424
  // ============= UTILIDADES (UTILITIES) ==============
1386
1425
 
1387
1426
  /**
@@ -1393,7 +1432,8 @@ export default class Balloc {
1393
1432
  * @returns {void}
1394
1433
  * @throws {Error} Si el bloque está prestado mediante `withView()`.
1395
1434
  * @private
1396
- */
1435
+ * @deprecated
1436
+ */
1397
1437
  assertNotBorrowed(headerAddress) {
1398
1438
  if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
1399
1439
  throw new Error("Block is borrowed");
@@ -1451,6 +1491,7 @@ export default class Balloc {
1451
1491
  out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
1452
1492
  out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
1453
1493
  out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
1494
+ out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
1454
1495
 
1455
1496
  return out;
1456
1497
  }
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
- this.assertNotBorrowed(headerAddress);
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
- this.assertNotBorrowed(meta.headerAddress);
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
- this.assertNotBorrowed(headerAddress);
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
- this.assertNotBorrowed(meta.headerAddress);
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
- this.assertNotBorrowed(meta.headerAddress);
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
- this.assertNotBorrowed(meta.headerAddress);
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
- this.assertNotBorrowed(meta.headerAddress);
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
- this.assertNotBorrowed(meta.headerAddress);
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
- this.assertNotBorrowed(meta.headerAddress);
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
- this.assertNotBorrowed(meta.headerAddress);
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
- this.assertNotBorrowed(meta.headerAddress);
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
- this.assertNotBorrowed(meta.headerAddress);
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)) {
@@ -757,28 +763,28 @@ var Balloc = class {
757
763
  */
758
764
  withView(...args) {
759
765
  if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
760
- const callback = args.pop();
766
+ const lastIndex = args.length - 1;
767
+ const callback = args[lastIndex];
761
768
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
762
- const handlers = args;
763
- const metadata = handlers.map((handler) => this.extractMetadata(handler, {}));
764
- const headers = metadata.map((meta) => meta.headerAddress);
765
- if (new Set(headers).size !== headers.length) throw new Error("Duplicate handler");
766
- const flags = headers.map((header) => this.controlBus[header + FLAGS_OFFSET]);
767
- if (flags.some((flag) => flag & VIEW_LOCK)) throw new Error("Block is already borrowed");
768
- let acquiredCount = 0;
769
+ const lookup = /* @__PURE__ */ Object.create(null);
770
+ const acquired = [];
771
+ const views = [];
769
772
  try {
770
- for (const header of headers) {
771
- this.controlBus[header + FLAGS_OFFSET] |= VIEW_LOCK;
772
- acquiredCount++;
773
+ for (let i = 0; i < lastIndex; i++) {
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");
777
+ if (metadata.borrowed) throw new Error("Block is already borrowed");
778
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
779
+ const rawAddress = metadata.dataAddress >>> this.wordShift;
780
+ lookup[headerAddr] = true;
781
+ acquired.push(headerAddr);
782
+ views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
773
783
  }
774
- const views = metadata.map((meta) => {
775
- const rawAddress = meta.dataAddress >>> this.wordShift;
776
- return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
777
- });
778
784
  return callback(...views);
779
785
  } finally {
780
- for (let i = 0; i < acquiredCount; i++) {
781
- this.controlBus[headers[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
786
+ for (let i = 0; i < acquired.length; i++) {
787
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
782
788
  }
783
789
  }
784
790
  }
@@ -862,7 +868,7 @@ var Balloc = class {
862
868
  const metaB = {};
863
869
  this.extractMetadata(source, metaA);
864
870
  this.extractMetadata(destination, metaB);
865
- this.assertNotBorrowed(metaB.headerAddress);
871
+ if (metaB.borrowed) throw new Error("Block is borrowed");
866
872
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
867
873
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
868
874
  const newOccupied = Math.max(metaB.occupied, slotsToCopy);
@@ -892,7 +898,7 @@ var Balloc = class {
892
898
  */
893
899
  copyWithin(handler, target, start, length) {
894
900
  const meta = this.extractMetadata(handler, this.metadata);
895
- this.assertNotBorrowed(meta.headerAddress);
901
+ if (meta.borrowed) throw new Error("Block is borrowed");
896
902
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
897
903
  target = Math.trunc(target);
898
904
  start = Math.trunc(start);
@@ -930,7 +936,7 @@ var Balloc = class {
930
936
  const metaSource = {};
931
937
  this.extractMetadata(target, metaTarget);
932
938
  this.extractMetadata(source, metaSource);
933
- this.assertNotBorrowed(metaTarget.headerAddress);
939
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
934
940
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
935
941
  const count = Math.min(available, metaSource.occupied);
936
942
  if (count === 0) return 0;
@@ -982,8 +988,8 @@ var Balloc = class {
982
988
  const metaB = {};
983
989
  this.extractMetadata(source, metaA);
984
990
  this.extractMetadata(target, metaB);
985
- this.assertNotBorrowed(metaA.headerAddress);
986
- this.assertNotBorrowed(metaB.headerAddress);
991
+ if (metaA.borrowed) throw new Error("Block is borrowed");
992
+ if (metaB.borrowed) throw new Error("Block is borrowed");
987
993
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
988
994
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
989
995
  this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
@@ -1072,6 +1078,28 @@ var Balloc = class {
1072
1078
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
1073
1079
  return handler;
1074
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
+ }
1075
1103
  // ============= ユーティリティ (UTILITIES) ==============
1076
1104
  /**
1077
1105
  * 借用中のブロックに対する変更操作を禁止します。
@@ -1082,7 +1110,8 @@ var Balloc = class {
1082
1110
  * @returns {void}
1083
1111
  * @throws {Error} ブロックが withView() によって借用されている場合。
1084
1112
  * @private
1085
- */
1113
+ * @deprecated
1114
+ */
1086
1115
  assertNotBorrowed(headerAddress) {
1087
1116
  if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
1088
1117
  throw new Error("Block is borrowed");
@@ -1132,6 +1161,7 @@ var Balloc = class {
1132
1161
  out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
1133
1162
  out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
1134
1163
  out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
1164
+ out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
1135
1165
  return out;
1136
1166
  }
1137
1167
  /**