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/ar/balloc.mjs CHANGED
@@ -104,6 +104,15 @@ const isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataVi
104
104
  /**
105
105
  * مخصص الذاكرة المتقاطع ذو الحافتين مع مكدس صدى وإدارة النفايات ReBump.
106
106
  */
107
+ /**
108
+ * عقد محوّل متزامن لإدخال البيانات.
109
+ *
110
+ * يتلقى المحوّل نسخة Balloc ويستخدم واجهتها العامة لتخزين البيانات، ثم يعيد المعالج الذي يحدد الكتلة الرئيسية.
111
+ *
112
+ * @typedef {Object} FormatAdapter
113
+ * @property {(allocator: Balloc) => number} load - يحمّل البيانات إلى المخصّص ويعيد المعالج المقابل.
114
+ */
115
+
107
116
  export default class Balloc {
108
117
 
109
118
  /**
@@ -265,12 +274,15 @@ export default class Balloc {
265
274
  const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
266
275
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
267
276
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
277
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
268
278
 
269
279
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
270
280
  throw new Error("Invalid or corrupted handle");
271
281
  }
272
282
 
273
- this.assertNotBorrowed(headerAddress);
283
+ if (borrowed) {
284
+ throw new Error("Block is borrowed");
285
+ }
274
286
 
275
287
  if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
276
288
 
@@ -317,7 +329,7 @@ export default class Balloc {
317
329
  */
318
330
  shrink(handler) {
319
331
  const meta = this.extractMetadata(handler, this.metadata);
320
- this.assertNotBorrowed(meta.headerAddress);
332
+ if (meta.borrowed) throw new Error("Block is borrowed");
321
333
 
322
334
  const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
323
335
  if (newCapacity >= meta.capacityExponent) return;
@@ -348,12 +360,15 @@ export default class Balloc {
348
360
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
349
361
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
350
362
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
363
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
351
364
 
352
365
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
353
366
  throw new Error("Invalid or corrupted handle");
354
367
  }
355
368
 
356
- this.assertNotBorrowed(headerAddress);
369
+ if (borrowed) {
370
+ throw new Error("Block is borrowed");
371
+ }
357
372
 
358
373
  const blockSize = (1 << capacity >>> 0) * this.wordSize;
359
374
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
@@ -552,7 +567,7 @@ export default class Balloc {
552
567
  */
553
568
  push(handler, value) {
554
569
  const meta = this.extractMetadata(handler, this.metadata);
555
- this.assertNotBorrowed(meta.headerAddress);
570
+ if (meta.borrowed) throw new Error("Block is borrowed");
556
571
 
557
572
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
558
573
 
@@ -576,7 +591,7 @@ export default class Balloc {
576
591
  */
577
592
  pop(handler) {
578
593
  const meta = this.extractMetadata(handler, this.metadata);
579
- this.assertNotBorrowed(meta.headerAddress);
594
+ if (meta.borrowed) throw new Error("Block is borrowed");
580
595
 
581
596
  if (meta.occupied === 0) return undefined;
582
597
 
@@ -620,7 +635,7 @@ export default class Balloc {
620
635
  */
621
636
  nudge(handler, value) {
622
637
  const meta = this.extractMetadata(handler, this.metadata);
623
- this.assertNotBorrowed(meta.headerAddress);
638
+ if (meta.borrowed) throw new Error("Block is borrowed");
624
639
 
625
640
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
626
641
 
@@ -646,7 +661,7 @@ export default class Balloc {
646
661
  */
647
662
  pull(handler) {
648
663
  const meta = this.extractMetadata(handler, this.metadata);
649
- this.assertNotBorrowed(meta.headerAddress);
664
+ if (meta.borrowed) throw new Error("Block is borrowed");
650
665
 
651
666
  if (meta.occupied === 0) return undefined;
652
667
 
@@ -789,7 +804,7 @@ export default class Balloc {
789
804
  */
790
805
  set(handler, index, value) {
791
806
  const meta = this.extractMetadata(handler, this.metadata);
792
- this.assertNotBorrowed(meta.headerAddress);
807
+ if (meta.borrowed) throw new Error("Block is borrowed");
793
808
 
794
809
  if (index < 0 || index >= meta.occupied) return false;
795
810
 
@@ -814,7 +829,7 @@ export default class Balloc {
814
829
  */
815
830
  clear(handler, value = 0) {
816
831
  const meta = this.extractMetadata(handler, this.metadata);
817
- this.assertNotBorrowed(meta.headerAddress);
832
+ if (meta.borrowed) throw new Error("Block is borrowed");
818
833
 
819
834
  const slots = meta.byteCapacity / this.wordSize;
820
835
  const rawAddress = meta.dataAddress >>> this.wordShift;
@@ -847,7 +862,7 @@ export default class Balloc {
847
862
  */
848
863
  fill(handler, value, start = 0, end = null) {
849
864
  const meta = this.extractMetadata(handler, this.metadata);
850
- this.assertNotBorrowed(meta.headerAddress);
865
+ if (meta.borrowed) throw new Error("Block is borrowed");
851
866
 
852
867
  const slots = meta.byteCapacity / this.wordSize;
853
868
  const lastIndex = end ?? slots;
@@ -880,7 +895,7 @@ export default class Balloc {
880
895
  */
881
896
  fillAll(handler, value) {
882
897
  const meta = this.extractMetadata(handler, this.metadata);
883
- this.assertNotBorrowed(meta.headerAddress);
898
+ if (meta.borrowed) throw new Error("Block is borrowed");
884
899
 
885
900
  const rawAddress = meta.dataAddress >>> this.wordShift;
886
901
  const capacityElements = meta.byteCapacity / this.wordSize;
@@ -909,7 +924,7 @@ export default class Balloc {
909
924
  */
910
925
  put(handler, data) {
911
926
  const meta = this.extractMetadata(handler, this.metadata);
912
- this.assertNotBorrowed(meta.headerAddress);
927
+ if (meta.borrowed) throw new Error("Block is borrowed");
913
928
 
914
929
  const rawAddress = meta.dataAddress >>> this.wordShift;
915
930
  const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
@@ -976,26 +991,30 @@ export default class Balloc {
976
991
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
977
992
 
978
993
  const lookup = Object.create(null);
994
+ const acquired = [];
979
995
  const views = [];
980
996
 
981
997
  try {
982
- for(let i = 0; i < lastIndex; i++) {
983
- const metadata = this.extractMetadata(args[i], {});
998
+ for (let i = 0; i < lastIndex; i++) {
999
+ const metadata = this.extractMetadata(args[i], this.metadata);
1000
+ const headerAddr = metadata.headerAddress;
1001
+
1002
+
1003
+ if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
1004
+ if (metadata.borrowed) throw new Error("Block is already borrowed");
984
1005
 
985
-
986
- if (Object.hasOwn(lookup, metadata.headerAddress)) throw new Error("Duplicate handler");
987
- if (metadata.borrowed) throw new Error("Block is already borrowed");
988
-
989
- this.controlBus[metadata.headerAddress + FLAGS_OFFSET] |= VIEW_LOCK;
1006
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
990
1007
 
991
1008
  const rawAddress = metadata.dataAddress >>> this.wordShift;
992
- lookup[metadata.headerAddress] = metadata.headerAddress;
1009
+ lookup[headerAddr] = true;
1010
+ acquired.push(headerAddr);
1011
+
993
1012
  views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied))
994
1013
  }
995
1014
  return callback(...views);
996
1015
  } finally {
997
- for (const key in lookup) {
998
- this.controlBus[lookup[key] + FLAGS_OFFSET] &= ~VIEW_LOCK;
1016
+ for (let i = 0; i < acquired.length; i++) {
1017
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
999
1018
  }
1000
1019
  }
1001
1020
  }
@@ -1094,7 +1113,7 @@ export default class Balloc {
1094
1113
 
1095
1114
  this.extractMetadata(source, metaA);
1096
1115
  this.extractMetadata(destination, metaB);
1097
- this.assertNotBorrowed(metaB.headerAddress);
1116
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1098
1117
 
1099
1118
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
1100
1119
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
@@ -1128,7 +1147,7 @@ export default class Balloc {
1128
1147
  */
1129
1148
  copyWithin(handler, target, start, length) {
1130
1149
  const meta = this.extractMetadata(handler, this.metadata);
1131
- this.assertNotBorrowed(meta.headerAddress);
1150
+ if (meta.borrowed) throw new Error("Block is borrowed");
1132
1151
 
1133
1152
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
1134
1153
  target = Math.trunc(target);
@@ -1178,7 +1197,8 @@ export default class Balloc {
1178
1197
 
1179
1198
  this.extractMetadata(target, metaTarget);
1180
1199
  this.extractMetadata(source, metaSource);
1181
- this.assertNotBorrowed(metaTarget.headerAddress);
1200
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
1201
+
1182
1202
 
1183
1203
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
1184
1204
  const count = Math.min(available, metaSource.occupied);
@@ -1248,8 +1268,9 @@ export default class Balloc {
1248
1268
  this.extractMetadata(source, metaA);
1249
1269
  this.extractMetadata(target, metaB);
1250
1270
 
1251
- this.assertNotBorrowed(metaA.headerAddress);
1252
- this.assertNotBorrowed(metaB.headerAddress);
1271
+ if (metaA.borrowed) throw new Error("Block is borrowed");
1272
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1273
+
1253
1274
 
1254
1275
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
1255
1276
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
@@ -1363,6 +1384,27 @@ export default class Balloc {
1363
1384
  return handler;
1364
1385
  }
1365
1386
 
1387
+ // ========== التكامل وإدخال البيانات ==========
1388
+
1389
+ /**
1390
+ * يحمّل البيانات من خلال محوّل متخصص.
1391
+ *
1392
+ * يتلقى المحوّل هذا المثيل ويستخدم واجهته العامة لتخصيص الكتل المطلوبة
1393
+ * وملئها. تقع مسؤولية تنظيم البيانات وتفسيرها بالكامل على عاتق المحوّل؛
1394
+ * أما Balloc فيفوّض العملية فقط ويعيد المعالج الناتج.
1395
+ *
1396
+ * العملية متزامنة. يجب على المحوّل إكمال إدخال البيانات قبل الإرجاع،
1397
+ * وهو مسؤول عن التراجع عن تخصيصاته إذا فشلت عملية التحميل.
1398
+ *
1399
+ * @param {FormatAdapter} adapter - محوّل يطبّق load(allocator).
1400
+ * @returns {number} المعالج الذي يعيده المحوّل.
1401
+ * @throws {TypeError} إذا لم يطبّق المحوّل الدالة load().
1402
+ * @throws {Error} إذا رفض المحوّل البنية التحتية المقدمة.
1403
+ */
1404
+ loadData(adapter) {
1405
+ if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
1406
+ return adapter.load(this);
1407
+ }
1366
1408
  // ============= الأدوات المساعدة (UTILITIES) ==============
1367
1409
 
1368
1410
  /**
@@ -1374,7 +1416,8 @@ export default class Balloc {
1374
1416
  * @returns {void}
1375
1417
  * @throws {Error} إذا كانت الكتلة معارة بواسطة withView().
1376
1418
  * @private
1377
- */
1419
+ * @deprecated
1420
+ */
1378
1421
  assertNotBorrowed(headerAddress) {
1379
1422
  if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
1380
1423
  throw new Error("Block is borrowed");
package/src/en/balloc.cjs CHANGED
@@ -193,10 +193,13 @@ var Balloc = class {
193
193
  const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
194
194
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
195
195
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
196
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
196
197
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
197
198
  throw new Error("Invalid or corrupted handle");
198
199
  }
199
- this.assertNotBorrowed(headerAddress);
200
+ if (borrowed) {
201
+ throw new Error("Block is borrowed");
202
+ }
200
203
  if (capacity >= 31) throw new AggregateError([new Error("Can\xB4t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
201
204
  const newCapacity = capacity + 1;
202
205
  const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
@@ -232,7 +235,7 @@ var Balloc = class {
232
235
  */
233
236
  shrink(handler) {
234
237
  const meta = this.extractMetadata(handler, this.metadata);
235
- this.assertNotBorrowed(meta.headerAddress);
238
+ if (meta.borrowed) throw new Error("Block is borrowed");
236
239
  const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
237
240
  if (newCapacity >= meta.capacityExponent) return;
238
241
  const oldBlockSize = meta.byteCapacity;
@@ -271,10 +274,13 @@ var Balloc = class {
271
274
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
272
275
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
273
276
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
277
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
274
278
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
275
279
  throw new Error("Invalid or corrupted handle");
276
280
  }
277
- this.assertNotBorrowed(headerAddress);
281
+ if (borrowed) {
282
+ throw new Error("Block is borrowed");
283
+ }
278
284
  const blockSize = (1 << capacity >>> 0) * this.wordSize;
279
285
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
280
286
  if (this.echoPointer + 4 <= this.controlBus.byteLength) {
@@ -452,7 +458,7 @@ var Balloc = class {
452
458
  */
453
459
  push(handler, value) {
454
460
  const meta = this.extractMetadata(handler, this.metadata);
455
- this.assertNotBorrowed(meta.headerAddress);
461
+ if (meta.borrowed) throw new Error("Block is borrowed");
456
462
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
457
463
  this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
458
464
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
@@ -472,7 +478,7 @@ var Balloc = class {
472
478
  */
473
479
  pop(handler) {
474
480
  const meta = this.extractMetadata(handler, this.metadata);
475
- this.assertNotBorrowed(meta.headerAddress);
481
+ if (meta.borrowed) throw new Error("Block is borrowed");
476
482
  if (meta.occupied === 0) return void 0;
477
483
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
478
484
  return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
@@ -509,7 +515,7 @@ var Balloc = class {
509
515
  */
510
516
  nudge(handler, value) {
511
517
  const meta = this.extractMetadata(handler, this.metadata);
512
- this.assertNotBorrowed(meta.headerAddress);
518
+ if (meta.borrowed) throw new Error("Block is borrowed");
513
519
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
514
520
  this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
515
521
  this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
@@ -531,7 +537,7 @@ var Balloc = class {
531
537
  */
532
538
  pull(handler) {
533
539
  const meta = this.extractMetadata(handler, this.metadata);
534
- this.assertNotBorrowed(meta.headerAddress);
540
+ if (meta.borrowed) throw new Error("Block is borrowed");
535
541
  if (meta.occupied === 0) return void 0;
536
542
  const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
537
543
  this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
@@ -649,7 +655,7 @@ var Balloc = class {
649
655
  */
650
656
  set(handler, index, value) {
651
657
  const meta = this.extractMetadata(handler, this.metadata);
652
- this.assertNotBorrowed(meta.headerAddress);
658
+ if (meta.borrowed) throw new Error("Block is borrowed");
653
659
  if (index < 0 || index >= meta.occupied) return false;
654
660
  this.writer(meta.dataAddress + index * this.wordSize, value, IS_LITTLE_ENDIAN);
655
661
  return true;
@@ -670,7 +676,7 @@ var Balloc = class {
670
676
  */
671
677
  clear(handler, value = 0) {
672
678
  const meta = this.extractMetadata(handler, this.metadata);
673
- this.assertNotBorrowed(meta.headerAddress);
679
+ if (meta.borrowed) throw new Error("Block is borrowed");
674
680
  const slots = meta.byteCapacity / this.wordSize;
675
681
  const rawAddress = meta.dataAddress >>> this.wordShift;
676
682
  this.auxiliary.fill(value, rawAddress, rawAddress + slots);
@@ -699,7 +705,7 @@ var Balloc = class {
699
705
  */
700
706
  fill(handler, value, start = 0, end = null) {
701
707
  const meta = this.extractMetadata(handler, this.metadata);
702
- this.assertNotBorrowed(meta.headerAddress);
708
+ if (meta.borrowed) throw new Error("Block is borrowed");
703
709
  const slots = meta.byteCapacity / this.wordSize;
704
710
  const lastIndex = end ?? slots;
705
711
  if (start > lastIndex) {
@@ -728,7 +734,7 @@ var Balloc = class {
728
734
  */
729
735
  fillAll(handler, value) {
730
736
  const meta = this.extractMetadata(handler, this.metadata);
731
- this.assertNotBorrowed(meta.headerAddress);
737
+ if (meta.borrowed) throw new Error("Block is borrowed");
732
738
  const rawAddress = meta.dataAddress >>> this.wordShift;
733
739
  const capacityElements = meta.byteCapacity / this.wordSize;
734
740
  this.auxiliary.fill(value, rawAddress, rawAddress + capacityElements);
@@ -753,7 +759,7 @@ var Balloc = class {
753
759
  */
754
760
  put(handler, data) {
755
761
  const meta = this.extractMetadata(handler, this.metadata);
756
- this.assertNotBorrowed(meta.headerAddress);
762
+ if (meta.borrowed) throw new Error("Block is borrowed");
757
763
  const rawAddress = meta.dataAddress >>> this.wordShift;
758
764
  const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
759
765
  if (Array.isArray(data)) {
@@ -811,21 +817,24 @@ var Balloc = class {
811
817
  const callback = args[lastIndex];
812
818
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
813
819
  const lookup = /* @__PURE__ */ Object.create(null);
820
+ const acquired = [];
814
821
  const views = [];
815
822
  try {
816
823
  for (let i = 0; i < lastIndex; i++) {
817
- const metadata = this.extractMetadata(args[i], {});
818
- if (Object.hasOwn(lookup, metadata.headerAddress)) throw new Error("Duplicate handler");
824
+ const metadata = this.extractMetadata(args[i], this.metadata);
825
+ const headerAddr = metadata.headerAddress;
826
+ if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
819
827
  if (metadata.borrowed) throw new Error("Block is already borrowed");
820
- this.controlBus[metadata.headerAddress + FLAGS_OFFSET] |= VIEW_LOCK;
828
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
821
829
  const rawAddress = metadata.dataAddress >>> this.wordShift;
822
- lookup[metadata.headerAddress] = metadata.headerAddress;
830
+ lookup[headerAddr] = true;
831
+ acquired.push(headerAddr);
823
832
  views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
824
833
  }
825
834
  return callback(...views);
826
835
  } finally {
827
- for (const key in lookup) {
828
- this.controlBus[lookup[key] + FLAGS_OFFSET] &= ~VIEW_LOCK;
836
+ for (let i = 0; i < acquired.length; i++) {
837
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
829
838
  }
830
839
  }
831
840
  }
@@ -913,7 +922,7 @@ var Balloc = class {
913
922
  const metaB = {};
914
923
  this.extractMetadata(source, metaA);
915
924
  this.extractMetadata(destination, metaB);
916
- this.assertNotBorrowed(metaB.headerAddress);
925
+ if (metaB.borrowed) throw new Error("Block is borrowed");
917
926
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
918
927
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
919
928
  const newOccupied = Math.max(metaB.occupied, slotsToCopy);
@@ -945,7 +954,7 @@ var Balloc = class {
945
954
  */
946
955
  copyWithin(handler, target, start, length) {
947
956
  const meta = this.extractMetadata(handler, this.metadata);
948
- this.assertNotBorrowed(meta.headerAddress);
957
+ if (meta.borrowed) throw new Error("Block is borrowed");
949
958
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
950
959
  target = Math.trunc(target);
951
960
  start = Math.trunc(start);
@@ -986,7 +995,7 @@ var Balloc = class {
986
995
  const metaSource = {};
987
996
  this.extractMetadata(target, metaTarget);
988
997
  this.extractMetadata(source, metaSource);
989
- this.assertNotBorrowed(metaTarget.headerAddress);
998
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
990
999
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
991
1000
  const count = Math.min(available, metaSource.occupied);
992
1001
  if (count === 0) return 0;
@@ -1042,8 +1051,8 @@ var Balloc = class {
1042
1051
  const metaB = {};
1043
1052
  this.extractMetadata(source, metaA);
1044
1053
  this.extractMetadata(target, metaB);
1045
- this.assertNotBorrowed(metaA.headerAddress);
1046
- this.assertNotBorrowed(metaB.headerAddress);
1054
+ if (metaA.borrowed) throw new Error("Block is borrowed");
1055
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1047
1056
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
1048
1057
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
1049
1058
  this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
@@ -1132,6 +1141,28 @@ var Balloc = class {
1132
1141
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
1133
1142
  return handler;
1134
1143
  }
1144
+ // ========== INTEGRATION AND DATA INGESTION ==========
1145
+ /**
1146
+ * Loads data through a specialized adapter.
1147
+ *
1148
+ * The adapter receives this instance and uses its public API to allocate
1149
+ * and populate the required blocks. Data organization and interpretation
1150
+ * are the adapter's sole responsibility; Balloc only delegates the
1151
+ * operation and returns the resulting handler.
1152
+ *
1153
+ * The operation is synchronous. The adapter must complete ingestion before
1154
+ * returning and is responsible for reverting its own allocations if
1155
+ * loading fails.
1156
+ *
1157
+ * @param {FormatAdapter} adapter - Adapter implementing load(allocator).
1158
+ * @returns {number} Handler returned by the adapter.
1159
+ * @throws {TypeError} If the adapter does not implement load().
1160
+ * @throws {Error} If the adapter rejects the subscribed infrastructure.
1161
+ */
1162
+ loadData(adapter) {
1163
+ if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
1164
+ return adapter.load(this);
1165
+ }
1135
1166
  // ============= UTILITIES ==============
1136
1167
  /**
1137
1168
  * Prevents modification operations on a borrowed block.
@@ -1142,7 +1173,8 @@ var Balloc = class {
1142
1173
  * @returns {void}
1143
1174
  * @throws {Error} If the block is borrowed via withView().
1144
1175
  * @private
1145
- */
1176
+ * @deprecated
1177
+ */
1146
1178
  assertNotBorrowed(headerAddress) {
1147
1179
  if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
1148
1180
  throw new Error("Block is borrowed");