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.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) {
|
|
257
|
+
|
|
248
258
|
const generation = (handler / ROTATE_32BITS) & CLAMP_11BITS;
|
|
249
|
-
|
|
250
|
-
|
|
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,72 @@ 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
|
* メモリブロックを解放します。
|
|
295
304
|
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
* 単に`dataPointer`が巻き戻されて領域が破棄されます。
|
|
305
|
+
* ブロックのヘッダーはクッキーをゼロに設定することで無効化され、
|
|
306
|
+
* そのアドレスは後で再利用するために登録されます。ブロックが `dataBus` の
|
|
307
|
+
* 正確なトップにある場合を除き、データ領域も再利用システムに戻されます。
|
|
308
|
+
* トップにある場合は、`dataPointer` が単に巻き戻され、再利用リストに
|
|
309
|
+
* エントリを作成せずに領域が破棄されます。
|
|
302
310
|
*
|
|
303
311
|
* @param {number} handler - 解放するブロックのハンドル。
|
|
304
312
|
* @returns {void}
|
|
305
|
-
* @throws {Error}
|
|
313
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが
|
|
314
|
+
* withView() によって借用されている場合。
|
|
306
315
|
*/
|
|
307
316
|
free(handler) {
|
|
317
|
+
|
|
308
318
|
const generation = (handler / ROTATE_32BITS) & CLAMP_11BITS;
|
|
309
|
-
|
|
310
|
-
|
|
319
|
+
const headerAddress = handler >>> 0;
|
|
311
320
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
312
321
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
313
322
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
314
323
|
|
|
315
|
-
|
|
316
324
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
317
325
|
throw new Error("Invalid or corrupted handle");
|
|
318
326
|
}
|
|
319
327
|
|
|
320
|
-
|
|
328
|
+
this.assertNotBorrowed(headerAddress);
|
|
321
329
|
|
|
330
|
+
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
322
331
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
323
332
|
|
|
324
333
|
if (this.echoPointer + 4 <= this.controlBus.byteLength) {
|
|
325
334
|
this.controlAdapter.setUint32(this.echoPointer, headerAddress, IS_LITTLE_ENDIAN);
|
|
326
335
|
this.echoPointer += 4;
|
|
327
|
-
}
|
|
328
|
-
|
|
336
|
+
}
|
|
329
337
|
|
|
330
338
|
if (dataAddress + blockSize === this.dataPointer) {
|
|
331
339
|
this.dataPointer = dataAddress;
|
|
332
340
|
} else {
|
|
333
341
|
this.recyclePush(blockSize, dataAddress);
|
|
334
342
|
}
|
|
335
|
-
|
|
336
343
|
}
|
|
337
|
-
|
|
338
|
-
// ========== 再利用 ==========
|
|
344
|
+
// ========== 再利用 (RECYCLING) ==========
|
|
339
345
|
|
|
340
346
|
/**
|
|
341
347
|
* [内部利用]
|
|
342
348
|
* 空き領域を再利用リストに挿入します。
|
|
343
349
|
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
* それ以降の位置には空き領域のアドレスが格納されます:
|
|
350
|
+
* リストはバイト単位の容量ごとに整理されます。各容量について、
|
|
351
|
+
* 格納される値は `Uint32Array` であり、位置0には登録されたアドレスの数が格納され、
|
|
352
|
+
* 後続の位置には空き領域のアドレスが格納されます:
|
|
348
353
|
*
|
|
349
354
|
* [count, address1, address2, ...]
|
|
350
355
|
*
|
|
351
356
|
* ページが一杯になると、そのバッファは現在の容量の2倍に拡張されます。
|
|
352
357
|
*
|
|
353
358
|
* @param {number} capacity - 空き領域の容量(バイト単位)。
|
|
354
|
-
* @param {number} address - `dataBus
|
|
359
|
+
* @param {number} address - `dataBus` 内における領域の開始アドレス。
|
|
355
360
|
* @returns {void}
|
|
356
361
|
* @private
|
|
357
362
|
*/
|
|
@@ -377,13 +382,13 @@ export default class Balloc {
|
|
|
377
382
|
* [内部利用]
|
|
378
383
|
* 再利用リストから空き領域を削除します。
|
|
379
384
|
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
385
|
+
* 検索は要求された正確な容量によって行われます。対応するページが存在する場合、
|
|
386
|
+
* 最後に登録されたアドレスがページから削除されて返され、LIFOスタックとしての
|
|
387
|
+
* 再利用を実現します。
|
|
383
388
|
*
|
|
384
389
|
* @param {number} capacity - 希望する容量(バイト単位)。
|
|
385
|
-
* @returns {number}
|
|
386
|
-
*
|
|
390
|
+
* @returns {number} 再利用可能な領域の開始アドレス、または要求された容量の領域が
|
|
391
|
+
* 存在しない場合は `UNKNOWN`。
|
|
387
392
|
* @private
|
|
388
393
|
*/
|
|
389
394
|
recyclePop(capacity) {
|
|
@@ -400,41 +405,48 @@ export default class Balloc {
|
|
|
400
405
|
return address;
|
|
401
406
|
}
|
|
402
407
|
|
|
403
|
-
// ========== ガベージコレクション ==========
|
|
408
|
+
// ========== ガベージコレクション (GARBAGE COLLECTION) ==========
|
|
409
|
+
|
|
404
410
|
/**
|
|
405
|
-
* ReBump
|
|
411
|
+
* ReBumpアルゴリズムを使用して `dataBus` をコンパクション(圧縮)します。
|
|
406
412
|
*
|
|
407
|
-
* `controlBus
|
|
408
|
-
*
|
|
409
|
-
* それらの間に存在する空きスペースを排除します。
|
|
413
|
+
* `controlBus` に登録されたヘッダーを順番に走査し、まだ有効な領域を
|
|
414
|
+
* 新しい `dataBus` にコピーすることで、それらの間にある空きスペースを排除します。
|
|
410
415
|
* 生存している各ブロックのアドレスは、それぞれのヘッダー内で更新されます。
|
|
411
416
|
*
|
|
412
|
-
* `controlBus
|
|
413
|
-
*
|
|
417
|
+
* `controlBus` 内のブロックの順序は保持されます。各ブロックの内部データも、
|
|
418
|
+
* 予約された容量や現在の占有数を含めて完全に保持されます。
|
|
414
419
|
*
|
|
415
420
|
* コンパクションの完了時:
|
|
416
421
|
* - すべての再利用可能な領域が排除されます。
|
|
417
|
-
* - `dataPointer
|
|
418
|
-
* - `dataBus
|
|
422
|
+
* - `dataPointer` は、最後に生存しているブロックの直後のバイトを指すようになります。
|
|
423
|
+
* - `dataBus` は、アクティブなブロックのための連続したヒープを表すようになります。
|
|
419
424
|
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
425
|
+
* 制御の計算量は O(n) です(`n` は走査されたヘッダーの数)。
|
|
426
|
+
* 実効的なメモリ移動コストは、生存ブロックに属するバイトのボリュームにも依存します。
|
|
422
427
|
*
|
|
423
428
|
* @returns {void}
|
|
429
|
+
* @throws {Error} withView() によって借用されているブロックが存在する場合。
|
|
424
430
|
*/
|
|
425
431
|
gc() {
|
|
432
|
+
this.assertNoActiveViews();
|
|
433
|
+
|
|
426
434
|
const newDataBus = new Uint8Array(this.dataBus.buffer.byteLength);
|
|
427
435
|
this.recycle.clear();
|
|
428
436
|
this.dataPointer = 0 >>> 0;
|
|
429
|
-
|
|
437
|
+
|
|
438
|
+
for (let i = 0; i < this.controlPointer; i += HEADER_SIZE) {
|
|
430
439
|
if (this.controlBus[i + COOKIE_OFFSET] === 0) continue;
|
|
440
|
+
|
|
431
441
|
const start = this.controlAdapter.getUint32(i + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
432
442
|
const size = (1 << this.controlBus[i + CAPACITY_OFFSET] >>> 0) * this.wordSize;
|
|
433
443
|
const end = start + size;
|
|
444
|
+
|
|
434
445
|
newDataBus.set(this.dataBus.subarray(start, end), this.dataPointer);
|
|
435
446
|
this.controlAdapter.setUint32(i + ADDRESS_OFFSET, this.dataPointer, IS_LITTLE_ENDIAN);
|
|
436
447
|
this.dataPointer += size;
|
|
437
448
|
}
|
|
449
|
+
|
|
438
450
|
this.dataBus = newDataBus;
|
|
439
451
|
this.dataAdapter = new DataView(this.dataBus.buffer);
|
|
440
452
|
this.auxiliary = new globalThis[`${this.bucketType}Array`](this.dataBus.buffer);
|
|
@@ -442,19 +454,23 @@ export default class Balloc {
|
|
|
442
454
|
this.reader = this.dataAdapter[`get${this.bucketType}`].bind(this.dataAdapter);
|
|
443
455
|
}
|
|
444
456
|
|
|
445
|
-
// ========== スケーリング ==========
|
|
457
|
+
// ========== スケーリング (SCALING) ==========
|
|
458
|
+
|
|
446
459
|
/**
|
|
447
|
-
* [!!!
|
|
448
|
-
* `dataBus
|
|
460
|
+
* [!!! 内部利用、コード内で使用しないでください !!!]
|
|
461
|
+
* `dataBus` の容量を現在の2倍に拡張します。
|
|
449
462
|
*
|
|
450
|
-
*
|
|
463
|
+
* 内部バッファはより大きな `ArrayBuffer` に転送され、既存のデータは保持されます。
|
|
451
464
|
* バッファに関連付けられているすべてのビューは、新しいストレージを参照するように再作成されます。
|
|
452
465
|
*
|
|
453
466
|
* @returns {void}
|
|
454
|
-
* @throws {
|
|
467
|
+
* @throws {Error} withView() によって借用されているブロックが存在する場合。
|
|
468
|
+
* @throws {RangeError} `dataBus` の容量がサポートされている最大制限を超過した場合。
|
|
455
469
|
* @private
|
|
456
470
|
*/
|
|
457
471
|
grow() {
|
|
472
|
+
this.assertNoActiveViews();
|
|
473
|
+
|
|
458
474
|
if (this.size > 0x80000000) throw new RangeError("Data bus capacity exceeded");
|
|
459
475
|
|
|
460
476
|
const newSize = (this.size * 2) >>> 0;
|
|
@@ -469,14 +485,15 @@ export default class Balloc {
|
|
|
469
485
|
}
|
|
470
486
|
|
|
471
487
|
/**
|
|
472
|
-
* [!!!
|
|
473
|
-
* `controlBus
|
|
488
|
+
* [!!! 内部利用、コード内で使用しないでください !!!]
|
|
489
|
+
* `controlBus` の容量を現在の2倍に拡張します。
|
|
474
490
|
*
|
|
475
|
-
*
|
|
476
|
-
*
|
|
491
|
+
* 内部バッファはより大きな `ArrayBuffer` に転送され、既存のヘッダーは保持されます。
|
|
492
|
+
* `controlAdapter` は新しいストレージを参照するように再作成されます。
|
|
477
493
|
*
|
|
478
494
|
* @returns {void}
|
|
479
|
-
* @throws {RangeError} `controlBus
|
|
495
|
+
* @throws {RangeError} `controlBus` の現在の容量がサポートされている最大制限を
|
|
496
|
+
* 超過した場合。
|
|
480
497
|
* @private
|
|
481
498
|
*/
|
|
482
499
|
headerGrow() {
|
|
@@ -488,57 +505,58 @@ export default class Balloc {
|
|
|
488
505
|
this.controlAdapter = new DataView(newBuffer);
|
|
489
506
|
}
|
|
490
507
|
|
|
491
|
-
// ========== スタック操作 ==========
|
|
508
|
+
// ========== スタック操作 (STACK OPERATIONS) ==========
|
|
492
509
|
|
|
493
510
|
/**
|
|
494
|
-
*
|
|
511
|
+
* ブロックによって占有されているシーケンスの末尾に要素を追加します。
|
|
495
512
|
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
513
|
+
* 要素は、最後尾の占有要素の直後の最初の空き位置に書き込まれます。
|
|
514
|
+
* ブロックの占有数は、その容量内に空きスペースが存在する場合にのみインクリメントされます。
|
|
498
515
|
*
|
|
499
516
|
* @param {number} handler - ブロックのハンドル。
|
|
500
517
|
* @param {number} value - 挿入する値。
|
|
501
|
-
* @returns {boolean}
|
|
502
|
-
*
|
|
503
|
-
* @throws {Error}
|
|
518
|
+
* @returns {boolean} 値が挿入された場合は `true`、ブロックに利用可能な
|
|
519
|
+
* スペースがない場合は `false`。
|
|
520
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
521
|
+
*
|
|
504
522
|
*/
|
|
505
523
|
push(handler, value) {
|
|
506
|
-
|
|
507
524
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
525
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
508
526
|
|
|
509
527
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
510
528
|
|
|
511
|
-
this.writer(meta.dataAddress + meta.occupied * this.wordSize
|
|
512
|
-
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1,IS_LITTLE_ENDIAN);
|
|
529
|
+
this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
530
|
+
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
|
|
513
531
|
|
|
514
532
|
return true;
|
|
515
533
|
}
|
|
516
534
|
|
|
517
535
|
/**
|
|
518
|
-
*
|
|
536
|
+
* ブロックから最後尾の占有要素を削除し、それを返します。
|
|
519
537
|
*
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
* 占有シーケンスの一部からは外れます。
|
|
538
|
+
* 占有数は、削除される要素を読み取る前にデクリメントされます。対応するバイトは
|
|
539
|
+
* クリアされず割り当てられた領域に残りますが、占有されているシーケンスの一部ではなくなります。
|
|
523
540
|
*
|
|
524
541
|
* @param {number} handler - ブロックのハンドル。
|
|
525
|
-
* @returns {number|undefined}
|
|
526
|
-
* @throws {Error}
|
|
542
|
+
* @returns {number|undefined} 削除された値、またはブロックが空の場合は `undefined`。
|
|
543
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
527
544
|
*/
|
|
528
545
|
pop(handler) {
|
|
529
546
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
547
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
530
548
|
|
|
531
549
|
if (meta.occupied === 0) return undefined;
|
|
532
550
|
|
|
533
|
-
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1,IS_LITTLE_ENDIAN);
|
|
551
|
+
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
534
552
|
return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
|
|
535
553
|
}
|
|
536
554
|
|
|
537
555
|
/**
|
|
538
|
-
*
|
|
556
|
+
* 最後尾の占有要素を削除せずに返します。
|
|
539
557
|
*
|
|
540
558
|
* @param {number} handler - ブロックのハンドル。
|
|
541
|
-
* @returns {number|undefined}
|
|
559
|
+
* @returns {number|undefined} 最後尾の占有値、またはブロックが空の場合は `undefined`。
|
|
542
560
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
543
561
|
*/
|
|
544
562
|
peek(handler) {
|
|
@@ -549,61 +567,64 @@ export default class Balloc {
|
|
|
549
567
|
return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
|
|
550
568
|
}
|
|
551
569
|
|
|
552
|
-
// ========== キュー操作 ==========
|
|
570
|
+
// ========== キュー操作 (QUEUE OPERATIONS) ==========
|
|
553
571
|
|
|
554
572
|
/**
|
|
555
|
-
*
|
|
573
|
+
* 占有されているシーケンスの先頭に要素を挿入します。
|
|
556
574
|
*
|
|
557
|
-
* 現在占有されている要素が1
|
|
575
|
+
* 現在占有されている要素が1つ前方にシフトされ、最初の位置に `value` が書き込まれます。
|
|
558
576
|
* この操作には、ブロックの容量内に空き位置が必要です。
|
|
559
577
|
*
|
|
560
578
|
* データのシフトが必要となるため、操作のコストは占有されている要素数に比例します。
|
|
561
579
|
*
|
|
562
580
|
* @param {number} handler - ブロックのハンドル。
|
|
563
581
|
* @param {number} value - 挿入する値。
|
|
564
|
-
* @returns {boolean}
|
|
565
|
-
* @throws {Error}
|
|
582
|
+
* @returns {boolean} 値が挿入された場合は `true`、ブロックが一杯の場合は `false`。
|
|
583
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
566
584
|
*/
|
|
567
585
|
nudge(handler, value) {
|
|
568
586
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
587
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
569
588
|
|
|
570
589
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
571
590
|
|
|
572
591
|
this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
|
|
573
592
|
this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
|
|
574
|
-
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1,IS_LITTLE_ENDIAN);
|
|
593
|
+
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
|
|
575
594
|
|
|
576
595
|
return true;
|
|
577
596
|
}
|
|
578
597
|
|
|
579
598
|
/**
|
|
580
|
-
*
|
|
599
|
+
* ブロックから先頭の占有要素を削除し、それを返します。
|
|
581
600
|
*
|
|
582
|
-
*
|
|
583
|
-
*
|
|
601
|
+
* 残りの要素は、占有領域の先頭に向かって1つシフトされます。ブロックの物理的容量は
|
|
602
|
+
* 変更されません。
|
|
584
603
|
*
|
|
585
604
|
* 操作のコストは、シフトされる要素数に比例します。
|
|
586
605
|
*
|
|
587
606
|
* @param {number} handler - ブロックのハンドル。
|
|
588
|
-
* @returns {number|undefined}
|
|
589
|
-
* @throws {Error}
|
|
607
|
+
* @returns {number|undefined} 削除された先頭の値、またはブロックが空の場合は `undefined`。
|
|
608
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
590
609
|
*/
|
|
591
610
|
pull(handler) {
|
|
592
611
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
612
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
593
613
|
|
|
594
614
|
if (meta.occupied === 0) return undefined;
|
|
595
615
|
|
|
596
616
|
const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
597
617
|
this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
|
|
598
618
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
619
|
+
|
|
599
620
|
return result;
|
|
600
621
|
}
|
|
601
622
|
|
|
602
623
|
/**
|
|
603
|
-
*
|
|
624
|
+
* 先頭の占有要素を削除せずに返します。
|
|
604
625
|
*
|
|
605
626
|
* @param {number} handler - ブロックのハンドル。
|
|
606
|
-
* @returns {number|undefined}
|
|
627
|
+
* @returns {number|undefined} 先頭の占有値、またはブロックが空の場合は `undefined`。
|
|
607
628
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
608
629
|
*/
|
|
609
630
|
head(handler) {
|
|
@@ -611,21 +632,20 @@ export default class Balloc {
|
|
|
611
632
|
|
|
612
633
|
if (meta.occupied === 0) return undefined;
|
|
613
634
|
|
|
614
|
-
return this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
635
|
+
return this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
615
636
|
}
|
|
616
637
|
|
|
617
|
-
// ========== ランダムアクセス ==========
|
|
638
|
+
// ========== ランダムアクセス (RANDOM ACCESS) ==========
|
|
618
639
|
|
|
619
640
|
/**
|
|
620
|
-
*
|
|
641
|
+
* ブロックの占有要素の中に特定の値が存在するかどうかを確認します。
|
|
621
642
|
*
|
|
622
|
-
*
|
|
623
|
-
*
|
|
643
|
+
* 検索は補助 `TypedArray` に対して直接実行され、予約された物理的容量全体ではなく、
|
|
644
|
+
* 現在占有されている範囲のみを対象とします。
|
|
624
645
|
*
|
|
625
646
|
* @param {number} handler - ブロックのハンドル。
|
|
626
647
|
* @param {number} value - 検索する値。
|
|
627
|
-
* @returns {boolean}
|
|
628
|
-
* それ以外の場合は`false`。
|
|
648
|
+
* @returns {boolean} `value` が占有要素の中に存在する場合は `true`、それ以外の場合は `false`。
|
|
629
649
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
630
650
|
*/
|
|
631
651
|
has(handler, value) {
|
|
@@ -638,18 +658,70 @@ export default class Balloc {
|
|
|
638
658
|
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).includes(value);
|
|
639
659
|
}
|
|
640
660
|
|
|
641
|
-
|
|
642
|
-
*
|
|
661
|
+
/**
|
|
662
|
+
* ブロックの占有シーケンス内における要素の最初の出現位置を検索します。
|
|
663
|
+
*
|
|
664
|
+
* 検索はインデックスの昇順で、配下の TypedArray の `indexOf` 操作を使用して実行されます。
|
|
665
|
+
* ブロックの未使用容量は無視され、占有されている要素のみが対象となります。
|
|
643
666
|
*
|
|
644
|
-
*
|
|
645
|
-
* `undefined`を返します。
|
|
667
|
+
* 検索の開始位置は `fromIndex` で指定でき、TypedArray のインデックス付けルールに従います。
|
|
646
668
|
*
|
|
647
669
|
* @param {number} handler - ブロックのハンドル。
|
|
648
|
-
* @param {number}
|
|
649
|
-
* @
|
|
650
|
-
*
|
|
670
|
+
* @param {number|bigint} searchElement - 検索する要素。
|
|
671
|
+
* @param {number} [fromIndex=0] - 検索を開始するインデックス。
|
|
672
|
+
* @returns {number} 最初に見つかったインデックス、または要素が見つからない場合や
|
|
673
|
+
* ブロックが空の場合は `-1`。
|
|
651
674
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
652
675
|
*/
|
|
676
|
+
indexOf(handler, searchElement, fromIndex = 0) {
|
|
677
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
678
|
+
|
|
679
|
+
if (meta.occupied === 0) return -1;
|
|
680
|
+
|
|
681
|
+
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
682
|
+
|
|
683
|
+
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).indexOf(searchElement, fromIndex);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* ブロックの占有シーケンス内における要素の最後の出現位置を検索します。
|
|
688
|
+
*
|
|
689
|
+
* 検索はインデックスの降順で、配下の TypedArray の `lastIndexOf` 操作を使用して実行されます。
|
|
690
|
+
* ブロックの未使用容量は無視され、占有されている要素のみが対象となります。
|
|
691
|
+
*
|
|
692
|
+
* 検索の開始位置は `fromIndex` で指定でき、TypedArray のインデックス付けルールに従います。
|
|
693
|
+
* デフォルトでは、検索は最後尾の占有要素から開始されます。
|
|
694
|
+
*
|
|
695
|
+
* @param {number} handler - ブロックのハンドル。
|
|
696
|
+
* @param {number|bigint} searchElement - 検索する要素。
|
|
697
|
+
* @param {number} [fromIndex=-1] - 検索を開始するインデックス。
|
|
698
|
+
* @returns {number} 最後に見つかったインデックス、または要素が見つからない場合や
|
|
699
|
+
* ブロックが空の場合は `-1`。
|
|
700
|
+
* @throws {Error} ハンドルが無効または破損している場合。
|
|
701
|
+
*/
|
|
702
|
+
lastIndexOf(handler, searchElement, fromIndex = -1) {
|
|
703
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
704
|
+
|
|
705
|
+
if (meta.occupied === 0) return -1;
|
|
706
|
+
|
|
707
|
+
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
708
|
+
|
|
709
|
+
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).lastIndexOf(searchElement, fromIndex);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* 0ベースのインデックスに対応する占有要素を返します。
|
|
715
|
+
*
|
|
716
|
+
* `[0, occupied)` の範囲外のインデックスはエラーとはみなされず、
|
|
717
|
+
* `undefined` を返します。
|
|
718
|
+
*
|
|
719
|
+
* @param {number} handler - Block handle.
|
|
720
|
+
* @param {number} index - 要素の0ベースのインデックス。
|
|
721
|
+
* @returns {number|undefined} インデックスに格納されている値、またはインデックスが
|
|
722
|
+
* 占有領域外である場合は `undefined`。
|
|
723
|
+
* @throws {Error} ハンドルが無効または破損している場合。
|
|
724
|
+
*/
|
|
653
725
|
get(handler, index) {
|
|
654
726
|
|
|
655
727
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
@@ -662,18 +734,18 @@ export default class Balloc {
|
|
|
662
734
|
/**
|
|
663
735
|
* 占有されているインデックスに格納されている値を置き換えます。
|
|
664
736
|
*
|
|
665
|
-
*
|
|
666
|
-
*
|
|
737
|
+
* この操作はブロックの占有数を変更しません。`[0, occupied)` の範囲外のインデックスは
|
|
738
|
+
* 無視され、`false` を返します。
|
|
667
739
|
*
|
|
668
740
|
* @param {number} handler - ブロックのハンドル。
|
|
669
|
-
* @param {number} index - 要素の0
|
|
741
|
+
* @param {number} index - 要素の0ベースのインデックス。
|
|
670
742
|
* @param {number} value - 新しい値。
|
|
671
|
-
* @returns {boolean}
|
|
672
|
-
*
|
|
673
|
-
* @throws {Error} ハンドルが無効または破損している場合。
|
|
743
|
+
* @returns {boolean} 値が書き込まれた場合は `true`、インデックスが占有領域外である場合は `false`。
|
|
744
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
674
745
|
*/
|
|
675
746
|
set(handler, index, value) {
|
|
676
747
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
748
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
677
749
|
|
|
678
750
|
if (index < 0 || index >= meta.occupied) return false;
|
|
679
751
|
|
|
@@ -681,53 +753,54 @@ export default class Balloc {
|
|
|
681
753
|
return true;
|
|
682
754
|
}
|
|
683
755
|
|
|
684
|
-
// ============= その他
|
|
756
|
+
// ============= その他 (MISCELLANEOUS) ==============
|
|
685
757
|
|
|
686
758
|
/**
|
|
687
|
-
*
|
|
759
|
+
* ブロックの占有数をゼロにし、物理的容量全体を同一の値で埋めます。
|
|
688
760
|
*
|
|
689
|
-
*
|
|
690
|
-
*
|
|
761
|
+
* 埋め立ては、現在占有されている要素だけでなく、利用可能なすべてのスロットを対象とします。
|
|
762
|
+
* 操作後、埋め立てに使用された値に関係なく、ブロックは空とみなされます。
|
|
691
763
|
*
|
|
692
764
|
* @param {number} handler - ブロックのハンドル。
|
|
693
765
|
* @param {number} [value=0] - すべてのスロットを埋めるために使用する値。
|
|
694
766
|
* @returns {void}
|
|
695
|
-
* @throws {Error}
|
|
767
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
696
768
|
*/
|
|
697
|
-
clear(handler, value=0) {
|
|
769
|
+
clear(handler, value = 0) {
|
|
698
770
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
771
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
699
772
|
|
|
700
773
|
const slots = meta.byteCapacity / this.wordSize;
|
|
701
774
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
702
775
|
|
|
703
|
-
this.auxiliary.fill(value, rawAddress, rawAddress + slots);
|
|
776
|
+
this.auxiliary.fill(value, rawAddress, rawAddress + slots);
|
|
704
777
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, 0, IS_LITTLE_ENDIAN);
|
|
705
778
|
}
|
|
706
779
|
|
|
707
780
|
|
|
708
781
|
/**
|
|
709
|
-
*
|
|
782
|
+
* ブロックのスロットの範囲を指定した値で埋めます。
|
|
710
783
|
*
|
|
711
|
-
* 範囲には0
|
|
712
|
-
* つまり、`start
|
|
713
|
-
* `end
|
|
784
|
+
* 範囲には0ベースのインデックスを使用し、`[start, end)` の規約に従います。
|
|
785
|
+
* つまり、`start` は包括的(inclusive)であり、`end` は排他的(exclusive)です。
|
|
786
|
+
* `end` が指定されていない場合、埋め立てはブロックの物理的容量の末尾まで拡張されます。
|
|
714
787
|
*
|
|
715
|
-
*
|
|
716
|
-
* ブロックの占有数は`end`に変更されます。
|
|
788
|
+
* 埋められた範囲が以前の占有数を超える場合を含め、ブロックの占有数は `end` になります。
|
|
717
789
|
*
|
|
718
790
|
* @param {number} handler - ブロックのハンドル。
|
|
719
|
-
* @param {number} value -
|
|
791
|
+
* @param {number} value - 埋め立てに使用する値。
|
|
720
792
|
* @param {number} [start=0] - 開始インデックス(包括的)。
|
|
721
|
-
* @param {number|null} [end=null] -
|
|
722
|
-
*
|
|
793
|
+
* @param {number|null} [end=null] - 終了インデックス(排他的)。省略された場合や
|
|
794
|
+
* `null` の場合は、ブロックの総容量を使用します。
|
|
723
795
|
* @returns {void}
|
|
724
|
-
* @throws {Error}
|
|
725
|
-
* @throws {RangeError} `start
|
|
726
|
-
*
|
|
796
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
797
|
+
* @throws {RangeError} `start` または `end` が容量の制限外である場合、
|
|
798
|
+
* または `start > end` である場合。
|
|
727
799
|
*/
|
|
728
800
|
fill(handler, value, start = 0, end = null) {
|
|
729
801
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
730
|
-
|
|
802
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
803
|
+
|
|
731
804
|
const slots = meta.byteCapacity / this.wordSize;
|
|
732
805
|
const lastIndex = end ?? slots;
|
|
733
806
|
|
|
@@ -742,25 +815,24 @@ export default class Balloc {
|
|
|
742
815
|
}
|
|
743
816
|
|
|
744
817
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
745
|
-
|
|
746
818
|
this.auxiliary.fill(value, rawAddress + start, rawAddress + lastIndex);
|
|
747
819
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, lastIndex, IS_LITTLE_ENDIAN);
|
|
748
820
|
}
|
|
749
821
|
|
|
750
822
|
/**
|
|
751
|
-
*
|
|
752
|
-
* 容量全体を占有状態としてマークします。
|
|
823
|
+
* ブロックのすべての物理的スロットを指定した値で埋め、容量全体を占有済みにマークします。
|
|
753
824
|
*
|
|
754
|
-
* `clear()
|
|
825
|
+
* `clear()` とは異なり、この操作結果は完全に占有されたブロックとなります。
|
|
755
826
|
*
|
|
756
827
|
* @param {number} handler - ブロックのハンドル。
|
|
757
|
-
* @param {number} value -
|
|
828
|
+
* @param {number} value - 埋め立てに使用する値。
|
|
758
829
|
* @returns {void}
|
|
759
|
-
* @throws {Error}
|
|
830
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
760
831
|
*/
|
|
761
832
|
fillAll(handler, value) {
|
|
762
833
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
763
|
-
|
|
834
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
835
|
+
|
|
764
836
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
765
837
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
766
838
|
|
|
@@ -769,30 +841,28 @@ export default class Balloc {
|
|
|
769
841
|
}
|
|
770
842
|
|
|
771
843
|
/**
|
|
772
|
-
* `Array
|
|
844
|
+
* `Array` または `TypedArray` の要素をブロックの先頭にコピーします。
|
|
773
845
|
*
|
|
774
|
-
*
|
|
775
|
-
*
|
|
776
|
-
*
|
|
777
|
-
* `put()`の呼び出しによって既存の占有数が減少することはありません。
|
|
846
|
+
* コピーされる量はブロックの容量によって制限されます。占有数は、以前の占有数と
|
|
847
|
+
* 実際にコピーされた量のうち大きい方に更新されます。したがって、より短いシーケンスが
|
|
848
|
+
* 渡された場合でも、`put()` の呼び出しによって既存の占有数が減少することはありません。
|
|
778
849
|
*
|
|
779
|
-
* `Array
|
|
780
|
-
* `TypedArray
|
|
850
|
+
* `Array` の場合、収まる最初の要素のみがコピーされます。
|
|
851
|
+
* `TypedArray` の場合、中間コピーなしでソースのサブアレイが使用されます。
|
|
781
852
|
*
|
|
782
853
|
* @param {number} handler - ブロックのハンドル。
|
|
783
854
|
* @param {Array|TypedArray} data - コピーする値のシーケンス。
|
|
784
855
|
* @returns {void}
|
|
785
|
-
* @throws {Error}
|
|
786
|
-
* @throws {TypeError} `data
|
|
856
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
857
|
+
* @throws {TypeError} `data` が `Array` でも `TypedArray` でも無い場合。
|
|
787
858
|
*/
|
|
788
859
|
put(handler, data) {
|
|
789
860
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
861
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
790
862
|
|
|
791
863
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
792
864
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
793
865
|
|
|
794
|
-
meta.occupied = Math.max(length, meta.occupied);
|
|
795
|
-
|
|
796
866
|
if (Array.isArray(data)) {
|
|
797
867
|
this.auxiliary.set(data.slice(0, length), rawAddress);
|
|
798
868
|
} else if (isTypedArray(data)) {
|
|
@@ -800,47 +870,82 @@ export default class Balloc {
|
|
|
800
870
|
} else {
|
|
801
871
|
const actual = data?.constructor?.name ?? (data === null ? 'null' : typeof data);
|
|
802
872
|
throw new TypeError(`data must be Array or TypedArray, but ${actual} was given.`);
|
|
803
|
-
}
|
|
804
|
-
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, Math.max(length, meta.occupied), IS_LITTLE_ENDIAN);
|
|
805
876
|
}
|
|
806
877
|
|
|
807
|
-
|
|
808
|
-
|
|
878
|
+
/**
|
|
879
|
+
* ブロックによって現在占有されている要素のコピーを返します。
|
|
880
|
+
*
|
|
881
|
+
* 結果は `bucketSize` によって設定された型と同じ `TypedArray` です。
|
|
882
|
+
* 占有されていない物理的容量は結果に含まれません。
|
|
883
|
+
*
|
|
884
|
+
* この操作は内部ストレージを直接公開しません。メソッドは `TypedArray.prototype.slice()` を
|
|
885
|
+
* 使用し、独立したストレージを持つ新しいビューを生成します。
|
|
886
|
+
*
|
|
887
|
+
* @param {number} handler - ブロックのハンドル。
|
|
888
|
+
* @returns {TypedArray} ブロックによって占有されている要素のコピー。ブロックが空の場合、
|
|
889
|
+
* 設定された型の空の `TypedArray` を返します。
|
|
890
|
+
* @throws {Error} ハンドルが無効または破損している場合。
|
|
891
|
+
*/
|
|
892
|
+
pick(handler) {
|
|
893
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
894
|
+
|
|
895
|
+
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
896
|
+
|
|
897
|
+
if (meta.occupied === 0) {
|
|
898
|
+
return this.auxiliary.slice(rawAddress, rawAddress);
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
return this.auxiliary.slice(rawAddress, rawAddress + meta.occupied);
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* ブロックの占有領域のダイレクトビューに対してコールバックを実行します。
|
|
809
906
|
*
|
|
810
|
-
*
|
|
811
|
-
*
|
|
907
|
+
* ビューは Balloc の内部ストレージを共有し、コピーは行われません。
|
|
908
|
+
* コールバックの実行中、ブロックは互換性のない操作から保護されます。
|
|
812
909
|
*
|
|
813
|
-
*
|
|
814
|
-
*
|
|
815
|
-
*
|
|
910
|
+
* コールバックは同期でなければなりません。受け取ったビューを、コールバックの終了後に
|
|
911
|
+
* 格納、返却、または使用してはなりません。この制限は、元のビューから派生した
|
|
912
|
+
* サブビューにも適用されます。
|
|
816
913
|
*
|
|
817
914
|
* @param {number} handler - ブロックのハンドル。
|
|
818
|
-
* @
|
|
819
|
-
*
|
|
820
|
-
* @throws {
|
|
915
|
+
* @param {Function} callback - 共有された TypedArray を受け取る関数。
|
|
916
|
+
* @returns {*} コールバックによって生成された戻り値。
|
|
917
|
+
* @throws {TypeError} コールバックが関数ではない場合。
|
|
918
|
+
* @throws {Error} ハンドルが無効であるか、すでに借用されている場合。
|
|
821
919
|
*/
|
|
822
|
-
|
|
920
|
+
withView(handler, callback) {
|
|
921
|
+
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
922
|
+
|
|
823
923
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
924
|
+
const flagsAddress = meta.headerAddress + FLAGS_OFFSET;
|
|
925
|
+
const flags = this.controlBus[flagsAddress];
|
|
824
926
|
|
|
825
|
-
|
|
927
|
+
if (flags & VIEW_LOCK) throw new Error("Block is already borrowed");
|
|
826
928
|
|
|
827
|
-
|
|
828
|
-
return this.auxiliary.slice(rawAddress, rawAddress);
|
|
829
|
-
}
|
|
929
|
+
this.controlBus[flagsAddress] = flags | VIEW_LOCK;
|
|
830
930
|
|
|
831
|
-
|
|
931
|
+
try {
|
|
932
|
+
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
933
|
+
return callback(this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied));
|
|
934
|
+
} finally {
|
|
935
|
+
this.controlBus[flagsAddress] &= ~VIEW_LOCK;
|
|
936
|
+
}
|
|
832
937
|
}
|
|
833
938
|
|
|
834
|
-
// ======== スロット単位の容量 ==========
|
|
939
|
+
// ======== スロット単位の容量 (CAPACITY IN SLOTS) ==========
|
|
835
940
|
|
|
836
941
|
/**
|
|
837
|
-
*
|
|
942
|
+
* ブロックの物理的容量をバイト単位で返します。
|
|
838
943
|
*
|
|
839
|
-
*
|
|
840
|
-
*
|
|
944
|
+
* 容量は、現在占有されている要素数に関係なく、ブロックのすべてのスロット用に
|
|
945
|
+
* 予約されたスペースに対応します。
|
|
841
946
|
*
|
|
842
947
|
* @param {number} handler - ブロックのハンドル。
|
|
843
|
-
* @returns {number}
|
|
948
|
+
* @returns {number} ブロックの物理的容量(バイト単位)。
|
|
844
949
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
845
950
|
*/
|
|
846
951
|
byteSize(handler) {
|
|
@@ -852,10 +957,10 @@ export default class Balloc {
|
|
|
852
957
|
* ブロック内で現在占有されている要素の数を返します。
|
|
853
958
|
*
|
|
854
959
|
* @param {number} handler - ブロックのハンドル。
|
|
855
|
-
* @returns {number}
|
|
960
|
+
* @returns {number} 占有されている要素数。
|
|
856
961
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
857
962
|
*/
|
|
858
|
-
occupied(handler){
|
|
963
|
+
occupied(handler) {
|
|
859
964
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
860
965
|
return meta.occupied;
|
|
861
966
|
|
|
@@ -864,8 +969,7 @@ export default class Balloc {
|
|
|
864
969
|
/**
|
|
865
970
|
* 要素数におけるブロックの容量を返します。
|
|
866
971
|
*
|
|
867
|
-
*
|
|
868
|
-
* 物理的に予約されたスロットの総数に対応します。
|
|
972
|
+
* この値は、現在占有されている数量ではなく、物理的に予約されたスロットの総数に対応します。
|
|
869
973
|
*
|
|
870
974
|
* @param {number} handler - ブロックのハンドル。
|
|
871
975
|
* @returns {number} ブロック内で利用可能なスロットの総数。
|
|
@@ -876,71 +980,88 @@ export default class Balloc {
|
|
|
876
980
|
return 1 << meta.capacityExponent >>> 0;
|
|
877
981
|
}
|
|
878
982
|
|
|
879
|
-
// ========== ブロック間操作 =============
|
|
880
|
-
|
|
881
983
|
/**
|
|
984
|
+
* メモリブロックが割り当て容量に達しているかどうかを確認します。
|
|
985
|
+
*
|
|
986
|
+
* 占有されている要素数が割り当てられた総容量に等しく、新しい要素を格納する
|
|
987
|
+
* 利用可能な位置が残っていない場合、ブロックは一杯とみなされます。
|
|
988
|
+
*
|
|
989
|
+
* @param {number} handler - ブロックのハンドル。
|
|
990
|
+
* @returns {boolean} ブロックが一杯の場合は `true`、それ以外の場合は `false`。
|
|
991
|
+
* @throws {Error} ハンドルが無効または破損している場合。
|
|
992
|
+
*/
|
|
993
|
+
isFull(handler) {
|
|
994
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
995
|
+
return (1 << meta.capacityExponent >>> 0) - meta.occupied === 0;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
// ========== ブロック間の操作 (Operations between blocks)=============
|
|
999
|
+
|
|
1000
|
+
/**
|
|
882
1001
|
* あるブロックから別のブロックへ占有要素をコピーします。
|
|
883
1002
|
*
|
|
884
|
-
*
|
|
885
|
-
*
|
|
886
|
-
*
|
|
1003
|
+
* コピーは `source` の最初の要素と `destination` の最初のスロットから開始されます。
|
|
1004
|
+
* 実際にコピーされる量は、ソース内の占有要素数、宛先の物理的容量、および
|
|
1005
|
+
* `slots` で指定された制限によって制限されます。
|
|
887
1006
|
*
|
|
888
|
-
*
|
|
889
|
-
*
|
|
1007
|
+
* 必要に応じて、コピーされた要素をカバーするように宛先の占有数が引き上げられます。
|
|
1008
|
+
* 宛先の容量は変更されません。
|
|
890
1009
|
*
|
|
891
|
-
*
|
|
892
|
-
*
|
|
1010
|
+
* この操作では `dataBus` に対して `Uint8Array.prototype.copyWithin()` を使用するため、
|
|
1011
|
+
* 戻り値はデータバスを表す `Uint8Array` 自体になります。
|
|
893
1012
|
*
|
|
894
1013
|
* @param {number} source - コピー元ブロックのハンドル。
|
|
895
|
-
* @param {number}
|
|
1014
|
+
* @param {number} destination - コピー先ブロックのハンドル。
|
|
896
1015
|
* @param {number} [slots=UNKNOWN] - コピーする最大要素数。
|
|
897
|
-
* `UNKNOWN
|
|
898
|
-
* @returns {Uint8Array}
|
|
899
|
-
* @throws {Error}
|
|
1016
|
+
* `UNKNOWN` は、追加の制限を課さないために使用されるセンチネル値を表します。
|
|
1017
|
+
* @returns {Uint8Array} 操作後の `dataBus`。
|
|
1018
|
+
* @throws {Error} ハンドルが無効、破損している場合、
|
|
1019
|
+
* またはコピー先ブロックが withView() によって借用されている場合。
|
|
900
1020
|
*/
|
|
901
|
-
copy(source,
|
|
1021
|
+
copy(source, destination, slots = UNKNOWN) {
|
|
902
1022
|
/** @type {Metadata} */
|
|
903
1023
|
const metaA = {};
|
|
904
1024
|
/** @type {Metadata} */
|
|
905
1025
|
const metaB = {};
|
|
906
1026
|
|
|
907
1027
|
this.extractMetadata(source, metaA);
|
|
908
|
-
this.extractMetadata(
|
|
1028
|
+
this.extractMetadata(destination, metaB);
|
|
1029
|
+
this.assertNotBorrowed(metaB.headerAddress);
|
|
909
1030
|
|
|
910
1031
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
911
1032
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
912
1033
|
const newOccupied = Math.max(metaB.occupied, slotsToCopy);
|
|
913
1034
|
|
|
914
1035
|
this.controlAdapter.setUint32(metaB.headerAddress + OCCUPIED_OFFSET, newOccupied, IS_LITTLE_ENDIAN);
|
|
915
|
-
|
|
916
1036
|
return this.dataBus.copyWithin(metaB.dataAddress, metaA.dataAddress, metaA.dataAddress + slotsToCopy * this.wordSize);
|
|
917
1037
|
}
|
|
918
1038
|
|
|
919
1039
|
/**
|
|
920
|
-
*
|
|
1040
|
+
* ブロック内の要素の領域をブロック自身の中でコピーします。
|
|
921
1041
|
*
|
|
922
|
-
* インデックスは0
|
|
923
|
-
* `target
|
|
924
|
-
* `length
|
|
1042
|
+
* インデックスは0ベースであり、`Array.prototype.copyWithin()` のセマンティクスに従います:
|
|
1043
|
+
* `target` は宛先位置を特定し、`start` はソースの開始位置を特定し、
|
|
1044
|
+
* `length` はコピーする最大要素数を特定します。
|
|
925
1045
|
*
|
|
926
|
-
*
|
|
927
|
-
*
|
|
928
|
-
*
|
|
1046
|
+
* 負のインデックスはブロックの容量の末尾から解釈されます。この正規化の後、インデックスは
|
|
1047
|
+
* 物理的容量にクランプされます。コピーされる量も、実際に占有されている要素と
|
|
1048
|
+
* `target` から利用可能なスペースによって制限されます。
|
|
929
1049
|
*
|
|
930
|
-
*
|
|
931
|
-
*
|
|
1050
|
+
* この操作は重複する領域をサポートしており、宛先の範囲が以前の占有数を超える場合に
|
|
1051
|
+
* ブロックの占有数を増やすことができます。
|
|
932
1052
|
*
|
|
933
1053
|
* @param {number} handler - ブロックのハンドル。
|
|
934
|
-
* @param {number} target -
|
|
935
|
-
* @param {number} start -
|
|
1054
|
+
* @param {number} target - 宛先の開始インデックス。
|
|
1055
|
+
* @param {number} start - ソースの開始インデックス。
|
|
936
1056
|
* @param {number} length - コピーする最大要素数。
|
|
937
|
-
* @returns {Uint8Array}
|
|
938
|
-
* @throws {Error}
|
|
1057
|
+
* @returns {Uint8Array} 操作後の `dataBus`。
|
|
1058
|
+
* @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
|
|
939
1059
|
*/
|
|
940
1060
|
copyWithin(handler, target, start, length) {
|
|
941
1061
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
942
|
-
|
|
1062
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
943
1063
|
|
|
1064
|
+
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
944
1065
|
target = Math.trunc(target);
|
|
945
1066
|
start = Math.trunc(start);
|
|
946
1067
|
length = Math.trunc(length);
|
|
@@ -948,11 +1069,9 @@ export default class Balloc {
|
|
|
948
1069
|
if (target < 0) target += slots;
|
|
949
1070
|
if (start < 0) start += slots;
|
|
950
1071
|
|
|
951
|
-
// 容量の制限内にインデックスをクランプします
|
|
952
1072
|
target = Math.max(0, Math.min(target, slots));
|
|
953
1073
|
start = Math.max(0, Math.min(start, slots));
|
|
954
1074
|
|
|
955
|
-
// targetから利用可能なスペース、およびstartからの占有要素数にコピーを制限します
|
|
956
1075
|
const maxRead = Math.max(0, meta.occupied - start);
|
|
957
1076
|
const maxWrite = slots - target;
|
|
958
1077
|
length = Math.max(0, Math.min(length, maxRead, maxWrite));
|
|
@@ -961,28 +1080,26 @@ export default class Balloc {
|
|
|
961
1080
|
|
|
962
1081
|
const targetAddress = meta.dataAddress + target * this.wordSize;
|
|
963
1082
|
const sourceAddress = meta.dataAddress + start * this.wordSize;
|
|
964
|
-
|
|
965
|
-
// コピーによってブロックの占有トップが拡張される場合、占有数を更新します
|
|
966
1083
|
const newOccupied = Math.max(meta.occupied, target + length);
|
|
1084
|
+
|
|
967
1085
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, newOccupied, IS_LITTLE_ENDIAN);
|
|
968
1086
|
|
|
969
1087
|
return this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + length * this.wordSize);
|
|
970
1088
|
}
|
|
971
1089
|
|
|
972
1090
|
/**
|
|
973
|
-
* `source
|
|
1091
|
+
* `source` の占有要素を `target` の末尾に追加します。
|
|
974
1092
|
*
|
|
975
|
-
*
|
|
976
|
-
* コピー先の容量に残っている空きスペースによって制限されます。
|
|
1093
|
+
* コピーされる量は、ソース内の占有要素数と、宛先の容量に残っている空きスペースによって制限されます。
|
|
977
1094
|
*
|
|
978
|
-
* この操作は、`target
|
|
979
|
-
*
|
|
980
|
-
* `copyWithin()`の重複セマンティクスを保持します。
|
|
1095
|
+
* この操作は、`target` と `source` が同じブロックを参照することをサポートしています。
|
|
1096
|
+
* その場合、コピーはブロック自身の領域上で実行され、`copyWithin()` の重複セマンティクスを保持します。
|
|
981
1097
|
*
|
|
982
1098
|
* @param {number} target - 要素を受け取るブロックのハンドル。
|
|
983
|
-
* @param {number} source -
|
|
984
|
-
* @returns {number}
|
|
985
|
-
* @throws {Error}
|
|
1099
|
+
* @param {number} source - 要素が追加される元となるブロックのハンドル。
|
|
1100
|
+
* @returns {number} 実際に追加された要素数。
|
|
1101
|
+
* @throws {Error} ハンドルが無効、破損している場合、
|
|
1102
|
+
* またはコピー先ブロックが withView() によって借用されている場合。
|
|
986
1103
|
*/
|
|
987
1104
|
append(target, source) {
|
|
988
1105
|
const metaTarget = {};
|
|
@@ -990,6 +1107,7 @@ export default class Balloc {
|
|
|
990
1107
|
|
|
991
1108
|
this.extractMetadata(target, metaTarget);
|
|
992
1109
|
this.extractMetadata(source, metaSource);
|
|
1110
|
+
this.assertNotBorrowed(metaTarget.headerAddress);
|
|
993
1111
|
|
|
994
1112
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
995
1113
|
const count = Math.min(available, metaSource.occupied);
|
|
@@ -999,11 +1117,7 @@ export default class Balloc {
|
|
|
999
1117
|
const targetAddress = metaTarget.dataAddress + metaTarget.occupied * this.wordSize;
|
|
1000
1118
|
const sourceAddress = metaSource.dataAddress;
|
|
1001
1119
|
|
|
1002
|
-
|
|
1003
|
-
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
1004
|
-
} else {
|
|
1005
|
-
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
1006
|
-
}
|
|
1120
|
+
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
1007
1121
|
|
|
1008
1122
|
this.controlAdapter.setUint32(metaTarget.headerAddress + OCCUPIED_OFFSET, metaTarget.occupied + count, IS_LITTLE_ENDIAN);
|
|
1009
1123
|
|
|
@@ -1013,10 +1127,11 @@ export default class Balloc {
|
|
|
1013
1127
|
/**
|
|
1014
1128
|
* ブロックの独立したコピーを作成します。
|
|
1015
1129
|
*
|
|
1016
|
-
*
|
|
1017
|
-
*
|
|
1130
|
+
* 新しいブロックは、元のブロックと同じ物理的容量と占有数を持ちます。
|
|
1131
|
+
* 現在占有されていない位置を含む、予約された領域内のすべてのバイトがコピーされます。
|
|
1018
1132
|
*
|
|
1019
|
-
*
|
|
1133
|
+
* 新しいブロックには新しいハンドルが割り当てられ、通常の割り当ておよび
|
|
1134
|
+
* 再利用のルールの対象となり続けます。
|
|
1020
1135
|
*
|
|
1021
1136
|
* @param {number} handler - クローンを作成するブロックのハンドル。
|
|
1022
1137
|
* @returns {number} 新しいブロックのハンドル。
|
|
@@ -1036,28 +1151,32 @@ export default class Balloc {
|
|
|
1036
1151
|
}
|
|
1037
1152
|
|
|
1038
1153
|
/**
|
|
1039
|
-
* 2
|
|
1154
|
+
* 2つのブロックに関連付けられたデータ領域をスワップ(交換)します。
|
|
1040
1155
|
*
|
|
1041
1156
|
* この操作は、ヘッダー間で領域のアドレス、容量、および占有数を交換します。
|
|
1042
|
-
*
|
|
1043
|
-
*
|
|
1157
|
+
* ハンドルは変更されません:操作後も、各ハンドルは自身のヘッダーを識別し続けますが、
|
|
1158
|
+
* そのヘッダーは以前もう一方のブロックに属していた領域を記述するようになります。
|
|
1044
1159
|
*
|
|
1045
|
-
* `dataBus
|
|
1160
|
+
* `dataBus` 内のデータは一切コピーされず、2つのヘッダーのメタデータのみが変更されます。
|
|
1046
1161
|
*
|
|
1047
1162
|
* @param {number} source - 1つ目のブロックのハンドル。
|
|
1048
1163
|
* @param {number} target - 2つ目のブロックのハンドル。
|
|
1049
1164
|
* @returns {void}
|
|
1050
|
-
* @throws {Error}
|
|
1165
|
+
* @throws {Error} ハンドルが無効、破損している場合、
|
|
1166
|
+
* またはいずれかのブロックが withView() によって借用されている場合。
|
|
1051
1167
|
*/
|
|
1052
1168
|
swap(source, target) {
|
|
1053
1169
|
/** @type {Metadata} */
|
|
1054
1170
|
const metaA = {};
|
|
1055
1171
|
/** @type {Metadata} */
|
|
1056
1172
|
const metaB = {};
|
|
1057
|
-
|
|
1173
|
+
|
|
1058
1174
|
this.extractMetadata(source, metaA);
|
|
1059
1175
|
this.extractMetadata(target, metaB);
|
|
1060
1176
|
|
|
1177
|
+
this.assertNotBorrowed(metaA.headerAddress);
|
|
1178
|
+
this.assertNotBorrowed(metaB.headerAddress);
|
|
1179
|
+
|
|
1061
1180
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
1062
1181
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
1063
1182
|
|
|
@@ -1066,20 +1185,20 @@ export default class Balloc {
|
|
|
1066
1185
|
|
|
1067
1186
|
this.controlAdapter.setUint32(metaA.headerAddress + OCCUPIED_OFFSET, metaB.occupied, IS_LITTLE_ENDIAN);
|
|
1068
1187
|
this.controlAdapter.setUint32(metaB.headerAddress + OCCUPIED_OFFSET, metaA.occupied, IS_LITTLE_ENDIAN);
|
|
1069
|
-
}
|
|
1188
|
+
}
|
|
1070
1189
|
|
|
1071
1190
|
|
|
1072
|
-
// ============= シリアライズ ==============
|
|
1191
|
+
// ============= シリアライズ (SERIALIZATION) ==============
|
|
1073
1192
|
|
|
1074
1193
|
/**
|
|
1075
|
-
*
|
|
1194
|
+
* メモリブロックのコンテンツとメタデータをシリアライズします。
|
|
1076
1195
|
*
|
|
1077
|
-
*
|
|
1078
|
-
*
|
|
1079
|
-
* CBOR
|
|
1196
|
+
* このメソッドは `pick()` を介してブロックの占有データを抽出し、
|
|
1197
|
+
* 容量およびワード型の情報とともにデータエンベロープにグループ化し、
|
|
1198
|
+
* CBORフォーマットを使用してエンコードします。
|
|
1080
1199
|
*
|
|
1081
1200
|
* @param {number} handler - シリアライズするブロックのハンドル。
|
|
1082
|
-
* @returns {Uint8Array}
|
|
1201
|
+
* @returns {Uint8Array} ブロックのシリアライズされたデータを格納するバッファ。
|
|
1083
1202
|
* @throws {Error} ハンドルが無効または破損している場合。
|
|
1084
1203
|
*/
|
|
1085
1204
|
dump(handler) {
|
|
@@ -1110,14 +1229,14 @@ export default class Balloc {
|
|
|
1110
1229
|
/**
|
|
1111
1230
|
* シリアライズされたデータからメモリブロックを復元します。
|
|
1112
1231
|
*
|
|
1113
|
-
* CBOR
|
|
1232
|
+
* CBORエンベロープをデコードし、シリアライズされたデータのワード型が
|
|
1114
1233
|
* 現在のアロケータインスタンスと互換性があるかどうかを検証し、
|
|
1115
|
-
*
|
|
1234
|
+
* コンテンツを格納するために必要な容量を持つ新しいブロックを割り当てます。
|
|
1116
1235
|
* その後、元のデータと占有数が新しいブロックに復元されます。
|
|
1117
1236
|
*
|
|
1118
1237
|
* @param {Uint8Array|ArrayBuffer} serialized - シリアライズされたデータを格納するバッファ。
|
|
1119
1238
|
* @returns {number} 再作成された新しいブロックのハンドル。
|
|
1120
|
-
* @throws {Error}
|
|
1239
|
+
* @throws {Error} デコードされたデータのワード型がアロケータの設定と異なる場合。
|
|
1121
1240
|
*/
|
|
1122
1241
|
load(serialized) {
|
|
1123
1242
|
if (!(serialized instanceof ArrayBuffer) && !(serialized instanceof Uint8Array)) {
|
|
@@ -1170,24 +1289,56 @@ export default class Balloc {
|
|
|
1170
1289
|
return handler;
|
|
1171
1290
|
}
|
|
1172
1291
|
|
|
1173
|
-
// ============= ユーティリティ ==============
|
|
1292
|
+
// ============= ユーティリティ (UTILITIES) ==============
|
|
1293
|
+
|
|
1294
|
+
/**
|
|
1295
|
+
* 借用中のブロックに対する変更操作を禁止します。
|
|
1296
|
+
*
|
|
1297
|
+
* 指定されたアドレスに関連付けられたヘッダー内の VIEW_LOCK フラグを確認します。
|
|
1298
|
+
*
|
|
1299
|
+
* @param {number} headerAddress - controlBus 内のヘッダーのアドレス。
|
|
1300
|
+
* @returns {void}
|
|
1301
|
+
* @throws {Error} ブロックが withView() によって借用されている場合。
|
|
1302
|
+
* @private
|
|
1303
|
+
*/
|
|
1304
|
+
assertNotBorrowed(headerAddress) {
|
|
1305
|
+
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1306
|
+
throw new Error("Block is borrowed");
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
/**
|
|
1311
|
+
* いずれかのセグメントが借用されている間、dataBus を置き換える
|
|
1312
|
+
* または再マッピングする操作を禁止します。
|
|
1313
|
+
*
|
|
1314
|
+
* @returns {void}
|
|
1315
|
+
* @throws {Error} withView() によって借用されているブロックが存在する場合。
|
|
1316
|
+
* @private
|
|
1317
|
+
*/
|
|
1318
|
+
assertNoActiveViews() {
|
|
1319
|
+
for (let i = 0; i < this.controlPointer; i += HEADER_SIZE) {
|
|
1320
|
+
if (this.controlBus[i + COOKIE_OFFSET] === 0) continue;
|
|
1321
|
+
if (this.controlBus[i + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1322
|
+
throw new Error("Cannot remap memory while a block is borrowed");
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1174
1326
|
|
|
1175
1327
|
/**
|
|
1176
1328
|
* [内部利用]
|
|
1177
1329
|
* ハンドルに関連付けられたメタデータをデコードします。
|
|
1178
1330
|
*
|
|
1179
|
-
*
|
|
1180
|
-
*
|
|
1181
|
-
* この検証が成功した後にのみメタデータが読み取られます。
|
|
1331
|
+
* ハンドルは世代とヘッダーアドレスに分割されます。次に、ヘッダーに格納されているクッキーが
|
|
1332
|
+
* 再計算されて組み合わせが検証され、この検証が成功した後にのみメタデータが読み取られます。
|
|
1182
1333
|
*
|
|
1183
|
-
*
|
|
1184
|
-
*
|
|
1334
|
+
* デコードされたフィールドは `out` オブジェクトに書き込まれ、この関数自身によって返されます。
|
|
1335
|
+
* 新しい割り当てを回避するために、このオブジェクトは呼び出し間で再利用できます。
|
|
1185
1336
|
*
|
|
1186
1337
|
* @param {number} handler - デコードおよび検証するハンドル。
|
|
1187
1338
|
* @param {Metadata} out - デコードされたメタデータを受け取るオブジェクト。
|
|
1188
|
-
* @returns {Metadata} `out
|
|
1339
|
+
* @returns {Metadata} `out` で提供されたものと同じ参照。
|
|
1189
1340
|
* @throws {Error} ヘッダーのクッキーがハンドルと一致しない場合、
|
|
1190
|
-
*
|
|
1341
|
+
* またはハンドルが有効なヘッダーを特定できない場合。
|
|
1191
1342
|
* @private
|
|
1192
1343
|
*/
|
|
1193
1344
|
extractMetadata(handler, out) {
|
|
@@ -1202,33 +1353,31 @@ export default class Balloc {
|
|
|
1202
1353
|
}
|
|
1203
1354
|
|
|
1204
1355
|
out.dataAddress = this.controlAdapter.getUint32(out.headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
1205
|
-
out.capacityExponent
|
|
1356
|
+
out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
|
|
1206
1357
|
out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
|
|
1207
1358
|
out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
1208
1359
|
|
|
1209
1360
|
return out;
|
|
1210
1361
|
}
|
|
1211
1362
|
|
|
1212
|
-
|
|
1213
1363
|
/**
|
|
1214
1364
|
* [内部利用]
|
|
1215
1365
|
* ヘッダーの検証用クッキーを計算します。
|
|
1216
1366
|
*
|
|
1217
|
-
*
|
|
1218
|
-
*
|
|
1367
|
+
* クッキーは、高速ハッシュ(fast hash)を使用してヘッダーアドレスと
|
|
1368
|
+
* 11個の世代ビットから導出されます。
|
|
1219
1369
|
* 結果は8ビットに縮小されます。
|
|
1220
1370
|
*
|
|
1221
|
-
*
|
|
1222
|
-
*
|
|
1371
|
+
* 値ゼロは無効なヘッダーインジケータとして予約されているため、
|
|
1372
|
+
* 計算結果がゼロになった場合、このメソッドは `1` を返します。
|
|
1223
1373
|
*
|
|
1224
|
-
*
|
|
1225
|
-
*
|
|
1226
|
-
*
|
|
1374
|
+
* クッキーは暗号化メカニズムを構成するものではありません。その目的は、
|
|
1375
|
+
* アロケータのメモリ管理モデル内において、無効、失効(stale)、または
|
|
1376
|
+
* 破損したハンドルを検出することです。
|
|
1227
1377
|
*
|
|
1228
|
-
* @param {number} addr -
|
|
1229
|
-
* @param {number} gen -
|
|
1230
|
-
*
|
|
1231
|
-
* @returns {number} [1, 255]の範囲内の8ビットクッキー。
|
|
1378
|
+
* @param {number} addr - ヘッダーのアドレス。
|
|
1379
|
+
* @param {number} gen - ハンドルに関連付けられた世代。下位11ビットのみが考慮されます。
|
|
1380
|
+
* @returns {number} [1, 255] の範囲の8ビットクッキー。
|
|
1232
1381
|
* @private
|
|
1233
1382
|
*/
|
|
1234
1383
|
bakeCookie(addr, gen) {
|
|
@@ -1238,10 +1387,10 @@ export default class Balloc {
|
|
|
1238
1387
|
/**
|
|
1239
1388
|
* 再利用リストに現在格納されている領域の数を返します。
|
|
1240
1389
|
*
|
|
1241
|
-
* 各領域は、`dataBus
|
|
1242
|
-
*
|
|
1390
|
+
* 各領域は、`dataBus` のトップに直接戻すことができなかった解放済みブロックに
|
|
1391
|
+
* 対応しており、将来の同一容量の割り当てによる再利用のために利用可能な状態を維持します。
|
|
1243
1392
|
*
|
|
1244
|
-
* @returns {number} `recycle
|
|
1393
|
+
* @returns {number} `recycle` に保持されている空き領域の数。
|
|
1245
1394
|
*/
|
|
1246
1395
|
recycleCount() {
|
|
1247
1396
|
let count = 0;
|
|
@@ -1256,8 +1405,8 @@ export default class Balloc {
|
|
|
1256
1405
|
/**
|
|
1257
1406
|
* 再利用リストに現在格納されている領域に属するバイトの総数を返します。
|
|
1258
1407
|
*
|
|
1259
|
-
*
|
|
1260
|
-
*
|
|
1408
|
+
* この値は、各空き領域の容量に、その容量に対して登録されている領域の数を
|
|
1409
|
+
* 乗算したものを合計して計算されます。
|
|
1261
1410
|
*
|
|
1262
1411
|
* @returns {number} 再利用可能な領域によって占有されているバイト数。
|
|
1263
1412
|
*/
|
|
@@ -1274,38 +1423,39 @@ export default class Balloc {
|
|
|
1274
1423
|
/**
|
|
1275
1424
|
* 現在再利用可能ではない領域に属する、割り当てられたバイト数を返します。
|
|
1276
1425
|
*
|
|
1277
|
-
* この値は、`dataPointer
|
|
1278
|
-
*
|
|
1426
|
+
* この値は、`dataPointer` の総進捗から、`recycle` に保持されている空き領域に
|
|
1427
|
+
* 属するバイトを減算して取得されます。
|
|
1279
1428
|
*
|
|
1280
|
-
* @returns {number} `dataBus
|
|
1281
|
-
*
|
|
1429
|
+
* @returns {number} `dataBus` によってすでに使用されている領域内における、
|
|
1430
|
+
* 再利用不可領域に関連付けられたバイト数。
|
|
1282
1431
|
*/
|
|
1283
1432
|
get allocatedBytes() {
|
|
1284
1433
|
return this.dataPointer - this.recycleBytes;
|
|
1285
1434
|
}
|
|
1286
1435
|
|
|
1287
1436
|
/**
|
|
1288
|
-
* `dataBus
|
|
1437
|
+
* `dataBus` のトップでまだ使用されていないバイト数を返します。
|
|
1289
1438
|
*
|
|
1290
|
-
* このスペースは、`dataPointer
|
|
1439
|
+
* このスペースは、`dataPointer` と、バッファ用に現在予約されている容量の
|
|
1440
|
+
* 終端の間の領域に対応します。
|
|
1291
1441
|
*
|
|
1292
|
-
* @returns {number} `dataPointer
|
|
1442
|
+
* @returns {number} `dataPointer` の後に利用可能なバイト数。
|
|
1293
1443
|
*/
|
|
1294
1444
|
get unallocatedBytes() {
|
|
1295
1445
|
return this.size - this.dataPointer;
|
|
1296
1446
|
}
|
|
1297
1447
|
|
|
1298
1448
|
/**
|
|
1299
|
-
*
|
|
1449
|
+
* 再利用可能なメモリと使用済みヒープ領域の比率を返します。
|
|
1300
1450
|
*
|
|
1301
|
-
*
|
|
1302
|
-
* `dataPointer
|
|
1303
|
-
*
|
|
1451
|
+
* この指標は、`recycle` に保持されている空き領域を断片化とみな軸とし、
|
|
1452
|
+
* `dataPointer` を分母として使用します。ヒープによってバイトが使用されていない場合、
|
|
1453
|
+
* 結果はゼロになります。
|
|
1304
1454
|
*
|
|
1305
|
-
*
|
|
1306
|
-
*
|
|
1455
|
+
* この値は `dataBus` の全容量の割合を表すものではなく、バンプポインタによって
|
|
1456
|
+
* すでに走査された領域内における再利用可能なバイトの比率を具体的に表します。
|
|
1307
1457
|
*
|
|
1308
|
-
* @returns {number}
|
|
1458
|
+
* @returns {number} 通常の稼働条件下における [0, 1] の範囲の断片化率。
|
|
1309
1459
|
*/
|
|
1310
1460
|
get fragmentationRatio() {
|
|
1311
1461
|
if (this.dataPointer === 0) return 0;
|