balloc 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.MD +433 -0
- package/README.ar.md +405 -0
- package/README.en.md +433 -0
- package/README.es.md +405 -0
- package/README.jp.md +404 -0
- package/README.pt.md +433 -0
- package/README.zh.md +404 -0
- package/docs/API /342/200/224 balloc.ar.md" +1365 -0
- package/docs/API /342/200/224 balloc.en.md" +1264 -0
- package/docs/API /342/200/224 balloc.es.md" +1180 -0
- package/docs/API /342/200/224 balloc.jp.md" +1087 -0
- package/docs/API /342/200/224 balloc.pt.md" +1214 -0
- package/docs/API /342/200/224 balloc.zh.md" +1168 -0
- package/package.json +107 -6
- package/src/ar/balloc.cjs +1079 -0
- package/src/ar/balloc.js +2 -0
- package/src/ar/balloc.mjs +1327 -0
- package/src/en/balloc.cjs +1091 -0
- package/src/en/balloc.js +2 -0
- package/src/en/balloc.mjs +1339 -0
- package/src/es/balloc.cjs +1111 -0
- package/src/es/balloc.js +2 -0
- package/src/es/balloc.mjs +1360 -0
- package/src/jp/balloc.cjs +1067 -0
- package/src/jp/balloc.js +2 -0
- package/src/jp/balloc.mjs +1315 -0
- package/src/pt/balloc.cjs +1114 -0
- package/src/pt/balloc.js +2 -0
- package/src/pt/balloc.mjs +1362 -0
- package/src/zh/balloc.cjs +1013 -0
- package/src/zh/balloc.js +2 -0
- package/src/zh/balloc.mjs +1262 -0
- package/types/ar/balloc.d.mts +647 -0
- package/types/en/balloc.d.mts +659 -0
- package/types/es/balloc.d.mts +679 -0
- package/types/jp/balloc.d.mts +635 -0
- package/types/pt/balloc.d.mts +682 -0
- package/types/zh/balloc.d.mts +581 -0
- package/README.md +0 -3
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
export type BucketType = '8u' | '16u' | '32u' | '64u' | '8i' | '16i' | '32i' | '64i' | '16f' | '32f' | '64f';
|
|
2
|
+
export type Metadata = {
|
|
3
|
+
/**
|
|
4
|
+
* - The capacity of the memory block in bytes.
|
|
5
|
+
*/
|
|
6
|
+
byteCapacity: number;
|
|
7
|
+
/**
|
|
8
|
+
* - The capacity of the memory block expressed as a base-two exponent.
|
|
9
|
+
*/
|
|
10
|
+
capacityExponent: number;
|
|
11
|
+
/**
|
|
12
|
+
* - Address of the memory block within the dataBus.
|
|
13
|
+
*/
|
|
14
|
+
dataAddress: number;
|
|
15
|
+
/**
|
|
16
|
+
* - Generation to which the handle belongs.
|
|
17
|
+
*/
|
|
18
|
+
generation: number;
|
|
19
|
+
/**
|
|
20
|
+
* - Address of the header within the controlBus.
|
|
21
|
+
*/
|
|
22
|
+
headerAddress: number;
|
|
23
|
+
/**
|
|
24
|
+
* - Number of occupied elements in the block.
|
|
25
|
+
*/
|
|
26
|
+
occupied: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Echo Stack Dual Bus Bump Allocator with ReBump GC.
|
|
30
|
+
*/
|
|
31
|
+
export default class Balloc {
|
|
32
|
+
wordType: BucketType;
|
|
33
|
+
bucketType: string;
|
|
34
|
+
bucketSize: number;
|
|
35
|
+
bucketArray: any;
|
|
36
|
+
wordSize: number;
|
|
37
|
+
wordShift: number;
|
|
38
|
+
size: number;
|
|
39
|
+
headerSize: number;
|
|
40
|
+
dataPointer: number;
|
|
41
|
+
controlPointer: number;
|
|
42
|
+
echoPointer: number;
|
|
43
|
+
generation: number;
|
|
44
|
+
dataBus: Uint8Array<ArrayBuffer>;
|
|
45
|
+
controlBus: Uint8Array<ArrayBuffer>;
|
|
46
|
+
dataAdapter: DataView<ArrayBuffer>;
|
|
47
|
+
controlAdapter: DataView<ArrayBuffer>;
|
|
48
|
+
auxiliary: any;
|
|
49
|
+
writer: any;
|
|
50
|
+
reader: any;
|
|
51
|
+
recycle: Map<any, any>;
|
|
52
|
+
/** @type {Metadata} */
|
|
53
|
+
metadata: Metadata;
|
|
54
|
+
/**
|
|
55
|
+
* Creates an instance of the `Balloc` memory allocator.
|
|
56
|
+
*
|
|
57
|
+
* The allocator maintains two independent buffers:
|
|
58
|
+
* - `dataBus`: stores the data of allocated blocks;
|
|
59
|
+
* - `controlBus`: stores headers and other control metadata.
|
|
60
|
+
*
|
|
61
|
+
* Each block has a fixed-size header (`HEADER_SIZE`) and is externally
|
|
62
|
+
* identified by a 43-bit logical handle, composed of an 11-bit generation
|
|
63
|
+
* and a 32-bit header address.
|
|
64
|
+
*
|
|
65
|
+
* The capacity of each block is rounded up to the next power of two
|
|
66
|
+
* in elements, respecting the configured bucket width.
|
|
67
|
+
*
|
|
68
|
+
* @param {number} [size=65535] - Initial capacity of the `dataBus`, in bytes.
|
|
69
|
+
* Must be a safe integer in the range [2, 0x80000000].
|
|
70
|
+
* @param {BucketType} [bucketSize="32u"] - Bucket type and size identifier
|
|
71
|
+
* (e.g., '32u' for Uint32, '64f' for Float64, '8i' for Int8).
|
|
72
|
+
* @param {number} [metadataSize=1024] - Initial capacity of the `controlBus`,
|
|
73
|
+
* in bytes. Must be a safe integer in the range
|
|
74
|
+
* [HEADER_SIZE, 0x80000000].
|
|
75
|
+
*
|
|
76
|
+
* @throws {RangeError} If `size` is out of the allowed range.
|
|
77
|
+
* @throws {RangeError} If `bucketSize` is not a valid bucket identifier.
|
|
78
|
+
* @throws {RangeError} If `metadataSize` is out of the allowed range.
|
|
79
|
+
*/
|
|
80
|
+
constructor(size?: number, bucketSize?: BucketType, metadataSize?: number);
|
|
81
|
+
/**
|
|
82
|
+
* Allocates a new memory block.
|
|
83
|
+
*
|
|
84
|
+
* The requested capacity is converted to the smallest power of two that
|
|
85
|
+
* accommodates `size` elements. The block is preferably obtained from the list
|
|
86
|
+
* of recyclable regions with the corresponding capacity; if no compatible
|
|
87
|
+
* region exists, the block is reserved at the top of the `dataBus`.
|
|
88
|
+
*
|
|
89
|
+
* Next, a header is obtained from the control space. Previously freed headers
|
|
90
|
+
* can be reused via the internal recycling stack; otherwise, the `controlBus`
|
|
91
|
+
* is expanded when necessary.
|
|
92
|
+
*
|
|
93
|
+
* The header receives the address, capacity, initial occupancy (zero), and
|
|
94
|
+
* a validation cookie. The returned value encodes the allocation generation
|
|
95
|
+
* and the header address:
|
|
96
|
+
*
|
|
97
|
+
* [11 bits generation][32 bits header address]
|
|
98
|
+
*
|
|
99
|
+
* @param {number} [size=16] - Minimum number of elements the block must
|
|
100
|
+
* accommodate. Must be a safe integer in the range [2, 0x80000000].
|
|
101
|
+
* @returns {number} Handle identifying the allocated block.
|
|
102
|
+
* @throws {RangeError} If `size` is out of the allowed range.
|
|
103
|
+
*/
|
|
104
|
+
alloc(size?: number): number;
|
|
105
|
+
/**
|
|
106
|
+
* Expands a block's capacity to the next power of two.
|
|
107
|
+
*
|
|
108
|
+
* When the block is located at the current top of the `dataBus`, expansion
|
|
109
|
+
* occurs in-place: only its capacity and `dataPointer` are updated.
|
|
110
|
+
*
|
|
111
|
+
* If another region exists after the block, a new region with double the
|
|
112
|
+
* capacity is obtained from the recyclable space or allocated at the top of
|
|
113
|
+
* the `dataBus`. Currently occupied elements are then copied to the new region,
|
|
114
|
+
* and the previous region is returned to the recycling list.
|
|
115
|
+
*
|
|
116
|
+
* The handle remains valid because its header is preserved, and only the
|
|
117
|
+
* address and capacity recorded within it are updated.
|
|
118
|
+
*
|
|
119
|
+
* @param {number} handler - Handle of the block to expand.
|
|
120
|
+
* @returns {void}
|
|
121
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
122
|
+
* @throws {AggregateError} If the capacity has reached its maximum
|
|
123
|
+
* supported limit.
|
|
124
|
+
*/
|
|
125
|
+
realloc(handler: number): void;
|
|
126
|
+
/**
|
|
127
|
+
* Frees a memory block.
|
|
128
|
+
*
|
|
129
|
+
* The block's header is invalidated by zeroing its cookie, and its address
|
|
130
|
+
* is registered for later reuse. The data region is also returned to the
|
|
131
|
+
* recycling system, except when the block is exactly at the top of the
|
|
132
|
+
* `dataBus`; in that case, `dataPointer` is simply rolled back, eliminating
|
|
133
|
+
* the region without creating an entry in the recycling list.
|
|
134
|
+
*
|
|
135
|
+
* @param {number} handler - Handle of the block to free.
|
|
136
|
+
* @returns {void}
|
|
137
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
138
|
+
*/
|
|
139
|
+
free(handler: number): void;
|
|
140
|
+
/**
|
|
141
|
+
* [INTERNAL USE]
|
|
142
|
+
* Inserts a free region into the recycling list.
|
|
143
|
+
*
|
|
144
|
+
* The list is organized by capacity in bytes. For each capacity, the
|
|
145
|
+
* stored value is a `Uint32Array` whose zero position contains the number
|
|
146
|
+
* of registered addresses, and subsequent positions contain the addresses
|
|
147
|
+
* of the free regions:
|
|
148
|
+
*
|
|
149
|
+
* [count, address1, address2, ...]
|
|
150
|
+
*
|
|
151
|
+
* When the page becomes full, its buffer is expanded to double the current
|
|
152
|
+
* capacity.
|
|
153
|
+
*
|
|
154
|
+
* @param {number} capacity - Capacity of the free region, in bytes.
|
|
155
|
+
* @param {number} address - Initial address of the region in the `dataBus`.
|
|
156
|
+
* @returns {void}
|
|
157
|
+
* @private
|
|
158
|
+
*/
|
|
159
|
+
private recyclePush;
|
|
160
|
+
/**
|
|
161
|
+
* [INTERNAL USE]
|
|
162
|
+
* Removes a free region from the recycling list.
|
|
163
|
+
*
|
|
164
|
+
* The search is performed by the exact requested capacity. When a corresponding
|
|
165
|
+
* page exists, the last registered address is removed from the page and returned,
|
|
166
|
+
* implementing recycling as a LIFO stack.
|
|
167
|
+
*
|
|
168
|
+
* @param {number} capacity - Desired capacity, in bytes.
|
|
169
|
+
* @returns {number} Initial address of a recyclable region or `UNKNOWN`
|
|
170
|
+
* when no region exists with the requested capacity.
|
|
171
|
+
* @private
|
|
172
|
+
*/
|
|
173
|
+
private recyclePop;
|
|
174
|
+
/**
|
|
175
|
+
* Compacts the `dataBus` using the ReBump algorithm.
|
|
176
|
+
*
|
|
177
|
+
* Sequentially traverses the headers registered in the `controlBus` and copies
|
|
178
|
+
* still-valid regions to a new `dataBus`, eliminating free spaces between them.
|
|
179
|
+
* The address of each surviving block is updated in its respective header.
|
|
180
|
+
*
|
|
181
|
+
* The order of blocks in the `controlBus` is preserved. Internal data of each
|
|
182
|
+
* block is also fully preserved, including its reserved capacity and current
|
|
183
|
+
* occupancy.
|
|
184
|
+
*
|
|
185
|
+
* At the end of compaction:
|
|
186
|
+
* - all recyclable regions are eliminated;
|
|
187
|
+
* - `dataPointer` points to the first byte after the last live block;
|
|
188
|
+
* - `dataBus` represents a contiguous heap for active blocks.
|
|
189
|
+
*
|
|
190
|
+
* Control complexity is O(n), where `n` is the number of headers traversed.
|
|
191
|
+
* The effective cost of memory movement also depends on the volume of bytes
|
|
192
|
+
* belonging to live blocks.
|
|
193
|
+
*
|
|
194
|
+
* @returns {void}
|
|
195
|
+
*/
|
|
196
|
+
gc(): void;
|
|
197
|
+
/**
|
|
198
|
+
* [!!! INTERNAL USE, DO NOT USE IN YOUR CODE !!!]
|
|
199
|
+
* Expands the `dataBus` to double its current capacity.
|
|
200
|
+
*
|
|
201
|
+
* The underlying buffer is transferred to a larger `ArrayBuffer`, preserving
|
|
202
|
+
* existing data. All views associated with the buffer are recreated to reference
|
|
203
|
+
* the new storage.
|
|
204
|
+
*
|
|
205
|
+
* @returns {void}
|
|
206
|
+
* @throws {RangeError} If the `dataBus` capacity has exceeded the maximum
|
|
207
|
+
* supported limit.
|
|
208
|
+
* @private
|
|
209
|
+
*/
|
|
210
|
+
private grow;
|
|
211
|
+
/**
|
|
212
|
+
* [!!! INTERNAL USE, DO NOT USE IN YOUR CODE !!!]
|
|
213
|
+
* Expands the `controlBus` to double its current capacity.
|
|
214
|
+
*
|
|
215
|
+
* The underlying buffer is transferred to a larger `ArrayBuffer`, preserving
|
|
216
|
+
* existing headers. The `controlAdapter` is recreated to reference the new
|
|
217
|
+
* storage.
|
|
218
|
+
*
|
|
219
|
+
* @returns {void}
|
|
220
|
+
* @throws {RangeError} If the current `controlBus` capacity has exceeded
|
|
221
|
+
* the maximum supported limit.
|
|
222
|
+
* @private
|
|
223
|
+
*/
|
|
224
|
+
private headerGrow;
|
|
225
|
+
/**
|
|
226
|
+
* Appends an element to the end of the block's occupied sequence.
|
|
227
|
+
*
|
|
228
|
+
* The element is written at the first free position after the last occupied
|
|
229
|
+
* element. Block occupancy is incremented only when space is available within
|
|
230
|
+
* its capacity.
|
|
231
|
+
*
|
|
232
|
+
* @param {number} handler - Block handle.
|
|
233
|
+
* @param {number} value - Value to insert.
|
|
234
|
+
* @returns {boolean} `true` if the value was inserted; `false` if the block
|
|
235
|
+
* has no available space.
|
|
236
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
237
|
+
*/
|
|
238
|
+
push(handler: number, value: number): boolean;
|
|
239
|
+
/**
|
|
240
|
+
* Removes and returns the last occupied element from the block.
|
|
241
|
+
*
|
|
242
|
+
* Occupancy is decremented before reading the removed element. Corresponding
|
|
243
|
+
* bytes are not cleared and remain in the allocated region, but cease to be
|
|
244
|
+
* part of the occupied sequence.
|
|
245
|
+
*
|
|
246
|
+
* @param {number} handler - Block handle.
|
|
247
|
+
* @returns {number|undefined} Removed value or `undefined` if the block
|
|
248
|
+
* is empty.
|
|
249
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
250
|
+
*/
|
|
251
|
+
pop(handler: number): number | undefined;
|
|
252
|
+
/**
|
|
253
|
+
* Returns the last occupied element without removing it.
|
|
254
|
+
*
|
|
255
|
+
* @param {number} handler - Block handle.
|
|
256
|
+
* @returns {number|undefined} Last occupied value or `undefined` if the
|
|
257
|
+
* block is empty.
|
|
258
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
259
|
+
*/
|
|
260
|
+
peek(handler: number): number | undefined;
|
|
261
|
+
/**
|
|
262
|
+
* Inserts an element at the beginning of the occupied sequence.
|
|
263
|
+
*
|
|
264
|
+
* Currently occupied elements are shifted one position forward and `value`
|
|
265
|
+
* is written to the first position. The operation requires a free position
|
|
266
|
+
* within the block's capacity.
|
|
267
|
+
*
|
|
268
|
+
* The operation's cost is proportional to the number of occupied elements,
|
|
269
|
+
* as data needs to be shifted.
|
|
270
|
+
*
|
|
271
|
+
* @param {number} handler - Block handle.
|
|
272
|
+
* @param {number} value - Value to insert.
|
|
273
|
+
* @returns {boolean} `true` if the value was inserted; `false` if the block
|
|
274
|
+
* is full.
|
|
275
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
276
|
+
*/
|
|
277
|
+
nudge(handler: number, value: number): boolean;
|
|
278
|
+
/**
|
|
279
|
+
* Removes and returns the first occupied element from the block.
|
|
280
|
+
*
|
|
281
|
+
* Remaining elements are shifted one position toward the beginning of the
|
|
282
|
+
* occupied region. The block's physical capacity remains unchanged.
|
|
283
|
+
*
|
|
284
|
+
* The operation's cost is proportional to the number of shifted elements.
|
|
285
|
+
*
|
|
286
|
+
* @param {number} handler - Block handle.
|
|
287
|
+
* @returns {number|undefined} First removed value or `undefined` if the
|
|
288
|
+
* block is empty.
|
|
289
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
290
|
+
*/
|
|
291
|
+
pull(handler: number): number | undefined;
|
|
292
|
+
/**
|
|
293
|
+
* Returns the first occupied element without removing it.
|
|
294
|
+
*
|
|
295
|
+
* @param {number} handler - Block handle.
|
|
296
|
+
* @returns {number|undefined} First occupied value or `undefined` if the
|
|
297
|
+
* block is empty.
|
|
298
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
299
|
+
*/
|
|
300
|
+
head(handler: number): number | undefined;
|
|
301
|
+
/**
|
|
302
|
+
* Checks if a value is present among the block's occupied elements.
|
|
303
|
+
*
|
|
304
|
+
* The search is performed directly on the auxiliary `TypedArray` and considers
|
|
305
|
+
* only the currently occupied range, not the entire reserved physical capacity.
|
|
306
|
+
*
|
|
307
|
+
* @param {number} handler - Block handle.
|
|
308
|
+
* @param {number} value - Value to search for.
|
|
309
|
+
* @returns {boolean} `true` if `value` is present among the occupied elements;
|
|
310
|
+
* otherwise, `false`.
|
|
311
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
312
|
+
*/
|
|
313
|
+
has(handler: number, value: number): boolean;
|
|
314
|
+
/**
|
|
315
|
+
* Returns the occupied element at a zero-based index.
|
|
316
|
+
*
|
|
317
|
+
* Indices outside the range `[0, occupied)` are not considered an error and
|
|
318
|
+
* result in `undefined`.
|
|
319
|
+
*
|
|
320
|
+
* @param {number} handler - Block handle.
|
|
321
|
+
* @param {number} index - Zero-based index of the element.
|
|
322
|
+
* @returns {number|undefined} Value stored at the index or `undefined` if
|
|
323
|
+
* the index is outside the occupied region.
|
|
324
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
325
|
+
*/
|
|
326
|
+
get(handler: number, index: number): number | undefined;
|
|
327
|
+
/**
|
|
328
|
+
* Replaces the value stored at an occupied index.
|
|
329
|
+
*
|
|
330
|
+
* The operation does not alter block occupancy. Indices outside the range
|
|
331
|
+
* `[0, occupied)` are ignored and result in `false`.
|
|
332
|
+
*
|
|
333
|
+
* @param {number} handler - Block handle.
|
|
334
|
+
* @param {number} index - Zero-based index of the element.
|
|
335
|
+
* @param {number} value - New value.
|
|
336
|
+
* @returns {boolean} `true` if the value was written; `false` if the index
|
|
337
|
+
* is outside the occupied region.
|
|
338
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
339
|
+
*/
|
|
340
|
+
set(handler: number, index: number, value: number): boolean;
|
|
341
|
+
/**
|
|
342
|
+
* Clears block occupancy and fills its entire physical capacity with a single value.
|
|
343
|
+
*
|
|
344
|
+
* Filling covers all available slots, not just currently occupied elements.
|
|
345
|
+
* After the operation, the block is considered empty regardless of the value
|
|
346
|
+
* used for filling.
|
|
347
|
+
*
|
|
348
|
+
* @param {number} handler - Block handle.
|
|
349
|
+
* @param {number} [value=0] - Value used to fill all slots.
|
|
350
|
+
* @returns {void}
|
|
351
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
352
|
+
*/
|
|
353
|
+
clear(handler: number, value?: number): void;
|
|
354
|
+
/**
|
|
355
|
+
* Fills a range of block slots with a value.
|
|
356
|
+
*
|
|
357
|
+
* The range uses zero-based indices and follows the convention `[start, end)`,
|
|
358
|
+
* meaning `start` is inclusive and `end` is exclusive. When `end` is not provided,
|
|
359
|
+
* filling extends to the end of the block's physical capacity.
|
|
360
|
+
*
|
|
361
|
+
* Block occupancy becomes `end`, including when the filled range extends beyond
|
|
362
|
+
* the previous occupancy.
|
|
363
|
+
*
|
|
364
|
+
* @param {number} handler - Block handle.
|
|
365
|
+
* @param {number} value - Value used for filling.
|
|
366
|
+
* @param {number} [start=0] - Initial index, inclusive.
|
|
367
|
+
* @param {number|null} [end=null] - Final index, exclusive. When omitted or
|
|
368
|
+
* `null`, uses the total block capacity.
|
|
369
|
+
* @returns {void}
|
|
370
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
371
|
+
* @throws {RangeError} If `start` or `end` is out of capacity bounds, or if
|
|
372
|
+
* `start > end`.
|
|
373
|
+
*/
|
|
374
|
+
fill(handler: number, value: number, start?: number, end?: number | null): void;
|
|
375
|
+
/**
|
|
376
|
+
* Fills all physical slots of the block with a value and marks the entire
|
|
377
|
+
* capacity as occupied.
|
|
378
|
+
*
|
|
379
|
+
* Unlike `clear()`, the operation results in a fully occupied block.
|
|
380
|
+
*
|
|
381
|
+
* @param {number} handler - Block handle.
|
|
382
|
+
* @param {number} value - Value used for filling.
|
|
383
|
+
* @returns {void}
|
|
384
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
385
|
+
*/
|
|
386
|
+
fillAll(handler: number, value: number): void;
|
|
387
|
+
/**
|
|
388
|
+
* Copies elements from an `Array` or `TypedArray` to the beginning of the block.
|
|
389
|
+
*
|
|
390
|
+
* The copied quantity is limited by the block's capacity. Occupancy is updated
|
|
391
|
+
* to the greater value between the previous occupancy and the quantity effectively
|
|
392
|
+
* copied; therefore, a call to `put()` does not reduce existing occupancy when
|
|
393
|
+
* receiving a smaller sequence.
|
|
394
|
+
*
|
|
395
|
+
* For `Array`, only the first fitting elements are copied. For `TypedArray`,
|
|
396
|
+
* a source subarray is used without intermediate copying.
|
|
397
|
+
*
|
|
398
|
+
* @param {number} handler - Block handle.
|
|
399
|
+
* @param {Array|TypedArray} data - Sequence of values to copy.
|
|
400
|
+
* @returns {void}
|
|
401
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
402
|
+
* @throws {TypeError} If `data` is neither an `Array` nor a `TypedArray`.
|
|
403
|
+
*/
|
|
404
|
+
put(handler: number, data: any[] | TypedArray): void;
|
|
405
|
+
/**
|
|
406
|
+
* Returns a copy of the elements currently occupied by the block.
|
|
407
|
+
*
|
|
408
|
+
* The result is a `TypedArray` of the same type configured by `bucketSize`.
|
|
409
|
+
* Unoccupied physical capacity is not included in the result.
|
|
410
|
+
*
|
|
411
|
+
* The operation does not directly expose internal storage: the method uses
|
|
412
|
+
* `TypedArray.prototype.slice()`, producing a new view with independent storage.
|
|
413
|
+
*
|
|
414
|
+
* @param {number} handler - Block handle.
|
|
415
|
+
* @returns {TypedArray} Copy of the elements occupied by the block. When the
|
|
416
|
+
* block is empty, returns an empty `TypedArray` of the configured type.
|
|
417
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
418
|
+
*/
|
|
419
|
+
pick(handler: number): TypedArray;
|
|
420
|
+
/**
|
|
421
|
+
* Returns the physical capacity of the block in bytes.
|
|
422
|
+
*
|
|
423
|
+
* Capacity corresponds to the space reserved for all slots in the block,
|
|
424
|
+
* regardless of how many elements are currently occupied.
|
|
425
|
+
*
|
|
426
|
+
* @param {number} handler - Block handle.
|
|
427
|
+
* @returns {number} Physical capacity of the block, in bytes.
|
|
428
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
429
|
+
*/
|
|
430
|
+
byteSize(handler: number): number;
|
|
431
|
+
/**
|
|
432
|
+
* Returns the number of elements currently occupied in the block.
|
|
433
|
+
*
|
|
434
|
+
* @param {number} handler - Block handle.
|
|
435
|
+
* @returns {number} Number of occupied elements.
|
|
436
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
437
|
+
*/
|
|
438
|
+
occupied(handler: number): number;
|
|
439
|
+
/**
|
|
440
|
+
* Returns the capacity of the block in number of elements.
|
|
441
|
+
*
|
|
442
|
+
* The value corresponds to the total quantity of physically reserved slots,
|
|
443
|
+
* not the currently occupied quantity.
|
|
444
|
+
*
|
|
445
|
+
* @param {number} handler - Block handle.
|
|
446
|
+
* @returns {number} Total number of slots available in the block.
|
|
447
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
448
|
+
*/
|
|
449
|
+
length(handler: number): number;
|
|
450
|
+
/**
|
|
451
|
+
* Copies occupied elements from one block to another.
|
|
452
|
+
*
|
|
453
|
+
* Copying begins at the first element of `source` and the first slot of
|
|
454
|
+
* `destiny`. The effectively copied quantity is limited by the number of occupied
|
|
455
|
+
* elements in the source, the destination's physical capacity, and the limit
|
|
456
|
+
* specified in `slots`.
|
|
457
|
+
*
|
|
458
|
+
* Destination occupancy is increased, when necessary, to cover the copied
|
|
459
|
+
* elements. Destination capacity is not changed.
|
|
460
|
+
*
|
|
461
|
+
* The operation uses `Uint8Array.prototype.copyWithin()` on the `dataBus`,
|
|
462
|
+
* therefore the return value is the `Uint8Array` representing the data bus.
|
|
463
|
+
*
|
|
464
|
+
* @param {number} source - Source block handle.
|
|
465
|
+
* @param {number} destiny - Destination block handle.
|
|
466
|
+
* @param {number} [slots=UNKNOWN] - Maximum number of elements to copy.
|
|
467
|
+
* `UNKNOWN` represents the sentinel value used not to impose an additional limit.
|
|
468
|
+
* @returns {Uint8Array} The `dataBus` after the operation.
|
|
469
|
+
* @throws {Error} If `source` or `destiny` is invalid or corrupted.
|
|
470
|
+
*/
|
|
471
|
+
copy(source: number, destiny: number, slots?: number): Uint8Array;
|
|
472
|
+
/**
|
|
473
|
+
* Copies a region of elements within the same block.
|
|
474
|
+
*
|
|
475
|
+
* Indices are zero-based and follow the semantics of `Array.prototype.copyWithin()`:
|
|
476
|
+
* `target` identifies the destination position, `start` the source starting
|
|
477
|
+
* position, and `length` the maximum number of elements to copy.
|
|
478
|
+
*
|
|
479
|
+
* Negative indices are interpreted from the end of the block's capacity. After
|
|
480
|
+
* this normalization, indices are clamped to physical capacity. The copied quantity
|
|
481
|
+
* is also limited by effectively occupied elements and available space from `target`.
|
|
482
|
+
*
|
|
483
|
+
* The operation supports overlapping regions and can increase block occupancy
|
|
484
|
+
* when the target range exceeds current occupancy.
|
|
485
|
+
*
|
|
486
|
+
* @param {number} handler - Block handle.
|
|
487
|
+
* @param {number} target - Initial destination index.
|
|
488
|
+
* @param {number} start - Initial source index.
|
|
489
|
+
* @param {number} length - Maximum number of elements to copy.
|
|
490
|
+
* @returns {Uint8Array} The `dataBus` after the operation.
|
|
491
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
492
|
+
*/
|
|
493
|
+
copyWithin(handler: number, target: number, start: number, length: number): Uint8Array;
|
|
494
|
+
/**
|
|
495
|
+
* Appends occupied elements of `source` to the end of `target`.
|
|
496
|
+
*
|
|
497
|
+
* The copied quantity is limited by the number of occupied elements in the
|
|
498
|
+
* source and the remaining free space in the destination's capacity.
|
|
499
|
+
*
|
|
500
|
+
* The operation supports `target` and `source` referencing the same block.
|
|
501
|
+
* In that case, the copy is performed on the block's own region and preserves
|
|
502
|
+
* `copyWithin()` overlap semantics.
|
|
503
|
+
*
|
|
504
|
+
* @param {number} target - Handle of the block that will receive the elements.
|
|
505
|
+
* @param {number} source - Handle of the block whose elements will be appended.
|
|
506
|
+
* @returns {number} Number of elements effectively appended.
|
|
507
|
+
* @throws {Error} If `target` or `source` is invalid or corrupted.
|
|
508
|
+
*/
|
|
509
|
+
append(target: number, source: number): number;
|
|
510
|
+
/**
|
|
511
|
+
* Creates an independent copy of a block.
|
|
512
|
+
*
|
|
513
|
+
* The new block has the same physical capacity and occupancy as the original block.
|
|
514
|
+
* All bytes in the reserved region, including currently unoccupied positions,
|
|
515
|
+
* are copied.
|
|
516
|
+
*
|
|
517
|
+
* The new block receives a new handle and remains subject to normal allocation
|
|
518
|
+
* and recycling rules.
|
|
519
|
+
*
|
|
520
|
+
* @param {number} handler - Handle of the block to clone.
|
|
521
|
+
* @returns {number} Handle of the new block.
|
|
522
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
523
|
+
*/
|
|
524
|
+
clone(handler: number): number;
|
|
525
|
+
/**
|
|
526
|
+
* Swaps the data regions associated with two blocks.
|
|
527
|
+
*
|
|
528
|
+
* The operation swaps region addresses, capacities, and occupancies between headers.
|
|
529
|
+
* Handles remain unchanged: after the operation, each handle continues identifying
|
|
530
|
+
* its own header, but that header now describes the region previously belonging
|
|
531
|
+
* to the other block.
|
|
532
|
+
*
|
|
533
|
+
* No data is copied in the `dataBus`; only the metadata of the two headers
|
|
534
|
+
* is modified.
|
|
535
|
+
*
|
|
536
|
+
* @param {number} source - Handle of the first block.
|
|
537
|
+
* @param {number} target - Handle of the second block.
|
|
538
|
+
* @returns {void}
|
|
539
|
+
* @throws {Error} If `source` or `target` is invalid or corrupted.
|
|
540
|
+
*/
|
|
541
|
+
swap(source: number, target: number): void;
|
|
542
|
+
/**
|
|
543
|
+
* Serializes the content and metadata of a memory block.
|
|
544
|
+
*
|
|
545
|
+
* The method extracts occupied data from the block via `pick()`, groups them
|
|
546
|
+
* along with capacity and word type information into a data envelope, and encodes
|
|
547
|
+
* them using the CBOR format.
|
|
548
|
+
*
|
|
549
|
+
* @param {number} handler - Handle of the block to serialize.
|
|
550
|
+
* @returns {Uint8Array} A buffer containing the serialized block data.
|
|
551
|
+
* @throws {Error} If the handle is invalid or corrupted.
|
|
552
|
+
*/
|
|
553
|
+
dump(handler: number): Uint8Array;
|
|
554
|
+
/**
|
|
555
|
+
* Restores a memory block from serialized data.
|
|
556
|
+
*
|
|
557
|
+
* Decodes the CBOR envelope, validates whether the word type of the serialized
|
|
558
|
+
* data is compatible with the current allocator instance, and allocates a new
|
|
559
|
+
* block with the necessary capacity to accommodate the content. Then, the original
|
|
560
|
+
* data and occupancy are restored in the new block.
|
|
561
|
+
*
|
|
562
|
+
* @param {Uint8Array|ArrayBuffer} serialized - Buffer containing serialized data.
|
|
563
|
+
* @returns {number} Handle of the newly recreated block.
|
|
564
|
+
* @throws {Error} If the decoded data's word type differs from the one configured in the allocator.
|
|
565
|
+
*/
|
|
566
|
+
load(serialized: Uint8Array | ArrayBuffer): number;
|
|
567
|
+
/**
|
|
568
|
+
* [INTERNAL USE]
|
|
569
|
+
* Decodes the metadata associated with a handle.
|
|
570
|
+
*
|
|
571
|
+
* The handle is split into generation and header address. The cookie stored
|
|
572
|
+
* in the header is then recalculated to validate the combination; only after
|
|
573
|
+
* this validation is metadata read.
|
|
574
|
+
*
|
|
575
|
+
* Decoded fields are written to the `out` object, which is returned by the function
|
|
576
|
+
* itself. The object can be reused across calls to avoid new allocations.
|
|
577
|
+
*
|
|
578
|
+
* @param {number} handler - Handle to decode and validate.
|
|
579
|
+
* @param {Metadata} out - Object that will receive the decoded metadata.
|
|
580
|
+
* @returns {Metadata} The same reference provided in `out`.
|
|
581
|
+
* @throws {Error} If the header cookie does not match the handle or if the
|
|
582
|
+
* handle does not identify a valid header.
|
|
583
|
+
* @private
|
|
584
|
+
*/
|
|
585
|
+
private extractMetadata;
|
|
586
|
+
/**
|
|
587
|
+
* [INTERNAL USE]
|
|
588
|
+
* Calculates the validation cookie of a header.
|
|
589
|
+
*
|
|
590
|
+
* The cookie is derived from the header address and the 11-bit generation via
|
|
591
|
+
* fast hash. The result is reduced to 8 bits.
|
|
592
|
+
*
|
|
593
|
+
* The value zero is reserved as an invalid header indicator; therefore, when
|
|
594
|
+
* the calculation yields zero, the method returns `1`.
|
|
595
|
+
*
|
|
596
|
+
* The cookie is not a cryptographic mechanism. Its purpose is to detect invalid,
|
|
597
|
+
* stale, or corrupted handles within the allocator's memory management model.
|
|
598
|
+
*
|
|
599
|
+
* @param {number} addr - Header address.
|
|
600
|
+
* @param {number} gen - Generation associated with the handle. Only the 11 least
|
|
601
|
+
* significant bits are considered.
|
|
602
|
+
* @returns {number} 8-bit cookie in the range [1, 255].
|
|
603
|
+
* @private
|
|
604
|
+
*/
|
|
605
|
+
private bakeCookie;
|
|
606
|
+
/**
|
|
607
|
+
* Returns the number of regions currently stored in the recycling list.
|
|
608
|
+
*
|
|
609
|
+
* Each region corresponds to a freed block that could not be returned directly
|
|
610
|
+
* to the top of the `dataBus` and therefore remains available for reuse by a
|
|
611
|
+
* future allocation of identical capacity.
|
|
612
|
+
*
|
|
613
|
+
* @returns {number} Number of free regions maintained in `recycle`.
|
|
614
|
+
*/
|
|
615
|
+
recycleCount(): number;
|
|
616
|
+
/**
|
|
617
|
+
* Returns the total bytes belonging to regions currently stored in the recycling list.
|
|
618
|
+
*
|
|
619
|
+
* The value is calculated by summing the capacity of each free region multiplied
|
|
620
|
+
* by the number of regions registered for that capacity.
|
|
621
|
+
*
|
|
622
|
+
* @returns {number} Number of bytes occupied by recyclable regions.
|
|
623
|
+
*/
|
|
624
|
+
get recycleBytes(): number;
|
|
625
|
+
/**
|
|
626
|
+
* Returns the amount of allocated bytes that do not belong to currently recyclable regions.
|
|
627
|
+
*
|
|
628
|
+
* The value is obtained by subtracting the bytes belonging to free regions maintained
|
|
629
|
+
* in `recycle` from the total advance of `dataPointer`.
|
|
630
|
+
*
|
|
631
|
+
* @returns {number} Number of bytes associated with non-recyclable regions within
|
|
632
|
+
* the area already used by the `dataBus`.
|
|
633
|
+
*/
|
|
634
|
+
get allocatedBytes(): number;
|
|
635
|
+
/**
|
|
636
|
+
* Returns the amount of bytes not yet used at the top of the `dataBus`.
|
|
637
|
+
*
|
|
638
|
+
* This space corresponds to the region between `dataPointer` and the end of the
|
|
639
|
+
* capacity currently reserved for the buffer.
|
|
640
|
+
*
|
|
641
|
+
* @returns {number} Number of available bytes after `dataPointer`.
|
|
642
|
+
*/
|
|
643
|
+
get unallocatedBytes(): number;
|
|
644
|
+
/**
|
|
645
|
+
* Returns the ratio between recyclable memory and the used heap area.
|
|
646
|
+
*
|
|
647
|
+
* The metric considers free regions maintained in `recycle` as fragmentation
|
|
648
|
+
* and uses `dataPointer` as the denominator. When no bytes have been used by the heap,
|
|
649
|
+
* the result is zero.
|
|
650
|
+
*
|
|
651
|
+
* The value does not represent the fraction of the entire `dataBus` capacity,
|
|
652
|
+
* but specifically the proportion of recyclable bytes within the area already
|
|
653
|
+
* traversed by the bump pointer.
|
|
654
|
+
*
|
|
655
|
+
* @returns {number} Fragmentation ratio in the range [0, 1] under normal
|
|
656
|
+
* operating conditions.
|
|
657
|
+
*/
|
|
658
|
+
get fragmentationRatio(): number;
|
|
659
|
+
}
|