balloc 1.0.1 → 1.3.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 +29 -0
- package/README.MD +22 -14
- package/README.ar.md +7 -3
- package/README.en.md +7 -3
- package/README.es.md +7 -3
- package/README.jp.md +7 -3
- package/README.pt.md +6 -6
- package/README.zh.md +7 -3
- package/docs/API /342/200/224 balloc.ar.md" +113 -25
- package/docs/API /342/200/224 balloc.en.md" +153 -15
- package/docs/API /342/200/224 balloc.es.md" +100 -11
- package/docs/API /342/200/224 balloc.jp.md" +137 -16
- package/docs/API /342/200/224 balloc.pt.md" +150 -25
- package/docs/API /342/200/224 balloc.zh.md" +108 -8
- package/package.json +22 -8
- package/src/ar/balloc.cjs +502 -306
- package/src/ar/balloc.js +1 -1
- package/src/ar/balloc.mjs +617 -392
- package/src/en/balloc.cjs +428 -218
- package/src/en/balloc.js +1 -1
- package/src/en/balloc.mjs +542 -303
- package/src/es/balloc.cjs +409 -230
- package/src/es/balloc.js +1 -1
- package/src/es/balloc.mjs +540 -333
- package/src/jp/balloc.cjs +437 -274
- package/src/jp/balloc.js +1 -1
- package/src/jp/balloc.mjs +551 -359
- package/src/pt/balloc.cjs +238 -58
- package/src/pt/balloc.js +1 -1
- package/src/pt/balloc.mjs +353 -144
- package/src/zh/balloc.cjs +358 -194
- package/src/zh/balloc.js +1 -1
- package/src/zh/balloc.mjs +475 -283
- package/types/ar/balloc.d.mts +388 -277
- package/types/en/balloc.d.mts +329 -204
- package/types/es/balloc.d.mts +302 -208
- package/types/jp/balloc.d.mts +330 -252
- package/types/pt/balloc.d.mts +138 -43
- package/types/zh/balloc.d.mts +262 -183
- package/img/pix.png +0 -0
package/src/jp/balloc.cjs
CHANGED
|
@@ -43,34 +43,36 @@ var ADDRESS_OFFSET = 0 >>> 0;
|
|
|
43
43
|
var OCCUPIED_OFFSET = 4 >>> 0;
|
|
44
44
|
var CAPACITY_OFFSET = 8 >>> 0;
|
|
45
45
|
var COOKIE_OFFSET = 9 >>> 0;
|
|
46
|
+
var FLAGS_OFFSET = 10 >>> 0;
|
|
47
|
+
var VIEW_LOCK = 1 >>> 0;
|
|
46
48
|
var ROTATE_32BITS = 4294967296;
|
|
47
49
|
var CLAMP_11BITS = 2047 >>> 0;
|
|
48
50
|
var isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataView);
|
|
49
51
|
var Balloc = class {
|
|
50
52
|
/**
|
|
51
|
-
* `Balloc
|
|
53
|
+
* `Balloc` メモリアロケータのインスタンスを作成します。
|
|
52
54
|
*
|
|
53
55
|
* アロケータは2つの独立したバッファを維持します:
|
|
54
56
|
* - `dataBus`: 割り当てられたブロックのデータを格納します。
|
|
55
57
|
* - `controlBus`: ヘッダーおよびその他の制御メタデータを格納します。
|
|
56
58
|
*
|
|
57
|
-
*
|
|
59
|
+
* 各ブロックは固定サイズのヘッダー (`HEADER_SIZE`) を持ち、
|
|
58
60
|
* 11ビットの世代と32ビットのヘッダーアドレスで構成される
|
|
59
61
|
* 43ビットの論理ハンドルによって外部から識別されます。
|
|
60
62
|
*
|
|
61
|
-
*
|
|
63
|
+
* 各ブロックの容量は、設定されたバケットの幅を考慮し、
|
|
62
64
|
* 要素数における次の2のべき乗に切り上げられます。
|
|
63
65
|
*
|
|
64
|
-
* @param {number} [size=65535] - `dataBus
|
|
65
|
-
* [2, 0x80000000]の範囲内の安全な整数である必要があります。
|
|
66
|
+
* @param {number} [size=65535] - `dataBus` の初期容量(バイト単位)。
|
|
67
|
+
* [2, 0x80000000] の範囲内の安全な整数である必要があります。
|
|
66
68
|
* @param {BucketType} [bucketSize="32u"] - バケットの型およびサイズの識別子
|
|
67
|
-
* (例: Uint32の場合は'32u'、Float64の場合は'64f'、Int8の場合は'8i')。
|
|
68
|
-
* @param {number} [metadataSize=1024] - `controlBus
|
|
69
|
-
* [HEADER_SIZE, 0x80000000]の範囲内の安全な整数である必要があります。
|
|
69
|
+
* (例: Uint32の場合は '32u'、Float64の場合は '64f'、Int8の場合は '8i')。
|
|
70
|
+
* @param {number} [metadataSize=1024] - `controlBus` の初期容量(バイト単位)。
|
|
71
|
+
* [HEADER_SIZE, 0x80000000] の範囲内の安全な整数である必要があります。
|
|
70
72
|
*
|
|
71
|
-
* @throws {RangeError} `size
|
|
72
|
-
* @throws {RangeError} `bucketSize
|
|
73
|
-
* @throws {RangeError} `metadataSize
|
|
73
|
+
* @throws {RangeError} `size` が許容範囲外の場合。
|
|
74
|
+
* @throws {RangeError} `bucketSize` が有効なバケット識別子ではない場合。
|
|
75
|
+
* @throws {RangeError} `metadataSize` が許容範囲外の場合。
|
|
74
76
|
*/
|
|
75
77
|
constructor(size = 65535, bucketSize = "32u", metadataSize = 1024) {
|
|
76
78
|
if (!Number.isSafeInteger(size) || size < 2 || size > 2147483648) throw new RangeError(`Parameter size must be in range [2, 0x80000000] but size is ${size}`);
|
|
@@ -84,8 +86,8 @@ var Balloc = class {
|
|
|
84
86
|
this.bucketArray = globalThis[`${this.bucketType}Array`];
|
|
85
87
|
this.wordSize = this.bucketSize >>> 3;
|
|
86
88
|
this.wordShift = this.bucketSize >>> 4;
|
|
87
|
-
this.size = Math.max(1 << 32 - Math.clz32(size) >>> 0, this.wordSize);
|
|
88
|
-
this.headerSize = 1 << 32 - Math.clz32(metadataSize) >>> 0;
|
|
89
|
+
this.size = Math.max(1 << 32 - Math.clz32(size - 1) >>> 0, this.wordSize);
|
|
90
|
+
this.headerSize = 1 << 32 - Math.clz32(metadataSize - 1) >>> 0;
|
|
89
91
|
this.dataPointer = 0 >>> 0;
|
|
90
92
|
this.controlPointer = 0 >>> 0;
|
|
91
93
|
this.echoPointer = 0 >>> 0;
|
|
@@ -107,27 +109,29 @@ var Balloc = class {
|
|
|
107
109
|
occupied: 0 >>> 0
|
|
108
110
|
};
|
|
109
111
|
}
|
|
110
|
-
// ========== 割り当て ==========
|
|
112
|
+
// ========== 割り当て (ALLOCATION) ==========
|
|
111
113
|
/**
|
|
112
114
|
* 新しいメモリブロックを割り当てます。
|
|
113
115
|
*
|
|
114
|
-
* 要求された容量は、`size
|
|
116
|
+
* 要求された容量は、`size` 要素を格納できる最小の2のべき乗に変換されます。
|
|
115
117
|
* ブロックは、対応する容量を持つ再利用可能な領域のリストから優先的に取得されます。
|
|
116
|
-
*
|
|
118
|
+
* 互換性のある領域が存在しない場合、ブロックは `dataBus` のトップに予約されます。
|
|
117
119
|
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
120
|
+
* 次に、コントロールスペースからヘッダーが取得されます。以前に解放されたヘッダーは
|
|
121
|
+
* 内部の再利用スタックを通じて再利用でき、そうでない場合は必要に応じて
|
|
122
|
+
* `controlBus` が拡張されます。
|
|
121
123
|
*
|
|
122
124
|
* ヘッダーには、アドレス、容量、初期占有数(ゼロ)、および検証用クッキーが格納されます。
|
|
123
|
-
*
|
|
125
|
+
* 返される値は、割り当て世代とヘッダーアドレスをエンコードしたものです:
|
|
124
126
|
*
|
|
125
|
-
* [11
|
|
127
|
+
* [11世代ビット][32ヘッダーアドレスビット]
|
|
126
128
|
*
|
|
127
|
-
* @param {number} [size=16] -
|
|
128
|
-
* [2, 0x80000000]の範囲内の安全な整数である必要があります。
|
|
129
|
+
* @param {number} [size=16] - ブロックが格納しなければならない最小要素数。
|
|
130
|
+
* [2, 0x80000000] の範囲内の安全な整数である必要があります。
|
|
129
131
|
* @returns {number} 割り当てられたブロックを識別するハンドル。
|
|
130
|
-
* @throws {RangeError} `size
|
|
132
|
+
* @throws {RangeError} `size` が許容範囲外の場合。
|
|
133
|
+
* @throws {Error} withView() によって借用されているブロックが存在する状態で
|
|
134
|
+
* `dataBus` の拡張が必要になった場合。
|
|
131
135
|
*/
|
|
132
136
|
alloc(size = 16) {
|
|
133
137
|
if (!Number.isSafeInteger(size) || size < 2 || size > 2147483648) throw new RangeError(`Parameter size must be in range [2, 0x80000000] but size is ${size}`);
|
|
@@ -153,30 +157,32 @@ var Balloc = class {
|
|
|
153
157
|
this.controlAdapter.setUint32(headerAddress + OCCUPIED_OFFSET, 0, IS_LITTLE_ENDIAN);
|
|
154
158
|
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, capacity);
|
|
155
159
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, this.bakeCookie(headerAddress, ++this.generation));
|
|
160
|
+
this.controlBus[headerAddress + FLAGS_OFFSET] = 0;
|
|
156
161
|
return (this.generation & CLAMP_11BITS) * ROTATE_32BITS + headerAddress;
|
|
157
162
|
}
|
|
158
163
|
/**
|
|
159
164
|
* ブロックの容量を次の2のべき乗に拡張します。
|
|
160
165
|
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
166
|
+
* ブロックが `dataBus` の現在のトップに位置している場合、拡張はインプレースで実行されます:
|
|
167
|
+
* 容量と `dataPointer` のみが更新されます。
|
|
163
168
|
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
* 古い領域は再利用リストに戻されます。
|
|
169
|
+
* ブロックの後に別の領域が存在する場合、2倍の容量を持つ新しい領域が
|
|
170
|
+
* 再利用可能な領域から取得されるか、`dataBus` のトップに割り当てられます。
|
|
171
|
+
* 現在占有されている要素が新しい領域にコピーされ、古い領域は再利用リストに戻されます。
|
|
168
172
|
*
|
|
169
|
-
*
|
|
173
|
+
* ヘッダーが保持され、そこに記録されているアドレスと容量のみが更新されるため、
|
|
170
174
|
* ハンドルは有効なまま維持されます。
|
|
171
175
|
*
|
|
172
176
|
* @param {number} handler - 拡張するブロックのハンドル。
|
|
173
177
|
* @returns {void}
|
|
174
|
-
* @throws {Error}
|
|
178
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが
|
|
179
|
+
* withView() によって借用されている場合。また、別のブロックが借用されている状態で
|
|
180
|
+
* 拡張により dataBus の再マッピングが必要になった場合にも発生します。
|
|
175
181
|
* @throws {AggregateError} 容量がサポートされている最大制限に達した場合。
|
|
176
182
|
*/
|
|
177
183
|
realloc(handler) {
|
|
178
184
|
const generation = handler / ROTATE_32BITS & CLAMP_11BITS;
|
|
179
|
-
|
|
185
|
+
const headerAddress = handler >>> 0;
|
|
180
186
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
181
187
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
182
188
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
@@ -184,50 +190,71 @@ var Balloc = class {
|
|
|
184
190
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
185
191
|
throw new Error("Invalid or corrupted handle");
|
|
186
192
|
}
|
|
193
|
+
this.assertNotBorrowed(headerAddress);
|
|
187
194
|
if (capacity >= 31) throw new AggregateError([new Error("Can\xB4t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
188
195
|
const newCapacity = capacity + 1;
|
|
189
196
|
const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
|
|
190
197
|
const oldBlockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
191
198
|
if (dataAddress + oldBlockSize === this.dataPointer) {
|
|
192
199
|
while (dataAddress + newBlockSize > this.size) this.grow();
|
|
193
|
-
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity
|
|
200
|
+
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
194
201
|
this.dataPointer = dataAddress + newBlockSize;
|
|
195
202
|
return;
|
|
196
203
|
}
|
|
197
|
-
this.recyclePush(oldBlockSize, dataAddress);
|
|
198
204
|
let newAddress = this.recyclePop(newBlockSize);
|
|
199
205
|
if (newAddress === UNKNOWN) {
|
|
200
206
|
while (this.dataPointer + newBlockSize > this.size) this.grow();
|
|
201
207
|
newAddress = this.dataPointer;
|
|
202
208
|
this.dataPointer += newBlockSize;
|
|
203
209
|
}
|
|
204
|
-
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
205
210
|
this.dataBus.copyWithin(newAddress, dataAddress, dataAddress + occupied * this.wordSize);
|
|
206
211
|
this.controlAdapter.setUint32(headerAddress + ADDRESS_OFFSET, newAddress, IS_LITTLE_ENDIAN);
|
|
212
|
+
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
213
|
+
this.recyclePush(oldBlockSize, dataAddress);
|
|
207
214
|
}
|
|
208
215
|
/**
|
|
209
|
-
*
|
|
216
|
+
* ブロックのアドレスとハンドルを維持したまま、使用中の要素を格納できる
|
|
217
|
+
* 最小の2のべき乗の容量まで縮小します。
|
|
210
218
|
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
* ブロックが`dataBus`の正確なトップにある場合は例外です。
|
|
215
|
-
* その場合、再利用リストのエントリを作成することなく、
|
|
216
|
-
* 単に`dataPointer`が巻き戻されて領域が破棄されます。
|
|
219
|
+
* ブロックが dataBus のトップにある場合、dataPointer を後退させます。
|
|
220
|
+
* それ以外の場合、解放された末尾領域を2のべき乗の領域に分割し、
|
|
221
|
+
* 各領域を再利用リストに戻します。
|
|
217
222
|
*
|
|
218
|
-
* @param {number} handler -
|
|
223
|
+
* @param {number} handler - 縮小するブロックのハンドル。
|
|
219
224
|
* @returns {void}
|
|
220
|
-
* @throws {Error}
|
|
225
|
+
* @throws {Error} ハンドルが無効または破損している場合、あるいは
|
|
226
|
+
* withView() によってブロックが借用されている場合。
|
|
221
227
|
*/
|
|
228
|
+
shrink(handler) {
|
|
229
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
230
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
231
|
+
const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
|
|
232
|
+
if (newCapacity >= meta.capacityExponent) return;
|
|
233
|
+
const oldBlockSize = meta.byteCapacity;
|
|
234
|
+
const newBlockSize = 2 ** newCapacity * this.wordSize;
|
|
235
|
+
const oldEnd = meta.dataAddress + oldBlockSize;
|
|
236
|
+
this.controlAdapter.setUint8(meta.headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
237
|
+
if (oldEnd === this.dataPointer) {
|
|
238
|
+
this.dataPointer = meta.dataAddress + newBlockSize;
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
let address = meta.dataAddress + newBlockSize;
|
|
242
|
+
for (let exponent = newCapacity; exponent < meta.capacityExponent; exponent++) {
|
|
243
|
+
const segmentSize = 2 ** exponent * this.wordSize;
|
|
244
|
+
this.recyclePush(segmentSize, address);
|
|
245
|
+
address += segmentSize;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
222
248
|
free(handler) {
|
|
223
249
|
const generation = handler / ROTATE_32BITS & CLAMP_11BITS;
|
|
224
|
-
|
|
250
|
+
const headerAddress = handler >>> 0;
|
|
225
251
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
226
252
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
227
253
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
228
254
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
229
255
|
throw new Error("Invalid or corrupted handle");
|
|
230
256
|
}
|
|
257
|
+
this.assertNotBorrowed(headerAddress);
|
|
231
258
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
232
259
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
233
260
|
if (this.echoPointer + 4 <= this.controlBus.byteLength) {
|
|
@@ -240,22 +267,21 @@ var Balloc = class {
|
|
|
240
267
|
this.recyclePush(blockSize, dataAddress);
|
|
241
268
|
}
|
|
242
269
|
}
|
|
243
|
-
// ========== 再利用 ==========
|
|
270
|
+
// ========== 再利用 (RECYCLING) ==========
|
|
244
271
|
/**
|
|
245
272
|
* [内部利用]
|
|
246
273
|
* 空き領域を再利用リストに挿入します。
|
|
247
274
|
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
* それ以降の位置には空き領域のアドレスが格納されます:
|
|
275
|
+
* リストはバイト単位の容量ごとに整理されます。各容量について、
|
|
276
|
+
* 格納される値は `Uint32Array` であり、位置0には登録されたアドレスの数が格納され、
|
|
277
|
+
* 後続の位置には空き領域のアドレスが格納されます:
|
|
252
278
|
*
|
|
253
279
|
* [count, address1, address2, ...]
|
|
254
280
|
*
|
|
255
281
|
* ページが一杯になると、そのバッファは現在の容量の2倍に拡張されます。
|
|
256
282
|
*
|
|
257
283
|
* @param {number} capacity - 空き領域の容量(バイト単位)。
|
|
258
|
-
* @param {number} address - `dataBus
|
|
284
|
+
* @param {number} address - `dataBus` 内における領域の開始アドレス。
|
|
259
285
|
* @returns {void}
|
|
260
286
|
* @private
|
|
261
287
|
*/
|
|
@@ -278,13 +304,13 @@ var Balloc = class {
|
|
|
278
304
|
* [内部利用]
|
|
279
305
|
* 再利用リストから空き領域を削除します。
|
|
280
306
|
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
307
|
+
* 検索は要求された正確な容量によって行われます。対応するページが存在する場合、
|
|
308
|
+
* 最後に登録されたアドレスがページから削除されて返され、LIFOスタックとしての
|
|
309
|
+
* 再利用を実現します。
|
|
284
310
|
*
|
|
285
311
|
* @param {number} capacity - 希望する容量(バイト単位)。
|
|
286
|
-
* @returns {number}
|
|
287
|
-
*
|
|
312
|
+
* @returns {number} 再利用可能な領域の開始アドレス、または要求された容量の領域が
|
|
313
|
+
* 存在しない場合は `UNKNOWN`。
|
|
288
314
|
* @private
|
|
289
315
|
*/
|
|
290
316
|
recyclePop(capacity) {
|
|
@@ -296,29 +322,30 @@ var Balloc = class {
|
|
|
296
322
|
page[0] = count - 1;
|
|
297
323
|
return address;
|
|
298
324
|
}
|
|
299
|
-
// ========== ガベージコレクション ==========
|
|
325
|
+
// ========== ガベージコレクション (GARBAGE COLLECTION) ==========
|
|
300
326
|
/**
|
|
301
|
-
* ReBump
|
|
327
|
+
* ReBumpアルゴリズムを使用して `dataBus` をコンパクション(圧縮)します。
|
|
302
328
|
*
|
|
303
|
-
* `controlBus
|
|
304
|
-
*
|
|
305
|
-
* それらの間に存在する空きスペースを排除します。
|
|
329
|
+
* `controlBus` に登録されたヘッダーを順番に走査し、まだ有効な領域を
|
|
330
|
+
* 新しい `dataBus` にコピーすることで、それらの間にある空きスペースを排除します。
|
|
306
331
|
* 生存している各ブロックのアドレスは、それぞれのヘッダー内で更新されます。
|
|
307
332
|
*
|
|
308
|
-
* `controlBus
|
|
309
|
-
*
|
|
333
|
+
* `controlBus` 内のブロックの順序は保持されます。各ブロックの内部データも、
|
|
334
|
+
* 予約された容量や現在の占有数を含めて完全に保持されます。
|
|
310
335
|
*
|
|
311
336
|
* コンパクションの完了時:
|
|
312
337
|
* - すべての再利用可能な領域が排除されます。
|
|
313
|
-
* - `dataPointer
|
|
314
|
-
* - `dataBus
|
|
338
|
+
* - `dataPointer` は、最後に生存しているブロックの直後のバイトを指すようになります。
|
|
339
|
+
* - `dataBus` は、アクティブなブロックのための連続したヒープを表すようになります。
|
|
315
340
|
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
341
|
+
* 制御の計算量は O(n) です(`n` は走査されたヘッダーの数)。
|
|
342
|
+
* 実効的なメモリ移動コストは、生存ブロックに属するバイトのボリュームにも依存します。
|
|
318
343
|
*
|
|
319
344
|
* @returns {void}
|
|
345
|
+
* @throws {Error} withView() によって借用されているブロックが存在する場合。
|
|
320
346
|
*/
|
|
321
347
|
gc() {
|
|
348
|
+
this.assertNoActiveViews();
|
|
322
349
|
const newDataBus = new Uint8Array(this.dataBus.buffer.byteLength);
|
|
323
350
|
this.recycle.clear();
|
|
324
351
|
this.dataPointer = 0 >>> 0;
|
|
@@ -337,19 +364,21 @@ var Balloc = class {
|
|
|
337
364
|
this.writer = this.dataAdapter[`set${this.bucketType}`].bind(this.dataAdapter);
|
|
338
365
|
this.reader = this.dataAdapter[`get${this.bucketType}`].bind(this.dataAdapter);
|
|
339
366
|
}
|
|
340
|
-
// ========== スケーリング ==========
|
|
367
|
+
// ========== スケーリング (SCALING) ==========
|
|
341
368
|
/**
|
|
342
|
-
* [!!!
|
|
343
|
-
* `dataBus
|
|
369
|
+
* [!!! 内部利用、コード内で使用しないでください !!!]
|
|
370
|
+
* `dataBus` の容量を現在の2倍に拡張します。
|
|
344
371
|
*
|
|
345
|
-
*
|
|
372
|
+
* 内部バッファはより大きな `ArrayBuffer` に転送され、既存のデータは保持されます。
|
|
346
373
|
* バッファに関連付けられているすべてのビューは、新しいストレージを参照するように再作成されます。
|
|
347
374
|
*
|
|
348
375
|
* @returns {void}
|
|
349
|
-
* @throws {
|
|
376
|
+
* @throws {Error} withView() によって借用されているブロックが存在する場合。
|
|
377
|
+
* @throws {RangeError} `dataBus` の容量がサポートされている最大制限を超過した場合。
|
|
350
378
|
* @private
|
|
351
379
|
*/
|
|
352
380
|
grow() {
|
|
381
|
+
this.assertNoActiveViews();
|
|
353
382
|
if (this.size > 2147483648) throw new RangeError("Data bus capacity exceeded");
|
|
354
383
|
const newSize = this.size * 2 >>> 0;
|
|
355
384
|
const newBuffer = this.dataBus.buffer.transfer(newSize);
|
|
@@ -361,14 +390,15 @@ var Balloc = class {
|
|
|
361
390
|
this.auxiliary = new this.bucketArray(newBuffer);
|
|
362
391
|
}
|
|
363
392
|
/**
|
|
364
|
-
* [!!!
|
|
365
|
-
* `controlBus
|
|
393
|
+
* [!!! 内部利用、コード内で使用しないでください !!!]
|
|
394
|
+
* `controlBus` の容量を現在の2倍に拡張します。
|
|
366
395
|
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
396
|
+
* 内部バッファはより大きな `ArrayBuffer` に転送され、既存のヘッダーは保持されます。
|
|
397
|
+
* `controlAdapter` は新しいストレージを参照するように再作成されます。
|
|
369
398
|
*
|
|
370
399
|
* @returns {void}
|
|
371
|
-
* @throws {RangeError} `controlBus
|
|
400
|
+
* @throws {RangeError} `controlBus` の現在の容量がサポートされている最大制限を
|
|
401
|
+
* 超過した場合。
|
|
372
402
|
* @private
|
|
373
403
|
*/
|
|
374
404
|
headerGrow() {
|
|
@@ -378,48 +408,49 @@ var Balloc = class {
|
|
|
378
408
|
this.controlBus = new Uint8Array(newBuffer);
|
|
379
409
|
this.controlAdapter = new DataView(newBuffer);
|
|
380
410
|
}
|
|
381
|
-
// ========== スタック操作 ==========
|
|
411
|
+
// ========== スタック操作 (STACK OPERATIONS) ==========
|
|
382
412
|
/**
|
|
383
|
-
*
|
|
413
|
+
* ブロックによって占有されているシーケンスの末尾に要素を追加します。
|
|
384
414
|
*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
415
|
+
* 要素は、最後尾の占有要素の直後の最初の空き位置に書き込まれます。
|
|
416
|
+
* ブロックの占有数は、その容量内に空きスペースが存在する場合にのみインクリメントされます。
|
|
387
417
|
*
|
|
388
|
-
* @param {number} handler - ブロックのハンドル。
|
|
389
418
|
* @param {number} value - 挿入する値。
|
|
390
|
-
* @returns {boolean}
|
|
391
|
-
*
|
|
392
|
-
* @throws {Error}
|
|
419
|
+
* @returns {boolean} 値が挿入された場合は `true`、ブロックに利用可能な
|
|
420
|
+
* スペースがない場合は `false`。
|
|
421
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
422
|
+
*
|
|
393
423
|
*/
|
|
394
424
|
push(handler, value) {
|
|
395
425
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
426
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
396
427
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
397
428
|
this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
398
429
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
|
|
399
430
|
return true;
|
|
400
431
|
}
|
|
401
432
|
/**
|
|
402
|
-
*
|
|
433
|
+
* ブロックから最後尾の占有要素を削除し、それを返します。
|
|
403
434
|
*
|
|
404
|
-
*
|
|
405
|
-
*
|
|
406
|
-
* 占有シーケンスの一部からは外れます。
|
|
435
|
+
* 占有数は、削除される要素を読み取る前にデクリメントされます。対応するバイトは
|
|
436
|
+
* クリアされず割り当てられた領域に残りますが、占有されているシーケンスの一部ではなくなります。
|
|
407
437
|
*
|
|
408
438
|
* @param {number} handler - ブロックのハンドル。
|
|
409
|
-
* @returns {number|undefined}
|
|
410
|
-
* @throws {Error}
|
|
439
|
+
* @returns {number|undefined} 削除された値、またはブロックが空の場合は `undefined`。
|
|
440
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
411
441
|
*/
|
|
412
442
|
pop(handler) {
|
|
413
443
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
444
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
414
445
|
if (meta.occupied === 0) return void 0;
|
|
415
446
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
416
447
|
return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
|
|
417
448
|
}
|
|
418
449
|
/**
|
|
419
|
-
*
|
|
450
|
+
* 最後尾の占有要素を削除せずに返します。
|
|
420
451
|
*
|
|
421
452
|
* @param {number} handler - ブロックのハンドル。
|
|
422
|
-
* @returns {number|undefined}
|
|
453
|
+
* @returns {number|undefined} 最後尾の占有値、またはブロックが空の場合は `undefined`。
|
|
423
454
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
424
455
|
*/
|
|
425
456
|
peek(handler) {
|
|
@@ -427,22 +458,23 @@ var Balloc = class {
|
|
|
427
458
|
if (meta.occupied === 0) return void 0;
|
|
428
459
|
return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
|
|
429
460
|
}
|
|
430
|
-
// ========== キュー操作 ==========
|
|
461
|
+
// ========== キュー操作 (QUEUE OPERATIONS) ==========
|
|
431
462
|
/**
|
|
432
|
-
*
|
|
463
|
+
* 占有されているシーケンスの先頭に要素を挿入します。
|
|
433
464
|
*
|
|
434
|
-
* 現在占有されている要素が1
|
|
465
|
+
* 現在占有されている要素が1つ前方にシフトされ、最初の位置に `value` が書き込まれます。
|
|
435
466
|
* この操作には、ブロックの容量内に空き位置が必要です。
|
|
436
467
|
*
|
|
437
468
|
* データのシフトが必要となるため、操作のコストは占有されている要素数に比例します。
|
|
438
469
|
*
|
|
439
470
|
* @param {number} handler - ブロックのハンドル。
|
|
440
471
|
* @param {number} value - 挿入する値。
|
|
441
|
-
* @returns {boolean}
|
|
442
|
-
* @throws {Error}
|
|
472
|
+
* @returns {boolean} 値が挿入された場合は `true`、ブロックが一杯の場合は `false`。
|
|
473
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
443
474
|
*/
|
|
444
475
|
nudge(handler, value) {
|
|
445
476
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
477
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
446
478
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
447
479
|
this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
|
|
448
480
|
this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
|
|
@@ -450,19 +482,20 @@ var Balloc = class {
|
|
|
450
482
|
return true;
|
|
451
483
|
}
|
|
452
484
|
/**
|
|
453
|
-
*
|
|
485
|
+
* ブロックから先頭の占有要素を削除し、それを返します。
|
|
454
486
|
*
|
|
455
|
-
*
|
|
456
|
-
*
|
|
487
|
+
* 残りの要素は、占有領域の先頭に向かって1つシフトされます。ブロックの物理的容量は
|
|
488
|
+
* 変更されません。
|
|
457
489
|
*
|
|
458
490
|
* 操作のコストは、シフトされる要素数に比例します。
|
|
459
491
|
*
|
|
460
492
|
* @param {number} handler - ブロックのハンドル。
|
|
461
|
-
* @returns {number|undefined}
|
|
462
|
-
* @throws {Error}
|
|
493
|
+
* @returns {number|undefined} 削除された先頭の値、またはブロックが空の場合は `undefined`。
|
|
494
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
463
495
|
*/
|
|
464
496
|
pull(handler) {
|
|
465
497
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
498
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
466
499
|
if (meta.occupied === 0) return void 0;
|
|
467
500
|
const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
468
501
|
this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
|
|
@@ -470,10 +503,10 @@ var Balloc = class {
|
|
|
470
503
|
return result;
|
|
471
504
|
}
|
|
472
505
|
/**
|
|
473
|
-
*
|
|
506
|
+
* 先頭の占有要素を削除せずに返します。
|
|
474
507
|
*
|
|
475
508
|
* @param {number} handler - ブロックのハンドル。
|
|
476
|
-
* @returns {number|undefined}
|
|
509
|
+
* @returns {number|undefined} 先頭の占有値、またはブロックが空の場合は `undefined`。
|
|
477
510
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
478
511
|
*/
|
|
479
512
|
head(handler) {
|
|
@@ -481,17 +514,16 @@ var Balloc = class {
|
|
|
481
514
|
if (meta.occupied === 0) return void 0;
|
|
482
515
|
return this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
483
516
|
}
|
|
484
|
-
// ========== ランダムアクセス ==========
|
|
517
|
+
// ========== ランダムアクセス (RANDOM ACCESS) ==========
|
|
485
518
|
/**
|
|
486
|
-
*
|
|
519
|
+
* ブロックの占有要素の中に特定の値が存在するかどうかを確認します。
|
|
487
520
|
*
|
|
488
|
-
*
|
|
489
|
-
*
|
|
521
|
+
* 検索は補助 `TypedArray` に対して直接実行され、予約された物理的容量全体ではなく、
|
|
522
|
+
* 現在占有されている範囲のみを対象とします。
|
|
490
523
|
*
|
|
491
524
|
* @param {number} handler - ブロックのハンドル。
|
|
492
525
|
* @param {number} value - 検索する値。
|
|
493
|
-
* @returns {boolean}
|
|
494
|
-
* それ以外の場合は`false`。
|
|
526
|
+
* @returns {boolean} `value` が占有要素の中に存在する場合は `true`、それ以外の場合は `false`。
|
|
495
527
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
496
528
|
*/
|
|
497
529
|
has(handler, value) {
|
|
@@ -501,15 +533,58 @@ var Balloc = class {
|
|
|
501
533
|
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).includes(value);
|
|
502
534
|
}
|
|
503
535
|
/**
|
|
504
|
-
|
|
536
|
+
* ブロックの占有シーケンス内における要素の最初の出現位置を検索します。
|
|
537
|
+
*
|
|
538
|
+
* 検索はインデックスの昇順で、配下の TypedArray の `indexOf` 操作を使用して実行されます。
|
|
539
|
+
* ブロックの未使用容量は無視され、占有されている要素のみが対象となります。
|
|
540
|
+
*
|
|
541
|
+
* 検索の開始位置は `fromIndex` で指定でき、TypedArray のインデックス付けルールに従います。
|
|
542
|
+
*
|
|
543
|
+
* @param {number} handler - ブロックのハンドル。
|
|
544
|
+
* @param {number|bigint} searchElement - 検索する要素。
|
|
545
|
+
* @param {number} [fromIndex=0] - 検索を開始するインデックス。
|
|
546
|
+
* @returns {number} 最初に見つかったインデックス、または要素が見つからない場合や
|
|
547
|
+
* ブロックが空の場合は `-1`。
|
|
548
|
+
* @throws {Error} ハンドルが無効または破損している場合。
|
|
549
|
+
*/
|
|
550
|
+
indexOf(handler, searchElement, fromIndex = 0) {
|
|
551
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
552
|
+
if (meta.occupied === 0) return -1;
|
|
553
|
+
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
554
|
+
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).indexOf(searchElement, fromIndex);
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* ブロックの占有シーケンス内における要素の最後の出現位置を検索します。
|
|
558
|
+
*
|
|
559
|
+
* 検索はインデックスの降順で、配下の TypedArray の `lastIndexOf` 操作を使用して実行されます。
|
|
560
|
+
* ブロックの未使用容量は無視され、占有されている要素のみが対象となります。
|
|
561
|
+
*
|
|
562
|
+
* 検索の開始位置は `fromIndex` で指定でき、TypedArray のインデックス付けルールに従います。
|
|
563
|
+
* デフォルトでは、検索は最後尾の占有要素から開始されます。
|
|
564
|
+
*
|
|
565
|
+
* @param {number} handler - ブロックのハンドル。
|
|
566
|
+
* @param {number|bigint} searchElement - 検索する要素。
|
|
567
|
+
* @param {number} [fromIndex=-1] - 検索を開始するインデックス。
|
|
568
|
+
* @returns {number} 最後に見つかったインデックス、または要素が見つからない場合や
|
|
569
|
+
* ブロックが空の場合は `-1`。
|
|
570
|
+
* @throws {Error} ハンドルが無効または破損している場合。
|
|
571
|
+
*/
|
|
572
|
+
lastIndexOf(handler, searchElement, fromIndex = -1) {
|
|
573
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
574
|
+
if (meta.occupied === 0) return -1;
|
|
575
|
+
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
576
|
+
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).lastIndexOf(searchElement, fromIndex);
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* 0ベースのインデックスに対応する占有要素を返します。
|
|
505
580
|
*
|
|
506
|
-
*
|
|
507
|
-
* `undefined
|
|
581
|
+
* `[0, occupied)` の範囲外のインデックスはエラーとはみなされず、
|
|
582
|
+
* `undefined` を返します。
|
|
508
583
|
*
|
|
509
|
-
* @param {number} handler -
|
|
510
|
-
* @param {number} index - 要素の0
|
|
511
|
-
* @returns {number|undefined}
|
|
512
|
-
*
|
|
584
|
+
* @param {number} handler - Block handle.
|
|
585
|
+
* @param {number} index - 要素の0ベースのインデックス。
|
|
586
|
+
* @returns {number|undefined} インデックスに格納されている値、またはインデックスが
|
|
587
|
+
* 占有領域外である場合は `undefined`。
|
|
513
588
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
514
589
|
*/
|
|
515
590
|
get(handler, index) {
|
|
@@ -520,63 +595,64 @@ var Balloc = class {
|
|
|
520
595
|
/**
|
|
521
596
|
* 占有されているインデックスに格納されている値を置き換えます。
|
|
522
597
|
*
|
|
523
|
-
*
|
|
524
|
-
*
|
|
598
|
+
* この操作はブロックの占有数を変更しません。`[0, occupied)` の範囲外のインデックスは
|
|
599
|
+
* 無視され、`false` を返します。
|
|
525
600
|
*
|
|
526
601
|
* @param {number} handler - ブロックのハンドル。
|
|
527
|
-
* @param {number} index - 要素の0
|
|
602
|
+
* @param {number} index - 要素の0ベースのインデックス。
|
|
528
603
|
* @param {number} value - 新しい値。
|
|
529
|
-
* @returns {boolean}
|
|
530
|
-
*
|
|
531
|
-
* @throws {Error} ハンドルが無効または破損している場合。
|
|
604
|
+
* @returns {boolean} 値が書き込まれた場合は `true`、インデックスが占有領域外である場合は `false`。
|
|
605
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
532
606
|
*/
|
|
533
607
|
set(handler, index, value) {
|
|
534
608
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
609
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
535
610
|
if (index < 0 || index >= meta.occupied) return false;
|
|
536
611
|
this.writer(meta.dataAddress + index * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
537
612
|
return true;
|
|
538
613
|
}
|
|
539
|
-
// ============= その他
|
|
614
|
+
// ============= その他 (MISCELLANEOUS) ==============
|
|
540
615
|
/**
|
|
541
|
-
*
|
|
616
|
+
* ブロックの占有数をゼロにし、物理的容量全体を同一の値で埋めます。
|
|
542
617
|
*
|
|
543
|
-
*
|
|
544
|
-
*
|
|
618
|
+
* 埋め立ては、現在占有されている要素だけでなく、利用可能なすべてのスロットを対象とします。
|
|
619
|
+
* 操作後、埋め立てに使用された値に関係なく、ブロックは空とみなされます。
|
|
545
620
|
*
|
|
546
621
|
* @param {number} handler - ブロックのハンドル。
|
|
547
622
|
* @param {number} [value=0] - すべてのスロットを埋めるために使用する値。
|
|
548
623
|
* @returns {void}
|
|
549
|
-
* @throws {Error}
|
|
624
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
550
625
|
*/
|
|
551
626
|
clear(handler, value = 0) {
|
|
552
627
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
628
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
553
629
|
const slots = meta.byteCapacity / this.wordSize;
|
|
554
630
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
555
631
|
this.auxiliary.fill(value, rawAddress, rawAddress + slots);
|
|
556
632
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, 0, IS_LITTLE_ENDIAN);
|
|
557
633
|
}
|
|
558
634
|
/**
|
|
559
|
-
*
|
|
635
|
+
* ブロックのスロットの範囲を指定した値で埋めます。
|
|
560
636
|
*
|
|
561
|
-
* 範囲には0
|
|
562
|
-
* つまり、`start
|
|
563
|
-
* `end
|
|
637
|
+
* 範囲には0ベースのインデックスを使用し、`[start, end)` の規約に従います。
|
|
638
|
+
* つまり、`start` は包括的(inclusive)であり、`end` は排他的(exclusive)です。
|
|
639
|
+
* `end` が指定されていない場合、埋め立てはブロックの物理的容量の末尾まで拡張されます。
|
|
564
640
|
*
|
|
565
|
-
*
|
|
566
|
-
* ブロックの占有数は`end`に変更されます。
|
|
641
|
+
* 埋められた範囲が以前の占有数を超える場合を含め、ブロックの占有数は `end` になります。
|
|
567
642
|
*
|
|
568
643
|
* @param {number} handler - ブロックのハンドル。
|
|
569
|
-
* @param {number} value -
|
|
644
|
+
* @param {number} value - 埋め立てに使用する値。
|
|
570
645
|
* @param {number} [start=0] - 開始インデックス(包括的)。
|
|
571
|
-
* @param {number|null} [end=null] -
|
|
572
|
-
*
|
|
646
|
+
* @param {number|null} [end=null] - 終了インデックス(排他的)。省略された場合や
|
|
647
|
+
* `null` の場合は、ブロックの総容量を使用します。
|
|
573
648
|
* @returns {void}
|
|
574
|
-
* @throws {Error}
|
|
575
|
-
* @throws {RangeError} `start
|
|
576
|
-
*
|
|
649
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
650
|
+
* @throws {RangeError} `start` または `end` が容量の制限外である場合、
|
|
651
|
+
* または `start > end` である場合。
|
|
577
652
|
*/
|
|
578
653
|
fill(handler, value, start = 0, end = null) {
|
|
579
654
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
655
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
580
656
|
const slots = meta.byteCapacity / this.wordSize;
|
|
581
657
|
const lastIndex = end ?? slots;
|
|
582
658
|
if (start > lastIndex) {
|
|
@@ -593,45 +669,44 @@ var Balloc = class {
|
|
|
593
669
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, lastIndex, IS_LITTLE_ENDIAN);
|
|
594
670
|
}
|
|
595
671
|
/**
|
|
596
|
-
*
|
|
597
|
-
* 容量全体を占有状態としてマークします。
|
|
672
|
+
* ブロックのすべての物理的スロットを指定した値で埋め、容量全体を占有済みにマークします。
|
|
598
673
|
*
|
|
599
|
-
* `clear()
|
|
674
|
+
* `clear()` とは異なり、この操作結果は完全に占有されたブロックとなります。
|
|
600
675
|
*
|
|
601
676
|
* @param {number} handler - ブロックのハンドル。
|
|
602
|
-
* @param {number} value -
|
|
677
|
+
* @param {number} value - 埋め立てに使用する値。
|
|
603
678
|
* @returns {void}
|
|
604
|
-
* @throws {Error}
|
|
679
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
605
680
|
*/
|
|
606
681
|
fillAll(handler, value) {
|
|
607
682
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
683
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
608
684
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
609
685
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
610
686
|
this.auxiliary.fill(value, rawAddress, rawAddress + capacityElements);
|
|
611
687
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, capacityElements, IS_LITTLE_ENDIAN);
|
|
612
688
|
}
|
|
613
689
|
/**
|
|
614
|
-
* `Array
|
|
690
|
+
* `Array` または `TypedArray` の要素をブロックの先頭にコピーします。
|
|
615
691
|
*
|
|
616
|
-
*
|
|
617
|
-
*
|
|
618
|
-
*
|
|
619
|
-
* `put()`の呼び出しによって既存の占有数が減少することはありません。
|
|
692
|
+
* コピーされる量はブロックの容量によって制限されます。占有数は、以前の占有数と
|
|
693
|
+
* 実際にコピーされた量のうち大きい方に更新されます。したがって、より短いシーケンスが
|
|
694
|
+
* 渡された場合でも、`put()` の呼び出しによって既存の占有数が減少することはありません。
|
|
620
695
|
*
|
|
621
|
-
* `Array
|
|
622
|
-
* `TypedArray
|
|
696
|
+
* `Array` の場合、収まる最初の要素のみがコピーされます。
|
|
697
|
+
* `TypedArray` の場合、中間コピーなしでソースのサブアレイが使用されます。
|
|
623
698
|
*
|
|
624
699
|
* @param {number} handler - ブロックのハンドル。
|
|
625
700
|
* @param {Array|TypedArray} data - コピーする値のシーケンス。
|
|
626
701
|
* @returns {void}
|
|
627
|
-
* @throws {Error}
|
|
628
|
-
* @throws {TypeError} `data
|
|
702
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
703
|
+
* @throws {TypeError} `data` が `Array` でも `TypedArray` でも無い場合。
|
|
629
704
|
*/
|
|
630
705
|
put(handler, data) {
|
|
631
706
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
707
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
632
708
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
633
709
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
634
|
-
meta.occupied = Math.max(length, meta.occupied);
|
|
635
710
|
if (Array.isArray(data)) {
|
|
636
711
|
this.auxiliary.set(data.slice(0, length), rawAddress);
|
|
637
712
|
} else if (isTypedArray(data)) {
|
|
@@ -640,21 +715,20 @@ var Balloc = class {
|
|
|
640
715
|
const actual = data?.constructor?.name ?? (data === null ? "null" : typeof data);
|
|
641
716
|
throw new TypeError(`data must be Array or TypedArray, but ${actual} was given.`);
|
|
642
717
|
}
|
|
643
|
-
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied, IS_LITTLE_ENDIAN);
|
|
718
|
+
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, Math.max(length, meta.occupied), IS_LITTLE_ENDIAN);
|
|
644
719
|
}
|
|
645
720
|
/**
|
|
646
721
|
* ブロックによって現在占有されている要素のコピーを返します。
|
|
647
722
|
*
|
|
648
|
-
*
|
|
649
|
-
*
|
|
723
|
+
* 結果は `bucketSize` によって設定された型と同じ `TypedArray` です。
|
|
724
|
+
* 占有されていない物理的容量は結果に含まれません。
|
|
650
725
|
*
|
|
651
|
-
*
|
|
652
|
-
*
|
|
653
|
-
* 独立したストレージを持つ新しいビューを生成します。
|
|
726
|
+
* この操作は内部ストレージを直接公開しません。メソッドは `TypedArray.prototype.slice()` を
|
|
727
|
+
* 使用し、独立したストレージを持つ新しいビューを生成します。
|
|
654
728
|
*
|
|
655
729
|
* @param {number} handler - ブロックのハンドル。
|
|
656
|
-
* @returns {TypedArray}
|
|
657
|
-
*
|
|
730
|
+
* @returns {TypedArray} ブロックによって占有されている要素のコピー。ブロックが空の場合、
|
|
731
|
+
* 設定された型の空の `TypedArray` を返します。
|
|
658
732
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
659
733
|
*/
|
|
660
734
|
pick(handler) {
|
|
@@ -665,15 +739,58 @@ var Balloc = class {
|
|
|
665
739
|
}
|
|
666
740
|
return this.auxiliary.slice(rawAddress, rawAddress + meta.occupied);
|
|
667
741
|
}
|
|
668
|
-
// ======== スロット単位の容量 ==========
|
|
669
742
|
/**
|
|
670
|
-
*
|
|
743
|
+
* ブロックの占有領域のダイレクトビューに対してコールバックを実行します。
|
|
744
|
+
*
|
|
745
|
+
* ビューは Balloc の内部ストレージを共有し、コピーは行われません。
|
|
746
|
+
* コールバックの実行中、ブロックは互換性のない操作から保護されます。
|
|
671
747
|
*
|
|
672
|
-
*
|
|
673
|
-
*
|
|
748
|
+
* コールバックは同期でなければなりません。受け取ったビューを、コールバックの終了後に
|
|
749
|
+
* 格納、返却、または使用してはなりません。この制限は、元のビューから派生した
|
|
750
|
+
* サブビューにも適用されます。
|
|
674
751
|
*
|
|
675
752
|
* @param {number} handler - ブロックのハンドル。
|
|
676
|
-
* @
|
|
753
|
+
* @param {...(number|Function)} args - 1つ以上のブロックハンドルと、その後に続くコールバック。
|
|
754
|
+
* @returns {*} コールバックによって生成された戻り値。
|
|
755
|
+
* @throws {TypeError} ハンドルが指定されていない場合、またはコールバックが関数ではない場合。
|
|
756
|
+
* @throws {Error} いずれかのハンドルが無効、重複、または借用中の場合。
|
|
757
|
+
*/
|
|
758
|
+
withView(...args) {
|
|
759
|
+
if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
|
|
760
|
+
const callback = args.pop();
|
|
761
|
+
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
762
|
+
const handlers = args;
|
|
763
|
+
const metadata = handlers.map((handler) => this.extractMetadata(handler, {}));
|
|
764
|
+
const headers = metadata.map((meta) => meta.headerAddress);
|
|
765
|
+
if (new Set(headers).size !== headers.length) throw new Error("Duplicate handler");
|
|
766
|
+
const flags = headers.map((header) => this.controlBus[header + FLAGS_OFFSET]);
|
|
767
|
+
if (flags.some((flag) => flag & VIEW_LOCK)) throw new Error("Block is already borrowed");
|
|
768
|
+
let acquiredCount = 0;
|
|
769
|
+
try {
|
|
770
|
+
for (const header of headers) {
|
|
771
|
+
this.controlBus[header + FLAGS_OFFSET] |= VIEW_LOCK;
|
|
772
|
+
acquiredCount++;
|
|
773
|
+
}
|
|
774
|
+
const views = metadata.map((meta) => {
|
|
775
|
+
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
776
|
+
return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
|
|
777
|
+
});
|
|
778
|
+
return callback(...views);
|
|
779
|
+
} finally {
|
|
780
|
+
for (let i = 0; i < acquiredCount; i++) {
|
|
781
|
+
this.controlBus[headers[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
// ======== スロット単位の容量 (CAPACITY IN SLOTS) ==========
|
|
786
|
+
/**
|
|
787
|
+
* ブロックの物理的容量をバイト単位で返します。
|
|
788
|
+
*
|
|
789
|
+
* 容量は、現在占有されている要素数に関係なく、ブロックのすべてのスロット用に
|
|
790
|
+
* 予約されたスペースに対応します。
|
|
791
|
+
*
|
|
792
|
+
* @param {number} handler - ブロックのハンドル。
|
|
793
|
+
* @returns {number} ブロックの物理的容量(バイト単位)。
|
|
677
794
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
678
795
|
*/
|
|
679
796
|
byteSize(handler) {
|
|
@@ -684,7 +801,7 @@ var Balloc = class {
|
|
|
684
801
|
* ブロック内で現在占有されている要素の数を返します。
|
|
685
802
|
*
|
|
686
803
|
* @param {number} handler - ブロックのハンドル。
|
|
687
|
-
* @returns {number}
|
|
804
|
+
* @returns {number} 占有されている要素数。
|
|
688
805
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
689
806
|
*/
|
|
690
807
|
occupied(handler) {
|
|
@@ -694,8 +811,7 @@ var Balloc = class {
|
|
|
694
811
|
/**
|
|
695
812
|
* 要素数におけるブロックの容量を返します。
|
|
696
813
|
*
|
|
697
|
-
*
|
|
698
|
-
* 物理的に予約されたスロットの総数に対応します。
|
|
814
|
+
* この値は、現在占有されている数量ではなく、物理的に予約されたスロットの総数に対応します。
|
|
699
815
|
*
|
|
700
816
|
* @param {number} handler - ブロックのハンドル。
|
|
701
817
|
* @returns {number} ブロック内で利用可能なスロットの総数。
|
|
@@ -705,32 +821,48 @@ var Balloc = class {
|
|
|
705
821
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
706
822
|
return 1 << meta.capacityExponent >>> 0;
|
|
707
823
|
}
|
|
708
|
-
// ========== ブロック間操作 =============
|
|
709
824
|
/**
|
|
710
|
-
*
|
|
711
|
-
*
|
|
712
|
-
* コピーは`source`の最初の要素と`destiny`の最初のスロットから開始されます。
|
|
713
|
-
* 実際にコピーされる量は、コピー元の占有要素数、コピー先の物理容量、
|
|
714
|
-
* および`slots`で指定された制限によって制限されます。
|
|
715
|
-
*
|
|
716
|
-
* コピー先の占有数は、必要に応じてコピーされた要素を網羅するように増加します。
|
|
717
|
-
* コピー先の容量は変更されません。
|
|
825
|
+
* メモリブロックが割り当て容量に達しているかどうかを確認します。
|
|
718
826
|
*
|
|
719
|
-
*
|
|
720
|
-
*
|
|
827
|
+
* 占有されている要素数が割り当てられた総容量に等しく、新しい要素を格納する
|
|
828
|
+
* 利用可能な位置が残っていない場合、ブロックは一杯とみなされます。
|
|
721
829
|
*
|
|
722
|
-
* @param {number}
|
|
723
|
-
* @
|
|
724
|
-
* @
|
|
725
|
-
* `UNKNOWN`は、追加の制限を課さないために使用されるセンチネル値を表します。
|
|
726
|
-
* @returns {Uint8Array} 操作後の`dataBus`。
|
|
727
|
-
* @throws {Error} `source`または`destiny`が無効または破損している場合。
|
|
830
|
+
* @param {number} handler - ブロックのハンドル。
|
|
831
|
+
* @returns {boolean} ブロックが一杯の場合は `true`、それ以外の場合は `false`。
|
|
832
|
+
* @throws {Error} ハンドルが無効または破損している場合。
|
|
728
833
|
*/
|
|
729
|
-
|
|
834
|
+
isFull(handler) {
|
|
835
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
836
|
+
return (1 << meta.capacityExponent >>> 0) - meta.occupied === 0;
|
|
837
|
+
}
|
|
838
|
+
// ========== ブロック間の操作 (Operations between blocks)=============
|
|
839
|
+
/**
|
|
840
|
+
* あるブロックから別のブロックへ占有要素をコピーします。
|
|
841
|
+
*
|
|
842
|
+
* コピーは `source` の最初の要素と `destination` の最初のスロットから開始されます。
|
|
843
|
+
* 実際にコピーされる量は、ソース内の占有要素数、宛先の物理的容量、および
|
|
844
|
+
* `slots` で指定された制限によって制限されます。
|
|
845
|
+
*
|
|
846
|
+
* 必要に応じて、コピーされた要素をカバーするように宛先の占有数が引き上げられます。
|
|
847
|
+
* 宛先の容量は変更されません。
|
|
848
|
+
*
|
|
849
|
+
* この操作では `dataBus` に対して `Uint8Array.prototype.copyWithin()` を使用するため、
|
|
850
|
+
* 戻り値はデータバスを表す `Uint8Array` 自体になります。
|
|
851
|
+
*
|
|
852
|
+
* @param {number} source - コピー元ブロックのハンドル。
|
|
853
|
+
* @param {number} destination - コピー先ブロックのハンドル。
|
|
854
|
+
* @param {number} [slots=UNKNOWN] - コピーする最大要素数。
|
|
855
|
+
* `UNKNOWN` は、追加の制限を課さないために使用されるセンチネル値を表します。
|
|
856
|
+
* @returns {Uint8Array} 操作後の `dataBus`。
|
|
857
|
+
* @throws {Error} ハンドルが無効、破損している場合、
|
|
858
|
+
* またはコピー先ブロックが withView() によって借用されている場合。
|
|
859
|
+
*/
|
|
860
|
+
copy(source, destination, slots = UNKNOWN) {
|
|
730
861
|
const metaA = {};
|
|
731
862
|
const metaB = {};
|
|
732
863
|
this.extractMetadata(source, metaA);
|
|
733
|
-
this.extractMetadata(
|
|
864
|
+
this.extractMetadata(destination, metaB);
|
|
865
|
+
this.assertNotBorrowed(metaB.headerAddress);
|
|
734
866
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
735
867
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
736
868
|
const newOccupied = Math.max(metaB.occupied, slotsToCopy);
|
|
@@ -738,28 +870,29 @@ var Balloc = class {
|
|
|
738
870
|
return this.dataBus.copyWithin(metaB.dataAddress, metaA.dataAddress, metaA.dataAddress + slotsToCopy * this.wordSize);
|
|
739
871
|
}
|
|
740
872
|
/**
|
|
741
|
-
*
|
|
873
|
+
* ブロック内の要素の領域をブロック自身の中でコピーします。
|
|
742
874
|
*
|
|
743
|
-
* インデックスは0
|
|
744
|
-
* `target
|
|
745
|
-
* `length
|
|
875
|
+
* インデックスは0ベースであり、`Array.prototype.copyWithin()` のセマンティクスに従います:
|
|
876
|
+
* `target` は宛先位置を特定し、`start` はソースの開始位置を特定し、
|
|
877
|
+
* `length` はコピーする最大要素数を特定します。
|
|
746
878
|
*
|
|
747
|
-
*
|
|
748
|
-
*
|
|
749
|
-
*
|
|
879
|
+
* 負のインデックスはブロックの容量の末尾から解釈されます。この正規化の後、インデックスは
|
|
880
|
+
* 物理的容量にクランプされます。コピーされる量も、実際に占有されている要素と
|
|
881
|
+
* `target` から利用可能なスペースによって制限されます。
|
|
750
882
|
*
|
|
751
|
-
*
|
|
752
|
-
*
|
|
883
|
+
* この操作は重複する領域をサポートしており、宛先の範囲が以前の占有数を超える場合に
|
|
884
|
+
* ブロックの占有数を増やすことができます。
|
|
753
885
|
*
|
|
754
886
|
* @param {number} handler - ブロックのハンドル。
|
|
755
|
-
* @param {number} target -
|
|
756
|
-
* @param {number} start -
|
|
887
|
+
* @param {number} target - 宛先の開始インデックス。
|
|
888
|
+
* @param {number} start - ソースの開始インデックス。
|
|
757
889
|
* @param {number} length - コピーする最大要素数。
|
|
758
|
-
* @returns {Uint8Array}
|
|
759
|
-
* @throws {Error}
|
|
890
|
+
* @returns {Uint8Array} 操作後の `dataBus`。
|
|
891
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
760
892
|
*/
|
|
761
893
|
copyWithin(handler, target, start, length) {
|
|
762
894
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
895
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
763
896
|
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
764
897
|
target = Math.trunc(target);
|
|
765
898
|
start = Math.trunc(start);
|
|
@@ -779,45 +912,42 @@ var Balloc = class {
|
|
|
779
912
|
return this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + length * this.wordSize);
|
|
780
913
|
}
|
|
781
914
|
/**
|
|
782
|
-
* `source
|
|
915
|
+
* `source` の占有要素を `target` の末尾に追加します。
|
|
783
916
|
*
|
|
784
|
-
*
|
|
785
|
-
* コピー先の容量に残っている空きスペースによって制限されます。
|
|
917
|
+
* コピーされる量は、ソース内の占有要素数と、宛先の容量に残っている空きスペースによって制限されます。
|
|
786
918
|
*
|
|
787
|
-
* この操作は、`target
|
|
788
|
-
*
|
|
789
|
-
* `copyWithin()`の重複セマンティクスを保持します。
|
|
919
|
+
* この操作は、`target` と `source` が同じブロックを参照することをサポートしています。
|
|
920
|
+
* その場合、コピーはブロック自身の領域上で実行され、`copyWithin()` の重複セマンティクスを保持します。
|
|
790
921
|
*
|
|
791
922
|
* @param {number} target - 要素を受け取るブロックのハンドル。
|
|
792
|
-
* @param {number} source -
|
|
793
|
-
* @returns {number}
|
|
794
|
-
* @throws {Error}
|
|
923
|
+
* @param {number} source - 要素が追加される元となるブロックのハンドル。
|
|
924
|
+
* @returns {number} 実際に追加された要素数。
|
|
925
|
+
* @throws {Error} ハンドルが無効、破損している場合、
|
|
926
|
+
* またはコピー先ブロックが withView() によって借用されている場合。
|
|
795
927
|
*/
|
|
796
928
|
append(target, source) {
|
|
797
929
|
const metaTarget = {};
|
|
798
930
|
const metaSource = {};
|
|
799
931
|
this.extractMetadata(target, metaTarget);
|
|
800
932
|
this.extractMetadata(source, metaSource);
|
|
933
|
+
this.assertNotBorrowed(metaTarget.headerAddress);
|
|
801
934
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
802
935
|
const count = Math.min(available, metaSource.occupied);
|
|
803
936
|
if (count === 0) return 0;
|
|
804
937
|
const targetAddress = metaTarget.dataAddress + metaTarget.occupied * this.wordSize;
|
|
805
938
|
const sourceAddress = metaSource.dataAddress;
|
|
806
|
-
|
|
807
|
-
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
808
|
-
} else {
|
|
809
|
-
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
810
|
-
}
|
|
939
|
+
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
811
940
|
this.controlAdapter.setUint32(metaTarget.headerAddress + OCCUPIED_OFFSET, metaTarget.occupied + count, IS_LITTLE_ENDIAN);
|
|
812
941
|
return count;
|
|
813
942
|
}
|
|
814
943
|
/**
|
|
815
944
|
* ブロックの独立したコピーを作成します。
|
|
816
945
|
*
|
|
817
|
-
*
|
|
818
|
-
*
|
|
946
|
+
* 新しいブロックは、元のブロックと同じ物理的容量と占有数を持ちます。
|
|
947
|
+
* 現在占有されていない位置を含む、予約された領域内のすべてのバイトがコピーされます。
|
|
819
948
|
*
|
|
820
|
-
*
|
|
949
|
+
* 新しいブロックには新しいハンドルが割り当てられ、通常の割り当ておよび
|
|
950
|
+
* 再利用のルールの対象となり続けます。
|
|
821
951
|
*
|
|
822
952
|
* @param {number} handler - クローンを作成するブロックのハンドル。
|
|
823
953
|
* @returns {number} 新しいブロックのハンドル。
|
|
@@ -833,24 +963,27 @@ var Balloc = class {
|
|
|
833
963
|
return cloned;
|
|
834
964
|
}
|
|
835
965
|
/**
|
|
836
|
-
* 2
|
|
966
|
+
* 2つのブロックに関連付けられたデータ領域をスワップ(交換)します。
|
|
837
967
|
*
|
|
838
968
|
* この操作は、ヘッダー間で領域のアドレス、容量、および占有数を交換します。
|
|
839
|
-
*
|
|
840
|
-
*
|
|
969
|
+
* ハンドルは変更されません:操作後も、各ハンドルは自身のヘッダーを識別し続けますが、
|
|
970
|
+
* そのヘッダーは以前もう一方のブロックに属していた領域を記述するようになります。
|
|
841
971
|
*
|
|
842
|
-
* `dataBus
|
|
972
|
+
* `dataBus` 内のデータは一切コピーされず、2つのヘッダーのメタデータのみが変更されます。
|
|
843
973
|
*
|
|
844
974
|
* @param {number} source - 1つ目のブロックのハンドル。
|
|
845
975
|
* @param {number} target - 2つ目のブロックのハンドル。
|
|
846
976
|
* @returns {void}
|
|
847
|
-
* @throws {Error}
|
|
977
|
+
* @throws {Error} ハンドルが無効、破損している場合、
|
|
978
|
+
* またはいずれかのブロックが withView() によって借用されている場合。
|
|
848
979
|
*/
|
|
849
980
|
swap(source, target) {
|
|
850
981
|
const metaA = {};
|
|
851
982
|
const metaB = {};
|
|
852
983
|
this.extractMetadata(source, metaA);
|
|
853
984
|
this.extractMetadata(target, metaB);
|
|
985
|
+
this.assertNotBorrowed(metaA.headerAddress);
|
|
986
|
+
this.assertNotBorrowed(metaB.headerAddress);
|
|
854
987
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
855
988
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
856
989
|
this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
|
|
@@ -858,16 +991,16 @@ var Balloc = class {
|
|
|
858
991
|
this.controlAdapter.setUint32(metaA.headerAddress + OCCUPIED_OFFSET, metaB.occupied, IS_LITTLE_ENDIAN);
|
|
859
992
|
this.controlAdapter.setUint32(metaB.headerAddress + OCCUPIED_OFFSET, metaA.occupied, IS_LITTLE_ENDIAN);
|
|
860
993
|
}
|
|
861
|
-
// ============= シリアライズ ==============
|
|
994
|
+
// ============= シリアライズ (SERIALIZATION) ==============
|
|
862
995
|
/**
|
|
863
|
-
*
|
|
996
|
+
* メモリブロックのコンテンツとメタデータをシリアライズします。
|
|
864
997
|
*
|
|
865
|
-
*
|
|
866
|
-
*
|
|
867
|
-
* CBOR
|
|
998
|
+
* このメソッドは `pick()` を介してブロックの占有データを抽出し、
|
|
999
|
+
* 容量およびワード型の情報とともにデータエンベロープにグループ化し、
|
|
1000
|
+
* CBORフォーマットを使用してエンコードします。
|
|
868
1001
|
*
|
|
869
1002
|
* @param {number} handler - シリアライズするブロックのハンドル。
|
|
870
|
-
* @returns {Uint8Array}
|
|
1003
|
+
* @returns {Uint8Array} ブロックのシリアライズされたデータを格納するバッファ。
|
|
871
1004
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
872
1005
|
*/
|
|
873
1006
|
dump(handler) {
|
|
@@ -893,14 +1026,14 @@ var Balloc = class {
|
|
|
893
1026
|
/**
|
|
894
1027
|
* シリアライズされたデータからメモリブロックを復元します。
|
|
895
1028
|
*
|
|
896
|
-
* CBOR
|
|
1029
|
+
* CBORエンベロープをデコードし、シリアライズされたデータのワード型が
|
|
897
1030
|
* 現在のアロケータインスタンスと互換性があるかどうかを検証し、
|
|
898
|
-
*
|
|
1031
|
+
* コンテンツを格納するために必要な容量を持つ新しいブロックを割り当てます。
|
|
899
1032
|
* その後、元のデータと占有数が新しいブロックに復元されます。
|
|
900
1033
|
*
|
|
901
1034
|
* @param {Uint8Array|ArrayBuffer} serialized - シリアライズされたデータを格納するバッファ。
|
|
902
1035
|
* @returns {number} 再作成された新しいブロックのハンドル。
|
|
903
|
-
* @throws {Error}
|
|
1036
|
+
* @throws {Error} デコードされたデータのワード型がアロケータの設定と異なる場合。
|
|
904
1037
|
*/
|
|
905
1038
|
load(serialized) {
|
|
906
1039
|
if (!(serialized instanceof ArrayBuffer) && !(serialized instanceof Uint8Array)) {
|
|
@@ -939,23 +1072,53 @@ var Balloc = class {
|
|
|
939
1072
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, envelope.occupied, IS_LITTLE_ENDIAN);
|
|
940
1073
|
return handler;
|
|
941
1074
|
}
|
|
942
|
-
// ============= ユーティリティ ==============
|
|
1075
|
+
// ============= ユーティリティ (UTILITIES) ==============
|
|
1076
|
+
/**
|
|
1077
|
+
* 借用中のブロックに対する変更操作を禁止します。
|
|
1078
|
+
*
|
|
1079
|
+
* 指定されたアドレスに関連付けられたヘッダー内の VIEW_LOCK フラグを確認します。
|
|
1080
|
+
*
|
|
1081
|
+
* @param {number} headerAddress - controlBus 内のヘッダーのアドレス。
|
|
1082
|
+
* @returns {void}
|
|
1083
|
+
* @throws {Error} ブロックが withView() によって借用されている場合。
|
|
1084
|
+
* @private
|
|
1085
|
+
*/
|
|
1086
|
+
assertNotBorrowed(headerAddress) {
|
|
1087
|
+
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1088
|
+
throw new Error("Block is borrowed");
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* いずれかのセグメントが借用されている間、dataBus を置き換える
|
|
1093
|
+
* または再マッピングする操作を禁止します。
|
|
1094
|
+
*
|
|
1095
|
+
* @returns {void}
|
|
1096
|
+
* @throws {Error} withView() によって借用されているブロックが存在する場合。
|
|
1097
|
+
* @private
|
|
1098
|
+
*/
|
|
1099
|
+
assertNoActiveViews() {
|
|
1100
|
+
for (let i = 0; i < this.controlPointer; i += HEADER_SIZE) {
|
|
1101
|
+
if (this.controlBus[i + COOKIE_OFFSET] === 0) continue;
|
|
1102
|
+
if (this.controlBus[i + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1103
|
+
throw new Error("Cannot remap memory while a block is borrowed");
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
943
1107
|
/**
|
|
944
1108
|
* [内部利用]
|
|
945
1109
|
* ハンドルに関連付けられたメタデータをデコードします。
|
|
946
1110
|
*
|
|
947
|
-
*
|
|
948
|
-
*
|
|
949
|
-
* この検証が成功した後にのみメタデータが読み取られます。
|
|
1111
|
+
* ハンドルは世代とヘッダーアドレスに分割されます。次に、ヘッダーに格納されているクッキーが
|
|
1112
|
+
* 再計算されて組み合わせが検証され、この検証が成功した後にのみメタデータが読み取られます。
|
|
950
1113
|
*
|
|
951
|
-
*
|
|
952
|
-
*
|
|
1114
|
+
* デコードされたフィールドは `out` オブジェクトに書き込まれ、この関数自身によって返されます。
|
|
1115
|
+
* 新しい割り当てを回避するために、このオブジェクトは呼び出し間で再利用できます。
|
|
953
1116
|
*
|
|
954
1117
|
* @param {number} handler - デコードおよび検証するハンドル。
|
|
955
1118
|
* @param {Metadata} out - デコードされたメタデータを受け取るオブジェクト。
|
|
956
|
-
* @returns {Metadata} `out
|
|
1119
|
+
* @returns {Metadata} `out` で提供されたものと同じ参照。
|
|
957
1120
|
* @throws {Error} ヘッダーのクッキーがハンドルと一致しない場合、
|
|
958
|
-
*
|
|
1121
|
+
* またはハンドルが有効なヘッダーを特定できない場合。
|
|
959
1122
|
* @private
|
|
960
1123
|
*/
|
|
961
1124
|
extractMetadata(handler, out) {
|
|
@@ -975,21 +1138,20 @@ var Balloc = class {
|
|
|
975
1138
|
* [内部利用]
|
|
976
1139
|
* ヘッダーの検証用クッキーを計算します。
|
|
977
1140
|
*
|
|
978
|
-
*
|
|
979
|
-
*
|
|
1141
|
+
* クッキーは、高速ハッシュ(fast hash)を使用してヘッダーアドレスと
|
|
1142
|
+
* 11個の世代ビットから導出されます。
|
|
980
1143
|
* 結果は8ビットに縮小されます。
|
|
981
1144
|
*
|
|
982
|
-
*
|
|
983
|
-
*
|
|
1145
|
+
* 値ゼロは無効なヘッダーインジケータとして予約されているため、
|
|
1146
|
+
* 計算結果がゼロになった場合、このメソッドは `1` を返します。
|
|
984
1147
|
*
|
|
985
|
-
*
|
|
986
|
-
*
|
|
987
|
-
*
|
|
1148
|
+
* クッキーは暗号化メカニズムを構成するものではありません。その目的は、
|
|
1149
|
+
* アロケータのメモリ管理モデル内において、無効、失効(stale)、または
|
|
1150
|
+
* 破損したハンドルを検出することです。
|
|
988
1151
|
*
|
|
989
|
-
* @param {number} addr -
|
|
990
|
-
* @param {number} gen -
|
|
991
|
-
*
|
|
992
|
-
* @returns {number} [1, 255]の範囲内の8ビットクッキー。
|
|
1152
|
+
* @param {number} addr - ヘッダーのアドレス。
|
|
1153
|
+
* @param {number} gen - ハンドルに関連付けられた世代。下位11ビットのみが考慮されます。
|
|
1154
|
+
* @returns {number} [1, 255] の範囲の8ビットクッキー。
|
|
993
1155
|
* @private
|
|
994
1156
|
*/
|
|
995
1157
|
bakeCookie(addr, gen) {
|
|
@@ -998,10 +1160,10 @@ var Balloc = class {
|
|
|
998
1160
|
/**
|
|
999
1161
|
* 再利用リストに現在格納されている領域の数を返します。
|
|
1000
1162
|
*
|
|
1001
|
-
* 各領域は、`dataBus
|
|
1002
|
-
*
|
|
1163
|
+
* 各領域は、`dataBus` のトップに直接戻すことができなかった解放済みブロックに
|
|
1164
|
+
* 対応しており、将来の同一容量の割り当てによる再利用のために利用可能な状態を維持します。
|
|
1003
1165
|
*
|
|
1004
|
-
* @returns {number} `recycle
|
|
1166
|
+
* @returns {number} `recycle` に保持されている空き領域の数。
|
|
1005
1167
|
*/
|
|
1006
1168
|
recycleCount() {
|
|
1007
1169
|
let count = 0;
|
|
@@ -1013,8 +1175,8 @@ var Balloc = class {
|
|
|
1013
1175
|
/**
|
|
1014
1176
|
* 再利用リストに現在格納されている領域に属するバイトの総数を返します。
|
|
1015
1177
|
*
|
|
1016
|
-
*
|
|
1017
|
-
*
|
|
1178
|
+
* この値は、各空き領域の容量に、その容量に対して登録されている領域の数を
|
|
1179
|
+
* 乗算したものを合計して計算されます。
|
|
1018
1180
|
*
|
|
1019
1181
|
* @returns {number} 再利用可能な領域によって占有されているバイト数。
|
|
1020
1182
|
*/
|
|
@@ -1028,36 +1190,37 @@ var Balloc = class {
|
|
|
1028
1190
|
/**
|
|
1029
1191
|
* 現在再利用可能ではない領域に属する、割り当てられたバイト数を返します。
|
|
1030
1192
|
*
|
|
1031
|
-
* この値は、`dataPointer
|
|
1032
|
-
*
|
|
1193
|
+
* この値は、`dataPointer` の総進捗から、`recycle` に保持されている空き領域に
|
|
1194
|
+
* 属するバイトを減算して取得されます。
|
|
1033
1195
|
*
|
|
1034
|
-
* @returns {number} `dataBus
|
|
1035
|
-
*
|
|
1196
|
+
* @returns {number} `dataBus` によってすでに使用されている領域内における、
|
|
1197
|
+
* 再利用不可領域に関連付けられたバイト数。
|
|
1036
1198
|
*/
|
|
1037
1199
|
get allocatedBytes() {
|
|
1038
1200
|
return this.dataPointer - this.recycleBytes;
|
|
1039
1201
|
}
|
|
1040
1202
|
/**
|
|
1041
|
-
* `dataBus
|
|
1203
|
+
* `dataBus` のトップでまだ使用されていないバイト数を返します。
|
|
1042
1204
|
*
|
|
1043
|
-
* このスペースは、`dataPointer
|
|
1205
|
+
* このスペースは、`dataPointer` と、バッファ用に現在予約されている容量の
|
|
1206
|
+
* 終端の間の領域に対応します。
|
|
1044
1207
|
*
|
|
1045
|
-
* @returns {number} `dataPointer
|
|
1208
|
+
* @returns {number} `dataPointer` の後に利用可能なバイト数。
|
|
1046
1209
|
*/
|
|
1047
1210
|
get unallocatedBytes() {
|
|
1048
1211
|
return this.size - this.dataPointer;
|
|
1049
1212
|
}
|
|
1050
1213
|
/**
|
|
1051
|
-
*
|
|
1214
|
+
* 再利用可能なメモリと使用済みヒープ領域の比率を返します。
|
|
1052
1215
|
*
|
|
1053
|
-
*
|
|
1054
|
-
* `dataPointer
|
|
1055
|
-
*
|
|
1216
|
+
* この指標は、`recycle` に保持されている空き領域を断片化とみな軸とし、
|
|
1217
|
+
* `dataPointer` を分母として使用します。ヒープによってバイトが使用されていない場合、
|
|
1218
|
+
* 結果はゼロになります。
|
|
1056
1219
|
*
|
|
1057
|
-
*
|
|
1058
|
-
*
|
|
1220
|
+
* この値は `dataBus` の全容量の割合を表すものではなく、バンプポインタによって
|
|
1221
|
+
* すでに走査された領域内における再利用可能なバイトの比率を具体的に表します。
|
|
1059
1222
|
*
|
|
1060
|
-
* @returns {number}
|
|
1223
|
+
* @returns {number} 通常の稼働条件下における [0, 1] の範囲の断片化率。
|
|
1061
1224
|
*/
|
|
1062
1225
|
get fragmentationRatio() {
|
|
1063
1226
|
if (this.dataPointer === 0) return 0;
|