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/src/pt/balloc.mjs CHANGED
@@ -101,6 +101,17 @@ const CLAMP_11BITS = 0x7FF >>> 0;
101
101
  */
102
102
  const isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataView);
103
103
 
104
+ /**
105
+ * Contrato de um adaptador síncrono para ingestão de dados.
106
+ *
107
+ * O adaptador recebe a instância do Balloc e utiliza sua API pública para
108
+ * armazenar os dados, retornando o handler que identifica o bloco principal.
109
+ *
110
+ * @typedef {Object} FormatAdapter
111
+ * @property {(allocator: Balloc) => number} load - Carrega os dados no alocador
112
+ * e retorna o handler correspondente.
113
+ */
114
+
104
115
  /**
105
116
  * Echo Stack Dual Bus Bump Allocator with ReBump GC.
106
117
  */
@@ -177,6 +188,8 @@ export default class Balloc {
177
188
 
178
189
  }
179
190
 
191
+
192
+
180
193
  // ========== ALOCAÇÃO ==========
181
194
 
182
195
  /**
@@ -269,12 +282,15 @@ export default class Balloc {
269
282
  const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
270
283
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
271
284
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
285
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
272
286
 
273
287
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
274
288
  throw new Error("Invalid or corrupted handle");
275
289
  }
276
290
 
277
- this.assertNotBorrowed(headerAddress);
291
+ if (borrowed) {
292
+ throw new Error("Block is borrowed");
293
+ }
278
294
 
279
295
  if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
280
296
 
@@ -322,7 +338,7 @@ export default class Balloc {
322
338
  */
323
339
  shrink(handler) {
324
340
  const meta = this.extractMetadata(handler, this.metadata);
325
- this.assertNotBorrowed(meta.headerAddress);
341
+ if (meta.borrowed) throw new Error("Block is borrowed");
326
342
 
327
343
  const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
328
344
  if (newCapacity >= meta.capacityExponent) return;
@@ -353,12 +369,15 @@ export default class Balloc {
353
369
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
354
370
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
355
371
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
372
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
356
373
 
357
374
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
358
375
  throw new Error("Invalid or corrupted handle");
359
376
  }
360
377
 
361
- this.assertNotBorrowed(headerAddress);
378
+ if (borrowed) {
379
+ throw new Error("Block is borrowed");
380
+ }
362
381
 
363
382
  const blockSize = (1 << capacity >>> 0) * this.wordSize;
364
383
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
@@ -565,7 +584,7 @@ export default class Balloc {
565
584
  */
566
585
  push(handler, value) {
567
586
  const meta = this.extractMetadata(handler, this.metadata);
568
- this.assertNotBorrowed(meta.headerAddress);
587
+ if (meta.borrowed) throw new Error("Block is borrowed");
569
588
 
570
589
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
571
590
 
@@ -589,7 +608,7 @@ export default class Balloc {
589
608
  */
590
609
  pop(handler) {
591
610
  const meta = this.extractMetadata(handler, this.metadata);
592
- this.assertNotBorrowed(meta.headerAddress);
611
+ if (meta.borrowed) throw new Error("Block is borrowed");
593
612
 
594
613
  if (meta.occupied === 0) return undefined;
595
614
 
@@ -633,7 +652,7 @@ export default class Balloc {
633
652
  */
634
653
  nudge(handler, value) {
635
654
  const meta = this.extractMetadata(handler, this.metadata);
636
- this.assertNotBorrowed(meta.headerAddress);
655
+ if (meta.borrowed) throw new Error("Block is borrowed");
637
656
 
638
657
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
639
658
 
@@ -659,7 +678,7 @@ export default class Balloc {
659
678
  */
660
679
  pull(handler) {
661
680
  const meta = this.extractMetadata(handler, this.metadata);
662
- this.assertNotBorrowed(meta.headerAddress);
681
+ if (meta.borrowed) throw new Error("Block is borrowed");
663
682
 
664
683
  if (meta.occupied === 0) return undefined;
665
684
 
@@ -803,7 +822,7 @@ export default class Balloc {
803
822
  */
804
823
  set(handler, index, value) {
805
824
  const meta = this.extractMetadata(handler, this.metadata);
806
- this.assertNotBorrowed(meta.headerAddress);
825
+ if (meta.borrowed) throw new Error("Block is borrowed");
807
826
 
808
827
  if (index < 0 || index >= meta.occupied) return false;
809
828
 
@@ -828,7 +847,7 @@ export default class Balloc {
828
847
  */
829
848
  clear(handler, value = 0) {
830
849
  const meta = this.extractMetadata(handler, this.metadata);
831
- this.assertNotBorrowed(meta.headerAddress);
850
+ if (meta.borrowed) throw new Error("Block is borrowed");
832
851
 
833
852
  const slots = meta.byteCapacity / this.wordSize;
834
853
  const rawAddress = meta.dataAddress >>> this.wordShift;
@@ -861,7 +880,7 @@ export default class Balloc {
861
880
  */
862
881
  fill(handler, value, start = 0, end = null) {
863
882
  const meta = this.extractMetadata(handler, this.metadata);
864
- this.assertNotBorrowed(meta.headerAddress);
883
+ if (meta.borrowed) throw new Error("Block is borrowed");
865
884
 
866
885
  const slots = meta.byteCapacity / this.wordSize;
867
886
  const lastIndex = end ?? slots;
@@ -895,7 +914,7 @@ export default class Balloc {
895
914
  */
896
915
  fillAll(handler, value) {
897
916
  const meta = this.extractMetadata(handler, this.metadata);
898
- this.assertNotBorrowed(meta.headerAddress);
917
+ if (meta.borrowed) throw new Error("Block is borrowed");
899
918
 
900
919
  const rawAddress = meta.dataAddress >>> this.wordShift;
901
920
  const capacityElements = meta.byteCapacity / this.wordSize;
@@ -924,7 +943,7 @@ export default class Balloc {
924
943
  */
925
944
  put(handler, data) {
926
945
  const meta = this.extractMetadata(handler, this.metadata);
927
- this.assertNotBorrowed(meta.headerAddress);
946
+ if (meta.borrowed) throw new Error("Block is borrowed");
928
947
 
929
948
  const rawAddress = meta.dataAddress >>> this.wordShift;
930
949
  const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
@@ -990,26 +1009,30 @@ export default class Balloc {
990
1009
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
991
1010
 
992
1011
  const lookup = Object.create(null);
1012
+ const acquired = [];
993
1013
  const views = [];
994
1014
 
995
1015
  try {
996
- for(let i = 0; i < lastIndex; i++) {
997
- const metadata = this.extractMetadata(args[i], {});
1016
+ for (let i = 0; i < lastIndex; i++) {
1017
+ const metadata = this.extractMetadata(args[i], this.metadata);
1018
+ const headerAddr = metadata.headerAddress;
1019
+
998
1020
 
999
-
1000
- if (Object.hasOwn(lookup, metadata.headerAddress)) throw new Error("Duplicate handler");
1001
- if (metadata.borrowed) throw new Error("Block is already borrowed");
1002
-
1003
- this.controlBus[metadata.headerAddress + FLAGS_OFFSET] |= VIEW_LOCK;
1021
+ if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
1022
+ if (metadata.borrowed) throw new Error("Block is already borrowed");
1023
+
1024
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
1004
1025
 
1005
1026
  const rawAddress = metadata.dataAddress >>> this.wordShift;
1006
- lookup[metadata.headerAddress] = metadata.headerAddress;
1027
+ lookup[headerAddr] = true;
1028
+ acquired.push(headerAddr);
1029
+
1007
1030
  views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied))
1008
1031
  }
1009
1032
  return callback(...views);
1010
1033
  } finally {
1011
- for (const key in lookup) {
1012
- this.controlBus[lookup[key] + FLAGS_OFFSET] &= ~VIEW_LOCK;
1034
+ for (let i = 0; i < acquired.length; i++) {
1035
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
1013
1036
  }
1014
1037
  }
1015
1038
  }
@@ -1110,7 +1133,7 @@ export default class Balloc {
1110
1133
 
1111
1134
  this.extractMetadata(source, metaA);
1112
1135
  this.extractMetadata(destination, metaB);
1113
- this.assertNotBorrowed(metaB.headerAddress);
1136
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1114
1137
 
1115
1138
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
1116
1139
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
@@ -1145,7 +1168,7 @@ export default class Balloc {
1145
1168
  */
1146
1169
  copyWithin(handler, target, start, length) {
1147
1170
  const meta = this.extractMetadata(handler, this.metadata);
1148
- this.assertNotBorrowed(meta.headerAddress);
1171
+ if (meta.borrowed) throw new Error("Block is borrowed");
1149
1172
 
1150
1173
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
1151
1174
  target = Math.trunc(target);
@@ -1196,7 +1219,8 @@ export default class Balloc {
1196
1219
 
1197
1220
  this.extractMetadata(target, metaTarget);
1198
1221
  this.extractMetadata(source, metaSource);
1199
- this.assertNotBorrowed(metaTarget.headerAddress);
1222
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
1223
+
1200
1224
 
1201
1225
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
1202
1226
  const count = Math.min(available, metaSource.occupied);
@@ -1267,8 +1291,9 @@ export default class Balloc {
1267
1291
  this.extractMetadata(source, metaA);
1268
1292
  this.extractMetadata(target, metaB);
1269
1293
 
1270
- this.assertNotBorrowed(metaA.headerAddress);
1271
- this.assertNotBorrowed(metaB.headerAddress);
1294
+ if (metaA.borrowed) throw new Error("Block is borrowed");
1295
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1296
+
1272
1297
 
1273
1298
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
1274
1299
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
@@ -1382,9 +1407,34 @@ export default class Balloc {
1382
1407
  return handler;
1383
1408
  }
1384
1409
 
1410
+ // ========== INTEGRAÇÃO E INGESTÃO DE DADOS ==========
1411
+
1412
+ /**
1413
+ * Carrega dados por meio de um adaptador especializado.
1414
+ *
1415
+ * O adaptador recebe esta instância e utiliza sua API pública para alocar
1416
+ * e preencher os blocos necessários. A organização e a interpretação dos
1417
+ * dados são responsabilidade exclusiva do adaptador; o Balloc apenas
1418
+ * delega a operação e retorna o handler produzido.
1419
+ *
1420
+ * A operação é síncrona. O adaptador deve concluir a ingestão antes de
1421
+ * retornar e é responsável por reverter suas próprias alocações caso
1422
+ * ocorra uma falha durante o carregamento.
1423
+ *
1424
+ * @param {FormatAdapter} adapter - Adaptador que implementa load(allocator).
1425
+ * @returns {number} Handler retornado pelo adaptador.
1426
+ * @throws {TypeError} Se o adaptador não implementar o método load().
1427
+ * @throws {Error} Se o adaptador recusar a infraestrutura subscrita.
1428
+ */
1429
+ loadData(adapter) {
1430
+ if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
1431
+ return adapter.load(this);
1432
+ }
1433
+
1385
1434
  // ============= UTILITÁRIOS ==============
1386
1435
 
1387
1436
  /**
1437
+ * [OBSOLETO]
1388
1438
  * Impede operações de modificação sobre um bloco emprestado.
1389
1439
  *
1390
1440
  * Verifica a flag VIEW_LOCK no header associado ao endereço informado.
@@ -1392,6 +1442,7 @@ export default class Balloc {
1392
1442
  * @param {number} headerAddress - Endereço do header no controlBus.
1393
1443
  * @returns {void}
1394
1444
  * @throws {Error} Se o bloco estiver emprestado por withView().
1445
+ * @deprecated
1395
1446
  * @private
1396
1447
  */
1397
1448
  assertNotBorrowed(headerAddress) {
@@ -1451,7 +1502,7 @@ export default class Balloc {
1451
1502
  out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
1452
1503
  out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
1453
1504
  out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
1454
- out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
1505
+ out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
1455
1506
 
1456
1507
  return out;
1457
1508
  }
package/src/zh/balloc.cjs CHANGED
@@ -181,10 +181,13 @@ var Balloc = class {
181
181
  const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
182
182
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
183
183
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
184
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
184
185
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
185
186
  throw new Error("Invalid or corrupted handle");
186
187
  }
187
- this.assertNotBorrowed(headerAddress);
188
+ if (borrowed) {
189
+ throw new Error("Block is borrowed");
190
+ }
188
191
  if (capacity >= 31) throw new AggregateError([new Error("Can\xB4t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
189
192
  const newCapacity = capacity + 1;
190
193
  const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
@@ -220,7 +223,7 @@ var Balloc = class {
220
223
  */
221
224
  shrink(handler) {
222
225
  const meta = this.extractMetadata(handler, this.metadata);
223
- this.assertNotBorrowed(meta.headerAddress);
226
+ if (meta.borrowed) throw new Error("Block is borrowed");
224
227
  const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
225
228
  if (newCapacity >= meta.capacityExponent) return;
226
229
  const oldBlockSize = meta.byteCapacity;
@@ -244,10 +247,13 @@ var Balloc = class {
244
247
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
245
248
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
246
249
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
250
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
247
251
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
248
252
  throw new Error("Invalid or corrupted handle");
249
253
  }
250
- this.assertNotBorrowed(headerAddress);
254
+ if (borrowed) {
255
+ throw new Error("Block is borrowed");
256
+ }
251
257
  const blockSize = (1 << capacity >>> 0) * this.wordSize;
252
258
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
253
259
  if (this.echoPointer + 4 <= this.controlBus.byteLength) {
@@ -410,7 +416,7 @@ var Balloc = class {
410
416
  */
411
417
  push(handler, value) {
412
418
  const meta = this.extractMetadata(handler, this.metadata);
413
- this.assertNotBorrowed(meta.headerAddress);
419
+ if (meta.borrowed) throw new Error("Block is borrowed");
414
420
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
415
421
  this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
416
422
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
@@ -428,7 +434,7 @@ var Balloc = class {
428
434
  */
429
435
  pop(handler) {
430
436
  const meta = this.extractMetadata(handler, this.metadata);
431
- this.assertNotBorrowed(meta.headerAddress);
437
+ if (meta.borrowed) throw new Error("Block is borrowed");
432
438
  if (meta.occupied === 0) return void 0;
433
439
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
434
440
  return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
@@ -461,7 +467,7 @@ var Balloc = class {
461
467
  */
462
468
  nudge(handler, value) {
463
469
  const meta = this.extractMetadata(handler, this.metadata);
464
- this.assertNotBorrowed(meta.headerAddress);
470
+ if (meta.borrowed) throw new Error("Block is borrowed");
465
471
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
466
472
  this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
467
473
  this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
@@ -481,7 +487,7 @@ var Balloc = class {
481
487
  */
482
488
  pull(handler) {
483
489
  const meta = this.extractMetadata(handler, this.metadata);
484
- this.assertNotBorrowed(meta.headerAddress);
490
+ if (meta.borrowed) throw new Error("Block is borrowed");
485
491
  if (meta.occupied === 0) return void 0;
486
492
  const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
487
493
  this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
@@ -587,7 +593,7 @@ var Balloc = class {
587
593
  */
588
594
  set(handler, index, value) {
589
595
  const meta = this.extractMetadata(handler, this.metadata);
590
- this.assertNotBorrowed(meta.headerAddress);
596
+ if (meta.borrowed) throw new Error("Block is borrowed");
591
597
  if (index < 0 || index >= meta.occupied) return false;
592
598
  this.writer(meta.dataAddress + index * this.wordSize, value, IS_LITTLE_ENDIAN);
593
599
  return true;
@@ -606,7 +612,7 @@ var Balloc = class {
606
612
  */
607
613
  clear(handler, value = 0) {
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
  const slots = meta.byteCapacity / this.wordSize;
611
617
  const rawAddress = meta.dataAddress >>> this.wordShift;
612
618
  this.auxiliary.fill(value, rawAddress, rawAddress + slots);
@@ -630,7 +636,7 @@ var Balloc = class {
630
636
  */
631
637
  fill(handler, value, start = 0, end = null) {
632
638
  const meta = this.extractMetadata(handler, this.metadata);
633
- this.assertNotBorrowed(meta.headerAddress);
639
+ if (meta.borrowed) throw new Error("Block is borrowed");
634
640
  const slots = meta.byteCapacity / this.wordSize;
635
641
  const lastIndex = end ?? slots;
636
642
  if (start > lastIndex) {
@@ -658,7 +664,7 @@ var Balloc = class {
658
664
  */
659
665
  fillAll(handler, value) {
660
666
  const meta = this.extractMetadata(handler, this.metadata);
661
- this.assertNotBorrowed(meta.headerAddress);
667
+ if (meta.borrowed) throw new Error("Block is borrowed");
662
668
  const rawAddress = meta.dataAddress >>> this.wordShift;
663
669
  const capacityElements = meta.byteCapacity / this.wordSize;
664
670
  this.auxiliary.fill(value, rawAddress, rawAddress + capacityElements);
@@ -681,7 +687,7 @@ var Balloc = class {
681
687
  */
682
688
  put(handler, data) {
683
689
  const meta = this.extractMetadata(handler, this.metadata);
684
- this.assertNotBorrowed(meta.headerAddress);
690
+ if (meta.borrowed) throw new Error("Block is borrowed");
685
691
  const rawAddress = meta.dataAddress >>> this.wordShift;
686
692
  const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
687
693
  if (Array.isArray(data)) {
@@ -735,21 +741,24 @@ var Balloc = class {
735
741
  const callback = args[lastIndex];
736
742
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
737
743
  const lookup = /* @__PURE__ */ Object.create(null);
744
+ const acquired = [];
738
745
  const views = [];
739
746
  try {
740
747
  for (let i = 0; i < lastIndex; i++) {
741
- const metadata = this.extractMetadata(args[i], {});
742
- if (Object.hasOwn(lookup, metadata.headerAddress)) throw new Error("Duplicate handler");
748
+ const metadata = this.extractMetadata(args[i], this.metadata);
749
+ const headerAddr = metadata.headerAddress;
750
+ if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
743
751
  if (metadata.borrowed) throw new Error("Block is already borrowed");
744
- this.controlBus[metadata.headerAddress + FLAGS_OFFSET] |= VIEW_LOCK;
752
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
745
753
  const rawAddress = metadata.dataAddress >>> this.wordShift;
746
- lookup[metadata.headerAddress] = metadata.headerAddress;
754
+ lookup[headerAddr] = true;
755
+ acquired.push(headerAddr);
747
756
  views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
748
757
  }
749
758
  return callback(...views);
750
759
  } finally {
751
- for (const key in lookup) {
752
- this.controlBus[lookup[key] + FLAGS_OFFSET] &= ~VIEW_LOCK;
760
+ for (let i = 0; i < acquired.length; i++) {
761
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
753
762
  }
754
763
  }
755
764
  }
@@ -828,7 +837,7 @@ var Balloc = class {
828
837
  const metaB = {};
829
838
  this.extractMetadata(source, metaA);
830
839
  this.extractMetadata(destination, metaB);
831
- this.assertNotBorrowed(metaB.headerAddress);
840
+ if (metaB.borrowed) throw new Error("Block is borrowed");
832
841
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
833
842
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
834
843
  const newOccupied = Math.max(metaB.occupied, slotsToCopy);
@@ -855,7 +864,7 @@ var Balloc = class {
855
864
  */
856
865
  copyWithin(handler, target, start, length) {
857
866
  const meta = this.extractMetadata(handler, this.metadata);
858
- this.assertNotBorrowed(meta.headerAddress);
867
+ if (meta.borrowed) throw new Error("Block is borrowed");
859
868
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
860
869
  target = Math.trunc(target);
861
870
  start = Math.trunc(start);
@@ -892,7 +901,7 @@ var Balloc = class {
892
901
  const metaSource = {};
893
902
  this.extractMetadata(target, metaTarget);
894
903
  this.extractMetadata(source, metaSource);
895
- this.assertNotBorrowed(metaTarget.headerAddress);
904
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
896
905
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
897
906
  const count = Math.min(available, metaSource.occupied);
898
907
  if (count === 0) return 0;
@@ -940,8 +949,8 @@ var Balloc = class {
940
949
  const metaB = {};
941
950
  this.extractMetadata(source, metaA);
942
951
  this.extractMetadata(target, metaB);
943
- this.assertNotBorrowed(metaA.headerAddress);
944
- this.assertNotBorrowed(metaB.headerAddress);
952
+ if (metaA.borrowed) throw new Error("Block is borrowed");
953
+ if (metaB.borrowed) throw new Error("Block is borrowed");
945
954
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
946
955
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
947
956
  this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
@@ -1027,6 +1036,26 @@ var Balloc = class {
1027
1036
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
1028
1037
  return handler;
1029
1038
  }
1039
+ // ========== 数据集成与导入 ==========
1040
+ /**
1041
+ * 通过专用适配器加载数据。
1042
+ *
1043
+ * 适配器接收此实例,并使用其公共 API 分配和填充所需的内存块。
1044
+ * 数据的组织和解释完全由适配器负责;Balloc 仅委托该操作并返回
1045
+ * 适配器生成的句柄。
1046
+ *
1047
+ * 此操作为同步操作。适配器必须在返回前完成数据导入;如果加载
1048
+ * 失败,适配器负责回滚自身的分配。
1049
+ *
1050
+ * @param {FormatAdapter} adapter - 实现 load(allocator) 的适配器。
1051
+ * @returns {number} 适配器返回的句柄。
1052
+ * @throws {TypeError} 适配器未实现 load() 方法时。
1053
+ * @throws {Error} 适配器拒绝所提供的基础设施时。
1054
+ */
1055
+ loadData(adapter) {
1056
+ if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
1057
+ return adapter.load(this);
1058
+ }
1030
1059
  // ============= 工具 (UTILITIES) ==============
1031
1060
  /**
1032
1061
  * 防止对已借用的块进行修改操作。
@@ -1037,7 +1066,8 @@ var Balloc = class {
1037
1066
  * @returns {void}
1038
1067
  * @throws {Error} 如果块正通过 withView() 被借用。
1039
1068
  * @private
1040
- */
1069
+ * @deprecated
1070
+ */
1041
1071
  assertNotBorrowed(headerAddress) {
1042
1072
  if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
1043
1073
  throw new Error("Block is borrowed");