balloc 1.0.1 → 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/src/zh/balloc.cjs CHANGED
@@ -43,6 +43,8 @@ var ADDRESS_OFFSET = 0 >>> 0;
43
43
  var OCCUPIED_OFFSET = 4 >>> 0;
44
44
  var CAPACITY_OFFSET = 8 >>> 0;
45
45
  var COOKIE_OFFSET = 9 >>> 0;
46
+ var FLAGS_OFFSET = 10 >>> 0;
47
+ var VIEW_LOCK = 1 >>> 0;
46
48
  var ROTATE_32BITS = 4294967296;
47
49
  var CLAMP_11BITS = 2047 >>> 0;
48
50
  var isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataView);
@@ -52,17 +54,18 @@ var Balloc = class {
52
54
  *
53
55
  * 该分配器维护两个独立的缓冲区:
54
56
  * - `dataBus`:存储已分配块的数据;
55
- * - `controlBus`:存储标头及其他控制元数据。
57
+ * - `controlBus`:存储标头和其他控制元数据。
56
58
  *
57
- * 每个块都有一个固定大小的标头(`HEADER_SIZE`),并由一个 43 位逻辑句柄
58
- * 在外部进行标识,该句柄由 11 位的代和 32 位的标头地址组成。
59
+ * 每个块都有一个固定大小的标头(`HEADER_SIZE`),并在外部通过一个 43 位逻辑句柄标识,
60
+ * 该句柄由一个 11 位的代数及其 32 位标头地址组成。
59
61
  *
60
- * 每个块的容量会按元素向上取整到下一个二的幂,同时遵循配置的桶宽度。
62
+ * 每个块的容量会按元素数量向上取整到下一个 2 的幂次,
63
+ * 同时遵循配置的桶的宽度。
61
64
  *
62
65
  * @param {number} [size=65535] - `dataBus` 的初始容量(以字节为单位)。
63
66
  * 必须是范围在 [2, 0x80000000] 内的安全整数。
64
- * @param {BucketType} [bucketSize="32u"] - 桶类型和大小标识符
65
- * (例如:'32u' 表示 Uint32,'64f' 表示 Float64,'8i' 表示 Int8)。
67
+ * @param {BucketType} [bucketSize="32u"] - 桶类型和大小的标识符
68
+ * (例如:Uint32 为 '32u',Float64 为 '64f',Int8 为 '8i')。
66
69
  * @param {number} [metadataSize=1024] - `controlBus` 的初始容量(以字节为单位)。
67
70
  * 必须是范围在 [HEADER_SIZE, 0x80000000] 内的安全整数。
68
71
  *
@@ -82,8 +85,8 @@ var Balloc = class {
82
85
  this.bucketArray = globalThis[`${this.bucketType}Array`];
83
86
  this.wordSize = this.bucketSize >>> 3;
84
87
  this.wordShift = this.bucketSize >>> 4;
85
- this.size = Math.max(1 << 32 - Math.clz32(size) >>> 0, this.wordSize);
86
- this.headerSize = 1 << 32 - Math.clz32(metadataSize) >>> 0;
88
+ this.size = Math.max(1 << 32 - Math.clz32(size - 1) >>> 0, this.wordSize);
89
+ this.headerSize = 1 << 32 - Math.clz32(metadataSize - 1) >>> 0;
87
90
  this.dataPointer = 0 >>> 0;
88
91
  this.controlPointer = 0 >>> 0;
89
92
  this.echoPointer = 0 >>> 0;
@@ -109,22 +112,22 @@ var Balloc = class {
109
112
  /**
110
113
  * 分配一个新的内存块。
111
114
  *
112
- * 请求的容量会被转换为能够容纳 `size` 个元素的最小二的幂。
113
- * 该块优先从具有相应容量的可回收区域列表中获取;如果不存在兼容的区域,
114
- * 则在 `dataBus` 的顶部保留该块。
115
+ * 请求的容量会被转换为能够容纳 `size` 个元素的最小 2 的幂次。
116
+ * 优先从具有相应容量的可回收区域列表中获取该块;若不存在兼容的区域,则在 `dataBus` 的顶部预留该块。
115
117
  *
116
- * 接下来,从控制空间获取一个标头。先前释放的标头可以通过内部回收栈进行重用;
117
- * 否则,会在必要时扩展 `controlBus`。
118
+ * 随后,从控制空间中获取一个标头。先前释放的标头可以通过内部回收栈进行复用;
119
+ * 否则,必要时会扩展 `controlBus`。
118
120
  *
119
- * 标头会接收地址、容量、初始占用数(零)以及验证 Cookie。
120
- * 返回的值对分配代数和标头地址进行编码:
121
+ * 标头会接收地址、容量、初始占用数(零)以及一个验证 cookie。
122
+ * 返回的值编码了分配代数与标头地址:
121
123
  *
122
- * [11 位代数][32 位标头地址]
124
+ * [11位代数][32位标头地址]
123
125
  *
124
- * @param {number} [size=16] - 块必须容纳的最小元素数量。
126
+ * @param {number} [size=16] - 块必须能够容纳的最少元素数。
125
127
  * 必须是范围在 [2, 0x80000000] 内的安全整数。
126
128
  * @returns {number} 标识已分配块的句柄。
127
129
  * @throws {RangeError} 如果 `size` 超出允许范围。
130
+ * @throws {Error} 如果在某个块通过 withView() 被借用的同时需要扩展 `dataBus`。
128
131
  */
129
132
  alloc(size = 16) {
130
133
  if (!Number.isSafeInteger(size) || size < 2 || size > 2147483648) throw new RangeError(`Parameter size must be in range [2, 0x80000000] but size is ${size}`);
@@ -150,28 +153,30 @@ var Balloc = class {
150
153
  this.controlAdapter.setUint32(headerAddress + OCCUPIED_OFFSET, 0, IS_LITTLE_ENDIAN);
151
154
  this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, capacity);
152
155
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, this.bakeCookie(headerAddress, ++this.generation));
156
+ this.controlBus[headerAddress + FLAGS_OFFSET] = 0;
153
157
  return (this.generation & CLAMP_11BITS) * ROTATE_32BITS + headerAddress;
154
158
  }
155
159
  /**
156
- * 将块的容量扩展到下一个二的幂。
160
+ * 将块的容量扩展到下一个 2 的幂次。
157
161
  *
158
- * 当块位于 `dataBus` 的当前顶部时,扩展以就地(in-place)方式发生:
162
+ * 当块位于 `dataBus` 的当前顶部时,会进行原地(in-place)扩展:
159
163
  * 仅更新其容量和 `dataPointer`。
160
164
  *
161
- * 如果块之后存在其他区域,则从可回收空间中获取或在 `dataBus` 顶部
162
- * 分配一个容量加倍的新区域。随后,当前占用的元素将被复制到新区域中,
163
- * 并且先前的区域被归还给回收列表。
165
+ * 如果块之后存在其他区域,则会从可回收空间中获取或在 `dataBus` 顶部
166
+ * 分配一个容量加倍的新区域。随后,当前已占用的元素会被复制到新区域中,
167
+ * 并且旧区域会被交还给回收列表。
164
168
  *
165
- * 句柄保持有效,因为它的标头得到了保留,并且仅更新了其中记录的地址和容量。
169
+ * 句柄保持有效,因为它的标头得到了保留,仅更新了其中记录的地址和容量。
166
170
  *
167
171
  * @param {number} handler - 要扩展的块的句柄。
168
172
  * @returns {void}
169
- * @throws {Error} 如果句柄无效或已损坏。
170
- * @throws {AggregateError} 如果容量已达到其支持的最大限制。
173
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
174
+ * 如果扩展需要重新映射 dataBus 且有其他块被借用,也会抛出此错误。
175
+ * @throws {AggregateError} 如果容量已达到支持的最大限制。
171
176
  */
172
177
  realloc(handler) {
173
178
  const generation = handler / ROTATE_32BITS & CLAMP_11BITS;
174
- let headerAddress = handler >>> 0;
179
+ const headerAddress = handler >>> 0;
175
180
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
176
181
  const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
177
182
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
@@ -179,47 +184,49 @@ var Balloc = class {
179
184
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
180
185
  throw new Error("Invalid or corrupted handle");
181
186
  }
187
+ this.assertNotBorrowed(headerAddress);
182
188
  if (capacity >= 31) throw new AggregateError([new Error("Can\xB4t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
183
189
  const newCapacity = capacity + 1;
184
190
  const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
185
191
  const oldBlockSize = (1 << capacity >>> 0) * this.wordSize;
186
192
  if (dataAddress + oldBlockSize === this.dataPointer) {
187
193
  while (dataAddress + newBlockSize > this.size) this.grow();
188
- this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity, IS_LITTLE_ENDIAN);
194
+ this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
189
195
  this.dataPointer = dataAddress + newBlockSize;
190
196
  return;
191
197
  }
192
- this.recyclePush(oldBlockSize, dataAddress);
193
198
  let newAddress = this.recyclePop(newBlockSize);
194
199
  if (newAddress === UNKNOWN) {
195
200
  while (this.dataPointer + newBlockSize > this.size) this.grow();
196
201
  newAddress = this.dataPointer;
197
202
  this.dataPointer += newBlockSize;
198
203
  }
199
- this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
200
204
  this.dataBus.copyWithin(newAddress, dataAddress, dataAddress + occupied * this.wordSize);
201
205
  this.controlAdapter.setUint32(headerAddress + ADDRESS_OFFSET, newAddress, IS_LITTLE_ENDIAN);
206
+ this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
207
+ this.recyclePush(oldBlockSize, dataAddress);
202
208
  }
203
209
  /**
204
- * 释放内存块。
205
- *
206
- * 通过将块的 Cookie 清零来使其标头失效,并记录其地址以便后续重用。
207
- * 数据区域也会被归还给回收系统,但块精确处于 `dataBus` 顶部的情况除外;
208
- * 在这种情况下,只需回退 `dataPointer` 即可消除该区域,而无需在回收列表中创建条目。
209
- *
210
- * @param {number} handler - 要释放的块的句柄。
211
- * @returns {void}
212
- * @throws {Error} 如果句柄无效或已损坏。
213
- */
210
+ * 释放内存块。
211
+ *
212
+ * 通过将块标头的 cookie 清零使其失效,并记录其地址以便后续复用。
213
+ * 数据区域也会被交还给回收系统,除非该块刚好位于 `dataBus` 的顶部;
214
+ * 在这种情况下,`dataPointer` 会直接回滚,从而消除该区域而无需在回收列表中创建条目。
215
+ *
216
+ * @param {number} handler - 要释放的块的句柄。
217
+ * @returns {void}
218
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
219
+ */
214
220
  free(handler) {
215
221
  const generation = handler / ROTATE_32BITS & CLAMP_11BITS;
216
- let headerAddress = handler >>> 0;
222
+ const headerAddress = handler >>> 0;
217
223
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
218
224
  const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
219
225
  const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
220
226
  if (cookie !== this.bakeCookie(headerAddress, generation)) {
221
227
  throw new Error("Invalid or corrupted handle");
222
228
  }
229
+ this.assertNotBorrowed(headerAddress);
223
230
  const blockSize = (1 << capacity >>> 0) * this.wordSize;
224
231
  this.controlAdapter.setUint8(headerAddress + COOKIE_OFFSET, 0);
225
232
  if (this.echoPointer + 4 <= this.controlBus.byteLength) {
@@ -238,7 +245,7 @@ var Balloc = class {
238
245
  * 将空闲区域插入回收列表。
239
246
  *
240
247
  * 该列表按字节容量进行组织。对于每个容量,存储的值是一个 `Uint32Array`,
241
- * 其零位置包含已注册地址的数量,后续位置包含空闲区域的地址:
248
+ * 其第 0 个位置包含已注册地址的数量,随后的位置包含空闲区域的地址:
242
249
  *
243
250
  * [count, address1, address2, ...]
244
251
  *
@@ -268,11 +275,11 @@ var Balloc = class {
268
275
  * [内部使用]
269
276
  * 从回收列表中移除空闲区域。
270
277
  *
271
- * 通过请求的确切容量进行搜索。当存在对应的页面时,
272
- * 从页面中移除最后一个注册的地址并将其返回,从而将回收实现为 LIFO 栈。
278
+ * 按请求的确切容量进行查找。当存在对应的页面时,从该页面中移除并返回最后注册的地址,
279
+ * 从而将回收实现为 LIFO(后进先出)栈。
273
280
  *
274
- * @param {number} capacity - 所需的容量(以字节为单位)。
275
- * @returns {number} 可回收区域的起始地址,如果不存在具有请求容量的区域则返回 `UNKNOWN`。
281
+ * @param {number} capacity - 期望的容量(以字节为单位)。
282
+ * @returns {number} 可回收区域的起始地址,若不存在具有请求容量的区域则返回 `UNKNOWN`。
276
283
  * @private
277
284
  */
278
285
  recyclePop(capacity) {
@@ -286,25 +293,27 @@ var Balloc = class {
286
293
  }
287
294
  // ========== 垃圾回收 (GARBAGE COLLECTION) ==========
288
295
  /**
289
- * 使用 ReBump 算法对 `dataBus` 进行压缩。
296
+ * 使用 ReBump 算法对 `dataBus` 进行碎片整理(压缩)。
290
297
  *
291
298
  * 顺序遍历 `controlBus` 中注册的标头,并将仍然有效的区域复制到新的 `dataBus` 中,
292
- * 从而消除它们之间存在的空闲空间。每个存活块的地址在其各自的标头中得到更新。
299
+ * 从而消除它们之间存在的空闲空间。每个存活块的地址都会在其各自的标头中进行更新。
293
300
  *
294
301
  * `controlBus` 中块的顺序得以保留。每个块的内部数据也完全得以保留,
295
- * 包括其预留的容量和当前的占用数。
302
+ * 包括其预留容量和当前占用数。
296
303
  *
297
- * 压缩结束时:
304
+ * 压缩完成后:
298
305
  * - 所有可回收区域均被消除;
299
306
  * - `dataPointer` 指向最后一个存活块之后的第一个字节;
300
- * - `dataBus` 表现为活动块的连续堆(heap)。
307
+ * - `dataBus` 为活动块呈现出一个连续的堆。
301
308
  *
302
309
  * 控制复杂度为 O(n),其中 `n` 是遍历的标头数量。
303
- * 内存移动的有效成本还取决于属于存活块的字节量。
310
+ * 实际的内存移动成本还取决于属于存活块的字节量。
304
311
  *
305
312
  * @returns {void}
313
+ * @throws {Error} 如果有任何块通过 withView() 被借用。
306
314
  */
307
315
  gc() {
316
+ this.assertNoActiveViews();
308
317
  const newDataBus = new Uint8Array(this.dataBus.buffer.byteLength);
309
318
  this.recycle.clear();
310
319
  this.dataPointer = 0 >>> 0;
@@ -326,16 +335,18 @@ var Balloc = class {
326
335
  // ========== 扩容 (SCALING) ==========
327
336
  /**
328
337
  * [!!! 内部使用,请勿在您的代码中使用 !!!]
329
- * 将 `dataBus` 的容量扩展为其当前容量的两倍。
338
+ * 将 `dataBus` 的容量扩展为当前的两倍。
330
339
  *
331
- * 底层缓冲区被传输到更大的 `ArrayBuffer` 中,同时保留现有数据。
332
- * 与该缓冲区关联的所有视图都会被重新创建以引用新的存储空间。
340
+ * 底层缓冲区被转移到一个更大的 `ArrayBuffer` 中,同时保留现有数据。
341
+ * 与该缓冲区关联的所有视图都会被重新创建,以引用新的存储空间。
333
342
  *
334
343
  * @returns {void}
335
- * @throws {RangeError} 如果 `dataBus` 容量超出支持的最大限制。
344
+ * @throws {Error} 如果有任何块通过 withView() 被借用。
345
+ * @throws {RangeError} 如果 `dataBus` 的容量已超过支持的最大限制。
336
346
  * @private
337
347
  */
338
348
  grow() {
349
+ this.assertNoActiveViews();
339
350
  if (this.size > 2147483648) throw new RangeError("Data bus capacity exceeded");
340
351
  const newSize = this.size * 2 >>> 0;
341
352
  const newBuffer = this.dataBus.buffer.transfer(newSize);
@@ -348,13 +359,13 @@ var Balloc = class {
348
359
  }
349
360
  /**
350
361
  * [!!! 内部使用,请勿在您的代码中使用 !!!]
351
- * 将 `controlBus` 的容量扩展为其当前容量的两倍。
362
+ * 将 `controlBus` 的容量扩展为当前的两倍。
352
363
  *
353
- * 底层缓冲区被传输到更大的 `ArrayBuffer` 中,同时保留现有标头。
354
- * 重新创建 `controlAdapter` 以引用新的存储空间。
364
+ * 底层缓冲区被转移到一个更大的 `ArrayBuffer` 中,同时保留现有标头。
365
+ * `controlAdapter` 被重新创建以引用新的存储空间。
355
366
  *
356
367
  * @returns {void}
357
- * @throws {RangeError} 如果当前 `controlBus` 容量超出支持的最大限制。
368
+ * @throws {RangeError} 如果当前的 `controlBus` 容量已超过支持的最大限制。
358
369
  * @private
359
370
  */
360
371
  headerGrow() {
@@ -366,44 +377,47 @@ var Balloc = class {
366
377
  }
367
378
  // ========== 栈操作 (STACK OPERATIONS) ==========
368
379
  /**
369
- * 将元素追加到块所占用序列的末尾。
380
+ * 在块所占用的序列末尾追加一个元素。
370
381
  *
371
- * 该元素写入最后一个已占用元素之后的第一个空闲位置。
372
- * 只有在其容量中有可用空间时,才会增加块的占用数。
382
+ * 该元素被写入最后一个占用元素之后的第一个空闲位置。
383
+ * 只有在容量范围内有可用空间时,块的占用数才会递增。
373
384
  *
374
385
  * @param {number} handler - 块句柄。
375
386
  * @param {number} value - 要插入的值。
376
- * @returns {boolean} 如果成功插入值则为 `true`;如果块没有可用空间则为 `false`。
377
- * @throws {Error} 如果句柄无效或已损坏。
387
+ * @returns {boolean} 如果值已成功插入则为 `true`;如果块已无可用空间则为 `false`。
388
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
389
+ *
378
390
  */
379
391
  push(handler, value) {
380
392
  const meta = this.extractMetadata(handler, this.metadata);
393
+ this.assertNotBorrowed(meta.headerAddress);
381
394
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
382
395
  this.writer(meta.dataAddress + meta.occupied * this.wordSize, value, IS_LITTLE_ENDIAN);
383
396
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied + 1, IS_LITTLE_ENDIAN);
384
397
  return true;
385
398
  }
386
399
  /**
387
- * 移除并返回块中最后占用的元素。
400
+ * 移除块中最后一个占用的元素并将其返回。
388
401
  *
389
- * 在读取被移除的元素之前,占用数会递减。对应的字节不会被清空,
390
- * 仍留在分配的区域中,但不再属于占用的序列。
402
+ * 在读取被移除的元素之前,占用数会先递减。对应的字节不会被清空,
403
+ * 并且仍保留在分配的区域中,但不再属于已占用的序列。
391
404
  *
392
405
  * @param {number} handler - 块句柄。
393
- * @returns {number|undefined} 被移除的值,如果块为空则返回 `undefined`。
394
- * @throws {Error} 如果句柄无效或已损坏。
406
+ * @returns {number|undefined} 被移除的值,若块为空则返回 `undefined`。
407
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
395
408
  */
396
409
  pop(handler) {
397
410
  const meta = this.extractMetadata(handler, this.metadata);
411
+ this.assertNotBorrowed(meta.headerAddress);
398
412
  if (meta.occupied === 0) return void 0;
399
413
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
400
414
  return this.reader(meta.dataAddress + (meta.occupied - 1) * this.wordSize, IS_LITTLE_ENDIAN);
401
415
  }
402
416
  /**
403
- * 返回最后占用的元素而不将其移除。
417
+ * 返回最后一个占用的元素,但不将其移除。
404
418
  *
405
419
  * @param {number} handler - 块句柄。
406
- * @returns {number|undefined} 最后占用的值,如果块为空则返回 `undefined`。
420
+ * @returns {number|undefined} 最后一个占用的值,若块为空则返回 `undefined`。
407
421
  * @throws {Error} 如果句柄无效或已损坏。
408
422
  */
409
423
  peek(handler) {
@@ -415,18 +429,19 @@ var Balloc = class {
415
429
  /**
416
430
  * 在占用序列的开头插入一个元素。
417
431
  *
418
- * 当前占用的元素向前移动一个位置,并将 `value` 写入第一个位置。
419
- * 该操作要求块的容量中有空闲位置。
432
+ * 当前已占用的元素向前移动一个位置,并将 `value` 写入第一个位置。
433
+ * 该操作要求块的容量中存在空闲位置。
420
434
  *
421
- * 该操作的成本与占用元素的数量成正比,因为需要移动数据。
435
+ * 由于需要移动数据,该操作的成本与占用元素的数量成正比。
422
436
  *
423
437
  * @param {number} handler - 块句柄。
424
438
  * @param {number} value - 要插入的值。
425
- * @returns {boolean} 如果成功插入值则为 `true`;如果块已满则为 `false`。
426
- * @throws {Error} 如果句柄无效或已损坏。
439
+ * @returns {boolean} 如果值已成功插入则为 `true`;如果块已满则为 `false`。
440
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
427
441
  */
428
442
  nudge(handler, value) {
429
443
  const meta = this.extractMetadata(handler, this.metadata);
444
+ this.assertNotBorrowed(meta.headerAddress);
430
445
  if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
431
446
  this.dataBus.copyWithin(meta.dataAddress + this.wordSize, meta.dataAddress, meta.dataAddress + meta.occupied * this.wordSize);
432
447
  this.writer(meta.dataAddress, value, IS_LITTLE_ENDIAN);
@@ -434,18 +449,19 @@ var Balloc = class {
434
449
  return true;
435
450
  }
436
451
  /**
437
- * 移除并返回块中第一个占用的元素。
452
+ * 移除块中第一个占用的元素并将其返回。
438
453
  *
439
- * 剩余元素向占用区域的开头方向移动一个位置。块的物理容量保持不变。
454
+ * 其余元素向占用区域的开头方向移动一个位置。块的物理容量保持不变。
440
455
  *
441
456
  * 该操作的成本与移动的元素数量成正比。
442
457
  *
443
458
  * @param {number} handler - 块句柄。
444
- * @returns {number|undefined} 第一个被移除的值,如果块为空则返回 `undefined`。
445
- * @throws {Error} 如果句柄无效或已损坏。
459
+ * @returns {number|undefined} 第一个被移除的值,若块为空则返回 `undefined`。
460
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
446
461
  */
447
462
  pull(handler) {
448
463
  const meta = this.extractMetadata(handler, this.metadata);
464
+ this.assertNotBorrowed(meta.headerAddress);
449
465
  if (meta.occupied === 0) return void 0;
450
466
  const result = this.reader(meta.dataAddress, IS_LITTLE_ENDIAN);
451
467
  this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
@@ -453,10 +469,10 @@ var Balloc = class {
453
469
  return result;
454
470
  }
455
471
  /**
456
- * 返回第一个占用的元素而不将其移除。
472
+ * 返回第一个占用的元素,但不将其移除。
457
473
  *
458
474
  * @param {number} handler - 块句柄。
459
- * @returns {number|undefined} 第一个占用的值,如果块为空则返回 `undefined`。
475
+ * @returns {number|undefined} 第一个占用的值,若块为空则返回 `undefined`。
460
476
  * @throws {Error} 如果句柄无效或已损坏。
461
477
  */
462
478
  head(handler) {
@@ -468,12 +484,12 @@ var Balloc = class {
468
484
  /**
469
485
  * 检查某个值是否存在于块的占用元素中。
470
486
  *
471
- * 该搜索直接在辅助 `TypedArray` 上执行,并且仅考虑当前占用的范围,
487
+ * 该查找直接在辅助 `TypedArray` 上执行,并且仅考虑当前占用的范围,
472
488
  * 而不是整个预留的物理容量。
473
489
  *
474
490
  * @param {number} handler - 块句柄。
475
- * .@param {number} value - 要寻找的值。
476
- * @returns {boolean} 如果 `value` 存在于占用元素中则为 `true`;否则为 `false`。
491
+ * @param {number} value - 要查找的值。
492
+ * @returns {boolean} 如果 `value` 存在于占用元素中则为 `true`,否则为 `false`。
477
493
  * @throws {Error} 如果句柄无效或已损坏。
478
494
  */
479
495
  has(handler, value) {
@@ -483,13 +499,54 @@ var Balloc = class {
483
499
  return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).includes(value);
484
500
  }
485
501
  /**
486
- * 返回基于零的索引处的占用元素。
502
+ * 定位元素在块的占用序列中首次出现的位置。
503
+ *
504
+ * 查找按索引升序执行,使用底层 TypedArray 的 `indexOf` 操作。
505
+ * 搜索仅考虑占用的元素,忽略块中未使用的容量。
506
+ *
507
+ * 可以通过 `fromIndex` 指定搜索的起始位置,该参数遵循 TypedArray 的索引规则。
508
+ *
509
+ * @param {number} handler - 块句柄。
510
+ * @param {number|bigint} searchElement - 要定位的元素。
511
+ * @param {number} [fromIndex=0] - 开始搜索的索引。
512
+ * @returns {number} 首次出现的索引;若未找到该元素或块为空,则返回 `-1`。
513
+ * @throws {Error} 如果句柄无效或已损坏。
514
+ */
515
+ indexOf(handler, searchElement, fromIndex = 0) {
516
+ const meta = this.extractMetadata(handler, this.metadata);
517
+ if (meta.occupied === 0) return -1;
518
+ const rawAddres = meta.dataAddress >>> this.wordShift;
519
+ return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).indexOf(searchElement, fromIndex);
520
+ }
521
+ /**
522
+ * 定位元素在块的占用序列中最后一次出现的位置。
523
+ *
524
+ * 查找按索引降序执行,使用底层 TypedArray 的 `lastIndexOf` 操作。
525
+ * 搜索仅考虑占用的元素,忽略块中未使用的容量。
526
+ *
527
+ * 可以通过 `fromIndex` 指定搜索的起始位置,该参数遵循 TypedArray 的索引规则。
528
+ * 默认情况下,搜索从最后一个占用元素开始。
529
+ *
530
+ * @param {number} handler - 块句柄。
531
+ * @param {number|bigint} searchElement - 要定位的元素。
532
+ * @param {number} [fromIndex=-1] - 开始搜索的索引。
533
+ * @returns {number} 最后一次出现的索引;若未找到该元素或块为空,则返回 `-1`。
534
+ * @throws {Error} 如果句柄无效或已损坏。
535
+ */
536
+ lastIndexOf(handler, searchElement, fromIndex = -1) {
537
+ const meta = this.extractMetadata(handler, this.metadata);
538
+ if (meta.occupied === 0) return -1;
539
+ const rawAddres = meta.dataAddress >>> this.wordShift;
540
+ return this.auxiliary.subarray(rawAddres, rawAddres + meta.occupied).lastIndexOf(searchElement, fromIndex);
541
+ }
542
+ /**
543
+ * 返回基于零的索引处所占用的元素。
487
544
  *
488
- * 超出范围 `[0, occupied)` 的索引不被视为错误,会返回 `undefined`。
545
+ * 超出 `[0, occupied)` 范围的索引不被视为错误,会返回 `undefined`。
489
546
  *
490
547
  * @param {number} handler - 块句柄。
491
548
  * @param {number} index - 元素的基于零的索引。
492
- * @returns {number|undefined} 存储在索引处的值,如果索引超出占用区域则返回 `undefined`。
549
+ * @returns {number|undefined} 存储在索引处的值;若索引超出占用区域,则返回 `undefined`。
493
550
  * @throws {Error} 如果句柄无效或已损坏。
494
551
  */
495
552
  get(handler, index) {
@@ -500,34 +557,36 @@ var Balloc = class {
500
557
  /**
501
558
  * 替换存储在已占用索引处的值。
502
559
  *
503
- * 该操作不会改变块的占用数。超出范围 `[0, occupied)` 的索引会被忽略并返回 `false`。
560
+ * 该操作不会改变块的占用数。超出 `[0, occupied)` 范围的索引将被忽略并返回 `false`。
504
561
  *
505
562
  * @param {number} handler - 块句柄。
506
563
  * @param {number} index - 元素的基于零的索引。
507
564
  * @param {number} value - 新值。
508
- * @returns {boolean} 如果成功写入值则为 `true`;如果索引超出占用区域则为 `false`。
509
- * @throws {Error} 如果句柄无效或已损坏。
565
+ * @returns {boolean} 如果值已成功写入则为 `true`;如果索引超出占用区域则为 `false`。
566
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
510
567
  */
511
568
  set(handler, index, value) {
512
569
  const meta = this.extractMetadata(handler, this.metadata);
570
+ this.assertNotBorrowed(meta.headerAddress);
513
571
  if (index < 0 || index >= meta.occupied) return false;
514
572
  this.writer(meta.dataAddress + index * this.wordSize, value, IS_LITTLE_ENDIAN);
515
573
  return true;
516
574
  }
517
- // ============= 杂项 (MISCELLANEOUS) ===============
575
+ // ============= 杂项 (MISCELLANEOUS) ==============
518
576
  /**
519
- * 清零块的占用数,并用相同的值填充其整个物理容量。
577
+ * 将块的占用数清零,并用相同的值填满其整个物理容量。
520
578
  *
521
- * 填充涵盖所有可用的槽位(slots),而不仅仅是当前占用的元素。
522
- * 操作完成后,无论填充时使用什么值,该块都被视为是空的。
579
+ * 填充范围涵盖所有可用的槽位(slots),而不仅仅是当前占用的元素。
580
+ * 操作完成后,无论填充使用的是什么值,该块都会被视为是空的。
523
581
  *
524
582
  * @param {number} handler - 块句柄。
525
583
  * @param {number} [value=0] - 用于填充所有槽位的值。
526
584
  * @returns {void}
527
- * @throws {Error} 如果句柄无效或已损坏。
585
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
528
586
  */
529
587
  clear(handler, value = 0) {
530
588
  const meta = this.extractMetadata(handler, this.metadata);
589
+ this.assertNotBorrowed(meta.headerAddress);
531
590
  const slots = meta.byteCapacity / this.wordSize;
532
591
  const rawAddress = meta.dataAddress >>> this.wordShift;
533
592
  this.auxiliary.fill(value, rawAddress, rawAddress + slots);
@@ -536,23 +595,22 @@ var Balloc = class {
536
595
  /**
537
596
  * 用某个值填充块的一段槽位范围。
538
597
  *
539
- * 该范围使用基于零的索引,并遵循 `[start, end)` 惯例,
540
- * 因此 `start` 是包含的(inclusive),而 `end` 是排他的(exclusive)。
541
- * 当未指定 `end` 时,填充会延伸到块物理容量的末尾。
598
+ * 该范围使用基于零的索引,并遵循 `[start, end)` 惯例,因此 `start` 是包含的(inclusive),
599
+ * 而 `end` 是排除的(exclusive)。当未提供 `end` 时,填充将一直延伸到块物理容量的末尾。
542
600
  *
543
- * 块的占用数变为 `end`,即使填充的范围超出了先前的占用数也是如此。
601
+ * 块的占用数将变为 `end`,即使填充的范围超出了先前的占用数也是如此。
544
602
  *
545
603
  * @param {number} handler - 块句柄。
546
604
  * @param {number} value - 用于填充的值。
547
- * @param {number} [start=0] - 起始索引,包含。
548
- * @param {number|null} [end=null] - 结束索引,排他。当省略或为 `null` 时,
549
- * 使用块的总容量。
605
+ * @param {number} [start=0] - 起始索引(包含)。
606
+ * @param {number|null} [end=null] - 结束索引(排除)。省略或为 `null` 时,使用块的总容量。
550
607
  * @returns {void}
551
- * @throws {Error} 如果句柄无效或已损坏。
608
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
552
609
  * @throws {RangeError} 如果 `start` 或 `end` 超出容量边界,或者 `start > end`。
553
610
  */
554
611
  fill(handler, value, start = 0, end = null) {
555
612
  const meta = this.extractMetadata(handler, this.metadata);
613
+ this.assertNotBorrowed(meta.headerAddress);
556
614
  const slots = meta.byteCapacity / this.wordSize;
557
615
  const lastIndex = end ?? slots;
558
616
  if (start > lastIndex) {
@@ -569,42 +627,43 @@ var Balloc = class {
569
627
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, lastIndex, IS_LITTLE_ENDIAN);
570
628
  }
571
629
  /**
572
- * 用一个值填充块的所有物理槽位,并将整个容量标记为已占用。
630
+ * 用某个值填满块的所有物理槽位,并将整个容量标记为已占用。
573
631
  *
574
- * 与 `clear()` 不同,该操作会产生一个完全被占用的块。
632
+ * 与 `clear()` 不同,该操作产生的是一个完全占用的块。
575
633
  *
576
634
  * @param {number} handler - 块句柄。
577
635
  * @param {number} value - 用于填充的值。
578
636
  * @returns {void}
579
- * @throws {Error} 如果句柄无效或已损坏。
637
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
580
638
  */
581
639
  fillAll(handler, value) {
582
640
  const meta = this.extractMetadata(handler, this.metadata);
641
+ this.assertNotBorrowed(meta.headerAddress);
583
642
  const rawAddress = meta.dataAddress >>> this.wordShift;
584
643
  const capacityElements = meta.byteCapacity / this.wordSize;
585
644
  this.auxiliary.fill(value, rawAddress, rawAddress + capacityElements);
586
645
  this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, capacityElements, IS_LITTLE_ENDIAN);
587
646
  }
588
647
  /**
589
- * 将 `Array` 或 `TypedArray` 的元素复制到块的开头。
648
+ * 将 `Array` 或 `TypedArray` 中的元素复制到块的开头。
590
649
  *
591
- * 复制的数量受块容量的限制。占用数更新为先前占用数与实际复制数量中的较大值;
592
- * 因此,当接收到较小的序列时,调用 `put()` 不会减少现有的占用数。
650
+ * 复制的数量受块容量的限制。占用数会被更新为先前占用数与实际复制数量中的较大者;
651
+ * 因此,当接收到较短的序列时,调用 `put()` 不会减少现有的占用数。
593
652
  *
594
653
  * 对于 `Array`,仅复制能装下的前几个元素。
595
- * 对于 `TypedArray`,使用源的子数组(subarray),无需中间复制。
654
+ * 对于 `TypedArray`,会直接使用源的子数组(subarray),无需进行中间复制。
596
655
  *
597
656
  * @param {number} handler - 块句柄。
598
657
  * @param {Array|TypedArray} data - 要复制的值序列。
599
658
  * @returns {void}
600
- * @throws {Error} 如果句柄无效或已损坏。
659
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
601
660
  * @throws {TypeError} 如果 `data` 既不是 `Array` 也不是 `TypedArray`。
602
661
  */
603
662
  put(handler, data) {
604
663
  const meta = this.extractMetadata(handler, this.metadata);
664
+ this.assertNotBorrowed(meta.headerAddress);
605
665
  const rawAddress = meta.dataAddress >>> this.wordShift;
606
666
  const length = Math.min(1 << meta.capacityExponent >>> 0, data.length);
607
- meta.occupied = Math.max(length, meta.occupied);
608
667
  if (Array.isArray(data)) {
609
668
  this.auxiliary.set(data.slice(0, length), rawAddress);
610
669
  } else if (isTypedArray(data)) {
@@ -613,18 +672,18 @@ var Balloc = class {
613
672
  const actual = data?.constructor?.name ?? (data === null ? "null" : typeof data);
614
673
  throw new TypeError(`data must be Array or TypedArray, but ${actual} was given.`);
615
674
  }
616
- this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied, IS_LITTLE_ENDIAN);
675
+ this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, Math.max(length, meta.occupied), IS_LITTLE_ENDIAN);
617
676
  }
618
677
  /**
619
678
  * 返回块当前占用元素的副本。
620
679
  *
621
- * 结果是由 `bucketSize` 配置的同类型 `TypedArray`。未占用的物理容量不包含在结果中。
680
+ * 结果是一个由 `bucketSize` 配置的同类型 `TypedArray`。未占用的物理容量不包含在结果中。
622
681
  *
623
682
  * 该操作不直接暴露内部存储:该方法使用 `TypedArray.prototype.slice()`,
624
- * 产生一个具有独立存储的新视图。
683
+ * 生成一个具有独立存储空间的新视图。
625
684
  *
626
685
  * @param {number} handler - 块句柄。
627
- * @returns {TypedArray} 块占用的元素副本。当块为空时,返回配置类型的空 `TypedArray`。
686
+ * @returns {TypedArray} 块占用元素的副本。当块为空时,返回配置类型的空 `TypedArray`。
628
687
  * @throws {Error} 如果句柄无效或已损坏。
629
688
  */
630
689
  pick(handler) {
@@ -635,14 +694,43 @@ var Balloc = class {
635
694
  }
636
695
  return this.auxiliary.slice(rawAddress, rawAddress + meta.occupied);
637
696
  }
638
- // ======== 按槽位计算的容量 (CAPACITY IN SLOTS) ==========
639
697
  /**
640
- * 返回块以字节为单位的物理容量。
698
+ * 在块占用区域的直接视图上执行回调函数。
699
+ *
700
+ * 该视图共享 Balloc 的内部存储且不进行复制。
701
+ * 在回调执行期间,该块受到保护,免受不兼容操作的影响。
641
702
  *
642
- * 容量对应于为块中所有槽位预留的空间,无论当前占用了多少元素。
703
+ * 回调必须是同步的。接收到的视图不得在回调结束后被存储、返回或使用。
704
+ * 此限制同样适用于从原始视图派生的子视图(subviews)。
643
705
  *
644
706
  * @param {number} handler - 块句柄。
645
- * @returns {number} 块的物理容量(以字节为单位)。
707
+ * @param {Function} callback - 接收共享 TypedArray 的函数。
708
+ * @returns {*} 回调函数产生 返回值。
709
+ * @throws {TypeError} 如果回调不是函数。
710
+ * @throws {Error} 如果句柄无效或已被借用。
711
+ */
712
+ withView(handler, callback) {
713
+ if (typeof callback !== "function") throw new TypeError("callback must be a function");
714
+ const meta = this.extractMetadata(handler, this.metadata);
715
+ const flagsAddress = meta.headerAddress + FLAGS_OFFSET;
716
+ const flags = this.controlBus[flagsAddress];
717
+ if (flags & VIEW_LOCK) throw new Error("Block is already borrowed");
718
+ this.controlBus[flagsAddress] = flags | VIEW_LOCK;
719
+ try {
720
+ const rawAddress = meta.dataAddress >>> this.wordShift;
721
+ return callback(this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied));
722
+ } finally {
723
+ this.controlBus[flagsAddress] &= ~VIEW_LOCK;
724
+ }
725
+ }
726
+ // ======== 槽位容量 (CAPACITY IN SLOTS) ==========
727
+ /**
728
+ * 返回块的物理容量(以字节为单位)。
729
+ *
730
+ * 容量对应于为块中所有槽位预留的空间,无论当前占用了多少个元素。
731
+ *
732
+ * @param {number} handler - 块句柄。
733
+ * @returns {number} 块的物理容量,以字节为单位。
646
734
  * @throws {Error} 如果句柄无效或已损坏。
647
735
  */
648
736
  byteSize(handler) {
@@ -653,7 +741,7 @@ var Balloc = class {
653
741
  * 返回块中当前占用的元素数量。
654
742
  *
655
743
  * @param {number} handler - 块句柄。
656
- * @returns {number} 已占用元素的数量。
744
+ * @returns {number} 已占用元素数量。
657
745
  * @throws {Error} 如果句柄无效或已损坏。
658
746
  */
659
747
  occupied(handler) {
@@ -661,9 +749,9 @@ var Balloc = class {
661
749
  return meta.occupied;
662
750
  }
663
751
  /**
664
- * 返回以元素数量计算的块容量。
752
+ * 返回以元素数量表示的块容量。
665
753
  *
666
- * 该值对应物理预留槽位的总数,而不是当前占用的数量。
754
+ * 该值对应于物理预留的总槽位数,而不是当前占用的数量。
667
755
  *
668
756
  * @param {number} handler - 块句柄。
669
757
  * @returns {number} 块中可用的槽位总数。
@@ -673,30 +761,44 @@ var Balloc = class {
673
761
  const meta = this.extractMetadata(handler, this.metadata);
674
762
  return 1 << meta.capacityExponent >>> 0;
675
763
  }
676
- // ========== 块间操作 (Inter-block Operations) =============
677
764
  /**
678
- * 将占用的元素从一个块复制到另一个块。
679
- *
680
- * 复制从 `source` 的第一个元素和 `destiny` 的第一个槽位开始。
681
- * 实际复制的数量受源中占用元素数量、目标物理容量以及 `slots` 中指定的限制所限制。
765
+ * 检查内存块是否已达到其分配容量。
682
766
  *
683
- * 必要时会增加目标的占用数以涵盖复制的元素。目标的容量保持不变。
767
+ * 当占用元素数量等于其分配的总容量、且没有剩余位置可存储新元素时,块被视为已满。
684
768
  *
685
- * 该操作在 `dataBus` 上使用 `Uint8Array.prototype.copyWithin()`,
686
- * 因此返回值为表示数据总线的 `Uint8Array` 本身。
687
- *
688
- * @param {number} source - 源块句柄。
689
- * @param {number} destiny - 目标块句柄。
690
- * @param {number} [slots=UNKNOWN] - 要复制的最大元素数量。
691
- * `UNKNOWN` 表示不施加额外限制所使用的哨兵值。
692
- * @returns {Uint8Array} 操作后的 `dataBus`。
693
- * @throws {Error} 如果 `source` 或 `destiny` 无效或已损坏。
769
+ * @param {number} handler - 块句柄。
770
+ * @returns {boolean} 如果块已满则为 `true`,否则为 `false`。
771
+ * @throws {Error} 如果句柄无效或已损坏。
694
772
  */
695
- copy(source, destiny, slots = UNKNOWN) {
773
+ isFull(handler) {
774
+ const meta = this.extractMetadata(handler, this.metadata);
775
+ return (1 << meta.capacityExponent >>> 0) - meta.occupied === 0;
776
+ }
777
+ // ========== 块间操作 (Operations between blocks)=============
778
+ /**
779
+ * 将占用元素从一个块复制到另一个块。
780
+ *
781
+ * 复制从 `source` 的第一个元素和 `destination` 的第一个槽位开始。
782
+ * 实际复制的数量受源中占用元素数量、目标物理容量以及 `slots` 中指定限制的约束。
783
+ *
784
+ * 必要时会提高目标的占用数,以涵盖复制的元素。目标的容量保持不变。
785
+ *
786
+ * 该操作在 `dataBus` 上使用 `Uint8Array.prototype.copyWithin()`,
787
+ * 因此返回值为代表数据总线的 `Uint8Array` 本身。
788
+ *
789
+ * @param {number} source - 源块句柄。
790
+ * @param {number} destination - 目标块句柄。
791
+ * @param {number} [slots=UNKNOWN] - 要复制的最大元素数。
792
+ * `UNKNOWN` 代表哨兵值,用于不施加额外的限制。
793
+ * @returns {Uint8Array} 操作后的 `dataBus`。
794
+ * @throws {Error} 如果句柄无效、已损坏,或者目标块正通过 withView() 被借用。
795
+ */
796
+ copy(source, destination, slots = UNKNOWN) {
696
797
  const metaA = {};
697
798
  const metaB = {};
698
799
  this.extractMetadata(source, metaA);
699
- this.extractMetadata(destiny, metaB);
800
+ this.extractMetadata(destination, metaB);
801
+ this.assertNotBorrowed(metaB.headerAddress);
700
802
  const maxSlotsDest = Math.floor(metaB.byteCapacity / this.wordSize);
701
803
  const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
702
804
  const newOccupied = Math.max(metaB.occupied, slotsToCopy);
@@ -706,23 +808,24 @@ var Balloc = class {
706
808
  /**
707
809
  * 在块内部复制一段元素区域。
708
810
  *
709
- * 索引基于零并遵循 `Array.prototype.copyWithin()` 的语义:
710
- * `target` 标识目标位置,`start` 标识源起始位置,`length` 标识要复制的最大元素数量。
811
+ * 索引基于零,并遵循 `Array.prototype.copyWithin()` 的语义:
812
+ * `target` 标识目标位置,`start` 标识源起始位置,`length` 标识要复制的最大元素数。
711
813
  *
712
- * 负索引从块容量的末尾开始解释。完成此规范化后,索引将被限制在物理容量之内。
814
+ * 负索引会从块容量的末尾开始解析。经过此规范化后,索引会被限制在物理容量内。
713
815
  * 复制的数量也受实际占用元素以及从 `target` 开始的可用空间的限制。
714
816
  *
715
- * 该操作支持重叠区域,并且当目标范围超过当前占用数时可以增加块的占用数。
817
+ * 该操作支持重叠区域,并且当目标范围超过先前的占用数时,可以增加块的占用数。
716
818
  *
717
819
  * @param {number} handler - 块句柄。
718
820
  * @param {number} target - 起始目标索引。
719
821
  * @param {number} start - 起始源索引。
720
- * @param {number} length - 要复制的最大元素数量。
822
+ * @param {number} length - 要复制的最大元素数。
721
823
  * @returns {Uint8Array} 操作后的 `dataBus`。
722
- * @throws {Error} 如果句柄无效或已损坏。
824
+ * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
723
825
  */
724
826
  copyWithin(handler, target, start, length) {
725
827
  const meta = this.extractMetadata(handler, this.metadata);
828
+ this.assertNotBorrowed(meta.headerAddress);
726
829
  const slots = Math.floor(meta.byteCapacity / this.wordSize);
727
830
  target = Math.trunc(target);
728
831
  start = Math.trunc(start);
@@ -746,41 +849,37 @@ var Balloc = class {
746
849
  *
747
850
  * 复制的数量受源中占用元素数量以及目标容量中剩余可用空间的限制。
748
851
  *
749
- * 该操作支持 `target` 和 `source` 引用同一个块。
750
- * 在这种情况下,复制在块自己的区域上执行,并保留 `copyWithin()` 的重叠语义。
852
+ * 该操作支持 `target` 和 `source` 引用同一个块。在这种情况下,
853
+ * 复制操作将在块自身的区域上执行,并保留 `copyWithin()` 的重叠语义。
751
854
  *
752
855
  * @param {number} target - 将接收元素的块句柄。
753
- * @param {number} source - 其元素将被追加的块句柄。
856
+ * @param {number} source - 要将其元素追加进来的块句柄。
754
857
  * @returns {number} 实际追加的元素数量。
755
- * @throws {Error} 如果 `target` 或 `source` 无效或已损坏。
858
+ * @throws {Error} 如果句柄无效、已损坏,或者目标块正通过 withView() 被借用。
756
859
  */
757
860
  append(target, source) {
758
861
  const metaTarget = {};
759
862
  const metaSource = {};
760
863
  this.extractMetadata(target, metaTarget);
761
864
  this.extractMetadata(source, metaSource);
865
+ this.assertNotBorrowed(metaTarget.headerAddress);
762
866
  const available = metaTarget.byteCapacity / this.wordSize - metaTarget.occupied;
763
867
  const count = Math.min(available, metaSource.occupied);
764
868
  if (count === 0) return 0;
765
869
  const targetAddress = metaTarget.dataAddress + metaTarget.occupied * this.wordSize;
766
870
  const sourceAddress = metaSource.dataAddress;
767
- if (target === source) {
768
- this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
769
- } else {
770
- this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
771
- }
871
+ this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + count * this.wordSize);
772
872
  this.controlAdapter.setUint32(metaTarget.headerAddress + OCCUPIED_OFFSET, metaTarget.occupied + count, IS_LITTLE_ENDIAN);
773
873
  return count;
774
874
  }
775
875
  /**
776
876
  * 创建块的独立副本。
777
877
  *
778
- * 新块具有与原始块相同的物理容量和占用数。
779
- * 预留区域中的所有字节(包括当前未占用的位置)都会被复制。
878
+ * 新块具有与原块相同的物理容量和占用数。预留区域中的所有字节(包括当前未占用的位置)均会被复制。
780
879
  *
781
- * 新块会获得一个新句柄,并受常规分配和回收规则的约束。
880
+ * 新块会获得一个新句柄,并继续受正常的分配和回收规则约束。
782
881
  *
783
- * @param {number} handler - 要克隆的块的句柄。
882
+ * @param {number} handler - 要克隆的块句柄。
784
883
  * @returns {number} 新块的句柄。
785
884
  * @throws {Error} 如果句柄无效或已损坏。
786
885
  */
@@ -794,23 +893,25 @@ var Balloc = class {
794
893
  return cloned;
795
894
  }
796
895
  /**
797
- * 交换与两个块关联的数据区域。
896
+ * 交换两个块关联的数据区域。
798
897
  *
799
898
  * 该操作在标头之间交换区域地址、容量和占用数。句柄保持不变:
800
- * 操作后,每个句柄继续标识其自己的标头,但该标头现在描述的是先前属于另一个块的区域。
899
+ * 操作完成后,每个句柄继续标识其自己的标头,但该标头现在描述的是先前属于另一个块的区域。
801
900
  *
802
- * `dataBus` 中不复制任何数据;仅修改两个标头的元数据。
901
+ * `dataBus` 中不会复制任何数据;仅修改两个标头的元数据。
803
902
  *
804
903
  * @param {number} source - 第一个块的句柄。
805
904
  * @param {number} target - 第二个块的句柄。
806
905
  * @returns {void}
807
- * @throws {Error} 如果 `source` 或 `target` 无效或已损坏。
906
+ * @throws {Error} 如果句柄无效、已损坏,或者任何一个块正通过 withView() 被借用。
808
907
  */
809
908
  swap(source, target) {
810
909
  const metaA = {};
811
910
  const metaB = {};
812
911
  this.extractMetadata(source, metaA);
813
912
  this.extractMetadata(target, metaB);
913
+ this.assertNotBorrowed(metaA.headerAddress);
914
+ this.assertNotBorrowed(metaB.headerAddress);
814
915
  this.controlAdapter.setUint32(metaA.headerAddress + ADDRESS_OFFSET, metaB.dataAddress, IS_LITTLE_ENDIAN);
815
916
  this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
816
917
  this.controlAdapter.setUint8(metaA.headerAddress + CAPACITY_OFFSET, metaB.capacityExponent);
@@ -822,10 +923,10 @@ var Balloc = class {
822
923
  /**
823
924
  * 序列化内存块的内容和元数据。
824
925
  *
825
- * 该方法通过 `pick()` 提取块的占用数据,将其与容量和单词类型信息一起
826
- * 组合到数据信封(envelope)中,并使用 CBOR 格式对其进行编码。
926
+ * 该方法通过 `pick()` 提取块的占用数据,连同容量和字类型(word type)信息
927
+ * 一起打包成一个数据信封(envelope),并使用 CBOR 格式对其进行编码。
827
928
  *
828
- * @param {number} handler - 要序列化的块的句柄。
929
+ * @param {number} handler - 要序列化的块句柄。
829
930
  * @returns {Uint8Array} 包含序列化块数据的缓冲区。
830
931
  * @throws {Error} 如果句柄无效或已损坏。
831
932
  */
@@ -852,12 +953,12 @@ var Balloc = class {
852
953
  /**
853
954
  * 从序列化数据恢复内存块。
854
955
  *
855
- * 解码 CBOR 信封,验证序列化数据的单词类型是否与当前分配器实例兼容,
856
- * 并分配一个容纳内容所需容量的新块。然后,在的新块中恢复原始数据和占用数。
956
+ * 解码 CBOR 信封,验证序列化数据的字类型是否与当前的分配器实例兼容,
957
+ * 并分配一个能够容纳该内容所需容量的新块。随后,在新的块中恢复原始数据和占用数。
857
958
  *
858
959
  * @param {Uint8Array|ArrayBuffer} serialized - 包含序列化数据的缓冲区。
859
960
  * @returns {number} 重新创建的新块的句柄。
860
- * @throws {Error} 如果解码数据的单词类型与分配器中配置的不同。
961
+ * @throws {Error} 如果解码数据的字类型与分配器配置的不一致。
861
962
  */
862
963
  load(serialized) {
863
964
  if (!(serialized instanceof ArrayBuffer) && !(serialized instanceof Uint8Array)) {
@@ -897,20 +998,50 @@ var Balloc = class {
897
998
  return handler;
898
999
  }
899
1000
  // ============= 工具 (UTILITIES) ==============
1001
+ /**
1002
+ * 防止对已借用的块进行修改操作。
1003
+ *
1004
+ * 检查与所提供地址关联的标头中的 VIEW_LOCK 标志。
1005
+ *
1006
+ * @param {number} headerAddress - controlBus 中的标头地址。
1007
+ * @returns {void}
1008
+ * @throws {Error} 如果块正通过 withView() 被借用。
1009
+ * @private
1010
+ */
1011
+ assertNotBorrowed(headerAddress) {
1012
+ if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
1013
+ throw new Error("Block is borrowed");
1014
+ }
1015
+ }
1016
+ /**
1017
+ * 防止在存在任何借用片段时替换或重新映射 dataBus 的操作。
1018
+ *
1019
+ * @returns {void}
1020
+ * @throws {Error} 如果有任何块通过 withView() 被借用。
1021
+ * @private
1022
+ */
1023
+ assertNoActiveViews() {
1024
+ for (let i = 0; i < this.controlPointer; i += HEADER_SIZE) {
1025
+ if (this.controlBus[i + COOKIE_OFFSET] === 0) continue;
1026
+ if (this.controlBus[i + FLAGS_OFFSET] & VIEW_LOCK) {
1027
+ throw new Error("Cannot remap memory while a block is borrowed");
1028
+ }
1029
+ }
1030
+ }
900
1031
  /**
901
1032
  * [内部使用]
902
1033
  * 解码与句柄关联的元数据。
903
1034
  *
904
- * 句柄被拆分为代和标头地址。然后重新计算标头中存储的 Cookie 以验证组合;
905
- * 只有在此验证之后才会读取元数据。
1035
+ * 句柄被拆分为代数和标头地址。然后重新计算标头中存储的 cookie 以验证该组合;
1036
+ * 只有在通过此验证后才会读取元数据。
906
1037
  *
907
- * 解码后的字段被写入 `out` 对象中,该对象由函数自身返回。
908
- * 可以在多次调用之间重用该对象以避免新的分配。
1038
+ * 解码后的字段写入 `out` 对象中,该对象由函数自身返回。
1039
+ * 可以在多次调用之间复用该对象以避免新的分配。
909
1040
  *
910
1041
  * @param {number} handler - 要解码和验证的句柄。
911
- * @param {Metadata} out - 将接收解码元数据的对象。
1042
+ * @param {Metadata} out - 将接收解码后元数据的对象。
912
1043
  * @returns {Metadata} 在 `out` 中提供的相同引用。
913
- * @throws {Error} 如果标头 Cookie 与句柄不匹配,或者句柄未标识有效的标头。
1044
+ * @throws {Error} 如果标头 cookie 与句柄不匹配,或者句柄未能标识有效的标头。
914
1045
  * @private
915
1046
  */
916
1047
  extractMetadata(handler, out) {
@@ -928,19 +1059,19 @@ var Balloc = class {
928
1059
  }
929
1060
  /**
930
1061
  * [内部使用]
931
- * 计算标头的验证 Cookie。
1062
+ * 计算标头的验证 cookie。
932
1063
  *
933
- * Cookie 通过快速哈希(fast hash)由标头地址和 11 位代数派生而来。
934
- * 结果被缩减为 8 位。
1064
+ * cookie 是通过快速哈希(fast hash)从标头地址和 11 位代数派生而来的。
1065
+ * 结果会被缩减为 8 位。
935
1066
  *
936
- * 零值保留为无效标头指示符;因此,当计算产生零时,该方法返回 `1`。
1067
+ * 值零被保留作为无效标头的指示符;因此,当计算结果为零时,该方法返回 `1`。
937
1068
  *
938
- * Cookie 不构成加密机制。其目的是在分配器的内存管理模型中
939
- * 检测无效、过时(stale)或损坏的句柄。
1069
+ * 该 cookie 不构成加密机制。其目的是在分配器的内存管理模型中
1070
+ * 检测无效、陈旧(stale)或损坏的句柄。
940
1071
  *
941
1072
  * @param {number} addr - 标头地址。
942
- * @param {number} gen - 与句柄关联的代。仅考虑最低的 11 位。
943
- * @returns {number} 范围在 [1, 255] 内的 8 位 Cookie。
1073
+ * @param {number} gen - 与句柄关联的代数。仅考虑最低的 11 位。
1074
+ * @returns {number} 范围在 [1, 255] 内的 8 位 cookie。
944
1075
  * @private
945
1076
  */
946
1077
  bakeCookie(addr, gen) {
@@ -949,10 +1080,10 @@ var Balloc = class {
949
1080
  /**
950
1081
  * 返回当前存储在回收列表中的区域数量。
951
1082
  *
952
- * 每个区域对应一个无法直接归还给 `dataBus`顶部的已释放块,
953
- * 因此保留下来以便未来容量相同的分配进行重用。
1083
+ * 每个区域对应一个无法直接交还给 `dataBus` 顶部的释放块,
1084
+ * 因此它仍然可供未来相同容量的分配进行复用。
954
1085
  *
955
- * @returns {number} `recycle` 中维护的空闲区域数量。
1086
+ * @returns {number} 保存在 `recycle` 中的空闲区域数量。
956
1087
  */
957
1088
  recycleCount() {
958
1089
  let count = 0;
@@ -962,9 +1093,9 @@ var Balloc = class {
962
1093
  return count;
963
1094
  }
964
1095
  /**
965
- * 返回当前存储在回收列表中的区域所属的字节总数。
1096
+ * 返回当前存储在回收列表中的区域所属的总字节数。
966
1097
  *
967
- * 该值通过将每个空闲区域的容量乘以针对该容量注册的区域数量相加来计算。
1098
+ * 该值是通过将每个空闲区域的容量乘以该容量注册的区域数量相加来计算的。
968
1099
  *
969
1100
  * @returns {number} 可回收区域占用的字节数。
970
1101
  */
@@ -978,9 +1109,9 @@ var Balloc = class {
978
1109
  /**
979
1110
  * 返回不属于当前可回收区域的已分配字节数。
980
1111
  *
981
- * 该值通过从 `dataPointer` 的总推进量中减去 `recycle` 中维护的空闲区域所属的字节来获得。
1112
+ * 该值是通过从 `dataPointer` 的总推进量中减去保存在 `recycle` 中的空闲区域所属的字节数来获取的。
982
1113
  *
983
- * @returns {number} `dataBus` 已使用区域内与不可回收区域关联的字节数。
1114
+ * @returns {number} 在 `dataBus` 已使用区域内,与不可回收区域关联的字节数。
984
1115
  */
985
1116
  get allocatedBytes() {
986
1117
  return this.dataPointer - this.recycleBytes;
@@ -989,7 +1120,7 @@ var Balloc = class {
989
1120
  * 返回 `dataBus` 顶部尚未使用的字节数。
990
1121
  *
991
1122
  * 此空间对应于 `dataPointer` 与当前为缓冲区预留容量末尾之间的区域。
992
- * @returns {number} `dataPointer` 之后的可用字节数。
1123
+ * @returns {number} `dataPointer` 之后可用的字节数。
993
1124
  */
994
1125
  get unallocatedBytes() {
995
1126
  return this.size - this.dataPointer;
@@ -997,13 +1128,13 @@ var Balloc = class {
997
1128
  /**
998
1129
  * 返回可回收内存与已使用堆区域之间的比率。
999
1130
  *
1000
- * 该指标将 `recycle` 中维护的空闲区域视为碎片化,并使用 `dataPointer` 作为分母。
1001
- * 当堆未曾使用任何字节时,结果为零。
1131
+ * 该指标将保存在 `recycle` 中的空闲区域视为碎片,并以 `dataPointer` 作为分母。
1132
+ * 当堆未消耗任何字节时,结果为零。
1002
1133
  *
1003
- * 该值不代表整个 `dataBus` 容量的分数,而是具体表示 bump 指针已遍历区域内
1004
- * 可回收字节的比例。
1134
+ * 该值不代表整个 `dataBus` 容量的分数,而是具体代表碰撞指针(bump pointer)
1135
+ * 已遍历区域内可回收字节的比例。
1005
1136
  *
1006
- * @returns {number} 在正常运行条件下的 [0, 1] 范围内的碎片化比率。
1137
+ * @returns {number} 正常运行条件下的碎片率,范围在 [0, 1] 内。
1007
1138
  */
1008
1139
  get fragmentationRatio() {
1009
1140
  if (this.dataPointer === 0) return 0;