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/jp/balloc.mjs CHANGED
@@ -104,6 +104,15 @@ const isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataVi
104
104
  /**
105
105
  * ReBump GCを備えたEcho Stackデュアルバス・バンプアロケータ。
106
106
  */
107
+ /**
108
+ * データ取り込み用の同期アダプター契約。
109
+ *
110
+ * アダプターは Balloc インスタンスを受け取り、その公開 API を使用してデータを格納し、主要ブロックを識別するハンドラーを返します。
111
+ *
112
+ * @typedef {Object} FormatAdapter
113
+ * @property {(allocator: Balloc) => number} load - アロケーターにデータを読み込み、対応するハンドラーを返します。
114
+ */
115
+
107
116
  export default class Balloc {
108
117
 
109
118
  /**
@@ -262,12 +271,15 @@ export default class Balloc {
262
271
  const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
263
272
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
264
273
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
274
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
265
275
 
266
276
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
267
277
  throw new Error("Invalid or corrupted handle");
268
278
  }
269
279
 
270
- this.assertNotBorrowed(headerAddress);
280
+ if (borrowed) {
281
+ throw new Error("Block is borrowed");
282
+ }
271
283
 
272
284
  if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
273
285
 
@@ -315,7 +327,7 @@ export default class Balloc {
315
327
  */
316
328
  shrink(handler) {
317
329
  const meta = this.extractMetadata(handler, this.metadata);
318
- this.assertNotBorrowed(meta.headerAddress);
330
+ if (meta.borrowed) throw new Error("Block is borrowed");
319
331
 
320
332
  const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
321
333
  if (newCapacity >= meta.capacityExponent) return;
@@ -346,12 +358,15 @@ export default class Balloc {
346
358
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
347
359
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
348
360
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
361
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
349
362
 
350
363
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
351
364
  throw new Error("Invalid or corrupted handle");
352
365
  }
353
366
 
354
- this.assertNotBorrowed(headerAddress);
367
+ if (borrowed) {
368
+ throw new Error("Block is borrowed");
369
+ }
355
370
 
356
371
  const blockSize = (1 << capacity >>> 0) * this.wordSize;
357
372
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
@@ -547,7 +562,7 @@ export default class Balloc {
547
562
  */
548
563
  push(handler, value) {
549
564
  const meta = this.extractMetadata(handler, this.metadata);
550
- this.assertNotBorrowed(meta.headerAddress);
565
+ if (meta.borrowed) throw new Error("Block is borrowed");
551
566
 
552
567
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
553
568
 
@@ -569,7 +584,7 @@ export default class Balloc {
569
584
  */
570
585
  pop(handler) {
571
586
  const meta = this.extractMetadata(handler, this.metadata);
572
- this.assertNotBorrowed(meta.headerAddress);
587
+ if (meta.borrowed) throw new Error("Block is borrowed");
573
588
 
574
589
  if (meta.occupied === 0) return undefined;
575
590
 
@@ -609,7 +624,7 @@ export default class Balloc {
609
624
  */
610
625
  nudge(handler, value) {
611
626
  const meta = this.extractMetadata(handler, this.metadata);
612
- this.assertNotBorrowed(meta.headerAddress);
627
+ if (meta.borrowed) throw new Error("Block is borrowed");
613
628
 
614
629
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
615
630
 
@@ -634,7 +649,7 @@ export default class Balloc {
634
649
  */
635
650
  pull(handler) {
636
651
  const meta = this.extractMetadata(handler, this.metadata);
637
- this.assertNotBorrowed(meta.headerAddress);
652
+ if (meta.borrowed) throw new Error("Block is borrowed");
638
653
 
639
654
  if (meta.occupied === 0) return undefined;
640
655
 
@@ -770,7 +785,7 @@ export default class Balloc {
770
785
  */
771
786
  set(handler, index, value) {
772
787
  const meta = this.extractMetadata(handler, this.metadata);
773
- this.assertNotBorrowed(meta.headerAddress);
788
+ if (meta.borrowed) throw new Error("Block is borrowed");
774
789
 
775
790
  if (index < 0 || index >= meta.occupied) return false;
776
791
 
@@ -793,7 +808,7 @@ export default class Balloc {
793
808
  */
794
809
  clear(handler, value = 0) {
795
810
  const meta = this.extractMetadata(handler, this.metadata);
796
- this.assertNotBorrowed(meta.headerAddress);
811
+ if (meta.borrowed) throw new Error("Block is borrowed");
797
812
 
798
813
  const slots = meta.byteCapacity / this.wordSize;
799
814
  const rawAddress = meta.dataAddress >>> this.wordShift;
@@ -824,7 +839,7 @@ export default class Balloc {
824
839
  */
825
840
  fill(handler, value, start = 0, end = null) {
826
841
  const meta = this.extractMetadata(handler, this.metadata);
827
- this.assertNotBorrowed(meta.headerAddress);
842
+ if (meta.borrowed) throw new Error("Block is borrowed");
828
843
 
829
844
  const slots = meta.byteCapacity / this.wordSize;
830
845
  const lastIndex = end ?? slots;
@@ -856,7 +871,7 @@ export default class Balloc {
856
871
  */
857
872
  fillAll(handler, value) {
858
873
  const meta = this.extractMetadata(handler, this.metadata);
859
- this.assertNotBorrowed(meta.headerAddress);
874
+ if (meta.borrowed) throw new Error("Block is borrowed");
860
875
 
861
876
  const rawAddress = meta.dataAddress >>> this.wordShift;
862
877
  const capacityElements = meta.byteCapacity / this.wordSize;
@@ -883,7 +898,7 @@ export default class Balloc {
883
898
  */
884
899
  put(handler, data) {
885
900
  const meta = this.extractMetadata(handler, this.metadata);
886
- this.assertNotBorrowed(meta.headerAddress);
901
+ if (meta.borrowed) throw new Error("Block is borrowed");
887
902
 
888
903
  const rawAddress = meta.dataAddress >>> this.wordShift;
889
904
  const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
@@ -949,26 +964,30 @@ export default class Balloc {
949
964
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
950
965
 
951
966
  const lookup = Object.create(null);
967
+ const acquired = [];
952
968
  const views = [];
953
969
 
954
970
  try {
955
- for(let i = 0; i < lastIndex; i++) {
956
- const metadata = this.extractMetadata(args[i], {});
971
+ for (let i = 0; i < lastIndex; i++) {
972
+ const metadata = this.extractMetadata(args[i], this.metadata);
973
+ const headerAddr = metadata.headerAddress;
974
+
975
+
976
+ if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
977
+ if (metadata.borrowed) throw new Error("Block is already borrowed");
957
978
 
958
-
959
- if (Object.hasOwn(lookup, metadata.headerAddress)) throw new Error("Duplicate handler");
960
- if (metadata.borrowed) throw new Error("Block is already borrowed");
961
-
962
- this.controlBus[metadata.headerAddress + FLAGS_OFFSET] |= VIEW_LOCK;
979
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
963
980
 
964
981
  const rawAddress = metadata.dataAddress >>> this.wordShift;
965
- lookup[metadata.headerAddress] = metadata.headerAddress;
982
+ lookup[headerAddr] = true;
983
+ acquired.push(headerAddr);
984
+
966
985
  views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied))
967
986
  }
968
987
  return callback(...views);
969
988
  } finally {
970
- for (const key in lookup) {
971
- this.controlBus[lookup[key] + FLAGS_OFFSET] &= ~VIEW_LOCK;
989
+ for (let i = 0; i < acquired.length; i++) {
990
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
972
991
  }
973
992
  }
974
993
  }
@@ -1063,7 +1082,7 @@ export default class Balloc {
1063
1082
 
1064
1083
  this.extractMetadata(source, metaA);
1065
1084
  this.extractMetadata(destination, metaB);
1066
- this.assertNotBorrowed(metaB.headerAddress);
1085
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1067
1086
 
1068
1087
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
1069
1088
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
@@ -1096,7 +1115,7 @@ export default class Balloc {
1096
1115
  */
1097
1116
  copyWithin(handler, target, start, length) {
1098
1117
  const meta = this.extractMetadata(handler, this.metadata);
1099
- this.assertNotBorrowed(meta.headerAddress);
1118
+ if (meta.borrowed) throw new Error("Block is borrowed");
1100
1119
 
1101
1120
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
1102
1121
  target = Math.trunc(target);
@@ -1144,7 +1163,8 @@ export default class Balloc {
1144
1163
 
1145
1164
  this.extractMetadata(target, metaTarget);
1146
1165
  this.extractMetadata(source, metaSource);
1147
- this.assertNotBorrowed(metaTarget.headerAddress);
1166
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
1167
+
1148
1168
 
1149
1169
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
1150
1170
  const count = Math.min(available, metaSource.occupied);
@@ -1211,8 +1231,9 @@ export default class Balloc {
1211
1231
  this.extractMetadata(source, metaA);
1212
1232
  this.extractMetadata(target, metaB);
1213
1233
 
1214
- this.assertNotBorrowed(metaA.headerAddress);
1215
- this.assertNotBorrowed(metaB.headerAddress);
1234
+ if (metaA.borrowed) throw new Error("Block is borrowed");
1235
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1236
+
1216
1237
 
1217
1238
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
1218
1239
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
@@ -1326,6 +1347,29 @@ export default class Balloc {
1326
1347
  return handler;
1327
1348
  }
1328
1349
 
1350
+ // ========== データ統合と取り込み ==========
1351
+
1352
+ /**
1353
+ * 専用アダプターを通じてデータを読み込みます。
1354
+ *
1355
+ * アダプターはこのインスタンスを受け取り、その公開 API を使用して
1356
+ * 必要なブロックを割り当て、データを書き込みます。データの構成と
1357
+ * 解釈はアダプターのみが担当し、Balloc は処理を委譲して生成された
1358
+ * ハンドラーを返します。
1359
+ *
1360
+ * この操作は同期的に実行されます。アダプターは戻る前にデータの
1361
+ * 取り込みを完了し、読み込みに失敗した場合は自身の割り当てを
1362
+ * 元に戻す責任を負います。
1363
+ *
1364
+ * @param {FormatAdapter} adapter - load(allocator) を実装するアダプター。
1365
+ * @returns {number} アダプターが返すハンドラー。
1366
+ * @throws {TypeError} アダプターが load() メソッドを実装していない場合。
1367
+ * @throws {Error} アダプターが指定されたインフラストラクチャを拒否した場合。
1368
+ */
1369
+ loadData(adapter) {
1370
+ if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
1371
+ return adapter.load(this);
1372
+ }
1329
1373
  // ============= ユーティリティ (UTILITIES) ==============
1330
1374
 
1331
1375
  /**
@@ -1337,7 +1381,8 @@ export default class Balloc {
1337
1381
  * @returns {void}
1338
1382
  * @throws {Error} ブロックが withView() によって借用されている場合。
1339
1383
  * @private
1340
- */
1384
+ * @deprecated
1385
+ */
1341
1386
  assertNotBorrowed(headerAddress) {
1342
1387
  if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
1343
1388
  throw new Error("Block is borrowed");
package/src/pt/balloc.cjs CHANGED
@@ -194,10 +194,13 @@ var Balloc = class {
194
194
  const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
195
195
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
196
196
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
197
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
197
198
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
198
199
  throw new Error("Invalid or corrupted handle");
199
200
  }
200
- this.assertNotBorrowed(headerAddress);
201
+ if (borrowed) {
202
+ throw new Error("Block is borrowed");
203
+ }
201
204
  if (capacity >= 31) throw new AggregateError([new Error("Can\xB4t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
202
205
  const newCapacity = capacity + 1;
203
206
  const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
@@ -234,7 +237,7 @@ var Balloc = class {
234
237
  */
235
238
  shrink(handler) {
236
239
  const meta = this.extractMetadata(handler, this.metadata);
237
- this.assertNotBorrowed(meta.headerAddress);
240
+ if (meta.borrowed) throw new Error("Block is borrowed");
238
241
  const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
239
242
  if (newCapacity >= meta.capacityExponent) return;
240
243
  const oldBlockSize = meta.byteCapacity;
@@ -258,10 +261,13 @@ var Balloc = class {
258
261
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
259
262
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
260
263
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
264
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
261
265
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
262
266
  throw new Error("Invalid or corrupted handle");
263
267
  }
264
- this.assertNotBorrowed(headerAddress);
268
+ if (borrowed) {
269
+ throw new Error("Block is borrowed");
270
+ }
265
271
  const blockSize = (1 << capacity >>> 0) * this.wordSize;
266
272
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
267
273
  if (this.echoPointer + 4 <= this.controlBus.byteLength) {
@@ -441,7 +447,7 @@ var Balloc = class {
441
447
  */
442
448
  push(handler, value) {
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 + 1) * this.wordSize > meta.byteCapacity) return false;
446
452
  this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
447
453
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
@@ -461,7 +467,7 @@ var Balloc = class {
461
467
  */
462
468
  pop(handler) {
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 === 0) return void 0;
466
472
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
467
473
  return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
@@ -498,7 +504,7 @@ var Balloc = class {
498
504
  */
499
505
  nudge(handler, value) {
500
506
  const meta = this.extractMetadata(handler, this.metadata);
501
- this.assertNotBorrowed(meta.headerAddress);
507
+ if (meta.borrowed) throw new Error("Block is borrowed");
502
508
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
503
509
  this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
504
510
  this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
@@ -520,7 +526,7 @@ var Balloc = class {
520
526
  */
521
527
  pull(handler) {
522
528
  const meta = this.extractMetadata(handler, this.metadata);
523
- this.assertNotBorrowed(meta.headerAddress);
529
+ if (meta.borrowed) throw new Error("Block is borrowed");
524
530
  if (meta.occupied === 0) return void 0;
525
531
  const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
526
532
  this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
@@ -639,7 +645,7 @@ var Balloc = class {
639
645
  */
640
646
  set(handler, index, value) {
641
647
  const meta = this.extractMetadata(handler, this.metadata);
642
- this.assertNotBorrowed(meta.headerAddress);
648
+ if (meta.borrowed) throw new Error("Block is borrowed");
643
649
  if (index < 0 || index >= meta.occupied) return false;
644
650
  this.writer(meta.dataAddress + index * this.wordSize, value, IS_LITTLE_ENDIAN);
645
651
  return true;
@@ -660,7 +666,7 @@ var Balloc = class {
660
666
  */
661
667
  clear(handler, value = 0) {
662
668
  const meta = this.extractMetadata(handler, this.metadata);
663
- this.assertNotBorrowed(meta.headerAddress);
669
+ if (meta.borrowed) throw new Error("Block is borrowed");
664
670
  const slots = meta.byteCapacity / this.wordSize;
665
671
  const rawAddress = meta.dataAddress >>> this.wordShift;
666
672
  this.auxiliary.fill(value, rawAddress, rawAddress + slots);
@@ -689,7 +695,7 @@ var Balloc = class {
689
695
  */
690
696
  fill(handler, value, start = 0, end = null) {
691
697
  const meta = this.extractMetadata(handler, this.metadata);
692
- this.assertNotBorrowed(meta.headerAddress);
698
+ if (meta.borrowed) throw new Error("Block is borrowed");
693
699
  const slots = meta.byteCapacity / this.wordSize;
694
700
  const lastIndex = end ?? slots;
695
701
  if (start > lastIndex) {
@@ -719,7 +725,7 @@ var Balloc = class {
719
725
  */
720
726
  fillAll(handler, value) {
721
727
  const meta = this.extractMetadata(handler, this.metadata);
722
- this.assertNotBorrowed(meta.headerAddress);
728
+ if (meta.borrowed) throw new Error("Block is borrowed");
723
729
  const rawAddress = meta.dataAddress >>> this.wordShift;
724
730
  const capacityElements = meta.byteCapacity / this.wordSize;
725
731
  this.auxiliary.fill(value, rawAddress, rawAddress + capacityElements);
@@ -745,7 +751,7 @@ var Balloc = class {
745
751
  */
746
752
  put(handler, data) {
747
753
  const meta = this.extractMetadata(handler, this.metadata);
748
- this.assertNotBorrowed(meta.headerAddress);
754
+ if (meta.borrowed) throw new Error("Block is borrowed");
749
755
  const rawAddress = meta.dataAddress >>> this.wordShift;
750
756
  const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
751
757
  if (Array.isArray(data)) {
@@ -802,21 +808,24 @@ var Balloc = class {
802
808
  const callback = args[lastIndex];
803
809
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
804
810
  const lookup = /* @__PURE__ */ Object.create(null);
811
+ const acquired = [];
805
812
  const views = [];
806
813
  try {
807
814
  for (let i = 0; i < lastIndex; i++) {
808
- const metadata = this.extractMetadata(args[i], {});
809
- if (Object.hasOwn(lookup, metadata.headerAddress)) throw new Error("Duplicate handler");
815
+ const metadata = this.extractMetadata(args[i], this.metadata);
816
+ const headerAddr = metadata.headerAddress;
817
+ if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
810
818
  if (metadata.borrowed) throw new Error("Block is already borrowed");
811
- this.controlBus[metadata.headerAddress + FLAGS_OFFSET] |= VIEW_LOCK;
819
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
812
820
  const rawAddress = metadata.dataAddress >>> this.wordShift;
813
- lookup[metadata.headerAddress] = metadata.headerAddress;
821
+ lookup[headerAddr] = true;
822
+ acquired.push(headerAddr);
814
823
  views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
815
824
  }
816
825
  return callback(...views);
817
826
  } finally {
818
- for (const key in lookup) {
819
- this.controlBus[lookup[key] + FLAGS_OFFSET] &= ~VIEW_LOCK;
827
+ for (let i = 0; i < acquired.length; i++) {
828
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
820
829
  }
821
830
  }
822
831
  }
@@ -906,7 +915,7 @@ var Balloc = class {
906
915
  const metaB = {};
907
916
  this.extractMetadata(source, metaA);
908
917
  this.extractMetadata(destination, metaB);
909
- this.assertNotBorrowed(metaB.headerAddress);
918
+ if (metaB.borrowed) throw new Error("Block is borrowed");
910
919
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
911
920
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
912
921
  const newOccupied = Math.max(metaB.occupied, slotsToCopy);
@@ -938,7 +947,7 @@ var Balloc = class {
938
947
  */
939
948
  copyWithin(handler, target, start, length) {
940
949
  const meta = this.extractMetadata(handler, this.metadata);
941
- this.assertNotBorrowed(meta.headerAddress);
950
+ if (meta.borrowed) throw new Error("Block is borrowed");
942
951
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
943
952
  target = Math.trunc(target);
944
953
  start = Math.trunc(start);
@@ -979,7 +988,7 @@ var Balloc = class {
979
988
  const metaSource = {};
980
989
  this.extractMetadata(target, metaTarget);
981
990
  this.extractMetadata(source, metaSource);
982
- this.assertNotBorrowed(metaTarget.headerAddress);
991
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
983
992
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
984
993
  const count = Math.min(available, metaSource.occupied);
985
994
  if (count === 0) return 0;
@@ -1035,8 +1044,8 @@ var Balloc = class {
1035
1044
  const metaB = {};
1036
1045
  this.extractMetadata(source, metaA);
1037
1046
  this.extractMetadata(target, metaB);
1038
- this.assertNotBorrowed(metaA.headerAddress);
1039
- this.assertNotBorrowed(metaB.headerAddress);
1047
+ if (metaA.borrowed) throw new Error("Block is borrowed");
1048
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1040
1049
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
1041
1050
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
1042
1051
  this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
@@ -1125,8 +1134,31 @@ var Balloc = class {
1125
1134
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
1126
1135
  return handler;
1127
1136
  }
1137
+ // ========== INTEGRAÇÃO E INGESTÃO DE DADOS ==========
1138
+ /**
1139
+ * Carrega dados por meio de um adaptador especializado.
1140
+ *
1141
+ * O adaptador recebe esta instância e utiliza sua API pública para alocar
1142
+ * e preencher os blocos necessários. A organização e a interpretação dos
1143
+ * dados são responsabilidade exclusiva do adaptador; o Balloc apenas
1144
+ * delega a operação e retorna o handler produzido.
1145
+ *
1146
+ * A operação é síncrona. O adaptador deve concluir a ingestão antes de
1147
+ * retornar e é responsável por reverter suas próprias alocações caso
1148
+ * ocorra uma falha durante o carregamento.
1149
+ *
1150
+ * @param {FormatAdapter} adapter - Adaptador que implementa load(allocator).
1151
+ * @returns {number} Handler retornado pelo adaptador.
1152
+ * @throws {TypeError} Se o adaptador não implementar o método load().
1153
+ * @throws {Error} Se o adaptador recusar a infraestrutura subscrita.
1154
+ */
1155
+ loadData(adapter) {
1156
+ if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
1157
+ return adapter.load(this);
1158
+ }
1128
1159
  // ============= UTILITÁRIOS ==============
1129
1160
  /**
1161
+ * [OBSOLETO]
1130
1162
  * Impede operações de modificação sobre um bloco emprestado.
1131
1163
  *
1132
1164
  * Verifica a flag VIEW_LOCK no header associado ao endereço informado.
@@ -1134,6 +1166,7 @@ var Balloc = class {
1134
1166
  * @param {number} headerAddress - Endereço do header no controlBus.
1135
1167
  * @returns {void}
1136
1168
  * @throws {Error} Se o bloco estiver emprestado por withView().
1169
+ * @deprecated
1137
1170
  * @private
1138
1171
  */
1139
1172
  assertNotBorrowed(headerAddress) {