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