balloc 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/zh/balloc.mjs CHANGED
@@ -248,7 +248,7 @@ export default class Balloc {
248
248
  * @throws {AggregateError} 如果容量已达到支持的最大限制。
249
249
  */
250
250
  realloc(handler) {
251
-
251
+
252
252
  const generation = (handler / ROTATE_32BITS) & CLAMP_11BITS;
253
253
  const headerAddress = handler >>> 0;
254
254
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
@@ -293,19 +293,46 @@ export default class Balloc {
293
293
  this.recyclePush(oldBlockSize, dataAddress);
294
294
  }
295
295
 
296
- /**
297
- * 释放内存块。
296
+ /**
297
+ * 将内存块缩小到能够容纳其已占用元素的最小 2 的幂次容量,
298
+ * 同时保留其地址和句柄。
298
299
  *
299
- * 通过将块标头的 cookie 清零使其失效,并记录其地址以便后续复用。
300
- * 数据区域也会被交还给回收系统,除非该块刚好位于 `dataBus` 的顶部;
301
- * 在这种情况下,`dataPointer` 会直接回滚,从而消除该区域而无需在回收列表中创建条目。
300
+ * 如果该块位于 dataBus 的顶部,则向后回退 dataPointer。
301
+ * 否则,将释放的尾部区域拆分为若干个容量为 2 的幂次的区域,
302
+ * 并将每个区域归还到回收列表。
302
303
  *
303
- * @param {number} handler - 要释放的块的句柄。
304
+ * @param {number} handler - 要缩小的内存块的句柄。
304
305
  * @returns {void}
305
- * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
306
+ * @throws {Error} 如果句柄无效、已损坏,或该块正通过 withView() 被借用。
306
307
  */
308
+ shrink(handler) {
309
+ const meta = this.extractMetadata(handler, this.metadata);
310
+ this.assertNotBorrowed(meta.headerAddress);
311
+
312
+ const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
313
+ if (newCapacity >= meta.capacityExponent) return;
314
+
315
+ const oldBlockSize = meta.byteCapacity;
316
+ const newBlockSize = (2 ** newCapacity) * this.wordSize;
317
+ const oldEnd = meta.dataAddress + oldBlockSize;
318
+
319
+ this.controlAdapter.setUint8(meta.headerAddress + CAPACITY_OFFSET, newCapacity);
320
+
321
+ if (oldEnd === this.dataPointer) {
322
+ this.dataPointer = meta.dataAddress + newBlockSize;
323
+ return;
324
+ }
325
+
326
+ let address = meta.dataAddress + newBlockSize;
327
+ for (let exponent = newCapacity; exponent < meta.capacityExponent; exponent++) {
328
+ const segmentSize = (2 ** exponent) * this.wordSize;
329
+ this.recyclePush(segmentSize, address);
330
+ address += segmentSize;
331
+ }
332
+ }
333
+
307
334
  free(handler) {
308
-
335
+
309
336
  const generation = (handler / ROTATE_32BITS) & CLAMP_11BITS;
310
337
  const headerAddress = handler >>> 0;
311
338
  const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
@@ -499,7 +526,6 @@ export default class Balloc {
499
526
  * 该元素被写入最后一个占用元素之后的第一个空闲位置。
500
527
  * 只有在容量范围内有可用空间时,块的占用数才会递增。
501
528
  *
502
- * @param {number} handler - 块句柄。
503
529
  * @param {number} value - 要插入的值。
504
530
  * @returns {boolean} 如果值已成功插入则为 `true`;如果块已无可用空间则为 `false`。
505
531
  * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
@@ -884,27 +910,45 @@ export default class Balloc {
884
910
  * 此限制同样适用于从原始视图派生的子视图(subviews)。
885
911
  *
886
912
  * @param {number} handler - 块句柄。
887
- * @param {Function} callback - 接收共享 TypedArray 的函数。
913
+ * @param {...(number|Function)} args - 一个或多个块句柄,最后一个参数为回调函数。
888
914
  * @returns {*} 回调函数产生 返回值。
889
- * @throws {TypeError} 如果回调不是函数。
890
- * @throws {Error} 如果句柄无效或已被借用。
915
+ * @throws {TypeError} 如果未提供句柄或回调不是函数。
916
+ * @throws {Error} 如果任一句柄无效、重复或已被借用。
891
917
  */
892
- withView(handler, callback) {
918
+ withView(...args) {
919
+ if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
920
+
921
+ const callback = args.pop();
893
922
  if (typeof callback !== "function") throw new TypeError("callback must be a function");
894
923
 
895
- const meta = this.extractMetadata(handler, this.metadata);
896
- const flagsAddress = meta.headerAddress + FLAGS_OFFSET;
897
- const flags = this.controlBus[flagsAddress];
924
+ const handlers = args;
925
+ const metadata = handlers.map(handler => this.extractMetadata(handler, {}));
926
+ const headers = metadata.map(meta => meta.headerAddress);
898
927
 
899
- if (flags & VIEW_LOCK) throw new Error("Block is already borrowed");
928
+ if (new Set(headers).size !== headers.length) throw new Error("Duplicate handler");
900
929
 
901
- this.controlBus[flagsAddress] = flags | VIEW_LOCK;
930
+ const flags = headers.map(header => this.controlBus[header + FLAGS_OFFSET]);
931
+
932
+ if (flags.some(flag => flag & VIEW_LOCK)) throw new Error("Block is already borrowed");
933
+
934
+ let acquiredCount = 0;
902
935
 
903
936
  try {
904
- const rawAddress = meta.dataAddress >>> this.wordShift;
905
- return callback(this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied));
937
+ for (const header of headers) {
938
+ this.controlBus[header + FLAGS_OFFSET] |= VIEW_LOCK;
939
+ acquiredCount++;
940
+ }
941
+
942
+ const views = metadata.map(meta => {
943
+ const rawAddress = meta.dataAddress >>> this.wordShift;
944
+ return this.auxiliary.subarray(rawAddress, rawAddress + meta.occupied);
945
+ });
946
+
947
+ return callback(...views);
906
948
  } finally {
907
- this.controlBus[flagsAddress] &= ~VIEW_LOCK;
949
+ for (let i = 0; i < acquiredCount; i++) {
950
+ this.controlBus[headers[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
951
+ }
908
952
  }
909
953
  }
910
954
 
@@ -967,24 +1011,24 @@ export default class Balloc {
967
1011
 
968
1012
  // ========== 块间操作 (Operations between blocks)=============
969
1013
 
970
- /**
971
- * 将占用元素从一个块复制到另一个块。
972
- *
973
- * 复制从 `source` 的第一个元素和 `destination` 的第一个槽位开始。
974
- * 实际复制的数量受源中占用元素数量、目标物理容量以及 `slots` 中指定限制的约束。
975
- *
976
- * 必要时会提高目标的占用数,以涵盖复制的元素。目标的容量保持不变。
977
- *
978
- * 该操作在 `dataBus` 上使用 `Uint8Array.prototype.copyWithin()`,
979
- * 因此返回值为代表数据总线的 `Uint8Array` 本身。
980
- *
981
- * @param {number} source - 源块句柄。
982
- * @param {number} destination - 目标块句柄。
983
- * @param {number} [slots=UNKNOWN] - 要复制的最大元素数。
984
- * `UNKNOWN` 代表哨兵值,用于不施加额外的限制。
985
- * @returns {Uint8Array} 操作后的 `dataBus`。
986
- * @throws {Error} 如果句柄无效、已损坏,或者目标块正通过 withView() 被借用。
987
- */
1014
+ /**
1015
+ * 将占用元素从一个块复制到另一个块。
1016
+ *
1017
+ * 复制从 `source` 的第一个元素和 `destination` 的第一个槽位开始。
1018
+ * 实际复制的数量受源中占用元素数量、目标物理容量以及 `slots` 中指定限制的约束。
1019
+ *
1020
+ * 必要时会提高目标的占用数,以涵盖复制的元素。目标的容量保持不变。
1021
+ *
1022
+ * 该操作在 `dataBus` 上使用 `Uint8Array.prototype.copyWithin()`,
1023
+ * 因此返回值为代表数据总线的 `Uint8Array` 本身。
1024
+ *
1025
+ * @param {number} source - 源块句柄。
1026
+ * @param {number} destination - 目标块句柄。
1027
+ * @param {number} [slots=UNKNOWN] - 要复制的最大元素数。
1028
+ * `UNKNOWN` 代表哨兵值,用于不施加额外的限制。
1029
+ * @returns {Uint8Array} 操作后的 `dataBus`。
1030
+ * @throws {Error} 如果句柄无效、已损坏,或者目标块正通过 withView() 被借用。
1031
+ */
988
1032
  copy(source, destination, slots = UNKNOWN) {
989
1033
  /** @type {Metadata} */
990
1034
  const metaA = {};
@@ -126,19 +126,19 @@ export default class Balloc {
126
126
  */
127
127
  realloc(handler: number): void;
128
128
  /**
129
- * يحرر كتلة ذاكرة.
130
- *
131
- * يتم إبطال ترويسة الكتلة عن طريق تصفير ملف تعريف الارتباط الخاص بها وتسجيل
132
- * عنوانها لإعادة استخدامها لاحقاً. كما يتم إرجاع منطقة البيانات
133
- * إلى نظام إعادة التدوير، إلا إذا كانت الكتلة في قمة `dataBus` تماماً؛
134
- * في هذه الحالة، يتم التراجع عن `dataPointer` ببساطة، مما يلغي المنطقة دون إنشاء إدخال في قائمة إعادة التدوير.
135
- *
136
- * @param {number} handler - مقبض الكتلة المراد تحريرها.
137
- * @returns {void}
138
- * @throws {Error} إذا كان المقبض غير صالح، أو تالفاً، أو إذا كانت الكتلة
139
- * معارة بواسطة withView().
140
- */
141
- free(handler: number): void;
129
+ * يقلّص الكتلة إلى أصغر سعة تمثل قوة للعدد اثنين وتكفي لاحتواء
130
+ * العناصر المشغولة فيها، مع الحفاظ على عنوانها ومعرّفها (handler).
131
+ *
132
+ * إذا كانت الكتلة في أعلى dataBus، يُرجع dataPointer إلى الخلف.
133
+ * وإلا، يُقسّم الجزء الطرفي المُحرر إلى مناطق ذات أحجام تمثل قوى للعدد
134
+ * اثنين، ثم تُعاد كل منطقة إلى قائمة إعادة التدوير.
135
+ *
136
+ * @param {number} handler - معرّف الكتلة المراد تقليصها.
137
+ * @returns {void}
138
+ * @throws {Error} إذا كان المعرّف غير صالح أو تالفاً أو مستعاراً عبر withView().
139
+ */
140
+ shrink(handler: number): void;
141
+ free(handler: any): void;
142
142
  /**
143
143
  * [استخدام داخلي]
144
144
  * يدخل منطقة حرة في قائمة إعادة التدوير.
@@ -228,14 +228,13 @@ export default class Balloc {
228
228
  * مشغول. يتم زيادة إشغال الكتلة فقط عندما تكون هناك مساحة
229
229
  * متاحة ضمن سعتها.
230
230
  *
231
- * @param {number} handler - مقبض الكتلة.
232
231
  * @param {number} value - القيمة المراد إدراجها.
233
232
  * @returns {boolean} `true` إذا تم إدراج القيمة؛ `false` إذا كانت الكتلة
234
233
  * بلا مساحة متاحة.
235
234
  * @throws {Error} إذا كان المقبض غير صالح، أو تالفاً، أو كانت الكتلة معارة بواسطة withView().
236
235
  *
237
236
  */
238
- push(handler: number, value: number): boolean;
237
+ push(handler: any, value: number): boolean;
239
238
  /**
240
239
  * يزيل آخر عنصر مشغول من الكتلة ويرجعه.
241
240
  *
@@ -469,12 +468,12 @@ export default class Balloc {
469
468
  * على العروض الفرعية (Subviews) المشتقة من العرض الأصلي.
470
469
  *
471
470
  * @param {number} handler - مقبض الكتلة.
472
- * @param {Function} callback - الدالة التي تتلقى TypedArray المشترك.
471
+ * @param {...(number|Function)} args - مقبض كتلة واحد أو أكثر، تليها دالة رد الاتصال.
473
472
  * @returns {*} القيمة المُرجعة بواسطة دالة رد الاتصال.
474
- * @throws {TypeError} إذا لم تكن دالة رد الاتصال دالة.
475
- * @throws {Error} إذا كان المقبض غير صالح أو معاراً مسبقاً.
473
+ * @throws {TypeError} إذا لم يتم توفير أي مقبض أو لم تكن دالة رد الاتصال دالة.
474
+ * @throws {Error} إذا كان أي مقبض غير صالح أو مكرراً أو معاراً مسبقاً.
476
475
  */
477
- withView(handler: number, callback: Function): any;
476
+ withView(...args: (number | Function)[]): any;
478
477
  /**
479
478
  * يرجع السعة الفيزيائية للكتلة بالبايت.
480
479
  *
@@ -128,6 +128,19 @@ export default class Balloc {
128
128
  * supported limit.
129
129
  */
130
130
  realloc(handler: number): void;
131
+ /**
132
+ * Reduces a block to the smallest power-of-two capacity that can contain
133
+ * its occupied elements, while preserving its address and handler.
134
+ *
135
+ * If the block is at the top of the dataBus, dataPointer is moved backward.
136
+ * Otherwise, the released tail is split into power-of-two regions and each
137
+ * region is returned to the recycling list.
138
+ *
139
+ * @param {number} handler - Handler of the block to shrink.
140
+ * @returns {void}
141
+ * @throws {Error} If the handler is invalid, corrupted, or borrowed by withView().
142
+ */
143
+ shrink(handler: number): void;
131
144
  /**
132
145
  * Frees a memory block.
133
146
  *
@@ -239,14 +252,13 @@ export default class Balloc {
239
252
  * element. Block occupancy is incremented only when space is available
240
253
  * within its capacity.
241
254
  *
242
- * @param {number} handler - Block handle.
243
255
  * @param {number} value - Value to be inserted.
244
256
  * @returns {boolean} `true` if the value was inserted; `false` if the block
245
257
  * is already out of available space.
246
258
  * @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
247
259
  *
248
260
  */
249
- push(handler: number, value: number): boolean;
261
+ push(handler: any, value: number): boolean;
250
262
  /**
251
263
  * Removes the last occupied element from the block and returns it.
252
264
  *
@@ -479,12 +491,12 @@ export default class Balloc {
479
491
  * also applies to subviews derived from the original view.
480
492
  *
481
493
  * @param {number} handler - Block handle.
482
- * @param {Function} callback - Function receiving the shared TypedArray.
494
+ * @param {...(number|Function)} args - One or more block handles followed by the callback.
483
495
  * @returns {*} Return value produced by the callback.
484
- * @throws {TypeError} If callback is not a function.
485
- * @throws {Error} If the handle is invalid or already borrowed.
496
+ * @throws {TypeError} If no handle is provided or the callback is not a function.
497
+ * @throws {Error} If a handle is invalid, duplicated, or already borrowed.
486
498
  */
487
- withView(handler: number, callback: Function): any;
499
+ withView(...args: (number | Function)[]): any;
488
500
  /**
489
501
  * Returns the physical capacity of the block in bytes.
490
502
  *
@@ -128,21 +128,20 @@ export default class Balloc {
128
128
  */
129
129
  realloc(handler: number): void;
130
130
  /**
131
- * Libera un bloque de memoria.
132
- *
133
- * La cabecera del bloque se invalida poniendo su cookie a cero y su
134
- * dirección se registra para su posterior reutilización. La región de datos
135
- * también se devuelve al sistema de reciclaje, excepto cuando el bloque está
136
- * exactamente en la cima del `dataBus`; en ese caso, el puntero `dataPointer` simplemente
137
- * retrocede, eliminando la región sin crear una entrada en la lista de
138
- * reciclaje.
139
- *
140
- * @param {number} handler - Descriptor del bloque a liberar.
141
- * @returns {void}
142
- * @throws {Error} Si el descriptor no es válido, está corrupto o el bloque
143
- * está prestado mediante `withView()`.
144
- */
145
- free(handler: number): void;
131
+ * Reduce un bloque a la menor capacidad potencia de dos que pueda contener
132
+ * sus elementos ocupados, preservando su dirección y su handler.
133
+ *
134
+ * Si el bloque está en la parte superior de dataBus, dataPointer retrocede.
135
+ * De lo contrario, la cola liberada se divide en regiones de potencias
136
+ * de dos y cada región se devuelve a la lista de reciclaje.
137
+ *
138
+ * @param {number} handler - Handler del bloque que se va a reducir.
139
+ * @returns {void}
140
+ * @throws {Error} Si el handler no es válido, está dañado o está prestado
141
+ * mediante withView().
142
+ */
143
+ shrink(handler: number): void;
144
+ free(handler: any): void;
146
145
  /**
147
146
  * [USO INTERNO]
148
147
  * Inserta una región libre en la lista de reciclaje.
@@ -239,14 +238,13 @@ export default class Balloc {
239
238
  * ocupado. La ocupación del bloque se incrementa únicamente cuando hay espacio
240
239
  * disponible dentro de su capacidad.
241
240
  *
242
- * @param {number} handler - Descriptor del bloque.
243
241
  * @param {number} value - Valor que se va a insertar.
244
242
  * @returns {boolean} `true` si el valor fue insertado; `false` si el bloque
245
243
  * ya no dispone de espacio libre.
246
244
  * @throws {Error} Si el descriptor no es válido, está corrupto o el bloque está prestado mediante `withView()`.
247
245
  *
248
246
  */
249
- push(handler: number, value: number): boolean;
247
+ push(handler: any, value: number): boolean;
250
248
  /**
251
249
  * Elimina el último elemento ocupado del bloque y lo devuelve.
252
250
  *
@@ -481,12 +479,12 @@ export default class Balloc {
481
479
  * también se aplica a las subvistas derivadas de la vista original.
482
480
  *
483
481
  * @param {number} handler - Descriptor del bloque.
484
- * @param {Function} callback - Función que recibe el TypedArray compartido.
482
+ * @param {...(number|Function)} args - Uno o más descriptores de bloque, seguidos de la retrollamada.
485
483
  * @returns {*} Valor devuelto por la retrollamada.
486
- * @throws {TypeError} Si la retrollamada no es una función.
487
- * @throws {Error} Si el descriptor no es válido o ya está prestado.
484
+ * @throws {TypeError} Si no se proporciona ningún descriptor o la retrollamada no es una función.
485
+ * @throws {Error} Si algún descriptor no es válido, está duplicado o ya está prestado.
488
486
  */
489
- withView(handler: number, callback: Function): any;
487
+ withView(...args: (number | Function)[]): any;
490
488
  /**
491
489
  * Devuelve la capacidad física del bloque en bytes.
492
490
  *
@@ -123,20 +123,20 @@ export default class Balloc {
123
123
  */
124
124
  realloc(handler: number): void;
125
125
  /**
126
- * メモリブロックを解放します。
127
- *
128
- * ブロックのヘッダーはクッキーをゼロに設定することで無効化され、
129
- * そのアドレスは後で再利用するために登録されます。ブロックが `dataBus` の
130
- * 正確なトップにある場合を除き、データ領域も再利用システムに戻されます。
131
- * トップにある場合は、`dataPointer` が単に巻き戻され、再利用リストに
132
- * エントリを作成せずに領域が破棄されます。
133
- *
134
- * @param {number} handler - 解放するブロックのハンドル。
135
- * @returns {void}
136
- * @throws {Error} ハンドルが無効、破損している場合、またはブロックが
137
- * withView() によって借用されている場合。
138
- */
139
- free(handler: number): void;
126
+ * ブロックのアドレスとハンドルを維持したまま、使用中の要素を格納できる
127
+ * 最小の2のべき乗の容量まで縮小します。
128
+ *
129
+ * ブロックが dataBus のトップにある場合、dataPointer を後退させます。
130
+ * それ以外の場合、解放された末尾領域を2のべき乗の領域に分割し、
131
+ * 各領域を再利用リストに戻します。
132
+ *
133
+ * @param {number} handler - 縮小するブロックのハンドル。
134
+ * @returns {void}
135
+ * @throws {Error} ハンドルが無効または破損している場合、あるいは
136
+ * withView() によってブロックが借用されている場合。
137
+ */
138
+ shrink(handler: number): void;
139
+ free(handler: any): void;
140
140
  /**
141
141
  * [内部利用]
142
142
  * 空き領域を再利用リストに挿入します。
@@ -223,14 +223,13 @@ export default class Balloc {
223
223
  * 要素は、最後尾の占有要素の直後の最初の空き位置に書き込まれます。
224
224
  * ブロックの占有数は、その容量内に空きスペースが存在する場合にのみインクリメントされます。
225
225
  *
226
- * @param {number} handler - ブロックのハンドル。
227
226
  * @param {number} value - 挿入する値。
228
227
  * @returns {boolean} 値が挿入された場合は `true`、ブロックに利用可能な
229
228
  * スペースがない場合は `false`。
230
229
  * @throws {Error} ハンドルが無効、破損している場合、またはブロックが withView() によって借用されている場合。
231
230
  *
232
231
  */
233
- push(handler: number, value: number): boolean;
232
+ push(handler: any, value: number): boolean;
234
233
  /**
235
234
  * ブロックから最後尾の占有要素を削除し、それを返します。
236
235
  *
@@ -442,12 +441,12 @@ export default class Balloc {
442
441
  * サブビューにも適用されます。
443
442
  *
444
443
  * @param {number} handler - ブロックのハンドル。
445
- * @param {Function} callback - 共有された TypedArray を受け取る関数。
444
+ * @param {...(number|Function)} args - 1つ以上のブロックハンドルと、その後に続くコールバック。
446
445
  * @returns {*} コールバックによって生成された戻り値。
447
- * @throws {TypeError} コールバックが関数ではない場合。
448
- * @throws {Error} ハンドルが無効であるか、すでに借用されている場合。
446
+ * @throws {TypeError} ハンドルが指定されていない場合、またはコールバックが関数ではない場合。
447
+ * @throws {Error} いずれかのハンドルが無効、重複、または借用中の場合。
449
448
  */
450
- withView(handler: number, callback: Function): any;
449
+ withView(...args: (number | Function)[]): any;
451
450
  /**
452
451
  * ブロックの物理的容量をバイト単位で返します。
453
452
  *
@@ -129,21 +129,20 @@ export default class Balloc {
129
129
  */
130
130
  realloc(handler: number): void;
131
131
  /**
132
- * Libera um bloco de memória.
133
- *
134
- * O header do bloco é invalidado pelo zeramento de seu cookie e seu
135
- * endereço é registrado para posterior reutilização. A região de dados
136
- * também é devolvida ao sistema de reciclagem, exceto quando o bloco está
137
- * exatamente no topo do `dataBus`; nesse caso, o `dataPointer` é simplesmente
138
- * retrocedido, eliminando a região sem criar uma entrada na lista de
139
- * reciclagem.
140
- *
141
- * @param {number} handler - Handler do bloco a ser liberado.
142
- * @returns {void}
143
- * @throws {Error} Se o handler for inválido, estiver corrompido ou o bloco
144
- * estiver emprestado por withView().
145
- */
146
- free(handler: number): void;
132
+ * Reduz um bloco à menor capacidade em potência de dois capaz de comportar
133
+ * seus elementos ocupados, preservando seu endereço e handler.
134
+ *
135
+ * Se o bloco estiver no topo do dataBus, dataPointer será recuado.
136
+ * Caso contrário, a cauda liberada será dividida em regiões de potências
137
+ * de dois, e cada região será devolvida à lista de reciclagem.
138
+ *
139
+ * @param {number} handler - Handler do bloco a ser reduzido.
140
+ * @returns {void}
141
+ * @throws {Error} Se o handler for inválido, estiver corrompido ou estiver
142
+ * emprestado por withView().
143
+ */
144
+ shrink(handler: number): void;
145
+ free(handler: any): void;
147
146
  /**
148
147
  * [USO INTERNO]
149
148
  * Insere uma região livre na lista de reciclagem.
@@ -482,13 +481,12 @@ export default class Balloc {
482
481
  * retornada ou utilizada após o término do callback. Essa restrição
483
482
  * também se aplica a subviews derivadas da view original.
484
483
  *
485
- * @param {number} handler - Handler do bloco.
486
- * @param {Function} callback - Função que recebe a TypedArray compartilhada.
484
+ * @param {...number|Function} args - Um ou mais handlers, seguidos pelo callback.
487
485
  * @returns {*} Retorno produzido pelo callback.
488
- * @throws {TypeError} Se callback não for uma função.
489
- * @throws {Error} Se o handler for inválido ou já estiver emprestado.
486
+ * @throws {TypeError} Se não houver handler ou se o callback não for uma função.
487
+ * @throws {Error} Se algum handler for inválido, duplicado ou já estiver emprestado.
490
488
  */
491
- withView(handler: number, callback: Function): any;
489
+ withView(...args: (number | Function)[]): any;
492
490
  /**
493
491
  * Retorna a capacidade física do bloco em bytes.
494
492
  *
@@ -117,17 +117,19 @@ export default class Balloc {
117
117
  */
118
118
  realloc(handler: number): void;
119
119
  /**
120
- * 释放内存块。
121
- *
122
- * 通过将块标头的 cookie 清零使其失效,并记录其地址以便后续复用。
123
- * 数据区域也会被交还给回收系统,除非该块刚好位于 `dataBus` 的顶部;
124
- * 在这种情况下,`dataPointer` 会直接回滚,从而消除该区域而无需在回收列表中创建条目。
125
- *
126
- * @param {number} handler - 要释放的块的句柄。
127
- * @returns {void}
128
- * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
129
- */
130
- free(handler: number): void;
120
+ * 将内存块缩小到能够容纳其已占用元素的最小 2 的幂次容量,
121
+ * 同时保留其地址和句柄。
122
+ *
123
+ * 如果该块位于 dataBus 的顶部,则向后回退 dataPointer。
124
+ * 否则,将释放的尾部区域拆分为若干个容量为 2 的幂次的区域,
125
+ * 并将每个区域归还到回收列表。
126
+ *
127
+ * @param {number} handler - 要缩小的内存块的句柄。
128
+ * @returns {void}
129
+ * @throws {Error} 如果句柄无效、已损坏,或该块正通过 withView() 被借用。
130
+ */
131
+ shrink(handler: number): void;
132
+ free(handler: any): void;
131
133
  /**
132
134
  * [内部使用]
133
135
  * 将空闲区域插入回收列表。
@@ -209,13 +211,12 @@ export default class Balloc {
209
211
  * 该元素被写入最后一个占用元素之后的第一个空闲位置。
210
212
  * 只有在容量范围内有可用空间时,块的占用数才会递增。
211
213
  *
212
- * @param {number} handler - 块句柄。
213
214
  * @param {number} value - 要插入的值。
214
215
  * @returns {boolean} 如果值已成功插入则为 `true`;如果块已无可用空间则为 `false`。
215
216
  * @throws {Error} 如果句柄无效、已损坏,或者块正通过 withView() 被借用。
216
217
  *
217
218
  */
218
- push(handler: number, value: number): boolean;
219
+ push(handler: any, value: number): boolean;
219
220
  /**
220
221
  * 移除块中最后一个占用的元素并将其返回。
221
222
  *
@@ -414,12 +415,12 @@ export default class Balloc {
414
415
  * 此限制同样适用于从原始视图派生的子视图(subviews)。
415
416
  *
416
417
  * @param {number} handler - 块句柄。
417
- * @param {Function} callback - 接收共享 TypedArray 的函数。
418
+ * @param {...(number|Function)} args - 一个或多个块句柄,最后一个参数为回调函数。
418
419
  * @returns {*} 回调函数产生 返回值。
419
- * @throws {TypeError} 如果回调不是函数。
420
- * @throws {Error} 如果句柄无效或已被借用。
420
+ * @throws {TypeError} 如果未提供句柄或回调不是函数。
421
+ * @throws {Error} 如果任一句柄无效、重复或已被借用。
421
422
  */
422
- withView(handler: number, callback: Function): any;
423
+ withView(...args: (number | Function)[]): any;
423
424
  /**
424
425
  * 返回块的物理容量(以字节为单位)。
425
426
  *
package/change.log.md DELETED
@@ -1,7 +0,0 @@
1
- # version 1.2
2
- - Fix duplicate memory allocation in constructor - https://github.com/AugustoVasques75/balloc/issues/4
3
- - Add `indexOf()` method - https://github.com/AugustoVasques75/balloc/issues/1
4
- - Add `lastIndexOf()` method
5
- - Add method isFull() - https://github.com/AugustoVasques75/balloc/issues/2
6
- - Typo correction
7
- - Add `withView()` method to address the Direct Mutable Access to Allocation Storage issue: https://github.com/AugustoVasques75/balloc/issues/3