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/pt/balloc.mjs CHANGED
@@ -90,6 +90,7 @@ const CLAMP_11BITS = 0x7FF >>> 0;
90
90
  * @property {number} generation - Geração cujo o handler pertence.
91
91
  * @property {number} headerAddress - Endereço do header dentro do controlBus.
92
92
  * @property {number} occupied - Número de elementos ocupados no bloco.
93
+ * @property {number} borrowed - Flag indicando se o foi o bloco foi emprestado por withView().
93
94
  */
94
95
 
95
96
  /**
@@ -100,6 +101,17 @@ const CLAMP_11BITS = 0x7FF >>> 0;
100
101
  */
101
102
  const isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataView);
102
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
+
103
115
  /**
104
116
  * Echo Stack Dual Bus Bump Allocator with ReBump GC.
105
117
  */
@@ -171,10 +183,13 @@ export default class Balloc {
171
183
  capacityExponent: 0 >>> 0,
172
184
  byteCapacity: 0 >>> 0,
173
185
  occupied: 0 >>> 0,
186
+ borrowed: 0 >>> 0
174
187
  };
175
188
 
176
189
  }
177
190
 
191
+
192
+
178
193
  // ========== ALOCAÇÃO ==========
179
194
 
180
195
  /**
@@ -267,12 +282,15 @@ export default class Balloc {
267
282
  const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
268
283
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
269
284
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
285
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
270
286
 
271
287
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
272
288
  throw new Error("Invalid or corrupted handle");
273
289
  }
274
290
 
275
- this.assertNotBorrowed(headerAddress);
291
+ if (borrowed) {
292
+ throw new Error("Block is borrowed");
293
+ }
276
294
 
277
295
  if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
278
296
 
@@ -320,7 +338,7 @@ export default class Balloc {
320
338
  */
321
339
  shrink(handler) {
322
340
  const meta = this.extractMetadata(handler, this.metadata);
323
- this.assertNotBorrowed(meta.headerAddress);
341
+ if (meta.borrowed) throw new Error("Block is borrowed");
324
342
 
325
343
  const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
326
344
  if (newCapacity >= meta.capacityExponent) return;
@@ -351,12 +369,15 @@ export default class Balloc {
351
369
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
352
370
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
353
371
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
372
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
354
373
 
355
374
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
356
375
  throw new Error("Invalid or corrupted handle");
357
376
  }
358
377
 
359
- this.assertNotBorrowed(headerAddress);
378
+ if (borrowed) {
379
+ throw new Error("Block is borrowed");
380
+ }
360
381
 
361
382
  const blockSize = (1 << capacity >>> 0) * this.wordSize;
362
383
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
@@ -563,7 +584,7 @@ export default class Balloc {
563
584
  */
564
585
  push(handler, value) {
565
586
  const meta = this.extractMetadata(handler, this.metadata);
566
- this.assertNotBorrowed(meta.headerAddress);
587
+ if (meta.borrowed) throw new Error("Block is borrowed");
567
588
 
568
589
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
569
590
 
@@ -587,7 +608,7 @@ export default class Balloc {
587
608
  */
588
609
  pop(handler) {
589
610
  const meta = this.extractMetadata(handler, this.metadata);
590
- this.assertNotBorrowed(meta.headerAddress);
611
+ if (meta.borrowed) throw new Error("Block is borrowed");
591
612
 
592
613
  if (meta.occupied === 0) return undefined;
593
614
 
@@ -631,7 +652,7 @@ export default class Balloc {
631
652
  */
632
653
  nudge(handler, value) {
633
654
  const meta = this.extractMetadata(handler, this.metadata);
634
- this.assertNotBorrowed(meta.headerAddress);
655
+ if (meta.borrowed) throw new Error("Block is borrowed");
635
656
 
636
657
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
637
658
 
@@ -657,7 +678,7 @@ export default class Balloc {
657
678
  */
658
679
  pull(handler) {
659
680
  const meta = this.extractMetadata(handler, this.metadata);
660
- this.assertNotBorrowed(meta.headerAddress);
681
+ if (meta.borrowed) throw new Error("Block is borrowed");
661
682
 
662
683
  if (meta.occupied === 0) return undefined;
663
684
 
@@ -801,7 +822,7 @@ export default class Balloc {
801
822
  */
802
823
  set(handler, index, value) {
803
824
  const meta = this.extractMetadata(handler, this.metadata);
804
- this.assertNotBorrowed(meta.headerAddress);
825
+ if (meta.borrowed) throw new Error("Block is borrowed");
805
826
 
806
827
  if (index < 0 || index >= meta.occupied) return false;
807
828
 
@@ -826,7 +847,7 @@ export default class Balloc {
826
847
  */
827
848
  clear(handler, value = 0) {
828
849
  const meta = this.extractMetadata(handler, this.metadata);
829
- this.assertNotBorrowed(meta.headerAddress);
850
+ if (meta.borrowed) throw new Error("Block is borrowed");
830
851
 
831
852
  const slots = meta.byteCapacity / this.wordSize;
832
853
  const rawAddress = meta.dataAddress >>> this.wordShift;
@@ -859,7 +880,7 @@ export default class Balloc {
859
880
  */
860
881
  fill(handler, value, start = 0, end = null) {
861
882
  const meta = this.extractMetadata(handler, this.metadata);
862
- this.assertNotBorrowed(meta.headerAddress);
883
+ if (meta.borrowed) throw new Error("Block is borrowed");
863
884
 
864
885
  const slots = meta.byteCapacity / this.wordSize;
865
886
  const lastIndex = end ?? slots;
@@ -893,7 +914,7 @@ export default class Balloc {
893
914
  */
894
915
  fillAll(handler, value) {
895
916
  const meta = this.extractMetadata(handler, this.metadata);
896
- this.assertNotBorrowed(meta.headerAddress);
917
+ if (meta.borrowed) throw new Error("Block is borrowed");
897
918
 
898
919
  const rawAddress = meta.dataAddress >>> this.wordShift;
899
920
  const capacityElements = meta.byteCapacity / this.wordSize;
@@ -922,7 +943,7 @@ export default class Balloc {
922
943
  */
923
944
  put(handler, data) {
924
945
  const meta = this.extractMetadata(handler, this.metadata);
925
- this.assertNotBorrowed(meta.headerAddress);
946
+ if (meta.borrowed) throw new Error("Block is borrowed");
926
947
 
927
948
  const rawAddress = meta.dataAddress >>> this.wordShift;
928
949
  const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
@@ -983,37 +1004,35 @@ export default class Balloc {
983
1004
  */
984
1005
  withView(...args) {
985
1006
  if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
986
-
987
- const callback = args.pop();
1007
+ const lastIndex = args.length - 1;
1008
+ const callback = args[lastIndex];
988
1009
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
989
1010
 
990
- const handlers = args;
991
- const metadata = handlers.map(handler => this.extractMetadata(handler, {}));
992
- const headers = metadata.map(meta => meta.headerAddress);
993
-
994
- if (new Set(headers).size !== headers.length) throw new Error("Duplicate handler");
1011
+ const lookup = Object.create(null);
1012
+ const acquired = [];
1013
+ const views = [];
995
1014
 
996
- const flags = headers.map(header => this.controlBus[header + FLAGS_OFFSET]);
1015
+ try {
1016
+ for (let i = 0; i < lastIndex; i++) {
1017
+ const metadata = this.extractMetadata(args[i], this.metadata);
1018
+ const headerAddr = metadata.headerAddress;
997
1019
 
998
- if (flags.some(flag => flag & VIEW_LOCK)) throw new Error("Block is already borrowed");
999
1020
 
1000
- let acquiredCount = 0;
1021
+ if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
1022
+ if (metadata.borrowed) throw new Error("Block is already borrowed");
1001
1023
 
1002
- try {
1003
- for (const header of headers) {
1004
- this.controlBus[header + FLAGS_OFFSET] |= VIEW_LOCK;
1005
- acquiredCount++;
1006
- }
1024
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
1007
1025
 
1008
- const views = metadata.map(meta => {
1009
- const rawAddress = meta.dataAddress >>> this.wordShift;
1010
- return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
1011
- });
1026
+ const rawAddress = metadata.dataAddress >>> this.wordShift;
1027
+ lookup[headerAddr] = true;
1028
+ acquired.push(headerAddr);
1012
1029
 
1030
+ views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied))
1031
+ }
1013
1032
  return callback(...views);
1014
1033
  } finally {
1015
- for (let i = 0; i < acquiredCount; i++) {
1016
- this.controlBus[headers[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
1034
+ for (let i = 0; i < acquired.length; i++) {
1035
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
1017
1036
  }
1018
1037
  }
1019
1038
  }
@@ -1114,7 +1133,7 @@ export default class Balloc {
1114
1133
 
1115
1134
  this.extractMetadata(source, metaA);
1116
1135
  this.extractMetadata(destination, metaB);
1117
- this.assertNotBorrowed(metaB.headerAddress);
1136
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1118
1137
 
1119
1138
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
1120
1139
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
@@ -1149,7 +1168,7 @@ export default class Balloc {
1149
1168
  */
1150
1169
  copyWithin(handler, target, start, length) {
1151
1170
  const meta = this.extractMetadata(handler, this.metadata);
1152
- this.assertNotBorrowed(meta.headerAddress);
1171
+ if (meta.borrowed) throw new Error("Block is borrowed");
1153
1172
 
1154
1173
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
1155
1174
  target = Math.trunc(target);
@@ -1200,7 +1219,8 @@ export default class Balloc {
1200
1219
 
1201
1220
  this.extractMetadata(target, metaTarget);
1202
1221
  this.extractMetadata(source, metaSource);
1203
- this.assertNotBorrowed(metaTarget.headerAddress);
1222
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
1223
+
1204
1224
 
1205
1225
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
1206
1226
  const count = Math.min(available, metaSource.occupied);
@@ -1271,8 +1291,9 @@ export default class Balloc {
1271
1291
  this.extractMetadata(source, metaA);
1272
1292
  this.extractMetadata(target, metaB);
1273
1293
 
1274
- this.assertNotBorrowed(metaA.headerAddress);
1275
- 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
+
1276
1297
 
1277
1298
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
1278
1299
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
@@ -1386,9 +1407,34 @@ export default class Balloc {
1386
1407
  return handler;
1387
1408
  }
1388
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
+
1389
1434
  // ============= UTILITÁRIOS ==============
1390
1435
 
1391
1436
  /**
1437
+ * [OBSOLETO]
1392
1438
  * Impede operações de modificação sobre um bloco emprestado.
1393
1439
  *
1394
1440
  * Verifica a flag VIEW_LOCK no header associado ao endereço informado.
@@ -1396,6 +1442,7 @@ export default class Balloc {
1396
1442
  * @param {number} headerAddress - Endereço do header no controlBus.
1397
1443
  * @returns {void}
1398
1444
  * @throws {Error} Se o bloco estiver emprestado por withView().
1445
+ * @deprecated
1399
1446
  * @private
1400
1447
  */
1401
1448
  assertNotBorrowed(headerAddress) {
@@ -1455,6 +1502,7 @@ export default class Balloc {
1455
1502
  out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
1456
1503
  out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
1457
1504
  out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
1505
+ out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
1458
1506
 
1459
1507
  return out;
1460
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)) {
@@ -731,28 +737,28 @@ var Balloc = class {
731
737
  */
732
738
  withView(...args) {
733
739
  if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
734
- const callback = args.pop();
740
+ const lastIndex = args.length - 1;
741
+ const callback = args[lastIndex];
735
742
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
736
- const handlers = args;
737
- const metadata = handlers.map((handler) => this.extractMetadata(handler, {}));
738
- const headers = metadata.map((meta) => meta.headerAddress);
739
- if (new Set(headers).size !== headers.length) throw new Error("Duplicate handler");
740
- const flags = headers.map((header) => this.controlBus[header + FLAGS_OFFSET]);
741
- if (flags.some((flag) => flag & VIEW_LOCK)) throw new Error("Block is already borrowed");
742
- let acquiredCount = 0;
743
+ const lookup = /* @__PURE__ */ Object.create(null);
744
+ const acquired = [];
745
+ const views = [];
743
746
  try {
744
- for (const header of headers) {
745
- this.controlBus[header + FLAGS_OFFSET] |= VIEW_LOCK;
746
- acquiredCount++;
747
+ for (let i = 0; i < lastIndex; i++) {
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");
751
+ if (metadata.borrowed) throw new Error("Block is already borrowed");
752
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
753
+ const rawAddress = metadata.dataAddress >>> this.wordShift;
754
+ lookup[headerAddr] = true;
755
+ acquired.push(headerAddr);
756
+ views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
747
757
  }
748
- const views = metadata.map((meta) => {
749
- const rawAddress = meta.dataAddress >>> this.wordShift;
750
- return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
751
- });
752
758
  return callback(...views);
753
759
  } finally {
754
- for (let i = 0; i < acquiredCount; i++) {
755
- this.controlBus[headers[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
760
+ for (let i = 0; i < acquired.length; i++) {
761
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
756
762
  }
757
763
  }
758
764
  }
@@ -831,7 +837,7 @@ var Balloc = class {
831
837
  const metaB = {};
832
838
  this.extractMetadata(source, metaA);
833
839
  this.extractMetadata(destination, metaB);
834
- this.assertNotBorrowed(metaB.headerAddress);
840
+ if (metaB.borrowed) throw new Error("Block is borrowed");
835
841
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
836
842
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
837
843
  const newOccupied = Math.max(metaB.occupied, slotsToCopy);
@@ -858,7 +864,7 @@ var Balloc = class {
858
864
  */
859
865
  copyWithin(handler, target, start, length) {
860
866
  const meta = this.extractMetadata(handler, this.metadata);
861
- this.assertNotBorrowed(meta.headerAddress);
867
+ if (meta.borrowed) throw new Error("Block is borrowed");
862
868
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
863
869
  target = Math.trunc(target);
864
870
  start = Math.trunc(start);
@@ -895,7 +901,7 @@ var Balloc = class {
895
901
  const metaSource = {};
896
902
  this.extractMetadata(target, metaTarget);
897
903
  this.extractMetadata(source, metaSource);
898
- this.assertNotBorrowed(metaTarget.headerAddress);
904
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
899
905
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
900
906
  const count = Math.min(available, metaSource.occupied);
901
907
  if (count === 0) return 0;
@@ -943,8 +949,8 @@ var Balloc = class {
943
949
  const metaB = {};
944
950
  this.extractMetadata(source, metaA);
945
951
  this.extractMetadata(target, metaB);
946
- this.assertNotBorrowed(metaA.headerAddress);
947
- this.assertNotBorrowed(metaB.headerAddress);
952
+ if (metaA.borrowed) throw new Error("Block is borrowed");
953
+ if (metaB.borrowed) throw new Error("Block is borrowed");
948
954
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
949
955
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
950
956
  this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
@@ -1030,6 +1036,26 @@ var Balloc = class {
1030
1036
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
1031
1037
  return handler;
1032
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
+ }
1033
1059
  // ============= 工具 (UTILITIES) ==============
1034
1060
  /**
1035
1061
  * 防止对已借用的块进行修改操作。
@@ -1040,7 +1066,8 @@ var Balloc = class {
1040
1066
  * @returns {void}
1041
1067
  * @throws {Error} 如果块正通过 withView() 被借用。
1042
1068
  * @private
1043
- */
1069
+ * @deprecated
1070
+ */
1044
1071
  assertNotBorrowed(headerAddress) {
1045
1072
  if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
1046
1073
  throw new Error("Block is borrowed");
@@ -1088,6 +1115,7 @@ var Balloc = class {
1088
1115
  out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
1089
1116
  out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
1090
1117
  out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
1118
+ out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
1091
1119
  return out;
1092
1120
  }
1093
1121
  /**