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/zh/balloc.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { encode, decode } from 'cbor-x';
|
|
|
6
6
|
const UNKNOWN = 0xFFFFFFFF >>> 0;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* 有效的桶(bucket)大小和类型。
|
|
10
10
|
* 整数类型仅支持 8、16、32 和 64 位大小,
|
|
11
11
|
* 浮点数类型仅支持 16、32 和 64 位大小。
|
|
12
12
|
* 支持的数据类型:
|
|
@@ -16,22 +16,22 @@ const UNKNOWN = 0xFFFFFFFF >>> 0;
|
|
|
16
16
|
*
|
|
17
17
|
* 桶标识符语法:
|
|
18
18
|
*
|
|
19
|
-
* [
|
|
19
|
+
* [位大小][数据类型]
|
|
20
20
|
*
|
|
21
21
|
* 示例:
|
|
22
|
-
* 32位无符号整数桶:32u
|
|
23
|
-
* 64位有符号整数桶:64i
|
|
22
|
+
* 一个 32 位无符号整数桶:32u
|
|
23
|
+
* 一个 64 位有符号整数桶:64i
|
|
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 }
|
|
@@ -47,9 +47,9 @@ const BUCKET_MAPPING = Object.freeze({
|
|
|
47
47
|
*/
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
* 系统字节序(
|
|
50
|
+
* 系统字节序(endianness)标志。
|
|
51
51
|
*
|
|
52
|
-
*
|
|
52
|
+
* 检测系统是否为小端序(Little Endian),这会影响
|
|
53
53
|
* DataView 操作中的字节顺序。
|
|
54
54
|
*/
|
|
55
55
|
const IS_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
@@ -57,44 +57,51 @@ 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
|
+
* 标志(Flags):
|
|
75
|
+
* bit 0: VIEW_LOCK
|
|
76
|
+
*/
|
|
77
|
+
const VIEW_LOCK = 1 >>> 0;
|
|
71
78
|
|
|
72
79
|
|
|
73
80
|
const ROTATE_32BITS = 4294967296;
|
|
74
81
|
const CLAMP_11BITS = 0x7FF >>> 0;
|
|
75
82
|
|
|
76
83
|
/**
|
|
77
|
-
*
|
|
84
|
+
* 包含句柄中编码的元数据的结构。
|
|
78
85
|
*
|
|
79
86
|
* @typedef {Object} Metadata
|
|
80
|
-
* @property {number} byteCapacity -
|
|
81
|
-
* @property {number} capacityExponent -
|
|
87
|
+
* @property {number} byteCapacity - 内存块的字节容量。
|
|
88
|
+
* @property {number} capacityExponent - 以 2 的幂次表示的内存块容量。
|
|
82
89
|
* @property {number} dataAddress - dataBus 内的内存块地址。
|
|
83
|
-
* @property {number} generation -
|
|
90
|
+
* @property {number} generation - 句柄所属的代数(generation)。
|
|
84
91
|
* @property {number} headerAddress - controlBus 内的标头地址。
|
|
85
92
|
* @property {number} occupied - 块中已占用的元素数量。
|
|
86
93
|
*/
|
|
87
94
|
|
|
88
95
|
/**
|
|
89
|
-
* 检查值是否为 TypedArray
|
|
96
|
+
* 检查值是否为 TypedArray 实例。
|
|
90
97
|
*
|
|
91
98
|
* @param {any} val - 要测试的值。
|
|
92
|
-
* @returns {boolean} 如果该值是 TypedArray
|
|
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
|
|
104
|
+
* 带有 ReBump GC 的 Echo 栈双总线指针碰撞分配器(Dual Bus Bump Allocator)。
|
|
98
105
|
*/
|
|
99
106
|
export default class Balloc {
|
|
100
107
|
|
|
@@ -103,17 +110,18 @@ export default class Balloc {
|
|
|
103
110
|
*
|
|
104
111
|
* 该分配器维护两个独立的缓冲区:
|
|
105
112
|
* - `dataBus`:存储已分配块的数据;
|
|
106
|
-
* - `controlBus
|
|
113
|
+
* - `controlBus`:存储标头和其他控制元数据。
|
|
107
114
|
*
|
|
108
|
-
* 每个块都有一个固定大小的标头(`HEADER_SIZE
|
|
109
|
-
*
|
|
115
|
+
* 每个块都有一个固定大小的标头(`HEADER_SIZE`),并在外部通过一个 43 位逻辑句柄标识,
|
|
116
|
+
* 该句柄由一个 11 位的代数及其 32 位标头地址组成。
|
|
110
117
|
*
|
|
111
|
-
*
|
|
118
|
+
* 每个块的容量会按元素数量向上取整到下一个 2 的幂次,
|
|
119
|
+
* 同时遵循配置的桶的宽度。
|
|
112
120
|
*
|
|
113
121
|
* @param {number} [size=65535] - `dataBus` 的初始容量(以字节为单位)。
|
|
114
122
|
* 必须是范围在 [2, 0x80000000] 内的安全整数。
|
|
115
|
-
* @param {BucketType} [bucketSize="32u"] -
|
|
116
|
-
* (例如:'32u'
|
|
123
|
+
* @param {BucketType} [bucketSize="32u"] - 桶类型和大小的标识符
|
|
124
|
+
* (例如:Uint32 为 '32u',Float64 为 '64f',Int8 为 '8i')。
|
|
117
125
|
* @param {number} [metadataSize=1024] - `controlBus` 的初始容量(以字节为单位)。
|
|
118
126
|
* 必须是范围在 [HEADER_SIZE, 0x80000000] 内的安全整数。
|
|
119
127
|
*
|
|
@@ -121,25 +129,25 @@ export default class Balloc {
|
|
|
121
129
|
* @throws {RangeError} 如果 `bucketSize` 不是有效的桶标识符。
|
|
122
130
|
* @throws {RangeError} 如果 `metadataSize` 超出允许范围。
|
|
123
131
|
*/
|
|
124
|
-
|
|
132
|
+
constructor(size = 65535, bucketSize = "32u", metadataSize = 1024) {
|
|
125
133
|
|
|
126
134
|
if (!Number.isSafeInteger(size) || size < 2 || size > 0x80000000) throw new RangeError(`Parameter size must be in range [2, 0x80000000] but size is ${size}`);
|
|
127
135
|
|
|
128
|
-
|
|
136
|
+
|
|
129
137
|
if (!BUCKET_MAPPING[bucketSize]) {
|
|
130
138
|
throw new RangeError(`bucketSize must be in ([ 8 || 16 || 32 || 64][ u || i ]) || ([ 16 || 32 || 64][ f ]) but I found ${bucketSize}`);
|
|
131
139
|
}
|
|
132
140
|
|
|
133
141
|
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}`);
|
|
134
|
-
|
|
142
|
+
|
|
135
143
|
this.wordType = bucketSize;
|
|
136
144
|
this.bucketType = BUCKET_MAPPING[bucketSize].type;
|
|
137
145
|
this.bucketSize = BUCKET_MAPPING[bucketSize].bits >>> 0;
|
|
138
146
|
this.bucketArray = globalThis[`${this.bucketType}Array`];
|
|
139
147
|
this.wordSize = this.bucketSize >>> 3;
|
|
140
148
|
this.wordShift = this.bucketSize >>> 4;
|
|
141
|
-
this.size = Math.max(1 << (32 - Math.clz32(size)) >>> 0, this.wordSize);
|
|
142
|
-
this.headerSize = 1 << (32 - Math.clz32(metadataSize)) >>> 0;
|
|
149
|
+
this.size = Math.max(1 << (32 - Math.clz32(size - 1)) >>> 0, this.wordSize);
|
|
150
|
+
this.headerSize = 1 << (32 - Math.clz32(metadataSize - 1)) >>> 0;
|
|
143
151
|
this.dataPointer = 0 >>> 0;
|
|
144
152
|
this.controlPointer = 0 >>> 0;
|
|
145
153
|
this.echoPointer = 0 >>> 0;
|
|
@@ -162,7 +170,7 @@ export default class Balloc {
|
|
|
162
170
|
byteCapacity: 0 >>> 0,
|
|
163
171
|
occupied: 0 >>> 0,
|
|
164
172
|
};
|
|
165
|
-
|
|
173
|
+
|
|
166
174
|
}
|
|
167
175
|
|
|
168
176
|
// ========== 分配 (ALLOCATION) ==========
|
|
@@ -170,27 +178,26 @@ export default class Balloc {
|
|
|
170
178
|
/**
|
|
171
179
|
* 分配一个新的内存块。
|
|
172
180
|
*
|
|
173
|
-
* 请求的容量会被转换为能够容纳 `size`
|
|
174
|
-
*
|
|
175
|
-
* 则在 `dataBus` 的顶部保留该块。
|
|
181
|
+
* 请求的容量会被转换为能够容纳 `size` 个元素的最小 2 的幂次。
|
|
182
|
+
* 优先从具有相应容量的可回收区域列表中获取该块;若不存在兼容的区域,则在 `dataBus` 的顶部预留该块。
|
|
176
183
|
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
184
|
+
* 随后,从控制空间中获取一个标头。先前释放的标头可以通过内部回收栈进行复用;
|
|
185
|
+
* 否则,必要时会扩展 `controlBus`。
|
|
179
186
|
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
187
|
+
* 标头会接收地址、容量、初始占用数(零)以及一个验证 cookie。
|
|
188
|
+
* 返回的值编码了分配代数与标头地址:
|
|
182
189
|
*
|
|
183
|
-
* [11
|
|
190
|
+
* [11位代数][32位标头地址]
|
|
184
191
|
*
|
|
185
|
-
* @param {number} [size=16] -
|
|
192
|
+
* @param {number} [size=16] - 块必须能够容纳的最少元素数。
|
|
186
193
|
* 必须是范围在 [2, 0x80000000] 内的安全整数。
|
|
187
194
|
* @returns {number} 标识已分配块的句柄。
|
|
188
195
|
* @throws {RangeError} 如果 `size` 超出允许范围。
|
|
196
|
+
* @throws {Error} 如果在某个块通过 withView() 被借用的同时需要扩展 `dataBus`。
|
|
189
197
|
*/
|
|
190
198
|
alloc(size = 16) {
|
|
191
199
|
if (!Number.isSafeInteger(size) || size < 2 || size > 0x80000000) throw new RangeError(`Parameter size must be in range [2, 0x80000000] but size is ${size}`);
|
|
192
200
|
|
|
193
|
-
|
|
194
201
|
const capacity = (32 - Math.clz32(size - 1));
|
|
195
202
|
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
196
203
|
|
|
@@ -203,57 +210,59 @@ export default class Balloc {
|
|
|
203
210
|
}
|
|
204
211
|
|
|
205
212
|
let headerAddress = 0 >>> 0;
|
|
206
|
-
|
|
207
213
|
if (this.echoPointer > this.controlPointer) {
|
|
208
214
|
this.echoPointer -= 4;
|
|
209
|
-
headerAddress = this.controlAdapter.getUint32(this.echoPointer, IS_LITTLE_ENDIAN);
|
|
215
|
+
headerAddress = this.controlAdapter.getUint32(this.echoPointer, IS_LITTLE_ENDIAN);
|
|
210
216
|
} else {
|
|
211
217
|
if (this.controlPointer + HEADER_SIZE > this.controlAdapter.byteLength) this.headerGrow();
|
|
212
218
|
headerAddress = this.controlPointer;
|
|
213
|
-
this.controlPointer+= HEADER_SIZE;
|
|
214
|
-
this.echoPointer+= HEADER_SIZE;
|
|
219
|
+
this.controlPointer += HEADER_SIZE;
|
|
220
|
+
this.echoPointer += HEADER_SIZE;
|
|
215
221
|
}
|
|
216
222
|
|
|
217
223
|
this.controlAdapter.setUint32(headerAddress + ADDRESS_OFFSET, dataAddress, IS_LITTLE_ENDIAN);
|
|
218
224
|
this.controlAdapter.setUint32(headerAddress + OCCUPIED_OFFSET, 0, IS_LITTLE_ENDIAN);
|
|
219
225
|
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, capacity);
|
|
220
226
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, this.bakeCookie(headerAddress, ++this.generation));
|
|
227
|
+
this.controlBus[headerAddress + FLAGS_OFFSET] = 0;
|
|
221
228
|
|
|
222
229
|
return (this.generation & CLAMP_11BITS) * ROTATE_32BITS + headerAddress;
|
|
223
230
|
}
|
|
224
231
|
|
|
225
232
|
/**
|
|
226
|
-
*
|
|
233
|
+
* 将块的容量扩展到下一个 2 的幂次。
|
|
227
234
|
*
|
|
228
|
-
* 当块位于 `dataBus`
|
|
235
|
+
* 当块位于 `dataBus` 的当前顶部时,会进行原地(in-place)扩展:
|
|
229
236
|
* 仅更新其容量和 `dataPointer`。
|
|
230
237
|
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
238
|
+
* 如果块之后存在其他区域,则会从可回收空间中获取或在 `dataBus` 顶部
|
|
239
|
+
* 分配一个容量加倍的新区域。随后,当前已占用的元素会被复制到新区域中,
|
|
240
|
+
* 并且旧区域会被交还给回收列表。
|
|
234
241
|
*
|
|
235
|
-
*
|
|
242
|
+
* 句柄保持有效,因为它的标头得到了保留,仅更新了其中记录的地址和容量。
|
|
236
243
|
*
|
|
237
244
|
* @param {number} handler - 要扩展的块的句柄。
|
|
238
245
|
* @returns {void}
|
|
239
|
-
* @throws {Error}
|
|
240
|
-
*
|
|
246
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
247
|
+
* 如果扩展需要重新映射 dataBus 且有其他块被借用,也会抛出此错误。
|
|
248
|
+
* @throws {AggregateError} 如果容量已达到支持的最大限制。
|
|
241
249
|
*/
|
|
242
250
|
realloc(handler) {
|
|
251
|
+
|
|
243
252
|
const generation = (handler / ROTATE_32BITS) & CLAMP_11BITS;
|
|
244
|
-
|
|
245
|
-
|
|
253
|
+
const headerAddress = handler >>> 0;
|
|
246
254
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
247
255
|
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
248
256
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
249
257
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
250
258
|
|
|
251
|
-
|
|
252
259
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
253
260
|
throw new Error("Invalid or corrupted handle");
|
|
254
261
|
}
|
|
255
262
|
|
|
256
|
-
|
|
263
|
+
this.assertNotBorrowed(headerAddress);
|
|
264
|
+
|
|
265
|
+
if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
257
266
|
|
|
258
267
|
const newCapacity = capacity + 1;
|
|
259
268
|
const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
|
|
@@ -262,14 +271,11 @@ export default class Balloc {
|
|
|
262
271
|
if (dataAddress + oldBlockSize === this.dataPointer) {
|
|
263
272
|
while (dataAddress + newBlockSize > this.size) this.grow();
|
|
264
273
|
|
|
265
|
-
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity
|
|
274
|
+
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
266
275
|
this.dataPointer = dataAddress + newBlockSize;
|
|
267
|
-
|
|
268
276
|
return;
|
|
269
277
|
}
|
|
270
278
|
|
|
271
|
-
this.recyclePush(oldBlockSize, dataAddress);
|
|
272
|
-
|
|
273
279
|
let newAddress = this.recyclePop(newBlockSize);
|
|
274
280
|
|
|
275
281
|
if (newAddress === UNKNOWN) {
|
|
@@ -279,54 +285,53 @@ export default class Balloc {
|
|
|
279
285
|
this.dataPointer += newBlockSize;
|
|
280
286
|
}
|
|
281
287
|
|
|
282
|
-
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
283
288
|
this.dataBus.copyWithin(newAddress, dataAddress, dataAddress + occupied * this.wordSize);
|
|
284
289
|
|
|
285
290
|
this.controlAdapter.setUint32(headerAddress + ADDRESS_OFFSET, newAddress, IS_LITTLE_ENDIAN);
|
|
291
|
+
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
292
|
+
|
|
293
|
+
this.recyclePush(oldBlockSize, dataAddress);
|
|
286
294
|
}
|
|
287
295
|
|
|
288
|
-
|
|
296
|
+
/**
|
|
289
297
|
* 释放内存块。
|
|
290
298
|
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
299
|
+
* 通过将块标头的 cookie 清零使其失效,并记录其地址以便后续复用。
|
|
300
|
+
* 数据区域也会被交还给回收系统,除非该块刚好位于 `dataBus` 的顶部;
|
|
301
|
+
* 在这种情况下,`dataPointer` 会直接回滚,从而消除该区域而无需在回收列表中创建条目。
|
|
294
302
|
*
|
|
295
303
|
* @param {number} handler - 要释放的块的句柄。
|
|
296
304
|
* @returns {void}
|
|
297
|
-
* @throws {Error}
|
|
305
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
298
306
|
*/
|
|
299
307
|
free(handler) {
|
|
308
|
+
|
|
300
309
|
const generation = (handler / ROTATE_32BITS) & CLAMP_11BITS;
|
|
301
|
-
|
|
302
|
-
|
|
310
|
+
const headerAddress = handler >>> 0;
|
|
303
311
|
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
304
312
|
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
305
313
|
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
306
314
|
|
|
307
|
-
|
|
308
315
|
if (cookie !== this.bakeCookie(headerAddress, generation)) {
|
|
309
316
|
throw new Error("Invalid or corrupted handle");
|
|
310
317
|
}
|
|
311
318
|
|
|
312
|
-
|
|
319
|
+
this.assertNotBorrowed(headerAddress);
|
|
313
320
|
|
|
321
|
+
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
314
322
|
this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
|
|
315
323
|
|
|
316
324
|
if (this.echoPointer + 4 <= this.controlBus.byteLength) {
|
|
317
325
|
this.controlAdapter.setUint32(this.echoPointer, headerAddress, IS_LITTLE_ENDIAN);
|
|
318
326
|
this.echoPointer += 4;
|
|
319
|
-
}
|
|
320
|
-
|
|
327
|
+
}
|
|
321
328
|
|
|
322
329
|
if (dataAddress + blockSize === this.dataPointer) {
|
|
323
330
|
this.dataPointer = dataAddress;
|
|
324
331
|
} else {
|
|
325
332
|
this.recyclePush(blockSize, dataAddress);
|
|
326
333
|
}
|
|
327
|
-
|
|
328
334
|
}
|
|
329
|
-
|
|
330
335
|
// ========== 回收 (RECYCLING) ==========
|
|
331
336
|
|
|
332
337
|
/**
|
|
@@ -334,7 +339,7 @@ export default class Balloc {
|
|
|
334
339
|
* 将空闲区域插入回收列表。
|
|
335
340
|
*
|
|
336
341
|
* 该列表按字节容量进行组织。对于每个容量,存储的值是一个 `Uint32Array`,
|
|
337
|
-
*
|
|
342
|
+
* 其第 0 个位置包含已注册地址的数量,随后的位置包含空闲区域的地址:
|
|
338
343
|
*
|
|
339
344
|
* [count, address1, address2, ...]
|
|
340
345
|
*
|
|
@@ -367,11 +372,11 @@ export default class Balloc {
|
|
|
367
372
|
* [内部使用]
|
|
368
373
|
* 从回收列表中移除空闲区域。
|
|
369
374
|
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
375
|
+
* 按请求的确切容量进行查找。当存在对应的页面时,从该页面中移除并返回最后注册的地址,
|
|
376
|
+
* 从而将回收实现为 LIFO(后进先出)栈。
|
|
372
377
|
*
|
|
373
|
-
* @param {number} capacity -
|
|
374
|
-
* @returns {number}
|
|
378
|
+
* @param {number} capacity - 期望的容量(以字节为单位)。
|
|
379
|
+
* @returns {number} 可回收区域的起始地址,若不存在具有请求容量的区域则返回 `UNKNOWN`。
|
|
375
380
|
* @private
|
|
376
381
|
*/
|
|
377
382
|
recyclePop(capacity) {
|
|
@@ -389,38 +394,46 @@ export default class Balloc {
|
|
|
389
394
|
}
|
|
390
395
|
|
|
391
396
|
// ========== 垃圾回收 (GARBAGE COLLECTION) ==========
|
|
397
|
+
|
|
392
398
|
/**
|
|
393
|
-
* 使用 ReBump 算法对 `dataBus`
|
|
399
|
+
* 使用 ReBump 算法对 `dataBus` 进行碎片整理(压缩)。
|
|
394
400
|
*
|
|
395
401
|
* 顺序遍历 `controlBus` 中注册的标头,并将仍然有效的区域复制到新的 `dataBus` 中,
|
|
396
|
-
*
|
|
402
|
+
* 从而消除它们之间存在的空闲空间。每个存活块的地址都会在其各自的标头中进行更新。
|
|
397
403
|
*
|
|
398
404
|
* `controlBus` 中块的顺序得以保留。每个块的内部数据也完全得以保留,
|
|
399
|
-
*
|
|
405
|
+
* 包括其预留容量和当前占用数。
|
|
400
406
|
*
|
|
401
|
-
*
|
|
407
|
+
* 压缩完成后:
|
|
402
408
|
* - 所有可回收区域均被消除;
|
|
403
409
|
* - `dataPointer` 指向最后一个存活块之后的第一个字节;
|
|
404
|
-
* - `dataBus`
|
|
410
|
+
* - `dataBus` 为活动块呈现出一个连续的堆。
|
|
405
411
|
*
|
|
406
412
|
* 控制复杂度为 O(n),其中 `n` 是遍历的标头数量。
|
|
407
|
-
*
|
|
413
|
+
* 实际的内存移动成本还取决于属于存活块的字节量。
|
|
408
414
|
*
|
|
409
415
|
* @returns {void}
|
|
416
|
+
* @throws {Error} 如果有任何块通过 withView() 被借用。
|
|
410
417
|
*/
|
|
411
418
|
gc() {
|
|
419
|
+
this.assertNoActiveViews();
|
|
420
|
+
|
|
412
421
|
const newDataBus = new Uint8Array(this.dataBus.buffer.byteLength);
|
|
413
422
|
this.recycle.clear();
|
|
414
423
|
this.dataPointer = 0 >>> 0;
|
|
415
|
-
|
|
424
|
+
|
|
425
|
+
for (let i = 0; i < this.controlPointer; i += HEADER_SIZE) {
|
|
416
426
|
if (this.controlBus[i + COOKIE_OFFSET] === 0) continue;
|
|
427
|
+
|
|
417
428
|
const start = this.controlAdapter.getUint32(i + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
418
429
|
const size = (1 << this.controlBus[i + CAPACITY_OFFSET] >>> 0) * this.wordSize;
|
|
419
430
|
const end = start + size;
|
|
431
|
+
|
|
420
432
|
newDataBus.set(this.dataBus.subarray(start, end), this.dataPointer);
|
|
421
433
|
this.controlAdapter.setUint32(i + ADDRESS_OFFSET, this.dataPointer, IS_LITTLE_ENDIAN);
|
|
422
434
|
this.dataPointer += size;
|
|
423
435
|
}
|
|
436
|
+
|
|
424
437
|
this.dataBus = newDataBus;
|
|
425
438
|
this.dataAdapter = new DataView(this.dataBus.buffer);
|
|
426
439
|
this.auxiliary = new globalThis[`${this.bucketType}Array`](this.dataBus.buffer);
|
|
@@ -429,18 +442,22 @@ export default class Balloc {
|
|
|
429
442
|
}
|
|
430
443
|
|
|
431
444
|
// ========== 扩容 (SCALING) ==========
|
|
445
|
+
|
|
432
446
|
/**
|
|
433
447
|
* [!!! 内部使用,请勿在您的代码中使用 !!!]
|
|
434
|
-
* 将 `dataBus`
|
|
448
|
+
* 将 `dataBus` 的容量扩展为当前的两倍。
|
|
435
449
|
*
|
|
436
|
-
*
|
|
437
|
-
*
|
|
450
|
+
* 底层缓冲区被转移到一个更大的 `ArrayBuffer` 中,同时保留现有数据。
|
|
451
|
+
* 与该缓冲区关联的所有视图都会被重新创建,以引用新的存储空间。
|
|
438
452
|
*
|
|
439
453
|
* @returns {void}
|
|
440
|
-
* @throws {
|
|
454
|
+
* @throws {Error} 如果有任何块通过 withView() 被借用。
|
|
455
|
+
* @throws {RangeError} 如果 `dataBus` 的容量已超过支持的最大限制。
|
|
441
456
|
* @private
|
|
442
457
|
*/
|
|
443
458
|
grow() {
|
|
459
|
+
this.assertNoActiveViews();
|
|
460
|
+
|
|
444
461
|
if (this.size > 0x80000000) throw new RangeError("Data bus capacity exceeded");
|
|
445
462
|
|
|
446
463
|
const newSize = (this.size * 2) >>> 0;
|
|
@@ -456,13 +473,13 @@ export default class Balloc {
|
|
|
456
473
|
|
|
457
474
|
/**
|
|
458
475
|
* [!!! 内部使用,请勿在您的代码中使用 !!!]
|
|
459
|
-
* 将 `controlBus`
|
|
476
|
+
* 将 `controlBus` 的容量扩展为当前的两倍。
|
|
460
477
|
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
478
|
+
* 底层缓冲区被转移到一个更大的 `ArrayBuffer` 中,同时保留现有标头。
|
|
479
|
+
* `controlAdapter` 被重新创建以引用新的存储空间。
|
|
463
480
|
*
|
|
464
481
|
* @returns {void}
|
|
465
|
-
* @throws {RangeError}
|
|
482
|
+
* @throws {RangeError} 如果当前的 `controlBus` 容量已超过支持的最大限制。
|
|
466
483
|
* @private
|
|
467
484
|
*/
|
|
468
485
|
headerGrow() {
|
|
@@ -477,52 +494,54 @@ export default class Balloc {
|
|
|
477
494
|
// ========== 栈操作 (STACK OPERATIONS) ==========
|
|
478
495
|
|
|
479
496
|
/**
|
|
480
|
-
*
|
|
497
|
+
* 在块所占用的序列末尾追加一个元素。
|
|
481
498
|
*
|
|
482
|
-
*
|
|
483
|
-
*
|
|
499
|
+
* 该元素被写入最后一个占用元素之后的第一个空闲位置。
|
|
500
|
+
* 只有在容量范围内有可用空间时,块的占用数才会递增。
|
|
484
501
|
*
|
|
485
502
|
* @param {number} handler - 块句柄。
|
|
486
503
|
* @param {number} value - 要插入的值。
|
|
487
|
-
* @returns {boolean}
|
|
488
|
-
* @throws {Error}
|
|
504
|
+
* @returns {boolean} 如果值已成功插入则为 `true`;如果块已无可用空间则为 `false`。
|
|
505
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
506
|
+
*
|
|
489
507
|
*/
|
|
490
508
|
push(handler, value) {
|
|
491
|
-
|
|
492
509
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
510
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
493
511
|
|
|
494
512
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
495
513
|
|
|
496
|
-
this.writer(meta.dataAddress + meta.occupied * this.wordSize
|
|
497
|
-
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1,IS_LITTLE_ENDIAN);
|
|
514
|
+
this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
|
|
515
|
+
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
|
|
498
516
|
|
|
499
517
|
return true;
|
|
500
518
|
}
|
|
501
519
|
|
|
502
520
|
/**
|
|
503
|
-
*
|
|
521
|
+
* 移除块中最后一个占用的元素并将其返回。
|
|
504
522
|
*
|
|
505
|
-
*
|
|
506
|
-
*
|
|
523
|
+
* 在读取被移除的元素之前,占用数会先递减。对应的字节不会被清空,
|
|
524
|
+
* 并且仍保留在分配的区域中,但不再属于已占用的序列。
|
|
507
525
|
*
|
|
508
526
|
* @param {number} handler - 块句柄。
|
|
509
|
-
* @returns {number|undefined}
|
|
510
|
-
* @throws {Error}
|
|
527
|
+
* @returns {number|undefined} 被移除的值,若块为空则返回 `undefined`。
|
|
528
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
511
529
|
*/
|
|
512
530
|
pop(handler) {
|
|
513
531
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
532
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
514
533
|
|
|
515
534
|
if (meta.occupied === 0) return undefined;
|
|
516
535
|
|
|
517
|
-
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1,IS_LITTLE_ENDIAN);
|
|
536
|
+
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
518
537
|
return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
|
|
519
538
|
}
|
|
520
539
|
|
|
521
540
|
/**
|
|
522
|
-
*
|
|
541
|
+
* 返回最后一个占用的元素,但不将其移除。
|
|
523
542
|
*
|
|
524
543
|
* @param {number} handler - 块句柄。
|
|
525
|
-
* @returns {number|undefined}
|
|
544
|
+
* @returns {number|undefined} 最后一个占用的值,若块为空则返回 `undefined`。
|
|
526
545
|
* @throws {Error} 如果句柄无效或已损坏。
|
|
527
546
|
*/
|
|
528
547
|
peek(handler) {
|
|
@@ -538,55 +557,58 @@ export default class Balloc {
|
|
|
538
557
|
/**
|
|
539
558
|
* 在占用序列的开头插入一个元素。
|
|
540
559
|
*
|
|
541
|
-
*
|
|
542
|
-
*
|
|
560
|
+
* 当前已占用的元素向前移动一个位置,并将 `value` 写入第一个位置。
|
|
561
|
+
* 该操作要求块的容量中存在空闲位置。
|
|
543
562
|
*
|
|
544
|
-
*
|
|
563
|
+
* 由于需要移动数据,该操作的成本与占用元素的数量成正比。
|
|
545
564
|
*
|
|
546
565
|
* @param {number} handler - 块句柄。
|
|
547
566
|
* @param {number} value - 要插入的值。
|
|
548
|
-
* @returns {boolean}
|
|
549
|
-
* @throws {Error}
|
|
567
|
+
* @returns {boolean} 如果值已成功插入则为 `true`;如果块已满则为 `false`。
|
|
568
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
550
569
|
*/
|
|
551
570
|
nudge(handler, value) {
|
|
552
571
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
572
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
553
573
|
|
|
554
574
|
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
555
575
|
|
|
556
576
|
this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
|
|
557
577
|
this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
|
|
558
|
-
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1,IS_LITTLE_ENDIAN);
|
|
578
|
+
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
|
|
559
579
|
|
|
560
580
|
return true;
|
|
561
581
|
}
|
|
562
582
|
|
|
563
583
|
/**
|
|
564
|
-
*
|
|
584
|
+
* 移除块中第一个占用的元素并将其返回。
|
|
565
585
|
*
|
|
566
|
-
*
|
|
586
|
+
* 其余元素向占用区域的开头方向移动一个位置。块的物理容量保持不变。
|
|
567
587
|
*
|
|
568
588
|
* 该操作的成本与移动的元素数量成正比。
|
|
569
589
|
*
|
|
570
590
|
* @param {number} handler - 块句柄。
|
|
571
|
-
* @returns {number|undefined}
|
|
572
|
-
* @throws {Error}
|
|
591
|
+
* @returns {number|undefined} 第一个被移除的值,若块为空则返回 `undefined`。
|
|
592
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
573
593
|
*/
|
|
574
594
|
pull(handler) {
|
|
575
595
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
596
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
576
597
|
|
|
577
598
|
if (meta.occupied === 0) return undefined;
|
|
578
599
|
|
|
579
600
|
const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
580
601
|
this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
|
|
581
602
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
603
|
+
|
|
582
604
|
return result;
|
|
583
605
|
}
|
|
584
606
|
|
|
585
607
|
/**
|
|
586
|
-
*
|
|
608
|
+
* 返回第一个占用的元素,但不将其移除。
|
|
587
609
|
*
|
|
588
610
|
* @param {number} handler - 块句柄。
|
|
589
|
-
* @returns {number|undefined}
|
|
611
|
+
* @returns {number|undefined} 第一个占用的值,若块为空则返回 `undefined`。
|
|
590
612
|
* @throws {Error} 如果句柄无效或已损坏。
|
|
591
613
|
*/
|
|
592
614
|
head(handler) {
|
|
@@ -594,7 +616,7 @@ export default class Balloc {
|
|
|
594
616
|
|
|
595
617
|
if (meta.occupied === 0) return undefined;
|
|
596
618
|
|
|
597
|
-
return this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
619
|
+
return this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
|
|
598
620
|
}
|
|
599
621
|
|
|
600
622
|
// ========== 随机访问 (RANDOM ACCESS) ==========
|
|
@@ -602,12 +624,12 @@ export default class Balloc {
|
|
|
602
624
|
/**
|
|
603
625
|
* 检查某个值是否存在于块的占用元素中。
|
|
604
626
|
*
|
|
605
|
-
*
|
|
627
|
+
* 该查找直接在辅助 `TypedArray` 上执行,并且仅考虑当前占用的范围,
|
|
606
628
|
* 而不是整个预留的物理容量。
|
|
607
629
|
*
|
|
608
630
|
* @param {number} handler - 块句柄。
|
|
609
|
-
*
|
|
610
|
-
* @returns {boolean} 如果 `value` 存在于占用元素中则为 `true
|
|
631
|
+
* @param {number} value - 要查找的值。
|
|
632
|
+
* @returns {boolean} 如果 `value` 存在于占用元素中则为 `true`,否则为 `false`。
|
|
611
633
|
* @throws {Error} 如果句柄无效或已损坏。
|
|
612
634
|
*/
|
|
613
635
|
has(handler, value) {
|
|
@@ -620,16 +642,66 @@ export default class Balloc {
|
|
|
620
642
|
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).includes(value);
|
|
621
643
|
}
|
|
622
644
|
|
|
623
|
-
|
|
624
|
-
*
|
|
645
|
+
/**
|
|
646
|
+
* 定位元素在块的占用序列中首次出现的位置。
|
|
647
|
+
*
|
|
648
|
+
* 查找按索引升序执行,使用底层 TypedArray 的 `indexOf` 操作。
|
|
649
|
+
* 搜索仅考虑占用的元素,忽略块中未使用的容量。
|
|
625
650
|
*
|
|
626
|
-
*
|
|
651
|
+
* 可以通过 `fromIndex` 指定搜索的起始位置,该参数遵循 TypedArray 的索引规则。
|
|
627
652
|
*
|
|
628
653
|
* @param {number} handler - 块句柄。
|
|
629
|
-
* @param {number}
|
|
630
|
-
* @
|
|
654
|
+
* @param {number|bigint} searchElement - 要定位的元素。
|
|
655
|
+
* @param {number} [fromIndex=0] - 开始搜索的索引。
|
|
656
|
+
* @returns {number} 首次出现的索引;若未找到该元素或块为空,则返回 `-1`。
|
|
657
|
+
* @throws {Error} 如果句柄无效或已损坏。
|
|
658
|
+
*/
|
|
659
|
+
indexOf(handler, searchElement, fromIndex = 0) {
|
|
660
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
661
|
+
|
|
662
|
+
if (meta.occupied === 0) return -1;
|
|
663
|
+
|
|
664
|
+
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
665
|
+
|
|
666
|
+
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).indexOf(searchElement, fromIndex);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* 定位元素在块的占用序列中最后一次出现的位置。
|
|
671
|
+
*
|
|
672
|
+
* 查找按索引降序执行,使用底层 TypedArray 的 `lastIndexOf` 操作。
|
|
673
|
+
* 搜索仅考虑占用的元素,忽略块中未使用的容量。
|
|
674
|
+
*
|
|
675
|
+
* 可以通过 `fromIndex` 指定搜索的起始位置,该参数遵循 TypedArray 的索引规则。
|
|
676
|
+
* 默认情况下,搜索从最后一个占用元素开始。
|
|
677
|
+
*
|
|
678
|
+
* @param {number} handler - 块句柄。
|
|
679
|
+
* @param {number|bigint} searchElement - 要定位的元素。
|
|
680
|
+
* @param {number} [fromIndex=-1] - 开始搜索的索引。
|
|
681
|
+
* @returns {number} 最后一次出现的索引;若未找到该元素或块为空,则返回 `-1`。
|
|
631
682
|
* @throws {Error} 如果句柄无效或已损坏。
|
|
632
683
|
*/
|
|
684
|
+
lastIndexOf(handler, searchElement, fromIndex = -1) {
|
|
685
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
686
|
+
|
|
687
|
+
if (meta.occupied === 0) return -1;
|
|
688
|
+
|
|
689
|
+
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
690
|
+
|
|
691
|
+
return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).lastIndexOf(searchElement, fromIndex);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* 返回基于零的索引处所占用的元素。
|
|
697
|
+
*
|
|
698
|
+
* 超出 `[0, occupied)` 范围的索引不被视为错误,会返回 `undefined`。
|
|
699
|
+
*
|
|
700
|
+
* @param {number} handler - 块句柄。
|
|
701
|
+
* @param {number} index - 元素的基于零的索引。
|
|
702
|
+
* @returns {number|undefined} 存储在索引处的值;若索引超出占用区域,则返回 `undefined`。
|
|
703
|
+
* @throws {Error} 如果句柄无效或已损坏。
|
|
704
|
+
*/
|
|
633
705
|
get(handler, index) {
|
|
634
706
|
|
|
635
707
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
@@ -642,16 +714,17 @@ export default class Balloc {
|
|
|
642
714
|
/**
|
|
643
715
|
* 替换存储在已占用索引处的值。
|
|
644
716
|
*
|
|
645
|
-
*
|
|
717
|
+
* 该操作不会改变块的占用数。超出 `[0, occupied)` 范围的索引将被忽略并返回 `false`。
|
|
646
718
|
*
|
|
647
719
|
* @param {number} handler - 块句柄。
|
|
648
720
|
* @param {number} index - 元素的基于零的索引。
|
|
649
721
|
* @param {number} value - 新值。
|
|
650
|
-
* @returns {boolean}
|
|
651
|
-
* @throws {Error}
|
|
722
|
+
* @returns {boolean} 如果值已成功写入则为 `true`;如果索引超出占用区域则为 `false`。
|
|
723
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
652
724
|
*/
|
|
653
725
|
set(handler, index, value) {
|
|
654
726
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
727
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
655
728
|
|
|
656
729
|
if (index < 0 || index >= meta.occupied) return false;
|
|
657
730
|
|
|
@@ -659,26 +732,27 @@ export default class Balloc {
|
|
|
659
732
|
return true;
|
|
660
733
|
}
|
|
661
734
|
|
|
662
|
-
// ============= 杂项 (MISCELLANEOUS)
|
|
735
|
+
// ============= 杂项 (MISCELLANEOUS) ==============
|
|
663
736
|
|
|
664
737
|
/**
|
|
665
|
-
*
|
|
738
|
+
* 将块的占用数清零,并用相同的值填满其整个物理容量。
|
|
666
739
|
*
|
|
667
|
-
*
|
|
668
|
-
*
|
|
740
|
+
* 填充范围涵盖所有可用的槽位(slots),而不仅仅是当前占用的元素。
|
|
741
|
+
* 操作完成后,无论填充使用的是什么值,该块都会被视为是空的。
|
|
669
742
|
*
|
|
670
743
|
* @param {number} handler - 块句柄。
|
|
671
744
|
* @param {number} [value=0] - 用于填充所有槽位的值。
|
|
672
745
|
* @returns {void}
|
|
673
|
-
* @throws {Error}
|
|
746
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
674
747
|
*/
|
|
675
|
-
clear(handler, value=0) {
|
|
748
|
+
clear(handler, value = 0) {
|
|
676
749
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
750
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
677
751
|
|
|
678
752
|
const slots = meta.byteCapacity / this.wordSize;
|
|
679
753
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
680
754
|
|
|
681
|
-
this.auxiliary.fill(value, rawAddress, rawAddress + slots);
|
|
755
|
+
this.auxiliary.fill(value, rawAddress, rawAddress + slots);
|
|
682
756
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, 0, IS_LITTLE_ENDIAN);
|
|
683
757
|
}
|
|
684
758
|
|
|
@@ -686,24 +760,23 @@ export default class Balloc {
|
|
|
686
760
|
/**
|
|
687
761
|
* 用某个值填充块的一段槽位范围。
|
|
688
762
|
*
|
|
689
|
-
* 该范围使用基于零的索引,并遵循 `[start, end)`
|
|
690
|
-
*
|
|
691
|
-
* 当未指定 `end` 时,填充会延伸到块物理容量的末尾。
|
|
763
|
+
* 该范围使用基于零的索引,并遵循 `[start, end)` 惯例,因此 `start` 是包含的(inclusive),
|
|
764
|
+
* 而 `end` 是排除的(exclusive)。当未提供 `end` 时,填充将一直延伸到块物理容量的末尾。
|
|
692
765
|
*
|
|
693
|
-
*
|
|
766
|
+
* 块的占用数将变为 `end`,即使填充的范围超出了先前的占用数也是如此。
|
|
694
767
|
*
|
|
695
768
|
* @param {number} handler - 块句柄。
|
|
696
769
|
* @param {number} value - 用于填充的值。
|
|
697
|
-
* @param {number} [start=0] -
|
|
698
|
-
* @param {number|null} [end=null] -
|
|
699
|
-
* 使用块的总容量。
|
|
770
|
+
* @param {number} [start=0] - 起始索引(包含)。
|
|
771
|
+
* @param {number|null} [end=null] - 结束索引(排除)。省略或为 `null` 时,使用块的总容量。
|
|
700
772
|
* @returns {void}
|
|
701
|
-
* @throws {Error}
|
|
773
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
702
774
|
* @throws {RangeError} 如果 `start` 或 `end` 超出容量边界,或者 `start > end`。
|
|
703
775
|
*/
|
|
704
776
|
fill(handler, value, start = 0, end = null) {
|
|
705
777
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
706
|
-
|
|
778
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
779
|
+
|
|
707
780
|
const slots = meta.byteCapacity / this.wordSize;
|
|
708
781
|
const lastIndex = end ?? slots;
|
|
709
782
|
|
|
@@ -718,24 +791,24 @@ export default class Balloc {
|
|
|
718
791
|
}
|
|
719
792
|
|
|
720
793
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
721
|
-
|
|
722
794
|
this.auxiliary.fill(value, rawAddress + start, rawAddress + lastIndex);
|
|
723
795
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, lastIndex, IS_LITTLE_ENDIAN);
|
|
724
796
|
}
|
|
725
797
|
|
|
726
798
|
/**
|
|
727
|
-
*
|
|
799
|
+
* 用某个值填满块的所有物理槽位,并将整个容量标记为已占用。
|
|
728
800
|
*
|
|
729
|
-
* 与 `clear()`
|
|
801
|
+
* 与 `clear()` 不同,该操作产生的是一个完全占用的块。
|
|
730
802
|
*
|
|
731
803
|
* @param {number} handler - 块句柄。
|
|
732
804
|
* @param {number} value - 用于填充的值。
|
|
733
805
|
* @returns {void}
|
|
734
|
-
* @throws {Error}
|
|
806
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
735
807
|
*/
|
|
736
808
|
fillAll(handler, value) {
|
|
737
809
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
738
|
-
|
|
810
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
811
|
+
|
|
739
812
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
740
813
|
const capacityElements = meta.byteCapacity / this.wordSize;
|
|
741
814
|
|
|
@@ -744,28 +817,27 @@ export default class Balloc {
|
|
|
744
817
|
}
|
|
745
818
|
|
|
746
819
|
/**
|
|
747
|
-
* 将 `Array` 或 `TypedArray`
|
|
820
|
+
* 将 `Array` 或 `TypedArray` 中的元素复制到块的开头。
|
|
748
821
|
*
|
|
749
|
-
*
|
|
750
|
-
*
|
|
822
|
+
* 复制的数量受块容量的限制。占用数会被更新为先前占用数与实际复制数量中的较大者;
|
|
823
|
+
* 因此,当接收到较短的序列时,调用 `put()` 不会减少现有的占用数。
|
|
751
824
|
*
|
|
752
825
|
* 对于 `Array`,仅复制能装下的前几个元素。
|
|
753
|
-
* 对于 `TypedArray
|
|
826
|
+
* 对于 `TypedArray`,会直接使用源的子数组(subarray),无需进行中间复制。
|
|
754
827
|
*
|
|
755
828
|
* @param {number} handler - 块句柄。
|
|
756
829
|
* @param {Array|TypedArray} data - 要复制的值序列。
|
|
757
830
|
* @returns {void}
|
|
758
|
-
* @throws {Error}
|
|
831
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
759
832
|
* @throws {TypeError} 如果 `data` 既不是 `Array` 也不是 `TypedArray`。
|
|
760
833
|
*/
|
|
761
834
|
put(handler, data) {
|
|
762
835
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
836
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
763
837
|
|
|
764
838
|
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
765
839
|
const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
|
|
766
840
|
|
|
767
|
-
meta.occupied = Math.max(length, meta.occupied);
|
|
768
|
-
|
|
769
841
|
if (Array.isArray(data)) {
|
|
770
842
|
this.auxiliary.set(data.slice(0, length), rawAddress);
|
|
771
843
|
} else if (isTypedArray(data)) {
|
|
@@ -773,43 +845,78 @@ export default class Balloc {
|
|
|
773
845
|
} else {
|
|
774
846
|
const actual = data?.constructor?.name ?? (data === null ? 'null' : typeof data);
|
|
775
847
|
throw new TypeError(`data must be Array or TypedArray, but ${actual} was given.`);
|
|
776
|
-
}
|
|
777
|
-
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, Math.max(length, meta.occupied), IS_LITTLE_ENDIAN);
|
|
778
851
|
}
|
|
779
852
|
|
|
780
|
-
|
|
781
|
-
|
|
853
|
+
/**
|
|
854
|
+
* 返回块当前占用元素的副本。
|
|
855
|
+
*
|
|
856
|
+
* 结果是一个由 `bucketSize` 配置的同类型 `TypedArray`。未占用的物理容量不包含在结果中。
|
|
857
|
+
*
|
|
858
|
+
* 该操作不直接暴露内部存储:该方法使用 `TypedArray.prototype.slice()`,
|
|
859
|
+
* 生成一个具有独立存储空间的新视图。
|
|
860
|
+
*
|
|
861
|
+
* @param {number} handler - 块句柄。
|
|
862
|
+
* @returns {TypedArray} 块占用元素的副本。当块为空时,返回配置类型的空 `TypedArray`。
|
|
863
|
+
* @throws {Error} 如果句柄无效或已损坏。
|
|
864
|
+
*/
|
|
865
|
+
pick(handler) {
|
|
866
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
867
|
+
|
|
868
|
+
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
869
|
+
|
|
870
|
+
if (meta.occupied === 0) {
|
|
871
|
+
return this.auxiliary.slice(rawAddress, rawAddress);
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
return this.auxiliary.slice(rawAddress, rawAddress + meta.occupied);
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* 在块占用区域的直接视图上执行回调函数。
|
|
782
879
|
*
|
|
783
|
-
*
|
|
880
|
+
* 该视图共享 Balloc 的内部存储且不进行复制。
|
|
881
|
+
* 在回调执行期间,该块受到保护,免受不兼容操作的影响。
|
|
784
882
|
*
|
|
785
|
-
*
|
|
786
|
-
*
|
|
883
|
+
* 回调必须是同步的。接收到的视图不得在回调结束后被存储、返回或使用。
|
|
884
|
+
* 此限制同样适用于从原始视图派生的子视图(subviews)。
|
|
787
885
|
*
|
|
788
886
|
* @param {number} handler - 块句柄。
|
|
789
|
-
* @
|
|
790
|
-
* @
|
|
887
|
+
* @param {Function} callback - 接收共享 TypedArray 的函数。
|
|
888
|
+
* @returns {*} 回调函数产生 返回值。
|
|
889
|
+
* @throws {TypeError} 如果回调不是函数。
|
|
890
|
+
* @throws {Error} 如果句柄无效或已被借用。
|
|
791
891
|
*/
|
|
792
|
-
|
|
892
|
+
withView(handler, callback) {
|
|
893
|
+
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
894
|
+
|
|
793
895
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
896
|
+
const flagsAddress = meta.headerAddress + FLAGS_OFFSET;
|
|
897
|
+
const flags = this.controlBus[flagsAddress];
|
|
794
898
|
|
|
795
|
-
|
|
899
|
+
if (flags & VIEW_LOCK) throw new Error("Block is already borrowed");
|
|
796
900
|
|
|
797
|
-
|
|
798
|
-
return this.auxiliary.slice(rawAddress, rawAddress);
|
|
799
|
-
}
|
|
901
|
+
this.controlBus[flagsAddress] = flags | VIEW_LOCK;
|
|
800
902
|
|
|
801
|
-
|
|
903
|
+
try {
|
|
904
|
+
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
905
|
+
return callback(this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied));
|
|
906
|
+
} finally {
|
|
907
|
+
this.controlBus[flagsAddress] &= ~VIEW_LOCK;
|
|
908
|
+
}
|
|
802
909
|
}
|
|
803
910
|
|
|
804
|
-
// ========
|
|
911
|
+
// ======== 槽位容量 (CAPACITY IN SLOTS) ==========
|
|
805
912
|
|
|
806
913
|
/**
|
|
807
|
-
*
|
|
914
|
+
* 返回块的物理容量(以字节为单位)。
|
|
808
915
|
*
|
|
809
|
-
*
|
|
916
|
+
* 容量对应于为块中所有槽位预留的空间,无论当前占用了多少个元素。
|
|
810
917
|
*
|
|
811
918
|
* @param {number} handler - 块句柄。
|
|
812
|
-
* @returns {number}
|
|
919
|
+
* @returns {number} 块的物理容量,以字节为单位。
|
|
813
920
|
* @throws {Error} 如果句柄无效或已损坏。
|
|
814
921
|
*/
|
|
815
922
|
byteSize(handler) {
|
|
@@ -821,19 +928,19 @@ export default class Balloc {
|
|
|
821
928
|
* 返回块中当前占用的元素数量。
|
|
822
929
|
*
|
|
823
930
|
* @param {number} handler - 块句柄。
|
|
824
|
-
* @returns {number}
|
|
931
|
+
* @returns {number} 已占用元素数量。
|
|
825
932
|
* @throws {Error} 如果句柄无效或已损坏。
|
|
826
933
|
*/
|
|
827
|
-
occupied(handler){
|
|
934
|
+
occupied(handler) {
|
|
828
935
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
829
936
|
return meta.occupied;
|
|
830
937
|
|
|
831
938
|
}
|
|
832
939
|
|
|
833
940
|
/**
|
|
834
|
-
*
|
|
941
|
+
* 返回以元素数量表示的块容量。
|
|
835
942
|
*
|
|
836
|
-
*
|
|
943
|
+
* 该值对应于物理预留的总槽位数,而不是当前占用的数量。
|
|
837
944
|
*
|
|
838
945
|
* @param {number} handler - 块句柄。
|
|
839
946
|
* @returns {number} 块中可用的槽位总数。
|
|
@@ -844,66 +951,81 @@ export default class Balloc {
|
|
|
844
951
|
return 1 << meta.capacityExponent >>> 0;
|
|
845
952
|
}
|
|
846
953
|
|
|
847
|
-
// ========== 块间操作 (Inter-block Operations) =============
|
|
848
|
-
|
|
849
954
|
/**
|
|
850
|
-
*
|
|
955
|
+
* 检查内存块是否已达到其分配容量。
|
|
851
956
|
*
|
|
852
|
-
*
|
|
853
|
-
* 实际复制的数量受源中占用元素数量、目标物理容量以及 `slots` 中指定的限制所限制。
|
|
957
|
+
* 当占用元素数量等于其分配的总容量、且没有剩余位置可存储新元素时,块被视为已满。
|
|
854
958
|
*
|
|
855
|
-
*
|
|
959
|
+
* @param {number} handler - 块句柄。
|
|
960
|
+
* @returns {boolean} 如果块已满则为 `true`,否则为 `false`。
|
|
961
|
+
* @throws {Error} 如果句柄无效或已损坏。
|
|
962
|
+
*/
|
|
963
|
+
isFull(handler) {
|
|
964
|
+
const meta = this.extractMetadata(handler, this.metadata);
|
|
965
|
+
return (1 << meta.capacityExponent >>> 0) - meta.occupied === 0;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
// ========== 块间操作 (Operations between blocks)=============
|
|
969
|
+
|
|
970
|
+
/**
|
|
971
|
+
* 将占用元素从一个块复制到另一个块。
|
|
972
|
+
*
|
|
973
|
+
* 复制从 `source` 的第一个元素和 `destination` 的第一个槽位开始。
|
|
974
|
+
* 实际复制的数量受源中占用元素数量、目标物理容量以及 `slots` 中指定限制的约束。
|
|
975
|
+
*
|
|
976
|
+
* 必要时会提高目标的占用数,以涵盖复制的元素。目标的容量保持不变。
|
|
856
977
|
*
|
|
857
978
|
* 该操作在 `dataBus` 上使用 `Uint8Array.prototype.copyWithin()`,
|
|
858
|
-
*
|
|
979
|
+
* 因此返回值为代表数据总线的 `Uint8Array` 本身。
|
|
859
980
|
*
|
|
860
981
|
* @param {number} source - 源块句柄。
|
|
861
|
-
* @param {number}
|
|
862
|
-
* @param {number} [slots=UNKNOWN] -
|
|
863
|
-
* `UNKNOWN`
|
|
982
|
+
* @param {number} destination - 目标块句柄。
|
|
983
|
+
* @param {number} [slots=UNKNOWN] - 要复制的最大元素数。
|
|
984
|
+
* `UNKNOWN` 代表哨兵值,用于不施加额外的限制。
|
|
864
985
|
* @returns {Uint8Array} 操作后的 `dataBus`。
|
|
865
|
-
* @throws {Error}
|
|
986
|
+
* @throws {Error} 如果句柄无效、已损坏,或者目标块正通过 withView() 被借用。
|
|
866
987
|
*/
|
|
867
|
-
copy(source,
|
|
988
|
+
copy(source, destination, slots = UNKNOWN) {
|
|
868
989
|
/** @type {Metadata} */
|
|
869
990
|
const metaA = {};
|
|
870
991
|
/** @type {Metadata} */
|
|
871
992
|
const metaB = {};
|
|
872
993
|
|
|
873
994
|
this.extractMetadata(source, metaA);
|
|
874
|
-
this.extractMetadata(
|
|
995
|
+
this.extractMetadata(destination, metaB);
|
|
996
|
+
this.assertNotBorrowed(metaB.headerAddress);
|
|
875
997
|
|
|
876
998
|
const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
|
|
877
999
|
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
878
1000
|
const newOccupied = Math.max(metaB.occupied, slotsToCopy);
|
|
879
1001
|
|
|
880
1002
|
this.controlAdapter.setUint32(metaB.headerAddress + OCCUPIED_OFFSET, newOccupied, IS_LITTLE_ENDIAN);
|
|
881
|
-
|
|
882
1003
|
return this.dataBus.copyWithin(metaB.dataAddress, metaA.dataAddress, metaA.dataAddress + slotsToCopy * this.wordSize);
|
|
883
1004
|
}
|
|
884
1005
|
|
|
885
1006
|
/**
|
|
886
1007
|
* 在块内部复制一段元素区域。
|
|
887
1008
|
*
|
|
888
|
-
*
|
|
889
|
-
* `target` 标识目标位置,`start` 标识源起始位置,`length`
|
|
1009
|
+
* 索引基于零,并遵循 `Array.prototype.copyWithin()` 的语义:
|
|
1010
|
+
* `target` 标识目标位置,`start` 标识源起始位置,`length` 标识要复制的最大元素数。
|
|
890
1011
|
*
|
|
891
|
-
*
|
|
1012
|
+
* 负索引会从块容量的末尾开始解析。经过此规范化后,索引会被限制在物理容量内。
|
|
892
1013
|
* 复制的数量也受实际占用元素以及从 `target` 开始的可用空间的限制。
|
|
893
1014
|
*
|
|
894
|
-
*
|
|
1015
|
+
* 该操作支持重叠区域,并且当目标范围超过先前的占用数时,可以增加块的占用数。
|
|
895
1016
|
*
|
|
896
1017
|
* @param {number} handler - 块句柄。
|
|
897
1018
|
* @param {number} target - 起始目标索引。
|
|
898
1019
|
* @param {number} start - 起始源索引。
|
|
899
|
-
* @param {number} length -
|
|
1020
|
+
* @param {number} length - 要复制的最大元素数。
|
|
900
1021
|
* @returns {Uint8Array} 操作后的 `dataBus`。
|
|
901
|
-
* @throws {Error}
|
|
1022
|
+
* @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
|
|
902
1023
|
*/
|
|
903
1024
|
copyWithin(handler, target, start, length) {
|
|
904
1025
|
const meta = this.extractMetadata(handler, this.metadata);
|
|
905
|
-
|
|
1026
|
+
this.assertNotBorrowed(meta.headerAddress);
|
|
906
1027
|
|
|
1028
|
+
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
907
1029
|
target = Math.trunc(target);
|
|
908
1030
|
start = Math.trunc(start);
|
|
909
1031
|
length = Math.trunc(length);
|
|
@@ -911,12 +1033,9 @@ export default class Balloc {
|
|
|
911
1033
|
if (target < 0) target += slots;
|
|
912
1034
|
if (start < 0) start += slots;
|
|
913
1035
|
|
|
914
|
-
// 将索引限制在容量范围内
|
|
915
1036
|
target = Math.max(0, Math.min(target, slots));
|
|
916
1037
|
start = Math.max(0, Math.min(start, slots));
|
|
917
1038
|
|
|
918
|
-
// 将复制限制在从 target 开始的可用空间
|
|
919
|
-
// 以及从 start 开始的实际占用元素
|
|
920
1039
|
const maxRead = Math.max(0, meta.occupied - start);
|
|
921
1040
|
const maxWrite = slots - target;
|
|
922
1041
|
length = Math.max(0, Math.min(length, maxRead, maxWrite));
|
|
@@ -925,9 +1044,8 @@ export default class Balloc {
|
|
|
925
1044
|
|
|
926
1045
|
const targetAddress = meta.dataAddress + target * this.wordSize;
|
|
927
1046
|
const sourceAddress = meta.dataAddress + start * this.wordSize;
|
|
928
|
-
|
|
929
|
-
// 如果复制扩展了块的占用顶部,则更新占用数
|
|
930
1047
|
const newOccupied = Math.max(meta.occupied, target + length);
|
|
1048
|
+
|
|
931
1049
|
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, newOccupied, IS_LITTLE_ENDIAN);
|
|
932
1050
|
|
|
933
1051
|
return this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + length * this.wordSize);
|
|
@@ -938,13 +1056,13 @@ export default class Balloc {
|
|
|
938
1056
|
*
|
|
939
1057
|
* 复制的数量受源中占用元素数量以及目标容量中剩余可用空间的限制。
|
|
940
1058
|
*
|
|
941
|
-
* 该操作支持 `target` 和 `source`
|
|
942
|
-
*
|
|
1059
|
+
* 该操作支持 `target` 和 `source` 引用同一个块。在这种情况下,
|
|
1060
|
+
* 复制操作将在块自身的区域上执行,并保留 `copyWithin()` 的重叠语义。
|
|
943
1061
|
*
|
|
944
1062
|
* @param {number} target - 将接收元素的块句柄。
|
|
945
|
-
* @param {number} source -
|
|
1063
|
+
* @param {number} source - 要将其元素追加进来的块句柄。
|
|
946
1064
|
* @returns {number} 实际追加的元素数量。
|
|
947
|
-
* @throws {Error}
|
|
1065
|
+
* @throws {Error} 如果句柄无效、已损坏,或者目标块正通过 withView() 被借用。
|
|
948
1066
|
*/
|
|
949
1067
|
append(target, source) {
|
|
950
1068
|
const metaTarget = {};
|
|
@@ -952,6 +1070,7 @@ export default class Balloc {
|
|
|
952
1070
|
|
|
953
1071
|
this.extractMetadata(target, metaTarget);
|
|
954
1072
|
this.extractMetadata(source, metaSource);
|
|
1073
|
+
this.assertNotBorrowed(metaTarget.headerAddress);
|
|
955
1074
|
|
|
956
1075
|
const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
|
|
957
1076
|
const count = Math.min(available, metaSource.occupied);
|
|
@@ -961,11 +1080,7 @@ export default class Balloc {
|
|
|
961
1080
|
const targetAddress = metaTarget.dataAddress + metaTarget.occupied * this.wordSize;
|
|
962
1081
|
const sourceAddress = metaSource.dataAddress;
|
|
963
1082
|
|
|
964
|
-
|
|
965
|
-
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
966
|
-
} else {
|
|
967
|
-
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
968
|
-
}
|
|
1083
|
+
this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
|
|
969
1084
|
|
|
970
1085
|
this.controlAdapter.setUint32(metaTarget.headerAddress + OCCUPIED_OFFSET, metaTarget.occupied + count, IS_LITTLE_ENDIAN);
|
|
971
1086
|
|
|
@@ -975,12 +1090,11 @@ export default class Balloc {
|
|
|
975
1090
|
/**
|
|
976
1091
|
* 创建块的独立副本。
|
|
977
1092
|
*
|
|
978
|
-
*
|
|
979
|
-
* 预留区域中的所有字节(包括当前未占用的位置)都会被复制。
|
|
1093
|
+
* 新块具有与原块相同的物理容量和占用数。预留区域中的所有字节(包括当前未占用的位置)均会被复制。
|
|
980
1094
|
*
|
|
981
|
-
*
|
|
1095
|
+
* 新块会获得一个新句柄,并继续受正常的分配和回收规则约束。
|
|
982
1096
|
*
|
|
983
|
-
* @param {number} handler -
|
|
1097
|
+
* @param {number} handler - 要克隆的块句柄。
|
|
984
1098
|
* @returns {number} 新块的句柄。
|
|
985
1099
|
* @throws {Error} 如果句柄无效或已损坏。
|
|
986
1100
|
*/
|
|
@@ -998,27 +1112,30 @@ export default class Balloc {
|
|
|
998
1112
|
}
|
|
999
1113
|
|
|
1000
1114
|
/**
|
|
1001
|
-
*
|
|
1115
|
+
* 交换两个块关联的数据区域。
|
|
1002
1116
|
*
|
|
1003
1117
|
* 该操作在标头之间交换区域地址、容量和占用数。句柄保持不变:
|
|
1004
|
-
*
|
|
1118
|
+
* 操作完成后,每个句柄继续标识其自己的标头,但该标头现在描述的是先前属于另一个块的区域。
|
|
1005
1119
|
*
|
|
1006
|
-
* `dataBus`
|
|
1120
|
+
* `dataBus` 中不会复制任何数据;仅修改两个标头的元数据。
|
|
1007
1121
|
*
|
|
1008
1122
|
* @param {number} source - 第一个块的句柄。
|
|
1009
1123
|
* @param {number} target - 第二个块的句柄。
|
|
1010
1124
|
* @returns {void}
|
|
1011
|
-
* @throws {Error}
|
|
1125
|
+
* @throws {Error} 如果句柄无效、已损坏,或者任何一个块正通过 withView() 被借用。
|
|
1012
1126
|
*/
|
|
1013
1127
|
swap(source, target) {
|
|
1014
1128
|
/** @type {Metadata} */
|
|
1015
1129
|
const metaA = {};
|
|
1016
1130
|
/** @type {Metadata} */
|
|
1017
1131
|
const metaB = {};
|
|
1018
|
-
|
|
1132
|
+
|
|
1019
1133
|
this.extractMetadata(source, metaA);
|
|
1020
1134
|
this.extractMetadata(target, metaB);
|
|
1021
1135
|
|
|
1136
|
+
this.assertNotBorrowed(metaA.headerAddress);
|
|
1137
|
+
this.assertNotBorrowed(metaB.headerAddress);
|
|
1138
|
+
|
|
1022
1139
|
this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
|
|
1023
1140
|
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
1024
1141
|
|
|
@@ -1027,7 +1144,7 @@ export default class Balloc {
|
|
|
1027
1144
|
|
|
1028
1145
|
this.controlAdapter.setUint32(metaA.headerAddress + OCCUPIED_OFFSET, metaB.occupied, IS_LITTLE_ENDIAN);
|
|
1029
1146
|
this.controlAdapter.setUint32(metaB.headerAddress + OCCUPIED_OFFSET, metaA.occupied, IS_LITTLE_ENDIAN);
|
|
1030
|
-
}
|
|
1147
|
+
}
|
|
1031
1148
|
|
|
1032
1149
|
|
|
1033
1150
|
// ============= 序列化 (SERIALIZATION) ==============
|
|
@@ -1035,10 +1152,10 @@ export default class Balloc {
|
|
|
1035
1152
|
/**
|
|
1036
1153
|
* 序列化内存块的内容和元数据。
|
|
1037
1154
|
*
|
|
1038
|
-
* 该方法通过 `pick()`
|
|
1039
|
-
*
|
|
1155
|
+
* 该方法通过 `pick()` 提取块的占用数据,连同容量和字类型(word type)信息
|
|
1156
|
+
* 一起打包成一个数据信封(envelope),并使用 CBOR 格式对其进行编码。
|
|
1040
1157
|
*
|
|
1041
|
-
* @param {number} handler -
|
|
1158
|
+
* @param {number} handler - 要序列化的块句柄。
|
|
1042
1159
|
* @returns {Uint8Array} 包含序列化块数据的缓冲区。
|
|
1043
1160
|
* @throws {Error} 如果句柄无效或已损坏。
|
|
1044
1161
|
*/
|
|
@@ -1070,12 +1187,12 @@ export default class Balloc {
|
|
|
1070
1187
|
/**
|
|
1071
1188
|
* 从序列化数据恢复内存块。
|
|
1072
1189
|
*
|
|
1073
|
-
* 解码 CBOR
|
|
1074
|
-
*
|
|
1190
|
+
* 解码 CBOR 信封,验证序列化数据的字类型是否与当前的分配器实例兼容,
|
|
1191
|
+
* 并分配一个能够容纳该内容所需容量的新块。随后,在新的块中恢复原始数据和占用数。
|
|
1075
1192
|
*
|
|
1076
1193
|
* @param {Uint8Array|ArrayBuffer} serialized - 包含序列化数据的缓冲区。
|
|
1077
1194
|
* @returns {number} 重新创建的新块的句柄。
|
|
1078
|
-
* @throws {Error}
|
|
1195
|
+
* @throws {Error} 如果解码数据的字类型与分配器配置的不一致。
|
|
1079
1196
|
*/
|
|
1080
1197
|
load(serialized) {
|
|
1081
1198
|
if (!(serialized instanceof ArrayBuffer) && !(serialized instanceof Uint8Array)) {
|
|
@@ -1130,20 +1247,52 @@ export default class Balloc {
|
|
|
1130
1247
|
|
|
1131
1248
|
// ============= 工具 (UTILITIES) ==============
|
|
1132
1249
|
|
|
1250
|
+
/**
|
|
1251
|
+
* 防止对已借用的块进行修改操作。
|
|
1252
|
+
*
|
|
1253
|
+
* 检查与所提供地址关联的标头中的 VIEW_LOCK 标志。
|
|
1254
|
+
*
|
|
1255
|
+
* @param {number} headerAddress - controlBus 中的标头地址。
|
|
1256
|
+
* @returns {void}
|
|
1257
|
+
* @throws {Error} 如果块正通过 withView() 被借用。
|
|
1258
|
+
* @private
|
|
1259
|
+
*/
|
|
1260
|
+
assertNotBorrowed(headerAddress) {
|
|
1261
|
+
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1262
|
+
throw new Error("Block is borrowed");
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* 防止在存在任何借用片段时替换或重新映射 dataBus 的操作。
|
|
1268
|
+
*
|
|
1269
|
+
* @returns {void}
|
|
1270
|
+
* @throws {Error} 如果有任何块通过 withView() 被借用。
|
|
1271
|
+
* @private
|
|
1272
|
+
*/
|
|
1273
|
+
assertNoActiveViews() {
|
|
1274
|
+
for (let i = 0; i < this.controlPointer; i += HEADER_SIZE) {
|
|
1275
|
+
if (this.controlBus[i + COOKIE_OFFSET] === 0) continue;
|
|
1276
|
+
if (this.controlBus[i + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1277
|
+
throw new Error("Cannot remap memory while a block is borrowed");
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1133
1282
|
/**
|
|
1134
1283
|
* [内部使用]
|
|
1135
1284
|
* 解码与句柄关联的元数据。
|
|
1136
1285
|
*
|
|
1137
|
-
*
|
|
1138
|
-
*
|
|
1286
|
+
* 句柄被拆分为代数和标头地址。然后重新计算标头中存储的 cookie 以验证该组合;
|
|
1287
|
+
* 只有在通过此验证后才会读取元数据。
|
|
1139
1288
|
*
|
|
1140
|
-
*
|
|
1141
|
-
*
|
|
1289
|
+
* 解码后的字段写入 `out` 对象中,该对象由函数自身返回。
|
|
1290
|
+
* 可以在多次调用之间复用该对象以避免新的分配。
|
|
1142
1291
|
*
|
|
1143
1292
|
* @param {number} handler - 要解码和验证的句柄。
|
|
1144
|
-
* @param {Metadata} out -
|
|
1293
|
+
* @param {Metadata} out - 将接收解码后元数据的对象。
|
|
1145
1294
|
* @returns {Metadata} 在 `out` 中提供的相同引用。
|
|
1146
|
-
* @throws {Error} 如果标头
|
|
1295
|
+
* @throws {Error} 如果标头 cookie 与句柄不匹配,或者句柄未能标识有效的标头。
|
|
1147
1296
|
* @private
|
|
1148
1297
|
*/
|
|
1149
1298
|
extractMetadata(handler, out) {
|
|
@@ -1158,29 +1307,28 @@ export default class Balloc {
|
|
|
1158
1307
|
}
|
|
1159
1308
|
|
|
1160
1309
|
out.dataAddress = this.controlAdapter.getUint32(out.headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
1161
|
-
out.capacityExponent
|
|
1310
|
+
out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
|
|
1162
1311
|
out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
|
|
1163
1312
|
out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
1164
1313
|
|
|
1165
1314
|
return out;
|
|
1166
1315
|
}
|
|
1167
1316
|
|
|
1168
|
-
|
|
1169
1317
|
/**
|
|
1170
1318
|
* [内部使用]
|
|
1171
|
-
* 计算标头的验证
|
|
1319
|
+
* 计算标头的验证 cookie。
|
|
1172
1320
|
*
|
|
1173
|
-
*
|
|
1174
|
-
*
|
|
1321
|
+
* cookie 是通过快速哈希(fast hash)从标头地址和 11 位代数派生而来的。
|
|
1322
|
+
* 结果会被缩减为 8 位。
|
|
1175
1323
|
*
|
|
1176
|
-
*
|
|
1324
|
+
* 值零被保留作为无效标头的指示符;因此,当计算结果为零时,该方法返回 `1`。
|
|
1177
1325
|
*
|
|
1178
|
-
*
|
|
1179
|
-
*
|
|
1326
|
+
* 该 cookie 不构成加密机制。其目的是在分配器的内存管理模型中
|
|
1327
|
+
* 检测无效、陈旧(stale)或损坏的句柄。
|
|
1180
1328
|
*
|
|
1181
1329
|
* @param {number} addr - 标头地址。
|
|
1182
|
-
* @param {number} gen -
|
|
1183
|
-
* @returns {number} 范围在 [1, 255] 内的 8 位
|
|
1330
|
+
* @param {number} gen - 与句柄关联的代数。仅考虑最低的 11 位。
|
|
1331
|
+
* @returns {number} 范围在 [1, 255] 内的 8 位 cookie。
|
|
1184
1332
|
* @private
|
|
1185
1333
|
*/
|
|
1186
1334
|
bakeCookie(addr, gen) {
|
|
@@ -1190,10 +1338,10 @@ export default class Balloc {
|
|
|
1190
1338
|
/**
|
|
1191
1339
|
* 返回当前存储在回收列表中的区域数量。
|
|
1192
1340
|
*
|
|
1193
|
-
*
|
|
1194
|
-
*
|
|
1341
|
+
* 每个区域对应一个无法直接交还给 `dataBus` 顶部的释放块,
|
|
1342
|
+
* 因此它仍然可供未来相同容量的分配进行复用。
|
|
1195
1343
|
*
|
|
1196
|
-
* @returns {number} `recycle`
|
|
1344
|
+
* @returns {number} 保存在 `recycle` 中的空闲区域数量。
|
|
1197
1345
|
*/
|
|
1198
1346
|
recycleCount() {
|
|
1199
1347
|
let count = 0;
|
|
@@ -1206,9 +1354,9 @@ export default class Balloc {
|
|
|
1206
1354
|
}
|
|
1207
1355
|
|
|
1208
1356
|
/**
|
|
1209
|
-
*
|
|
1357
|
+
* 返回当前存储在回收列表中的区域所属的总字节数。
|
|
1210
1358
|
*
|
|
1211
|
-
*
|
|
1359
|
+
* 该值是通过将每个空闲区域的容量乘以该容量注册的区域数量相加来计算的。
|
|
1212
1360
|
*
|
|
1213
1361
|
* @returns {number} 可回收区域占用的字节数。
|
|
1214
1362
|
*/
|
|
@@ -1225,9 +1373,9 @@ export default class Balloc {
|
|
|
1225
1373
|
/**
|
|
1226
1374
|
* 返回不属于当前可回收区域的已分配字节数。
|
|
1227
1375
|
*
|
|
1228
|
-
*
|
|
1376
|
+
* 该值是通过从 `dataPointer` 的总推进量中减去保存在 `recycle` 中的空闲区域所属的字节数来获取的。
|
|
1229
1377
|
*
|
|
1230
|
-
* @returns {number} `dataBus`
|
|
1378
|
+
* @returns {number} 在 `dataBus` 已使用区域内,与不可回收区域关联的字节数。
|
|
1231
1379
|
*/
|
|
1232
1380
|
get allocatedBytes() {
|
|
1233
1381
|
return this.dataPointer - this.recycleBytes;
|
|
@@ -1237,7 +1385,7 @@ export default class Balloc {
|
|
|
1237
1385
|
* 返回 `dataBus` 顶部尚未使用的字节数。
|
|
1238
1386
|
*
|
|
1239
1387
|
* 此空间对应于 `dataPointer` 与当前为缓冲区预留容量末尾之间的区域。
|
|
1240
|
-
* @returns {number} `dataPointer`
|
|
1388
|
+
* @returns {number} `dataPointer` 之后可用的字节数。
|
|
1241
1389
|
*/
|
|
1242
1390
|
get unallocatedBytes() {
|
|
1243
1391
|
return this.size - this.dataPointer;
|
|
@@ -1246,13 +1394,13 @@ export default class Balloc {
|
|
|
1246
1394
|
/**
|
|
1247
1395
|
* 返回可回收内存与已使用堆区域之间的比率。
|
|
1248
1396
|
*
|
|
1249
|
-
*
|
|
1250
|
-
*
|
|
1397
|
+
* 该指标将保存在 `recycle` 中的空闲区域视为碎片,并以 `dataPointer` 作为分母。
|
|
1398
|
+
* 当堆未消耗任何字节时,结果为零。
|
|
1251
1399
|
*
|
|
1252
|
-
* 该值不代表整个 `dataBus`
|
|
1253
|
-
*
|
|
1400
|
+
* 该值不代表整个 `dataBus` 容量的分数,而是具体代表碰撞指针(bump pointer)
|
|
1401
|
+
* 已遍历区域内可回收字节的比例。
|
|
1254
1402
|
*
|
|
1255
|
-
* @returns {number}
|
|
1403
|
+
* @returns {number} 正常运行条件下的碎片率,范围在 [0, 1] 内。
|
|
1256
1404
|
*/
|
|
1257
1405
|
get fragmentationRatio() {
|
|
1258
1406
|
if (this.dataPointer === 0) return 0;
|