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