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/ar/balloc.mjs CHANGED
@@ -90,6 +90,7 @@ const CLAMP_11BITS = 0x7FF >>> 0;
90
90
  * @property {number} generation - الجيل الذي ينتمي إليه المقبض.
91
91
  * @property {number} headerAddress - عنوان الترويسة (Header) داخل controlBus.
92
92
  * @property {number} occupied - عدد العناصر المشغولة في الكتلة.
93
+ * @property {number} borrowed - علامة تشير إلى ما إذا كانت الكتلة قد أُعيرت بواسطة withView().
93
94
  */
94
95
 
95
96
  /**
@@ -103,6 +104,15 @@ const isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataVi
103
104
  /**
104
105
  * مخصص الذاكرة المتقاطع ذو الحافتين مع مكدس صدى وإدارة النفايات ReBump.
105
106
  */
107
+ /**
108
+ * عقد محوّل متزامن لإدخال البيانات.
109
+ *
110
+ * يتلقى المحوّل نسخة Balloc ويستخدم واجهتها العامة لتخزين البيانات، ثم يعيد المعالج الذي يحدد الكتلة الرئيسية.
111
+ *
112
+ * @typedef {Object} FormatAdapter
113
+ * @property {(allocator: Balloc) => number} load - يحمّل البيانات إلى المخصّص ويعيد المعالج المقابل.
114
+ */
115
+
106
116
  export default class Balloc {
107
117
 
108
118
  /**
@@ -264,12 +274,15 @@ export default class Balloc {
264
274
  const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
265
275
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
266
276
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
277
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
267
278
 
268
279
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
269
280
  throw new Error("Invalid or corrupted handle");
270
281
  }
271
282
 
272
- this.assertNotBorrowed(headerAddress);
283
+ if (borrowed) {
284
+ throw new Error("Block is borrowed");
285
+ }
273
286
 
274
287
  if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
275
288
 
@@ -316,7 +329,7 @@ export default class Balloc {
316
329
  */
317
330
  shrink(handler) {
318
331
  const meta = this.extractMetadata(handler, this.metadata);
319
- this.assertNotBorrowed(meta.headerAddress);
332
+ if (meta.borrowed) throw new Error("Block is borrowed");
320
333
 
321
334
  const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
322
335
  if (newCapacity >= meta.capacityExponent) return;
@@ -347,12 +360,15 @@ export default class Balloc {
347
360
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
348
361
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
349
362
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
363
+ const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
350
364
 
351
365
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
352
366
  throw new Error("Invalid or corrupted handle");
353
367
  }
354
368
 
355
- this.assertNotBorrowed(headerAddress);
369
+ if (borrowed) {
370
+ throw new Error("Block is borrowed");
371
+ }
356
372
 
357
373
  const blockSize = (1 << capacity >>> 0) * this.wordSize;
358
374
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
@@ -551,7 +567,7 @@ export default class Balloc {
551
567
  */
552
568
  push(handler, value) {
553
569
  const meta = this.extractMetadata(handler, this.metadata);
554
- this.assertNotBorrowed(meta.headerAddress);
570
+ if (meta.borrowed) throw new Error("Block is borrowed");
555
571
 
556
572
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
557
573
 
@@ -575,7 +591,7 @@ export default class Balloc {
575
591
  */
576
592
  pop(handler) {
577
593
  const meta = this.extractMetadata(handler, this.metadata);
578
- this.assertNotBorrowed(meta.headerAddress);
594
+ if (meta.borrowed) throw new Error("Block is borrowed");
579
595
 
580
596
  if (meta.occupied === 0) return undefined;
581
597
 
@@ -619,7 +635,7 @@ export default class Balloc {
619
635
  */
620
636
  nudge(handler, value) {
621
637
  const meta = this.extractMetadata(handler, this.metadata);
622
- this.assertNotBorrowed(meta.headerAddress);
638
+ if (meta.borrowed) throw new Error("Block is borrowed");
623
639
 
624
640
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
625
641
 
@@ -645,7 +661,7 @@ export default class Balloc {
645
661
  */
646
662
  pull(handler) {
647
663
  const meta = this.extractMetadata(handler, this.metadata);
648
- this.assertNotBorrowed(meta.headerAddress);
664
+ if (meta.borrowed) throw new Error("Block is borrowed");
649
665
 
650
666
  if (meta.occupied === 0) return undefined;
651
667
 
@@ -788,7 +804,7 @@ export default class Balloc {
788
804
  */
789
805
  set(handler, index, value) {
790
806
  const meta = this.extractMetadata(handler, this.metadata);
791
- this.assertNotBorrowed(meta.headerAddress);
807
+ if (meta.borrowed) throw new Error("Block is borrowed");
792
808
 
793
809
  if (index < 0 || index >= meta.occupied) return false;
794
810
 
@@ -813,7 +829,7 @@ export default class Balloc {
813
829
  */
814
830
  clear(handler, value = 0) {
815
831
  const meta = this.extractMetadata(handler, this.metadata);
816
- this.assertNotBorrowed(meta.headerAddress);
832
+ if (meta.borrowed) throw new Error("Block is borrowed");
817
833
 
818
834
  const slots = meta.byteCapacity / this.wordSize;
819
835
  const rawAddress = meta.dataAddress >>> this.wordShift;
@@ -846,7 +862,7 @@ export default class Balloc {
846
862
  */
847
863
  fill(handler, value, start = 0, end = null) {
848
864
  const meta = this.extractMetadata(handler, this.metadata);
849
- this.assertNotBorrowed(meta.headerAddress);
865
+ if (meta.borrowed) throw new Error("Block is borrowed");
850
866
 
851
867
  const slots = meta.byteCapacity / this.wordSize;
852
868
  const lastIndex = end ?? slots;
@@ -879,7 +895,7 @@ export default class Balloc {
879
895
  */
880
896
  fillAll(handler, value) {
881
897
  const meta = this.extractMetadata(handler, this.metadata);
882
- this.assertNotBorrowed(meta.headerAddress);
898
+ if (meta.borrowed) throw new Error("Block is borrowed");
883
899
 
884
900
  const rawAddress = meta.dataAddress >>> this.wordShift;
885
901
  const capacityElements = meta.byteCapacity / this.wordSize;
@@ -908,7 +924,7 @@ export default class Balloc {
908
924
  */
909
925
  put(handler, data) {
910
926
  const meta = this.extractMetadata(handler, this.metadata);
911
- this.assertNotBorrowed(meta.headerAddress);
927
+ if (meta.borrowed) throw new Error("Block is borrowed");
912
928
 
913
929
  const rawAddress = meta.dataAddress >>> this.wordShift;
914
930
  const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
@@ -970,37 +986,35 @@ export default class Balloc {
970
986
  */
971
987
  withView(...args) {
972
988
  if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
973
-
974
- const callback = args.pop();
989
+ const lastIndex = args.length - 1;
990
+ const callback = args[lastIndex];
975
991
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
976
992
 
977
- const handlers = args;
978
- const metadata = handlers.map(handler => this.extractMetadata(handler, {}));
979
- const headers = metadata.map(meta => meta.headerAddress);
993
+ const lookup = Object.create(null);
994
+ const acquired = [];
995
+ const views = [];
980
996
 
981
- if (new Set(headers).size !== headers.length) throw new Error("Duplicate handler");
997
+ try {
998
+ for (let i = 0; i < lastIndex; i++) {
999
+ const metadata = this.extractMetadata(args[i], this.metadata);
1000
+ const headerAddr = metadata.headerAddress;
982
1001
 
983
- const flags = headers.map(header => this.controlBus[header + FLAGS_OFFSET]);
984
1002
 
985
- if (flags.some(flag => flag & VIEW_LOCK)) throw new Error("Block is already borrowed");
1003
+ if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
1004
+ if (metadata.borrowed) throw new Error("Block is already borrowed");
986
1005
 
987
- let acquiredCount = 0;
1006
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
988
1007
 
989
- try {
990
- for (const header of headers) {
991
- this.controlBus[header + FLAGS_OFFSET] |= VIEW_LOCK;
992
- acquiredCount++;
993
- }
994
-
995
- const views = metadata.map(meta => {
996
- const rawAddress = meta.dataAddress >>> this.wordShift;
997
- return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
998
- });
1008
+ const rawAddress = metadata.dataAddress >>> this.wordShift;
1009
+ lookup[headerAddr] = true;
1010
+ acquired.push(headerAddr);
999
1011
 
1012
+ views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied))
1013
+ }
1000
1014
  return callback(...views);
1001
1015
  } finally {
1002
- for (let i = 0; i < acquiredCount; i++) {
1003
- this.controlBus[headers[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
1016
+ for (let i = 0; i < acquired.length; i++) {
1017
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
1004
1018
  }
1005
1019
  }
1006
1020
  }
@@ -1099,7 +1113,7 @@ export default class Balloc {
1099
1113
 
1100
1114
  this.extractMetadata(source, metaA);
1101
1115
  this.extractMetadata(destination, metaB);
1102
- this.assertNotBorrowed(metaB.headerAddress);
1116
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1103
1117
 
1104
1118
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
1105
1119
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
@@ -1133,7 +1147,7 @@ export default class Balloc {
1133
1147
  */
1134
1148
  copyWithin(handler, target, start, length) {
1135
1149
  const meta = this.extractMetadata(handler, this.metadata);
1136
- this.assertNotBorrowed(meta.headerAddress);
1150
+ if (meta.borrowed) throw new Error("Block is borrowed");
1137
1151
 
1138
1152
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
1139
1153
  target = Math.trunc(target);
@@ -1183,7 +1197,8 @@ export default class Balloc {
1183
1197
 
1184
1198
  this.extractMetadata(target, metaTarget);
1185
1199
  this.extractMetadata(source, metaSource);
1186
- this.assertNotBorrowed(metaTarget.headerAddress);
1200
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
1201
+
1187
1202
 
1188
1203
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
1189
1204
  const count = Math.min(available, metaSource.occupied);
@@ -1253,8 +1268,9 @@ export default class Balloc {
1253
1268
  this.extractMetadata(source, metaA);
1254
1269
  this.extractMetadata(target, metaB);
1255
1270
 
1256
- this.assertNotBorrowed(metaA.headerAddress);
1257
- 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
+
1258
1274
 
1259
1275
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
1260
1276
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
@@ -1368,6 +1384,27 @@ export default class Balloc {
1368
1384
  return handler;
1369
1385
  }
1370
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
+ }
1371
1408
  // ============= الأدوات المساعدة (UTILITIES) ==============
1372
1409
 
1373
1410
  /**
@@ -1379,7 +1416,8 @@ export default class Balloc {
1379
1416
  * @returns {void}
1380
1417
  * @throws {Error} إذا كانت الكتلة معارة بواسطة withView().
1381
1418
  * @private
1382
- */
1419
+ * @deprecated
1420
+ */
1383
1421
  assertNotBorrowed(headerAddress) {
1384
1422
  if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
1385
1423
  throw new Error("Block is borrowed");
@@ -1436,6 +1474,7 @@ export default class Balloc {
1436
1474
  out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
1437
1475
  out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
1438
1476
  out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
1477
+ out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
1439
1478
 
1440
1479
  return out;
1441
1480
  }
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)) {
@@ -807,28 +813,28 @@ var Balloc = class {
807
813
  */
808
814
  withView(...args) {
809
815
  if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
810
- const callback = args.pop();
816
+ const lastIndex = args.length - 1;
817
+ const callback = args[lastIndex];
811
818
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
812
- const handlers = args;
813
- const metadata = handlers.map((handler) => this.extractMetadata(handler, {}));
814
- const headers = metadata.map((meta) => meta.headerAddress);
815
- if (new Set(headers).size !== headers.length) throw new Error("Duplicate handler");
816
- const flags = headers.map((header) => this.controlBus[header + FLAGS_OFFSET]);
817
- if (flags.some((flag) => flag & VIEW_LOCK)) throw new Error("Block is already borrowed");
818
- let acquiredCount = 0;
819
+ const lookup = /* @__PURE__ */ Object.create(null);
820
+ const acquired = [];
821
+ const views = [];
819
822
  try {
820
- for (const header of headers) {
821
- this.controlBus[header + FLAGS_OFFSET] |= VIEW_LOCK;
822
- acquiredCount++;
823
+ for (let i = 0; i < lastIndex; i++) {
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");
827
+ if (metadata.borrowed) throw new Error("Block is already borrowed");
828
+ this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
829
+ const rawAddress = metadata.dataAddress >>> this.wordShift;
830
+ lookup[headerAddr] = true;
831
+ acquired.push(headerAddr);
832
+ views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
823
833
  }
824
- const views = metadata.map((meta) => {
825
- const rawAddress = meta.dataAddress >>> this.wordShift;
826
- return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
827
- });
828
834
  return callback(...views);
829
835
  } finally {
830
- for (let i = 0; i < acquiredCount; i++) {
831
- this.controlBus[headers[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
836
+ for (let i = 0; i < acquired.length; i++) {
837
+ this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
832
838
  }
833
839
  }
834
840
  }
@@ -916,7 +922,7 @@ var Balloc = class {
916
922
  const metaB = {};
917
923
  this.extractMetadata(source, metaA);
918
924
  this.extractMetadata(destination, metaB);
919
- this.assertNotBorrowed(metaB.headerAddress);
925
+ if (metaB.borrowed) throw new Error("Block is borrowed");
920
926
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
921
927
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
922
928
  const newOccupied = Math.max(metaB.occupied, slotsToCopy);
@@ -948,7 +954,7 @@ var Balloc = class {
948
954
  */
949
955
  copyWithin(handler, target, start, length) {
950
956
  const meta = this.extractMetadata(handler, this.metadata);
951
- this.assertNotBorrowed(meta.headerAddress);
957
+ if (meta.borrowed) throw new Error("Block is borrowed");
952
958
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
953
959
  target = Math.trunc(target);
954
960
  start = Math.trunc(start);
@@ -989,7 +995,7 @@ var Balloc = class {
989
995
  const metaSource = {};
990
996
  this.extractMetadata(target, metaTarget);
991
997
  this.extractMetadata(source, metaSource);
992
- this.assertNotBorrowed(metaTarget.headerAddress);
998
+ if (metaTarget.borrowed) throw new Error("Block is borrowed");
993
999
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
994
1000
  const count = Math.min(available, metaSource.occupied);
995
1001
  if (count === 0) return 0;
@@ -1045,8 +1051,8 @@ var Balloc = class {
1045
1051
  const metaB = {};
1046
1052
  this.extractMetadata(source, metaA);
1047
1053
  this.extractMetadata(target, metaB);
1048
- this.assertNotBorrowed(metaA.headerAddress);
1049
- this.assertNotBorrowed(metaB.headerAddress);
1054
+ if (metaA.borrowed) throw new Error("Block is borrowed");
1055
+ if (metaB.borrowed) throw new Error("Block is borrowed");
1050
1056
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
1051
1057
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
1052
1058
  this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
@@ -1135,6 +1141,28 @@ var Balloc = class {
1135
1141
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
1136
1142
  return handler;
1137
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
+ }
1138
1166
  // ============= UTILITIES ==============
1139
1167
  /**
1140
1168
  * Prevents modification operations on a borrowed block.
@@ -1145,7 +1173,8 @@ var Balloc = class {
1145
1173
  * @returns {void}
1146
1174
  * @throws {Error} If the block is borrowed via withView().
1147
1175
  * @private
1148
- */
1176
+ * @deprecated
1177
+ */
1149
1178
  assertNotBorrowed(headerAddress) {
1150
1179
  if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
1151
1180
  throw new Error("Block is borrowed");
@@ -1197,6 +1226,7 @@ var Balloc = class {
1197
1226
  out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
1198
1227
  out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
1199
1228
  out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
1229
+ out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
1200
1230
  return out;
1201
1231
  }
1202
1232
  /**