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/CHANGELOG.md +18 -0
- package/README.MD +37 -7
- package/README.ar.md +31 -1
- package/README.en.md +37 -7
- package/README.es.md +31 -1
- package/README.jp.md +31 -1
- package/README.pt.md +36 -7
- package/README.zh.md +31 -1
- package/docs/API /342/200/224 balloc.ar.md" +56 -0
- package/docs/API /342/200/224 balloc.en.md" +55 -0
- package/docs/API /342/200/224 balloc.es.md" +56 -0
- package/docs/API /342/200/224 balloc.jp.md" +56 -0
- package/docs/API /342/200/224 balloc.pt.md" +56 -0
- package/docs/API /342/200/224 balloc.zh.md" +56 -0
- package/package.json +1 -1
- package/src/ar/balloc.cjs +63 -35
- package/src/ar/balloc.js +1 -1
- package/src/ar/balloc.mjs +78 -39
- package/src/en/balloc.cjs +65 -35
- package/src/en/balloc.js +1 -1
- package/src/en/balloc.mjs +80 -39
- package/src/es/balloc.cjs +65 -35
- package/src/es/balloc.js +1 -1
- package/src/es/balloc.mjs +80 -39
- package/src/jp/balloc.cjs +65 -35
- package/src/jp/balloc.js +1 -1
- package/src/jp/balloc.mjs +80 -39
- package/src/pt/balloc.cjs +67 -35
- package/src/pt/balloc.js +1 -1
- package/src/pt/balloc.mjs +86 -38
- package/src/zh/balloc.cjs +63 -35
- package/src/zh/balloc.js +1 -1
- package/src/zh/balloc.mjs +78 -39
- package/types/ar/balloc.d.mts +36 -1
- package/types/en/balloc.d.mts +38 -1
- package/types/es/balloc.d.mts +38 -1
- package/types/jp/balloc.d.mts +38 -1
- package/types/pt/balloc.d.mts +41 -0
- package/types/zh/balloc.d.mts +36 -1
package/src/jp/balloc.mjs
CHANGED
|
@@ -90,6 +90,7 @@ const CLAMP_11BITS = 0x7FF >>> 0;
|
|
|
90
90
|
* @property {number} generation - ハンドルが属する世代。
|
|
91
91
|
* @property {number} headerAddress - 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 GCを備えたEcho Stackデュアルバス・バンプアロケータ。
|
|
105
106
|
*/
|
|
107
|
+
/**
|
|
108
|
+
* データ取り込み用の同期アダプター契約。
|
|
109
|
+
*
|
|
110
|
+
* アダプターは Balloc インスタンスを受け取り、その公開 API を使用してデータを格納し、主要ブロックを識別するハンドラーを返します。
|
|
111
|
+
*
|
|
112
|
+
* @typedef {Object} FormatAdapter
|
|
113
|
+
* @property {(allocator: Balloc) => number} load - アロケーターにデータを読み込み、対応するハンドラーを返します。
|
|
114
|
+
*/
|
|
115
|
+
|
|
106
116
|
export default class Balloc {
|
|
107
117
|
|
|
108
118
|
/**
|
|
@@ -261,12 +271,15 @@ export default class Balloc {
|
|
|
261
271
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
262
272
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
263
273
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
274
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
264
275
|
|
|
265
276
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
266
277
|
throw new Error("Invalid or corrupted handle");
|
|
267
278
|
}
|
|
268
279
|
|
|
269
|
-
|
|
280
|
+
if (borrowed) {
|
|
281
|
+
throw new Error("Block is borrowed");
|
|
282
|
+
}
|
|
270
283
|
|
|
271
284
|
if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
272
285
|
|
|
@@ -314,7 +327,7 @@ export default class Balloc {
|
|
|
314
327
|
*/
|
|
315
328
|
shrink(handler) {
|
|
316
329
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
317
|
-
|
|
330
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
318
331
|
|
|
319
332
|
const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
|
|
320
333
|
if (newCapacity >= meta.capacityExponent) return;
|
|
@@ -345,12 +358,15 @@ export default class Balloc {
|
|
|
345
358
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
346
359
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
347
360
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
361
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
348
362
|
|
|
349
363
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
350
364
|
throw new Error("Invalid or corrupted handle");
|
|
351
365
|
}
|
|
352
366
|
|
|
353
|
-
|
|
367
|
+
if (borrowed) {
|
|
368
|
+
throw new Error("Block is borrowed");
|
|
369
|
+
}
|
|
354
370
|
|
|
355
371
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
356
372
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
@@ -546,7 +562,7 @@ export default class Balloc {
|
|
|
546
562
|
*/
|
|
547
563
|
push(handler, value) {
|
|
548
564
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
549
|
-
|
|
565
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
550
566
|
|
|
551
567
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
552
568
|
|
|
@@ -568,7 +584,7 @@ export default class Balloc {
|
|
|
568
584
|
*/
|
|
569
585
|
pop(handler) {
|
|
570
586
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
571
|
-
|
|
587
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
572
588
|
|
|
573
589
|
if (meta.occupied === 0) return undefined;
|
|
574
590
|
|
|
@@ -608,7 +624,7 @@ export default class Balloc {
|
|
|
608
624
|
*/
|
|
609
625
|
nudge(handler, value) {
|
|
610
626
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
611
|
-
|
|
627
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
612
628
|
|
|
613
629
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
614
630
|
|
|
@@ -633,7 +649,7 @@ export default class Balloc {
|
|
|
633
649
|
*/
|
|
634
650
|
pull(handler) {
|
|
635
651
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
636
|
-
|
|
652
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
637
653
|
|
|
638
654
|
if (meta.occupied === 0) return undefined;
|
|
639
655
|
|
|
@@ -769,7 +785,7 @@ export default class Balloc {
|
|
|
769
785
|
*/
|
|
770
786
|
set(handler, index, value) {
|
|
771
787
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
772
|
-
|
|
788
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
773
789
|
|
|
774
790
|
if (index < 0 || index >= meta.occupied) return false;
|
|
775
791
|
|
|
@@ -792,7 +808,7 @@ export default class Balloc {
|
|
|
792
808
|
*/
|
|
793
809
|
clear(handler, value = 0) {
|
|
794
810
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
795
|
-
|
|
811
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
796
812
|
|
|
797
813
|
const slots = meta.byteCapacity / this.wordSize;
|
|
798
814
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
@@ -823,7 +839,7 @@ export default class Balloc {
|
|
|
823
839
|
*/
|
|
824
840
|
fill(handler, value, start = 0, end = null) {
|
|
825
841
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
826
|
-
|
|
842
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
827
843
|
|
|
828
844
|
const slots = meta.byteCapacity / this.wordSize;
|
|
829
845
|
const lastIndex = end ?? slots;
|
|
@@ -855,7 +871,7 @@ export default class Balloc {
|
|
|
855
871
|
*/
|
|
856
872
|
fillAll(handler, value) {
|
|
857
873
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
858
|
-
|
|
874
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
859
875
|
|
|
860
876
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
861
877
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
@@ -882,7 +898,7 @@ export default class Balloc {
|
|
|
882
898
|
*/
|
|
883
899
|
put(handler, data) {
|
|
884
900
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
885
|
-
|
|
901
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
886
902
|
|
|
887
903
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
888
904
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
@@ -943,37 +959,35 @@ export default class Balloc {
|
|
|
943
959
|
*/
|
|
944
960
|
withView(...args) {
|
|
945
961
|
if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
|
|
946
|
-
|
|
947
|
-
const callback = args
|
|
962
|
+
const lastIndex = args.length - 1;
|
|
963
|
+
const callback = args[lastIndex];
|
|
948
964
|
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
949
965
|
|
|
950
|
-
const
|
|
951
|
-
const
|
|
952
|
-
const
|
|
966
|
+
const lookup = Object.create(null);
|
|
967
|
+
const acquired = [];
|
|
968
|
+
const views = [];
|
|
953
969
|
|
|
954
|
-
|
|
970
|
+
try {
|
|
971
|
+
for (let i = 0; i < lastIndex; i++) {
|
|
972
|
+
const metadata = this.extractMetadata(args[i], this.metadata);
|
|
973
|
+
const headerAddr = metadata.headerAddress;
|
|
955
974
|
|
|
956
|
-
const flags = headers.map(header => this.controlBus[header + FLAGS_OFFSET]);
|
|
957
975
|
|
|
958
|
-
|
|
976
|
+
if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
|
|
977
|
+
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
959
978
|
|
|
960
|
-
|
|
979
|
+
this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
961
980
|
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
acquiredCount++;
|
|
966
|
-
}
|
|
967
|
-
|
|
968
|
-
const views = metadata.map(meta => {
|
|
969
|
-
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
970
|
-
return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
|
|
971
|
-
});
|
|
981
|
+
const rawAddress = metadata.dataAddress >>> this.wordShift;
|
|
982
|
+
lookup[headerAddr] = true;
|
|
983
|
+
acquired.push(headerAddr);
|
|
972
984
|
|
|
985
|
+
views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied))
|
|
986
|
+
}
|
|
973
987
|
return callback(...views);
|
|
974
988
|
} finally {
|
|
975
|
-
for (let i = 0; i <
|
|
976
|
-
this.controlBus[
|
|
989
|
+
for (let i = 0; i < acquired.length; i++) {
|
|
990
|
+
this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
977
991
|
}
|
|
978
992
|
}
|
|
979
993
|
}
|
|
@@ -1068,7 +1082,7 @@ export default class Balloc {
|
|
|
1068
1082
|
|
|
1069
1083
|
this.extractMetadata(source, metaA);
|
|
1070
1084
|
this.extractMetadata(destination, metaB);
|
|
1071
|
-
|
|
1085
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1072
1086
|
|
|
1073
1087
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
1074
1088
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
@@ -1101,7 +1115,7 @@ export default class Balloc {
|
|
|
1101
1115
|
*/
|
|
1102
1116
|
copyWithin(handler, target, start, length) {
|
|
1103
1117
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
1104
|
-
|
|
1118
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
1105
1119
|
|
|
1106
1120
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
1107
1121
|
target = Math.trunc(target);
|
|
@@ -1149,7 +1163,8 @@ export default class Balloc {
|
|
|
1149
1163
|
|
|
1150
1164
|
this.extractMetadata(target, metaTarget);
|
|
1151
1165
|
this.extractMetadata(source, metaSource);
|
|
1152
|
-
|
|
1166
|
+
if (metaTarget.borrowed) throw new Error("Block is borrowed");
|
|
1167
|
+
|
|
1153
1168
|
|
|
1154
1169
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
1155
1170
|
const count = Math.min(available, metaSource.occupied);
|
|
@@ -1216,8 +1231,9 @@ export default class Balloc {
|
|
|
1216
1231
|
this.extractMetadata(source, metaA);
|
|
1217
1232
|
this.extractMetadata(target, metaB);
|
|
1218
1233
|
|
|
1219
|
-
|
|
1220
|
-
|
|
1234
|
+
if (metaA.borrowed) throw new Error("Block is borrowed");
|
|
1235
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1236
|
+
|
|
1221
1237
|
|
|
1222
1238
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
1223
1239
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
@@ -1331,6 +1347,29 @@ export default class Balloc {
|
|
|
1331
1347
|
return handler;
|
|
1332
1348
|
}
|
|
1333
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
|
+
}
|
|
1334
1373
|
// ============= ユーティリティ (UTILITIES) ==============
|
|
1335
1374
|
|
|
1336
1375
|
/**
|
|
@@ -1342,7 +1381,8 @@ export default class Balloc {
|
|
|
1342
1381
|
* @returns {void}
|
|
1343
1382
|
* @throws {Error} ブロックが withView() によって借用されている場合。
|
|
1344
1383
|
* @private
|
|
1345
|
-
|
|
1384
|
+
* @deprecated
|
|
1385
|
+
*/
|
|
1346
1386
|
assertNotBorrowed(headerAddress) {
|
|
1347
1387
|
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1348
1388
|
throw new Error("Block is borrowed");
|
|
@@ -1398,6 +1438,7 @@ export default class Balloc {
|
|
|
1398
1438
|
out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
|
|
1399
1439
|
out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
|
|
1400
1440
|
out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
1441
|
+
out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
|
|
1401
1442
|
|
|
1402
1443
|
return out;
|
|
1403
1444
|
}
|
package/src/pt/balloc.cjs
CHANGED
|
@@ -107,7 +107,8 @@ var Balloc = class {
|
|
|
107
107
|
dataAddress: 0 >>> 0,
|
|
108
108
|
capacityExponent: 0 >>> 0,
|
|
109
109
|
byteCapacity: 0 >>> 0,
|
|
110
|
-
occupied: 0 >>> 0
|
|
110
|
+
occupied: 0 >>> 0,
|
|
111
|
+
borrowed: 0 >>> 0
|
|
111
112
|
};
|
|
112
113
|
}
|
|
113
114
|
// ========== ALOCAÇÃO ==========
|
|
@@ -193,10 +194,13 @@ var Balloc = class {
|
|
|
193
194
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
194
195
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
195
196
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
197
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
196
198
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
197
199
|
throw new Error("Invalid or corrupted handle");
|
|
198
200
|
}
|
|
199
|
-
|
|
201
|
+
if (borrowed) {
|
|
202
|
+
throw new Error("Block is borrowed");
|
|
203
|
+
}
|
|
200
204
|
if (capacity >= 31) throw new AggregateError([new Error("Can\xB4t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
201
205
|
const newCapacity = capacity + 1;
|
|
202
206
|
const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
|
|
@@ -233,7 +237,7 @@ var Balloc = class {
|
|
|
233
237
|
*/
|
|
234
238
|
shrink(handler) {
|
|
235
239
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
236
|
-
|
|
240
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
237
241
|
const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
|
|
238
242
|
if (newCapacity >= meta.capacityExponent) return;
|
|
239
243
|
const oldBlockSize = meta.byteCapacity;
|
|
@@ -257,10 +261,13 @@ var Balloc = class {
|
|
|
257
261
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
258
262
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
259
263
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
264
|
+
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
260
265
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
261
266
|
throw new Error("Invalid or corrupted handle");
|
|
262
267
|
}
|
|
263
|
-
|
|
268
|
+
if (borrowed) {
|
|
269
|
+
throw new Error("Block is borrowed");
|
|
270
|
+
}
|
|
264
271
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
265
272
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
266
273
|
if (this.echoPointer + 4 <= this.controlBus.byteLength) {
|
|
@@ -440,7 +447,7 @@ var Balloc = class {
|
|
|
440
447
|
*/
|
|
441
448
|
push(handler, value) {
|
|
442
449
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
443
|
-
|
|
450
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
444
451
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
445
452
|
this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
446
453
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
|
|
@@ -460,7 +467,7 @@ var Balloc = class {
|
|
|
460
467
|
*/
|
|
461
468
|
pop(handler) {
|
|
462
469
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
463
|
-
|
|
470
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
464
471
|
if (meta.occupied === 0) return void 0;
|
|
465
472
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
466
473
|
return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
|
|
@@ -497,7 +504,7 @@ var Balloc = class {
|
|
|
497
504
|
*/
|
|
498
505
|
nudge(handler, value) {
|
|
499
506
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
500
|
-
|
|
507
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
501
508
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
502
509
|
this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
|
|
503
510
|
this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
|
|
@@ -519,7 +526,7 @@ var Balloc = class {
|
|
|
519
526
|
*/
|
|
520
527
|
pull(handler) {
|
|
521
528
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
522
|
-
|
|
529
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
523
530
|
if (meta.occupied === 0) return void 0;
|
|
524
531
|
const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
525
532
|
this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
|
|
@@ -638,7 +645,7 @@ var Balloc = class {
|
|
|
638
645
|
*/
|
|
639
646
|
set(handler, index, value) {
|
|
640
647
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
641
|
-
|
|
648
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
642
649
|
if (index < 0 || index >= meta.occupied) return false;
|
|
643
650
|
this.writer(meta.dataAddress + index * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
644
651
|
return true;
|
|
@@ -659,7 +666,7 @@ var Balloc = class {
|
|
|
659
666
|
*/
|
|
660
667
|
clear(handler, value = 0) {
|
|
661
668
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
662
|
-
|
|
669
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
663
670
|
const slots = meta.byteCapacity / this.wordSize;
|
|
664
671
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
665
672
|
this.auxiliary.fill(value, rawAddress, rawAddress + slots);
|
|
@@ -688,7 +695,7 @@ var Balloc = class {
|
|
|
688
695
|
*/
|
|
689
696
|
fill(handler, value, start = 0, end = null) {
|
|
690
697
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
691
|
-
|
|
698
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
692
699
|
const slots = meta.byteCapacity / this.wordSize;
|
|
693
700
|
const lastIndex = end ?? slots;
|
|
694
701
|
if (start > lastIndex) {
|
|
@@ -718,7 +725,7 @@ var Balloc = class {
|
|
|
718
725
|
*/
|
|
719
726
|
fillAll(handler, value) {
|
|
720
727
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
721
|
-
|
|
728
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
722
729
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
723
730
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
724
731
|
this.auxiliary.fill(value, rawAddress, rawAddress + capacityElements);
|
|
@@ -744,7 +751,7 @@ var Balloc = class {
|
|
|
744
751
|
*/
|
|
745
752
|
put(handler, data) {
|
|
746
753
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
747
|
-
|
|
754
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
748
755
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
749
756
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
750
757
|
if (Array.isArray(data)) {
|
|
@@ -797,28 +804,28 @@ var Balloc = class {
|
|
|
797
804
|
*/
|
|
798
805
|
withView(...args) {
|
|
799
806
|
if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
|
|
800
|
-
const
|
|
807
|
+
const lastIndex = args.length - 1;
|
|
808
|
+
const callback = args[lastIndex];
|
|
801
809
|
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
802
|
-
const
|
|
803
|
-
const
|
|
804
|
-
const
|
|
805
|
-
if (new Set(headers).size !== headers.length) throw new Error("Duplicate handler");
|
|
806
|
-
const flags = headers.map((header) => this.controlBus[header + FLAGS_OFFSET]);
|
|
807
|
-
if (flags.some((flag) => flag & VIEW_LOCK)) throw new Error("Block is already borrowed");
|
|
808
|
-
let acquiredCount = 0;
|
|
810
|
+
const lookup = /* @__PURE__ */ Object.create(null);
|
|
811
|
+
const acquired = [];
|
|
812
|
+
const views = [];
|
|
809
813
|
try {
|
|
810
|
-
for (
|
|
811
|
-
this.
|
|
812
|
-
|
|
814
|
+
for (let i = 0; i < lastIndex; i++) {
|
|
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");
|
|
818
|
+
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
819
|
+
this.controlBus[headerAddr + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
820
|
+
const rawAddress = metadata.dataAddress >>> this.wordShift;
|
|
821
|
+
lookup[headerAddr] = true;
|
|
822
|
+
acquired.push(headerAddr);
|
|
823
|
+
views.push(this.auxiliary.subarray(rawAddress, rawAddress + metadata.occupied));
|
|
813
824
|
}
|
|
814
|
-
const views = metadata.map((meta) => {
|
|
815
|
-
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
816
|
-
return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
|
|
817
|
-
});
|
|
818
825
|
return callback(...views);
|
|
819
826
|
} finally {
|
|
820
|
-
for (let i = 0; i <
|
|
821
|
-
this.controlBus[
|
|
827
|
+
for (let i = 0; i < acquired.length; i++) {
|
|
828
|
+
this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
822
829
|
}
|
|
823
830
|
}
|
|
824
831
|
}
|
|
@@ -908,7 +915,7 @@ var Balloc = class {
|
|
|
908
915
|
const metaB = {};
|
|
909
916
|
this.extractMetadata(source, metaA);
|
|
910
917
|
this.extractMetadata(destination, metaB);
|
|
911
|
-
|
|
918
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
912
919
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
913
920
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
914
921
|
const newOccupied = Math.max(metaB.occupied, slotsToCopy);
|
|
@@ -940,7 +947,7 @@ var Balloc = class {
|
|
|
940
947
|
*/
|
|
941
948
|
copyWithin(handler, target, start, length) {
|
|
942
949
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
943
|
-
|
|
950
|
+
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
944
951
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
945
952
|
target = Math.trunc(target);
|
|
946
953
|
start = Math.trunc(start);
|
|
@@ -981,7 +988,7 @@ var Balloc = class {
|
|
|
981
988
|
const metaSource = {};
|
|
982
989
|
this.extractMetadata(target, metaTarget);
|
|
983
990
|
this.extractMetadata(source, metaSource);
|
|
984
|
-
|
|
991
|
+
if (metaTarget.borrowed) throw new Error("Block is borrowed");
|
|
985
992
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
986
993
|
const count = Math.min(available, metaSource.occupied);
|
|
987
994
|
if (count === 0) return 0;
|
|
@@ -1037,8 +1044,8 @@ var Balloc = class {
|
|
|
1037
1044
|
const metaB = {};
|
|
1038
1045
|
this.extractMetadata(source, metaA);
|
|
1039
1046
|
this.extractMetadata(target, metaB);
|
|
1040
|
-
|
|
1041
|
-
|
|
1047
|
+
if (metaA.borrowed) throw new Error("Block is borrowed");
|
|
1048
|
+
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
1042
1049
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
1043
1050
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
1044
1051
|
this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
|
|
@@ -1127,8 +1134,31 @@ var Balloc = class {
|
|
|
1127
1134
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
|
|
1128
1135
|
return handler;
|
|
1129
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
|
+
}
|
|
1130
1159
|
// ============= UTILITÁRIOS ==============
|
|
1131
1160
|
/**
|
|
1161
|
+
* [OBSOLETO]
|
|
1132
1162
|
* Impede operações de modificação sobre um bloco emprestado.
|
|
1133
1163
|
*
|
|
1134
1164
|
* Verifica a flag VIEW_LOCK no header associado ao endereço informado.
|
|
@@ -1136,6 +1166,7 @@ var Balloc = class {
|
|
|
1136
1166
|
* @param {number} headerAddress - Endereço do header no controlBus.
|
|
1137
1167
|
* @returns {void}
|
|
1138
1168
|
* @throws {Error} Se o bloco estiver emprestado por withView().
|
|
1169
|
+
* @deprecated
|
|
1139
1170
|
* @private
|
|
1140
1171
|
*/
|
|
1141
1172
|
assertNotBorrowed(headerAddress) {
|
|
@@ -1189,6 +1220,7 @@ var Balloc = class {
|
|
|
1189
1220
|
out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
|
|
1190
1221
|
out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
|
|
1191
1222
|
out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
1223
|
+
out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
|
|
1192
1224
|
return out;
|
|
1193
1225
|
}
|
|
1194
1226
|
/**
|