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