balloc 1.4.0 → 2.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/CHANGELOG.md +35 -0
- package/README.MD +157 -334
- package/docs/API /342/200/224 balloc.en.md" +478 -1217
- package/docs/MIGRATION /342/200/224 balloc.en.md" +208 -0
- package/package.json +2 -44
- package/src/README.MD +0 -0
- package/src/en/balloc.cjs +510 -960
- package/src/en/balloc.js +1 -1
- package/src/en/balloc.mjs +551 -1099
- package/types/README.MD +0 -0
- package/types/en/balloc.d.mts +275 -583
- package/README.ar.md +0 -439
- package/README.en.md +0 -467
- package/README.es.md +0 -439
- package/README.jp.md +0 -438
- package/README.pt.md +0 -462
- package/README.zh.md +0 -438
- package/docs/API /342/200/224 balloc.ar.md" +0 -1509
- package/docs/API /342/200/224 balloc.es.md" +0 -1325
- package/docs/API /342/200/224 balloc.jp.md" +0 -1264
- package/docs/API /342/200/224 balloc.pt.md" +0 -1395
- package/docs/API /342/200/224 balloc.zh.md" +0 -1324
- package/src/ar/balloc.cjs +0 -1303
- package/src/ar/balloc.js +0 -2
- package/src/ar/balloc.mjs +0 -1591
- package/src/es/balloc.cjs +0 -1320
- package/src/es/balloc.js +0 -2
- package/src/es/balloc.mjs +0 -1608
- package/src/jp/balloc.cjs +0 -1260
- package/src/jp/balloc.js +0 -2
- package/src/jp/balloc.mjs +0 -1548
- package/src/pt/balloc.cjs +0 -1326
- package/src/pt/balloc.js +0 -2
- package/src/pt/balloc.mjs +0 -1619
- package/src/zh/balloc.cjs +0 -1205
- package/src/zh/balloc.js +0 -2
- package/src/zh/balloc.mjs +0 -1493
- package/types/ar/balloc.d.mts +0 -793
- package/types/es/balloc.d.mts +0 -810
- package/types/jp/balloc.d.mts +0 -750
- package/types/pt/balloc.d.mts +0 -818
- package/types/zh/balloc.d.mts +0 -695
package/src/en/balloc.mjs
CHANGED
|
@@ -6,22 +6,15 @@ import { encode, decode } from 'cbor-x';
|
|
|
6
6
|
const UNKNOWN = 0xFFFFFFFF >>> 0;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* u
|
|
14
|
-
* i
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* [Size in bits][data type]
|
|
20
|
-
*
|
|
21
|
-
* Examples:
|
|
22
|
-
* A 32-bit unsigned integer bucket: 32u
|
|
23
|
-
* A 64-bit signed integer bucket: 64i
|
|
24
|
-
*
|
|
9
|
+
* Maps bucket identifiers to the TypedArray constructor and
|
|
10
|
+
* the element width in bits.
|
|
11
|
+
*
|
|
12
|
+
* Integers support 8-, 16-, 32-, and 64-bit widths; floating-point values
|
|
13
|
+
* support 16, 32, and 64 bits. The suffixes are u (unsigned integer),
|
|
14
|
+
* i (signed integer), and f (floating-point).
|
|
15
|
+
*
|
|
16
|
+
* The identifier follows the [width][type] format, for example, 32u,
|
|
17
|
+
* 64i or 32f.
|
|
25
18
|
*/
|
|
26
19
|
const BUCKET_MAPPING = Object.freeze({
|
|
27
20
|
"8u": { type: "Uint8", bits: 8 },
|
|
@@ -41,927 +34,601 @@ const BUCKET_MAPPING = Object.freeze({
|
|
|
41
34
|
* Bucket types supported by the allocator:
|
|
42
35
|
* - Unsigned integers: '8u', '16u', '32u', '64u'
|
|
43
36
|
* - Signed integers: '8i', '16i', '32i', '64i'
|
|
44
|
-
* - Floating
|
|
37
|
+
* - Floating-point: '16f', '32f', '64f'
|
|
45
38
|
*
|
|
46
39
|
* @typedef {'8u'|'16u'|'32u'|'64u'|'8i'|'16i'|'32i'|'64i'|'16f'|'32f'|'64f'} BucketType
|
|
47
40
|
*/
|
|
48
41
|
|
|
49
|
-
/**
|
|
50
|
-
* System endianness flag.
|
|
51
|
-
*
|
|
52
|
-
* Detects whether the system is Little Endian, which affects
|
|
53
|
-
* byte order in DataView operations.
|
|
54
|
-
*/
|
|
55
|
-
const IS_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
56
42
|
|
|
57
43
|
|
|
58
|
-
/**
|
|
59
|
-
* Metadata format.
|
|
60
|
-
*
|
|
61
|
-
* [32bits address][32bits occupied][8bits capacity][8bits cookie][8bits flags][40bits reserved]
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* Total: 16 bytes
|
|
65
|
-
*/
|
|
66
|
-
const HEADER_SIZE = 16 >>> 0;
|
|
67
|
-
const ADDRESS_OFFSET = 0 >>> 0;
|
|
68
|
-
const OCCUPIED_OFFSET = 4 >>> 0;
|
|
69
|
-
const CAPACITY_OFFSET = 8 >>> 0;
|
|
70
|
-
const COOKIE_OFFSET = 9 >>> 0;
|
|
71
|
-
const FLAGS_OFFSET = 10 >>> 0;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Flags:
|
|
75
|
-
* bit 0: VIEW_LOCK
|
|
76
|
-
*/
|
|
77
|
-
const VIEW_LOCK = 1 >>> 0;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const ROTATE_32BITS = 4294967296;
|
|
81
|
-
const CLAMP_11BITS = 0x7FF >>> 0;
|
|
82
44
|
|
|
83
45
|
/**
|
|
84
|
-
*
|
|
46
|
+
* Checks whether a value is a TypedArray instance.
|
|
85
47
|
*
|
|
86
|
-
* @
|
|
87
|
-
* @
|
|
88
|
-
* @property {number} capacityExponent - The capacity of the memory block expressed as a base-two exponent.
|
|
89
|
-
* @property {number} dataAddress - Address of the memory block within the dataBus.
|
|
90
|
-
* @property {number} generation - Generation to which the handle belongs.
|
|
91
|
-
* @property {number} headerAddress - Address of the header within the controlBus.
|
|
92
|
-
* @property {number} occupied - Number of occupied elements in the block.
|
|
93
|
-
* @property {number} borrowed - Flag indicating whether the block has been borrowed by withView().
|
|
94
|
-
*/
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Checks if a value is a TypedArray instance.
|
|
98
|
-
*
|
|
99
|
-
* @param {any} val - Value to be tested.
|
|
100
|
-
* @returns {boolean} `true` if the value is a TypedArray, otherwise `false`.
|
|
48
|
+
* @param {any} val - Value to test.
|
|
49
|
+
* @returns {boolean} `true` if the value is a TypedArray; otherwise `false`.
|
|
101
50
|
*/
|
|
102
51
|
const isTypedArray = (val) => ArrayBuffer.isView(val) && !(val instanceof DataView);
|
|
103
52
|
|
|
104
53
|
/**
|
|
105
|
-
*
|
|
106
|
-
*/
|
|
107
|
-
/**
|
|
108
|
-
* Synchronous adapter contract for data ingestion.
|
|
54
|
+
* Contract for a synchronous data-ingestion adapter.
|
|
109
55
|
*
|
|
110
|
-
* The adapter receives the Balloc instance and uses its public API to
|
|
56
|
+
* The adapter receives the Balloc instance and uses its public API to
|
|
57
|
+
* store the data, returning the handler identifying the main block.
|
|
111
58
|
*
|
|
112
59
|
* @typedef {Object} FormatAdapter
|
|
113
|
-
* @property {(allocator: Balloc) =>
|
|
60
|
+
* @property {(allocator: Balloc) => *} load - Loads the data into the allocator
|
|
61
|
+
* and returns the operation result. Balloc passes this value through without validating it.
|
|
114
62
|
*/
|
|
115
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Memory block allocator with monotonic handles and typed views.
|
|
66
|
+
*
|
|
67
|
+
* Each allocation has its own ArrayBuffer; the instance keeps the blocks
|
|
68
|
+
* in a handler-indexed registry and tracks each block's occupancy.
|
|
69
|
+
*/
|
|
116
70
|
export default class Balloc {
|
|
117
71
|
|
|
118
72
|
/**
|
|
119
|
-
* Creates an instance
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* @param {number} [size=65535] - Initial capacity of the `dataBus`, in bytes.
|
|
133
|
-
* Must be a safe integer in the range [2, 0x80000000].
|
|
134
|
-
* @param {BucketType} [bucketSize="32u"] - Identifier for the bucket type and size
|
|
135
|
-
* (e.g., '32u' for Uint32, '64f' for Float64, '8i' for Int8).
|
|
136
|
-
* @param {number} [metadataSize=1024] - Initial capacity of the `controlBus`,
|
|
137
|
-
* in bytes. Must be a safe integer in the range
|
|
138
|
-
* [HEADER_SIZE, 0x80000000].
|
|
139
|
-
*
|
|
140
|
-
* @throws {RangeError} If `size` is outside the allowed range.
|
|
141
|
-
* @throws {RangeError} If `bucketSize` is not a valid bucket identifier.
|
|
142
|
-
* @throws {RangeError} If `metadataSize` is outside the allowed range.
|
|
73
|
+
* Creates an allocator instance.
|
|
74
|
+
*
|
|
75
|
+
* Initializes the limit used to validate allocation requests,
|
|
76
|
+
* the optional default bucket type, and the structures that associate
|
|
77
|
+
* handles with blocks. Each block receives its own ArrayBuffer
|
|
78
|
+
* when alloc() is called.
|
|
79
|
+
*
|
|
80
|
+
* @param {number|null} [size=0x80000000] Limit used to validate
|
|
81
|
+
* the size requested by alloc(). Must be a safe integer
|
|
82
|
+
* between 2 and 0x80000000.
|
|
83
|
+
* @param {BucketType|null} [bucketSize=null] Default bucket type.
|
|
84
|
+
* When set, it takes precedence over bucketType in alloc().
|
|
85
|
+
* @throws {RangeError} If size or bucketSize is invalid.
|
|
143
86
|
*/
|
|
144
|
-
constructor(size =
|
|
145
|
-
|
|
87
|
+
constructor(size = null, bucketSize = null) {
|
|
88
|
+
size = size ?? 0x80000000;
|
|
146
89
|
if (!Number.isSafeInteger(size) || size < 2 || size > 0x80000000) throw new RangeError(`Parameter size must be in range [2, 0x80000000] but size is ${size}`);
|
|
147
90
|
|
|
148
|
-
|
|
149
|
-
if (!BUCKET_MAPPING[bucketSize]) {
|
|
91
|
+
if (bucketSize !== null && !BUCKET_MAPPING[bucketSize]) {
|
|
150
92
|
throw new RangeError(`bucketSize must be in ([ 8 || 16 || 32 || 64][ u || i ]) || ([ 16 || 32 || 64][ f ]) but I found ${bucketSize}`);
|
|
151
93
|
}
|
|
152
94
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
this.
|
|
156
|
-
this.
|
|
157
|
-
this.bucketSize = BUCKET_MAPPING[bucketSize].bits >>> 0;
|
|
158
|
-
this.bucketArray = globalThis[`${this.bucketType}Array`];
|
|
159
|
-
this.wordSize = this.bucketSize >>> 3;
|
|
160
|
-
this.wordShift = this.bucketSize >>> 4;
|
|
161
|
-
this.size = Math.max(1 << (32 - Math.clz32(size - 1)) >>> 0, this.wordSize);
|
|
162
|
-
this.headerSize = 1 << (32 - Math.clz32(metadataSize - 1)) >>> 0;
|
|
163
|
-
this.dataPointer = 0 >>> 0;
|
|
164
|
-
this.controlPointer = 0 >>> 0;
|
|
165
|
-
this.echoPointer = 0 >>> 0;
|
|
166
|
-
this.generation = 0 >>> 0;
|
|
167
|
-
this.dataBus = new Uint8Array(this.size);
|
|
168
|
-
this.controlBus = new Uint8Array(this.headerSize);
|
|
169
|
-
this.dataAdapter = new DataView(this.dataBus.buffer);
|
|
170
|
-
this.controlAdapter = new DataView(this.controlBus.buffer);
|
|
171
|
-
this.auxiliary = new globalThis[`${this.bucketType}Array`](this.dataBus.buffer);
|
|
172
|
-
this.writer = this.dataAdapter[`set${this.bucketType}`].bind(this.dataAdapter);
|
|
173
|
-
this.reader = this.dataAdapter[`get${this.bucketType}`].bind(this.dataAdapter);
|
|
174
|
-
this.recycle = new Map();
|
|
175
|
-
|
|
176
|
-
/** @type {Metadata} */
|
|
177
|
-
this.metadata = {
|
|
178
|
-
generation: 0 >>> 0,
|
|
179
|
-
headerAddress: 0 >>> 0,
|
|
180
|
-
dataAddress: 0 >>> 0,
|
|
181
|
-
capacityExponent: 0 >>> 0,
|
|
182
|
-
byteCapacity: 0 >>> 0,
|
|
183
|
-
occupied: 0 >>> 0,
|
|
184
|
-
};
|
|
185
|
-
|
|
95
|
+
this.maxByteSize = size;
|
|
96
|
+
this.bucketSize = bucketSize;
|
|
97
|
+
this.bus = Object.create(null);
|
|
98
|
+
this.lastHandler = 0;
|
|
186
99
|
}
|
|
187
100
|
|
|
188
|
-
// ========== ALLOCATION ==========
|
|
189
101
|
|
|
190
102
|
/**
|
|
191
|
-
* Allocates a
|
|
103
|
+
* Allocates a memory block and returns its handle.
|
|
192
104
|
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
105
|
+
* size specifies the ArrayBuffer size in bytes. The typed view is
|
|
106
|
+
* selected by bucketType and contains the number of elements
|
|
107
|
+
* that fit this buffer size. If the instance has
|
|
108
|
+
* bucketSize set, it takes precedence over bucketType.
|
|
197
109
|
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* otherwise, the `controlBus` is expanded when necessary.
|
|
110
|
+
* Each allocation receives a monotonic handle that is never reused. The identifier
|
|
111
|
+
* space is limited to 32 unsigned bits.
|
|
201
112
|
*
|
|
202
|
-
*
|
|
203
|
-
* a
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
* @param {number} [size=16] - Minimum number of elements the block must
|
|
209
|
-
* accommodate. Must be a safe integer in the range [2, 0x80000000].
|
|
210
|
-
* @returns {number} Handle identifying the allocated block.
|
|
211
|
-
* @throws {RangeError} If `size` is outside the allowed range.
|
|
212
|
-
* @throws {Error} If it is necessary to expand the `dataBus` while any
|
|
213
|
-
* block is borrowed via withView().
|
|
113
|
+
* @param {number} [size=16] ArrayBuffer size in bytes. Must be
|
|
114
|
+
* a safe integer greater than or equal to 2 and less than maxByteSize.
|
|
115
|
+
* @param {BucketType} [bucketType="32u"] Typed view type.
|
|
116
|
+
* @returns {number} Handle of the allocated block.
|
|
117
|
+
* @throws {RangeError} If the bucket type or size is invalid.
|
|
214
118
|
*/
|
|
215
|
-
alloc(size = 16) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const blockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
220
|
-
|
|
221
|
-
let dataAddress = this.recyclePop(blockSize);
|
|
222
|
-
|
|
223
|
-
if (dataAddress === UNKNOWN) {
|
|
224
|
-
while (this.dataPointer + blockSize > this.size) this.grow();
|
|
225
|
-
dataAddress = this.dataPointer;
|
|
226
|
-
this.dataPointer += blockSize;
|
|
119
|
+
alloc(size = 16, bucketType = "32u") {
|
|
120
|
+
bucketType = this.bucketSize ?? bucketType;
|
|
121
|
+
if (!BUCKET_MAPPING[bucketType]) {
|
|
122
|
+
throw new RangeError(`bucketSize must be in ([ 8 || 16 || 32 || 64][ u || i ]) || ([ 16 || 32 || 64][ f ]) but I found ${bucketType}`);
|
|
227
123
|
}
|
|
124
|
+
if (!Number.isSafeInteger(size) || size < 2 || size >= this.maxByteSize || size % (BUCKET_MAPPING[bucketType].bits / 8) !== 0) throw new RangeError(`Parameter size must be an aligned integer in range [2, ${this.maxByteSize}) for ${bucketType}`);
|
|
228
125
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if (this.controlPointer + HEADER_SIZE > this.controlAdapter.byteLength) this.headerGrow();
|
|
235
|
-
headerAddress = this.controlPointer;
|
|
236
|
-
this.controlPointer += HEADER_SIZE;
|
|
237
|
-
this.echoPointer += HEADER_SIZE;
|
|
238
|
-
}
|
|
126
|
+
if (this.lastHandler >= 0xFFFFFFFF) throw new RangeError("Handler space exhausted");
|
|
127
|
+
const newHandler = ++this.lastHandler;
|
|
128
|
+
const dataBuffer = new ArrayBuffer(size);
|
|
129
|
+
const dataBus = new globalThis[`${BUCKET_MAPPING[bucketType].type}Array`](dataBuffer);
|
|
130
|
+
const newPack = this.bus[newHandler] = Object.create(null);
|
|
239
131
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
132
|
+
newPack.bucketType = bucketType;
|
|
133
|
+
newPack.dataBuffer = dataBuffer;
|
|
134
|
+
newPack.dataBus = dataBus;
|
|
135
|
+
newPack.occupied = 0;
|
|
136
|
+
newPack.borrowed = 0;
|
|
245
137
|
|
|
246
|
-
return
|
|
138
|
+
return newHandler;
|
|
247
139
|
}
|
|
248
140
|
|
|
249
141
|
/**
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
* When the block is located at the current top of the `dataBus`, expansion
|
|
253
|
-
* occurs in-place: only its capacity and `dataPointer` are
|
|
254
|
-
* updated.
|
|
255
|
-
*
|
|
256
|
-
* If another region exists after the block, a new region with double the
|
|
257
|
-
* capacity is obtained from the recyclable space or allocated at the top of the `dataBus`.
|
|
258
|
-
* The currently occupied elements are then copied to the new region,
|
|
259
|
-
* and the previous region is returned to the recycling list.
|
|
142
|
+
* Replaces a block's storage with a buffer of greater capacity.
|
|
260
143
|
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
144
|
+
* newSize is expressed in elements. If omitted or null, the capacity
|
|
145
|
+
* doubles; the result is capped by maxByteSize converted to
|
|
146
|
+
* elements according to BYTES_PER_ELEMENT. The occupied elements are
|
|
147
|
+
* copied to the new buffer.
|
|
263
148
|
*
|
|
264
|
-
* @param {number} handler
|
|
149
|
+
* @param {number} handler Block handle.
|
|
150
|
+
* @param {number|null} [newSize=null] New capacity in elements.
|
|
265
151
|
* @returns {void}
|
|
266
|
-
* @throws {Error} If the handle
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* @throws {AggregateError} If the capacity has reached the maximum
|
|
270
|
-
* supported limit.
|
|
152
|
+
* @throws {Error} If the handle does not exist or the block is borrowed.
|
|
153
|
+
* @throws {RangeError} If the new capacity is not a safe integer
|
|
154
|
+
* greater than the current capacity.
|
|
271
155
|
*/
|
|
272
|
-
realloc(handler) {
|
|
156
|
+
realloc(handler, newSize = null) {
|
|
157
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
273
158
|
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
277
|
-
const occupied = this.controlAdapter.getUint32(headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
278
|
-
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
279
|
-
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
280
|
-
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
159
|
+
const pack = this.bus[handler];
|
|
160
|
+
if (pack.borrowed) throw new Error("Block is borrowed");
|
|
281
161
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
if (borrowed) {
|
|
287
|
-
throw new Error("Block is borrowed");
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
if (capacity >= 31) throw new AggregateError([new Error("Can´t reallocate handler."), new RangeError("Capacity has reached its limit.")], "Internal Error.");
|
|
291
|
-
|
|
292
|
-
const newCapacity = capacity + 1;
|
|
293
|
-
const newBlockSize = (1 << newCapacity >>> 0) * this.wordSize;
|
|
294
|
-
const oldBlockSize = (1 << capacity >>> 0) * this.wordSize;
|
|
295
|
-
|
|
296
|
-
if (dataAddress + oldBlockSize === this.dataPointer) {
|
|
297
|
-
while (dataAddress + newBlockSize > this.size) this.grow();
|
|
298
|
-
|
|
299
|
-
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
300
|
-
this.dataPointer = dataAddress + newBlockSize;
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
162
|
+
const oldCapacity = pack.dataBus.length;
|
|
163
|
+
if (newSize !== null && (!Number.isSafeInteger(newSize) || newSize <= oldCapacity)) throw new RangeError("Invalid reallocation size");
|
|
303
164
|
|
|
304
|
-
|
|
165
|
+
const maxCapacity = Math.floor((this.maxByteSize - 1) / pack.dataBus.BYTES_PER_ELEMENT);
|
|
166
|
+
const newCapacity = Math.min(newSize ?? oldCapacity * 2, maxCapacity);
|
|
167
|
+
if (newCapacity <= oldCapacity) throw new RangeError("Invalid reallocation size");
|
|
305
168
|
|
|
306
|
-
|
|
307
|
-
|
|
169
|
+
const newBuffer = new ArrayBuffer(newCapacity * pack.dataBus.BYTES_PER_ELEMENT);
|
|
170
|
+
const newDataBus = new globalThis[`${BUCKET_MAPPING[pack.bucketType].type}Array`](newBuffer);
|
|
308
171
|
|
|
309
|
-
|
|
310
|
-
this.dataPointer += newBlockSize;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
this.dataBus.copyWithin(newAddress, dataAddress, dataAddress + occupied * this.wordSize);
|
|
314
|
-
|
|
315
|
-
this.controlAdapter.setUint32(headerAddress + ADDRESS_OFFSET, newAddress, IS_LITTLE_ENDIAN);
|
|
316
|
-
this.controlAdapter.setUint8(headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
172
|
+
newDataBus.set(pack.dataBus.subarray(0, pack.occupied));
|
|
317
173
|
|
|
318
|
-
|
|
174
|
+
pack.dataBuffer = newBuffer;
|
|
175
|
+
pack.dataBus = newDataBus;
|
|
319
176
|
}
|
|
320
177
|
|
|
321
178
|
/**
|
|
322
|
-
* Reduces
|
|
323
|
-
* its
|
|
179
|
+
* Reduces the block capacity to an amount calculated from
|
|
180
|
+
* its occupancy.
|
|
324
181
|
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
182
|
+
* The new capacity is the smallest power of two that can hold the occupied
|
|
183
|
+
* elements, with a minimum of two elements. The method creates a new ArrayBuffer
|
|
184
|
+
* and copies the occupied elements. If the calculated capacity is not smaller
|
|
185
|
+
* than the current capacity, a RangeError is thrown. No region recycling or
|
|
186
|
+
* heap compaction is performed.
|
|
328
187
|
*
|
|
329
|
-
* @param {number} handler
|
|
188
|
+
* @param {number} handler Block handle.
|
|
330
189
|
* @returns {void}
|
|
331
|
-
* @throws {Error} If the
|
|
190
|
+
* @throws {Error} If the handle does not exist or the block is borrowed.
|
|
191
|
+
* @throws {RangeError} If the calculated capacity is not smaller than the current capacity.
|
|
332
192
|
*/
|
|
333
193
|
shrink(handler) {
|
|
334
|
-
|
|
335
|
-
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
336
|
-
|
|
337
|
-
const newCapacity = Math.max(1, 32 - Math.clz32(Math.max(2, meta.occupied) - 1));
|
|
338
|
-
if (newCapacity >= meta.capacityExponent) return;
|
|
339
|
-
|
|
340
|
-
const oldBlockSize = meta.byteCapacity;
|
|
341
|
-
const newBlockSize = (2 ** newCapacity) * this.wordSize;
|
|
342
|
-
const oldEnd = meta.dataAddress + oldBlockSize;
|
|
343
|
-
|
|
344
|
-
this.controlAdapter.setUint8(meta.headerAddress + CAPACITY_OFFSET, newCapacity);
|
|
345
|
-
|
|
346
|
-
if (oldEnd === this.dataPointer) {
|
|
347
|
-
this.dataPointer = meta.dataAddress + newBlockSize;
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
let address = meta.dataAddress + newBlockSize;
|
|
352
|
-
for (let exponent = newCapacity; exponent < meta.capacityExponent; exponent++) {
|
|
353
|
-
const segmentSize = (2 ** exponent) * this.wordSize;
|
|
354
|
-
this.recyclePush(segmentSize, address);
|
|
355
|
-
address += segmentSize;
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* Frees a memory block.
|
|
361
|
-
*
|
|
362
|
-
* The block's header is invalidated by zeroing its cookie, and its
|
|
363
|
-
* address is registered for later reuse. The data region
|
|
364
|
-
* is also returned to the recycling system, except when the block is
|
|
365
|
-
* exactly at the top of the `dataBus`; in that case, the `dataPointer` is simply
|
|
366
|
-
* rolled back, eliminating the region without creating an entry in the recycling
|
|
367
|
-
* list.
|
|
368
|
-
*
|
|
369
|
-
* @param {number} handler - Handle of the block to be freed.
|
|
370
|
-
* @returns {void}
|
|
371
|
-
* @throws {Error} If the handle is invalid, corrupted, or the block
|
|
372
|
-
* is borrowed via withView().
|
|
373
|
-
*/
|
|
374
|
-
free(handler) {
|
|
194
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
375
195
|
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
const dataAddress = this.controlAdapter.getUint32(headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
379
|
-
const capacity = this.controlAdapter.getUint8(headerAddress + CAPACITY_OFFSET);
|
|
380
|
-
const cookie = this.controlAdapter.getUint8(headerAddress + COOKIE_OFFSET);
|
|
381
|
-
const borrowed = this.controlAdapter.getUint8(headerAddress + FLAGS_OFFSET) & VIEW_LOCK;
|
|
196
|
+
const pack = this.bus[handler];
|
|
197
|
+
if (pack.borrowed) throw new Error("Block is borrowed");
|
|
382
198
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
199
|
+
const oldCapacity = pack.dataBus.length;
|
|
200
|
+
const newCapacity = 2 ** Math.ceil(Math.log2(Math.max(2, pack.occupied)));
|
|
386
201
|
|
|
387
|
-
if (
|
|
388
|
-
|
|
389
|
-
}
|
|
202
|
+
if (newCapacity >= oldCapacity) return;
|
|
203
|
+
if (!Number.isSafeInteger(newCapacity)) throw new RangeError("Invalid reallocation size");
|
|
390
204
|
|
|
391
|
-
const
|
|
392
|
-
|
|
205
|
+
const newBuffer = new ArrayBuffer(newCapacity * pack.dataBus.BYTES_PER_ELEMENT);
|
|
206
|
+
const newDataBus = new globalThis[`${BUCKET_MAPPING[pack.bucketType].type}Array`](newBuffer);
|
|
393
207
|
|
|
394
|
-
|
|
395
|
-
this.controlAdapter.setUint32(this.echoPointer, headerAddress, IS_LITTLE_ENDIAN);
|
|
396
|
-
this.echoPointer += 4;
|
|
397
|
-
}
|
|
208
|
+
newDataBus.set(pack.dataBus.subarray(0, pack.occupied));
|
|
398
209
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
} else {
|
|
402
|
-
this.recyclePush(blockSize, dataAddress);
|
|
403
|
-
}
|
|
210
|
+
pack.dataBuffer = newBuffer;
|
|
211
|
+
pack.dataBus = newDataBus;
|
|
404
212
|
}
|
|
405
|
-
// ========== RECYCLING ==========
|
|
406
213
|
|
|
407
|
-
|
|
408
|
-
*
|
|
409
|
-
* Inserts a free region into the recycling list.
|
|
410
|
-
*
|
|
411
|
-
* The list is organized by capacity in bytes. For each capacity, the
|
|
412
|
-
* stored value is a `Uint32Array` whose zero position contains the quantity
|
|
413
|
-
* of registered addresses and subsequent positions contain the addresses of
|
|
414
|
-
* the free regions:
|
|
415
|
-
*
|
|
416
|
-
* [count, address1, address2, ...]
|
|
417
|
-
*
|
|
418
|
-
* When the page becomes full, its buffer is expanded to double its current
|
|
419
|
-
* capacity.
|
|
420
|
-
*
|
|
421
|
-
* @param {number} capacity - Capacity of the free region, in bytes.
|
|
422
|
-
* @param {number} address - Initial address of the region in the `dataBus`.
|
|
423
|
-
* @returns {void}
|
|
424
|
-
* @private
|
|
425
|
-
*/
|
|
426
|
-
recyclePush(capacity, address) {
|
|
427
|
-
if (this.recycle.has(capacity)) {
|
|
428
|
-
let page = this.recycle.get(capacity);
|
|
429
|
-
const count = page[0];
|
|
430
|
-
|
|
431
|
-
if (count === page.length - 1) {
|
|
432
|
-
const newBuffer = page.buffer.transfer(page.buffer.byteLength * 2);
|
|
433
|
-
page = new Uint32Array(newBuffer);
|
|
434
|
-
this.recycle.set(capacity, page);
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
page[count + 1] = address;
|
|
438
|
-
page[0] = count + 1;
|
|
439
|
-
} else {
|
|
440
|
-
this.recycle.set(capacity, new Uint32Array([1, address]));
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
/**
|
|
445
|
-
* [INTERNAL USE]
|
|
446
|
-
* Removes a free region from the recycling list.
|
|
447
|
-
*
|
|
448
|
-
* The search is performed by the exact requested capacity. When a corresponding
|
|
449
|
-
* page exists, the last registered address is removed from the page and returned,
|
|
450
|
-
* implementing recycling as a LIFO stack.
|
|
451
|
-
*
|
|
452
|
-
* @param {number} capacity - Desired capacity, in bytes.
|
|
453
|
-
* @returns {number} Initial address of a recyclable region or `UNKNOWN`
|
|
454
|
-
* when a region with the requested capacity does not exist.
|
|
455
|
-
* @private
|
|
456
|
-
*/
|
|
457
|
-
recyclePop(capacity) {
|
|
458
|
-
if (!this.recycle.has(capacity)) return UNKNOWN;
|
|
459
|
-
|
|
460
|
-
const page = this.recycle.get(capacity);
|
|
461
|
-
const count = page[0];
|
|
462
|
-
|
|
463
|
-
if (count === 0) return UNKNOWN;
|
|
464
|
-
|
|
465
|
-
const address = page[count];
|
|
466
|
-
page[0] = count - 1;
|
|
467
|
-
|
|
468
|
-
return address;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// ========== GARBAGE COLLECTION ==========
|
|
472
|
-
|
|
473
|
-
/**
|
|
474
|
-
* Compacts the `dataBus` using the ReBump algorithm.
|
|
475
|
-
*
|
|
476
|
-
* Sequentially traverses the headers registered in the `controlBus` and copies
|
|
477
|
-
* still-valid regions to a new `dataBus`, eliminating free spaces
|
|
478
|
-
* between them. Each surviving block's address is updated in its
|
|
479
|
-
* respective header.
|
|
480
|
-
*
|
|
481
|
-
* The order of blocks in the `controlBus` is preserved. The internal data of
|
|
482
|
-
* each block is also fully preserved, including its reserved capacity
|
|
483
|
-
* and current occupancy.
|
|
484
|
-
*
|
|
485
|
-
* Upon completion of compaction:
|
|
486
|
-
* - all recyclable regions are eliminated;
|
|
487
|
-
* - `dataPointer` points to the first byte after the last live block;
|
|
488
|
-
* - the `dataBus` represents a contiguous heap for active blocks.
|
|
214
|
+
/**
|
|
215
|
+
* Frees a block and permanently invalidates its handle.
|
|
489
216
|
*
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
* of bytes belonging to live blocks.
|
|
217
|
+
* Removes the entry from bus. Handles are not reused. There is no
|
|
218
|
+
* coalescing or recycling of regions in a shared heap.
|
|
493
219
|
*
|
|
220
|
+
* @param {number} handler Handle of the block to free.
|
|
494
221
|
* @returns {void}
|
|
495
|
-
* @throws {Error} If
|
|
222
|
+
* @throws {Error} If the handle does not exist or the block is borrowed.
|
|
496
223
|
*/
|
|
497
|
-
|
|
498
|
-
this.
|
|
499
|
-
|
|
500
|
-
const newDataBus = new Uint8Array(this.dataBus.buffer.byteLength);
|
|
501
|
-
this.recycle.clear();
|
|
502
|
-
this.dataPointer = 0 >>> 0;
|
|
503
|
-
|
|
504
|
-
for (let i = 0; i < this.controlPointer; i += HEADER_SIZE) {
|
|
505
|
-
if (this.controlBus[i + COOKIE_OFFSET] === 0) continue;
|
|
506
|
-
|
|
507
|
-
const start = this.controlAdapter.getUint32(i + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
508
|
-
const size = (1 << this.controlBus[i + CAPACITY_OFFSET] >>> 0) * this.wordSize;
|
|
509
|
-
const end = start + size;
|
|
224
|
+
free(handler) {
|
|
225
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
510
226
|
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
this.dataPointer += size;
|
|
514
|
-
}
|
|
227
|
+
const pack = this.bus[handler];
|
|
228
|
+
if (pack.borrowed) throw new Error("Block is borrowed");
|
|
515
229
|
|
|
516
|
-
this.
|
|
517
|
-
this.dataAdapter = new DataView(this.dataBus.buffer);
|
|
518
|
-
this.auxiliary = new globalThis[`${this.bucketType}Array`](this.dataBus.buffer);
|
|
519
|
-
this.writer = this.dataAdapter[`set${this.bucketType}`].bind(this.dataAdapter);
|
|
520
|
-
this.reader = this.dataAdapter[`get${this.bucketType}`].bind(this.dataAdapter);
|
|
230
|
+
delete this.bus[handler];
|
|
521
231
|
}
|
|
232
|
+
// ========== RECYCLING ==========
|
|
522
233
|
|
|
523
|
-
// ========== SCALING ==========
|
|
524
|
-
|
|
525
|
-
/**
|
|
526
|
-
* [!!! INTERNAL USE, DO NOT USE IN YOUR CODE !!!]
|
|
527
|
-
* Expands the `dataBus` to double its current capacity.
|
|
528
|
-
*
|
|
529
|
-
* The underlying buffer is transferred to a larger `ArrayBuffer`, preserving
|
|
530
|
-
* existing data. All views associated with the buffer are recreated
|
|
531
|
-
* to reference the new storage.
|
|
532
|
-
*
|
|
533
|
-
* @returns {void}
|
|
534
|
-
* @throws {Error} If any block is borrowed via withView().
|
|
535
|
-
* @throws {RangeError} If the `dataBus` capacity has already exceeded the
|
|
536
|
-
* maximum supported limit.
|
|
537
|
-
* @private
|
|
538
|
-
*/
|
|
539
|
-
grow() {
|
|
540
|
-
this.assertNoActiveViews();
|
|
541
234
|
|
|
542
|
-
if (this.size > 0x80000000) throw new RangeError("Data bus capacity exceeded");
|
|
543
235
|
|
|
544
|
-
|
|
545
|
-
const newBuffer = this.dataBus.buffer.transfer(newSize);
|
|
236
|
+
// ========== GARBAGE COLLECTION ==========
|
|
546
237
|
|
|
547
|
-
|
|
548
|
-
this.dataBus = new Uint8Array(newBuffer);
|
|
549
|
-
this.dataAdapter = new DataView(newBuffer);
|
|
550
|
-
this.writer = this.dataAdapter[`set${this.bucketType}`].bind(this.dataAdapter);
|
|
551
|
-
this.reader = this.dataAdapter[`get${this.bucketType}`].bind(this.dataAdapter);
|
|
552
|
-
this.auxiliary = new this.bucketArray(newBuffer);
|
|
553
|
-
}
|
|
238
|
+
|
|
554
239
|
|
|
555
|
-
|
|
556
|
-
* [!!! INTERNAL USE, DO NOT USE IN YOUR CODE !!!]
|
|
557
|
-
* Expands the `controlBus` to double its current capacity.
|
|
558
|
-
*
|
|
559
|
-
* The underlying buffer is transferred to a larger `ArrayBuffer`, preserving
|
|
560
|
-
* existing headers. The `controlAdapter` is recreated to reference
|
|
561
|
-
* the new storage.
|
|
562
|
-
*
|
|
563
|
-
* @returns {void}
|
|
564
|
-
* @throws {RangeError} If the current `controlBus` capacity has already
|
|
565
|
-
* exceeded the maximum supported limit.
|
|
566
|
-
* @private
|
|
567
|
-
*/
|
|
568
|
-
headerGrow() {
|
|
569
|
-
if (this.controlBus.byteLength > 0x80000000) throw new RangeError("Control bus capacity exceeded");
|
|
570
|
-
const newSize = (this.controlBus.byteLength * 2) >>> 0;
|
|
571
|
-
const newBuffer = this.controlBus.buffer.transfer(newSize);
|
|
240
|
+
// ========== SCHEDULING ==========
|
|
572
241
|
|
|
573
|
-
this.controlBus = new Uint8Array(newBuffer);
|
|
574
|
-
this.controlAdapter = new DataView(newBuffer);
|
|
575
|
-
}
|
|
576
242
|
|
|
577
243
|
// ========== STACK OPERATIONS ==========
|
|
578
244
|
|
|
579
245
|
/**
|
|
580
|
-
*
|
|
246
|
+
* Adds an element to the end of the block's occupied sequence.
|
|
581
247
|
*
|
|
582
|
-
* The element is written
|
|
583
|
-
* element.
|
|
248
|
+
* The element is written to the first free position after the last occupied
|
|
249
|
+
* element. The block's occupancy is incremented only when space is available
|
|
584
250
|
* within its capacity.
|
|
585
251
|
*
|
|
586
|
-
* @param {number}
|
|
252
|
+
* @param {number} handler - Block handle.
|
|
253
|
+
* @param {number} value - Value to insert.
|
|
587
254
|
* @returns {boolean} `true` if the value was inserted; `false` if the block
|
|
588
|
-
*
|
|
589
|
-
* @throws {Error} If the handle is invalid
|
|
255
|
+
* has no available space.
|
|
256
|
+
* @throws {Error} If the handle is invalid or corrupted, or the block is borrowed by withView().
|
|
590
257
|
*
|
|
591
258
|
*/
|
|
592
259
|
push(handler, value) {
|
|
593
|
-
|
|
594
|
-
|
|
260
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
261
|
+
throw new Error("Invalid or corrupted handle");
|
|
262
|
+
}
|
|
595
263
|
|
|
596
|
-
|
|
264
|
+
const pack = this.bus[handler];
|
|
597
265
|
|
|
598
|
-
|
|
599
|
-
|
|
266
|
+
if (pack.borrowed) {
|
|
267
|
+
throw new Error("Block is borrowed");
|
|
268
|
+
}
|
|
600
269
|
|
|
270
|
+
if (pack.occupied >= pack.dataBus.length) return false;
|
|
271
|
+
pack.dataBus[pack.occupied++] = value;
|
|
601
272
|
return true;
|
|
602
273
|
}
|
|
603
274
|
|
|
604
275
|
/**
|
|
605
|
-
* Removes the last occupied element
|
|
276
|
+
* Removes and returns the last occupied element. If the block is empty,
|
|
277
|
+
* returns undefined without changing its occupancy.
|
|
606
278
|
*
|
|
607
|
-
*
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
*
|
|
611
|
-
* @param {number} handler - Block handle.
|
|
612
|
-
* @returns {number|undefined} Removed value or `undefined` if the block
|
|
613
|
-
* is empty.
|
|
614
|
-
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
279
|
+
* @param {number} handler Block handle.
|
|
280
|
+
* @returns {number|bigint|undefined} Value at the index immediately before
|
|
281
|
+
* the occupancy is decremented.
|
|
282
|
+
* @throws {Error} If the handle does not exist or the block is borrowed.
|
|
615
283
|
*/
|
|
616
284
|
pop(handler) {
|
|
617
|
-
|
|
618
|
-
|
|
285
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
286
|
+
throw new Error("Invalid or corrupted handle");
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const pack = this.bus[handler];
|
|
619
290
|
|
|
620
|
-
if (
|
|
291
|
+
if (pack.borrowed) {
|
|
292
|
+
throw new Error("Block is borrowed");
|
|
293
|
+
}
|
|
621
294
|
|
|
622
|
-
|
|
623
|
-
return
|
|
295
|
+
if (pack.occupied === 0) return undefined;
|
|
296
|
+
return pack.dataBus[--pack.occupied];
|
|
624
297
|
}
|
|
625
298
|
|
|
626
299
|
/**
|
|
627
300
|
* Returns the last occupied element without removing it.
|
|
628
301
|
*
|
|
629
302
|
* @param {number} handler - Block handle.
|
|
630
|
-
* @returns {number|undefined} Last occupied value or `undefined` if the
|
|
303
|
+
* @returns {number|undefined} Last occupied value, or `undefined` if the
|
|
631
304
|
* block is empty.
|
|
632
|
-
* @throws {Error} If the handle
|
|
305
|
+
* @throws {Error} If the handle does not exist or the block is borrowed.
|
|
633
306
|
*/
|
|
634
307
|
peek(handler) {
|
|
635
|
-
|
|
308
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
309
|
+
throw new Error("Invalid or corrupted handle");
|
|
310
|
+
}
|
|
636
311
|
|
|
637
|
-
|
|
312
|
+
const pack = this.bus[handler];
|
|
638
313
|
|
|
639
|
-
|
|
314
|
+
if (pack.borrowed) {
|
|
315
|
+
throw new Error("Block is borrowed");
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return pack.dataBus[pack.occupied - 1];
|
|
640
319
|
}
|
|
641
320
|
|
|
642
321
|
// ========== QUEUE OPERATIONS ==========
|
|
643
322
|
|
|
644
323
|
/**
|
|
645
|
-
*
|
|
324
|
+
* Shifts the occupied elements one position toward the end of the block.
|
|
646
325
|
*
|
|
647
|
-
*
|
|
648
|
-
*
|
|
649
|
-
*
|
|
326
|
+
* Runs copyWithin(1, 0, occupied) on the typed view. The
|
|
327
|
+
* value argument is not used, occupancy is not changed, and the method returns
|
|
328
|
+
* the typed view itself.
|
|
650
329
|
*
|
|
651
|
-
*
|
|
652
|
-
*
|
|
653
|
-
*
|
|
654
|
-
* @
|
|
655
|
-
* @param {number} value - Value to be inserted.
|
|
656
|
-
* @returns {boolean} `true` if the value was inserted; `false` if the block
|
|
657
|
-
* is full.
|
|
658
|
-
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
330
|
+
* @param {number} handler Block handle.
|
|
331
|
+
* @param {number} value Currently unused parameter.
|
|
332
|
+
* @returns {TypedArray} Typed view of the block.
|
|
333
|
+
* @throws {Error} If the handle does not exist or the block is borrowed.
|
|
659
334
|
*/
|
|
660
335
|
nudge(handler, value) {
|
|
661
|
-
|
|
662
|
-
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
663
|
-
|
|
664
|
-
if ((meta.occupied + 1) * this.wordSize > meta.byteCapacity) return false;
|
|
336
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
665
337
|
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
338
|
+
const pack = this.bus[handler];
|
|
339
|
+
if (pack.borrowed) throw new Error("Block is borrowed");
|
|
340
|
+
if (pack.occupied >= pack.dataBus.length) return false;
|
|
669
341
|
|
|
342
|
+
pack.dataBus.copyWithin(1, 0, pack.occupied);
|
|
343
|
+
pack.dataBus[0] = value;
|
|
344
|
+
pack.occupied++;
|
|
670
345
|
return true;
|
|
671
346
|
}
|
|
672
347
|
|
|
673
348
|
/**
|
|
674
|
-
*
|
|
675
|
-
*
|
|
676
|
-
* Remaining elements are shifted one position toward the beginning
|
|
677
|
-
* of the occupied region. The physical capacity of the block remains unchanged.
|
|
349
|
+
* Returns the value in the first slot and shifts the remaining occupied
|
|
350
|
+
* elements one position toward the beginning of the block.
|
|
678
351
|
*
|
|
679
|
-
* The
|
|
352
|
+
* The implementation does not decrement occupancy after shifting.
|
|
680
353
|
*
|
|
681
|
-
* @param {number} handler
|
|
682
|
-
* @returns {number|undefined}
|
|
683
|
-
* block is
|
|
684
|
-
* @throws {Error} If the handle is invalid, corrupted, or the block is borrowed via withView().
|
|
354
|
+
* @param {number} handler Block handle.
|
|
355
|
+
* @returns {number|bigint|undefined} Value stored in the first slot.
|
|
356
|
+
* @throws {Error} If the handle does not exist or the block is borrowed.
|
|
685
357
|
*/
|
|
686
358
|
pull(handler) {
|
|
687
|
-
|
|
688
|
-
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
359
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
689
360
|
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
this.dataBus.copyWithin(meta.dataAddress, meta.dataAddress + this.wordSize, meta.dataAddress + meta.occupied * this.wordSize);
|
|
694
|
-
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, meta.occupied - 1, IS_LITTLE_ENDIAN);
|
|
361
|
+
const pack = this.bus[handler];
|
|
362
|
+
if (pack.borrowed) throw new Error("Block is borrowed");
|
|
363
|
+
if (pack.occupied === 0) return undefined;
|
|
695
364
|
|
|
365
|
+
const result = pack.dataBus[0];
|
|
366
|
+
pack.dataBus.copyWithin(0, 1, pack.occupied);
|
|
367
|
+
pack.occupied--;
|
|
696
368
|
return result;
|
|
697
369
|
}
|
|
698
370
|
|
|
699
371
|
/**
|
|
700
|
-
* Returns the
|
|
372
|
+
* Returns the value stored in the block's first slot.
|
|
701
373
|
*
|
|
702
|
-
*
|
|
703
|
-
*
|
|
704
|
-
*
|
|
705
|
-
* @
|
|
374
|
+
* Reading does not change occupancy. The method does not check the borrowed flag.
|
|
375
|
+
*
|
|
376
|
+
* @param {number} handler Block handle.
|
|
377
|
+
* @returns {number|bigint|undefined} Value in the first slot.
|
|
378
|
+
* @throws {Error} If the handle does not exist.
|
|
706
379
|
*/
|
|
707
380
|
head(handler) {
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
381
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
382
|
+
throw new Error("Invalid or corrupted handle");
|
|
383
|
+
}
|
|
711
384
|
|
|
712
|
-
return this.
|
|
385
|
+
return this.bus[handler].dataBus[0];
|
|
713
386
|
}
|
|
714
387
|
|
|
715
388
|
// ========== RANDOM ACCESS ==========
|
|
716
389
|
|
|
717
390
|
/**
|
|
718
|
-
* Checks
|
|
391
|
+
* Checks whether a value is present among the block's occupied elements.
|
|
719
392
|
*
|
|
720
393
|
* The search is performed directly on the auxiliary `TypedArray` and considers
|
|
721
|
-
* only the currently occupied range, not the entire
|
|
394
|
+
* only the currently occupied range, not the entire physical capacity
|
|
395
|
+
* reserved.
|
|
722
396
|
*
|
|
723
397
|
* @param {number} handler - Block handle.
|
|
724
398
|
* @param {number} value - Value to search for.
|
|
725
|
-
* @returns {boolean} `true` if `value` is present among the
|
|
726
|
-
* elements; otherwise, `false`.
|
|
399
|
+
* @returns {boolean} `true` if `value` is present among the
|
|
400
|
+
* occupied elements; otherwise, `false`.
|
|
727
401
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
728
402
|
*/
|
|
729
403
|
has(handler, value) {
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
404
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
405
|
+
throw new Error("Invalid or corrupted handle");
|
|
406
|
+
}
|
|
735
407
|
|
|
736
|
-
|
|
408
|
+
const pack = this.bus[handler];
|
|
409
|
+
return pack.dataBus.subarray(0, pack.occupied).includes(value);
|
|
737
410
|
}
|
|
738
411
|
|
|
739
412
|
/**
|
|
740
|
-
*
|
|
741
|
-
*
|
|
742
|
-
* The search is performed in ascending order of indices, using the `indexOf`
|
|
743
|
-
* operation of the underlying TypedArray. The search exclusively considers
|
|
744
|
-
* occupied elements, ignoring the block's unused capacity.
|
|
413
|
+
* Finds the first occurrence of an element in the block's occupied sequence.
|
|
745
414
|
*
|
|
746
|
-
* The starting search position can be specified by `fromIndex`,
|
|
747
|
-
* following TypedArray indexing rules.
|
|
748
415
|
*
|
|
749
416
|
* @param {number} handler - Block handle.
|
|
750
|
-
* @param {number|bigint} searchElement - Element to
|
|
751
|
-
* @param {number} [fromIndex=0] - Index from which
|
|
752
|
-
* @returns {number} Index of the first occurrence or `-1` if the
|
|
753
|
-
* element is not found or
|
|
417
|
+
* @param {number|bigint} searchElement - Element to find.
|
|
418
|
+
* @param {number} [fromIndex=0] - Index from which the search starts.
|
|
419
|
+
* @returns {number} Index of the first occurrence, or `-1` if the
|
|
420
|
+
* element is not found or the block is empty.
|
|
754
421
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
755
422
|
*/
|
|
756
423
|
indexOf(handler, searchElement, fromIndex = 0) {
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
424
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
425
|
+
throw new Error("Invalid or corrupted handle");
|
|
426
|
+
}
|
|
762
427
|
|
|
763
|
-
|
|
428
|
+
const pack = this.bus[handler];
|
|
429
|
+
return pack.dataBus.subarray(0, pack.occupied).indexOf(searchElement, fromIndex);
|
|
764
430
|
}
|
|
765
431
|
|
|
766
432
|
/**
|
|
767
|
-
*
|
|
768
|
-
*
|
|
769
|
-
* The search is performed in descending order of indices, using the `lastIndexOf`
|
|
770
|
-
* operation of the underlying TypedArray. The search exclusively considers
|
|
771
|
-
* occupied elements, ignoring the block's unused capacity.
|
|
433
|
+
* Finds the last occurrence of an element in the block's occupied sequence.
|
|
772
434
|
*
|
|
773
|
-
*
|
|
774
|
-
* following TypedArray indexing rules. By default, the search starts
|
|
775
|
-
* at the last occupied element.
|
|
435
|
+
* By default, the search starts at the last occupied element.
|
|
776
436
|
*
|
|
777
437
|
* @param {number} handler - Block handle.
|
|
778
|
-
* @param {number|bigint} searchElement - Element to
|
|
779
|
-
* @param {number} [fromIndex=-1] - Index from which
|
|
780
|
-
* @returns {number} Index of the last occurrence or `-1` if the
|
|
781
|
-
* element is not found or
|
|
438
|
+
* @param {number|bigint} searchElement - Element to find.
|
|
439
|
+
* @param {number} [fromIndex=-1] - Index from which the search starts.
|
|
440
|
+
* @returns {number} Index of the last occurrence, or `-1` if the
|
|
441
|
+
* element is not found or the block is empty.
|
|
782
442
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
783
443
|
*/
|
|
784
444
|
lastIndexOf(handler, searchElement, fromIndex = -1) {
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
const rawAddres = meta.dataAddress >>> this.wordShift;
|
|
445
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
446
|
+
throw new Error("Invalid or corrupted handle");
|
|
447
|
+
}
|
|
790
448
|
|
|
791
|
-
|
|
449
|
+
const pack = this.bus[handler];
|
|
450
|
+
return pack.dataBus.subarray(0, pack.occupied).lastIndexOf(searchElement, fromIndex);
|
|
792
451
|
}
|
|
793
452
|
|
|
794
453
|
|
|
795
454
|
/**
|
|
796
455
|
* Returns the occupied element at a zero-based index.
|
|
797
456
|
*
|
|
798
|
-
* Indices outside the
|
|
457
|
+
* Indices outside the `[0, occupied)` range are not considered errors and
|
|
799
458
|
* result in `undefined`.
|
|
800
459
|
*
|
|
801
460
|
* @param {number} handler - Block handle.
|
|
802
461
|
* @param {number} index - Zero-based index of the element.
|
|
803
|
-
* @returns {number|undefined} Value stored at the index or `undefined` if
|
|
462
|
+
* @returns {number|undefined} Value stored at the index, or `undefined` if
|
|
804
463
|
* the index is outside the occupied region.
|
|
805
464
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
806
465
|
*/
|
|
807
466
|
get(handler, index) {
|
|
467
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
468
|
+
throw new Error("Invalid or corrupted handle");
|
|
469
|
+
}
|
|
808
470
|
|
|
809
|
-
const
|
|
471
|
+
const pack = this.bus[handler];
|
|
810
472
|
|
|
811
|
-
if (index < 0 || index >=
|
|
473
|
+
if (index < 0 || index >= pack.occupied) return undefined;
|
|
812
474
|
|
|
813
|
-
return
|
|
475
|
+
return pack.dataBus[index];
|
|
814
476
|
}
|
|
815
477
|
|
|
816
478
|
/**
|
|
817
479
|
* Replaces the value stored at an occupied index.
|
|
818
480
|
*
|
|
819
|
-
* The operation does not
|
|
820
|
-
* `[0, occupied)` are ignored and result in `false`.
|
|
481
|
+
* The operation does not change block occupancy. Indices outside the
|
|
482
|
+
* `[0, occupied)` range are ignored and result in `false`.
|
|
821
483
|
*
|
|
822
484
|
* @param {number} handler - Block handle.
|
|
823
485
|
* @param {number} index - Zero-based index of the element.
|
|
824
486
|
* @param {number} value - New value.
|
|
825
487
|
* @returns {boolean} `true` if the value was written; `false` if the index
|
|
826
488
|
* is outside the occupied region.
|
|
827
|
-
* @throws {Error} If the handle is invalid
|
|
489
|
+
* @throws {Error} If the handle is invalid or corrupted, or the block is borrowed by withView().
|
|
828
490
|
*/
|
|
829
491
|
set(handler, index, value) {
|
|
830
|
-
|
|
831
|
-
|
|
492
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
493
|
+
throw new Error("Invalid or corrupted handle");
|
|
494
|
+
}
|
|
832
495
|
|
|
833
|
-
|
|
496
|
+
const pack = this.bus[handler];
|
|
834
497
|
|
|
835
|
-
|
|
498
|
+
if (pack.borrowed) {
|
|
499
|
+
throw new Error("Block is borrowed");
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (index < 0 || index >= pack.occupied) return false;
|
|
503
|
+
|
|
504
|
+
pack.dataBus[index] = value;
|
|
836
505
|
return true;
|
|
837
506
|
}
|
|
838
507
|
|
|
839
508
|
// ============= MISCELLANEOUS ===============
|
|
840
509
|
|
|
841
510
|
/**
|
|
842
|
-
*
|
|
843
|
-
*
|
|
511
|
+
* Resets the block's occupancy and fills its entire physical capacity with
|
|
512
|
+
* the same value.
|
|
844
513
|
*
|
|
845
|
-
*
|
|
846
|
-
* occupied elements. After the operation, the block is considered
|
|
847
|
-
* empty regardless of the value used
|
|
514
|
+
* Filling covers all available slots, not just the
|
|
515
|
+
* currently occupied elements. After the operation, the block is considered
|
|
516
|
+
* empty regardless of the value used to fill it.
|
|
848
517
|
*
|
|
849
518
|
* @param {number} handler - Block handle.
|
|
850
519
|
* @param {number} [value=0] - Value used to fill all slots.
|
|
851
520
|
* @returns {void}
|
|
852
|
-
* @throws {Error} If the handle is invalid
|
|
521
|
+
* @throws {Error} If the handle is invalid or corrupted, or the block is borrowed by withView().
|
|
853
522
|
*/
|
|
854
523
|
clear(handler, value = 0) {
|
|
855
|
-
|
|
856
|
-
|
|
524
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
525
|
+
throw new Error("Invalid or corrupted handle");
|
|
526
|
+
}
|
|
857
527
|
|
|
858
|
-
const
|
|
859
|
-
const rawAddress = meta.dataAddress >>> this.wordShift;
|
|
528
|
+
const pack = this.bus[handler];
|
|
860
529
|
|
|
861
|
-
|
|
862
|
-
|
|
530
|
+
if (pack.borrowed) {
|
|
531
|
+
throw new Error("Block is borrowed");
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
pack.dataBus.fill(value);
|
|
535
|
+
pack.occupied = 0;
|
|
863
536
|
}
|
|
864
537
|
|
|
865
538
|
|
|
866
539
|
/**
|
|
867
|
-
* Fills
|
|
868
|
-
*
|
|
869
|
-
* The
|
|
870
|
-
*
|
|
871
|
-
*
|
|
872
|
-
*
|
|
873
|
-
*
|
|
874
|
-
*
|
|
875
|
-
*
|
|
876
|
-
*
|
|
877
|
-
* @param {number}
|
|
878
|
-
* @param {number}
|
|
879
|
-
* @param {number} [start=0] - Initial index, inclusive.
|
|
880
|
-
* @param {number|null} [end=null] - Final index, exclusive. When
|
|
881
|
-
* omitted or `null`, uses the block's total capacity.
|
|
540
|
+
* Fills the typed view using the supplied bounds.
|
|
541
|
+
*
|
|
542
|
+
* The operation delegates filling to TypedArray.prototype.fill().
|
|
543
|
+
* Because end defaults to null and is passed explicitly, it is
|
|
544
|
+
* converted to zero by the native API; therefore, the default call
|
|
545
|
+
* normally fills no elements. Regardless of the range,
|
|
546
|
+
* the implementation sets occupancy to the view's total length.
|
|
547
|
+
*
|
|
548
|
+
* @param {number} handler Block handle.
|
|
549
|
+
* @param {number|bigint} value Value used to fill the view.
|
|
550
|
+
* @param {number} [start=0] Starting index.
|
|
551
|
+
* @param {number} [end=pack.dataBus.length] Exclusive end index.
|
|
882
552
|
* @returns {void}
|
|
883
|
-
* @throws {Error} If the handle
|
|
884
|
-
* @throws {RangeError} If `start` or `end` is outside capacity limits,
|
|
885
|
-
* or if `start > end`.
|
|
553
|
+
* @throws {Error} If the handle does not exist or the block is borrowed.
|
|
886
554
|
*/
|
|
887
|
-
fill(handler, value, start = 0, end =
|
|
888
|
-
|
|
889
|
-
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
555
|
+
fill(handler, value, start = 0, end = undefined) {
|
|
556
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
890
557
|
|
|
891
|
-
const
|
|
892
|
-
|
|
558
|
+
const pack = this.bus[handler];
|
|
559
|
+
if (pack.borrowed) throw new Error("Block is borrowed");
|
|
893
560
|
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
if (start < 0 || start > slots) {
|
|
898
|
-
throw new RangeError(`start ${start} out of range [0, ${slots}].`);
|
|
899
|
-
}
|
|
900
|
-
if (lastIndex < 0 || lastIndex > slots) {
|
|
901
|
-
throw new RangeError(`end ${lastIndex} out of range [0, ${slots}].`);
|
|
561
|
+
const finalEnd = end === undefined ? pack.dataBus.length : end;
|
|
562
|
+
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(finalEnd) || start < 0 || finalEnd < start || finalEnd > pack.dataBus.length) {
|
|
563
|
+
throw new RangeError("Invalid fill range");
|
|
902
564
|
}
|
|
903
565
|
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
this.controlAdapter.setUint32(meta.headerAddress + OCCUPIED_OFFSET, lastIndex, IS_LITTLE_ENDIAN);
|
|
566
|
+
pack.dataBus.fill(value, start, finalEnd);
|
|
567
|
+
pack.occupied = Math.max(pack.occupied, finalEnd);
|
|
907
568
|
}
|
|
908
569
|
|
|
909
570
|
/**
|
|
910
|
-
* Fills all physical slots
|
|
571
|
+
* Fills all physical slots in the block with a value and marks the entire
|
|
911
572
|
* capacity as occupied.
|
|
912
573
|
*
|
|
913
|
-
* Unlike `clear()`, this operation results in a fully
|
|
574
|
+
* Unlike `clear()`, this operation results in a fully
|
|
575
|
+
* occupied block.
|
|
914
576
|
*
|
|
915
577
|
* @param {number} handler - Block handle.
|
|
916
|
-
* @param {number} value - Value used
|
|
578
|
+
* @param {number} value - Value used to fill the block.
|
|
917
579
|
* @returns {void}
|
|
918
|
-
* @throws {Error} If the handle is invalid
|
|
580
|
+
* @throws {Error} If the handle is invalid or corrupted, or the block is borrowed by withView().
|
|
919
581
|
*/
|
|
920
582
|
fillAll(handler, value) {
|
|
921
|
-
|
|
922
|
-
|
|
583
|
+
if (!Object.hasOwn(this.bus, handler)) {
|
|
584
|
+
throw new Error("Invalid or corrupted handle");
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const pack = this.bus[handler];
|
|
923
588
|
|
|
924
|
-
|
|
925
|
-
|
|
589
|
+
if (pack.borrowed) {
|
|
590
|
+
throw new Error("Block is borrowed");
|
|
591
|
+
}
|
|
926
592
|
|
|
927
|
-
|
|
928
|
-
|
|
593
|
+
pack.dataBus.fill(value);
|
|
594
|
+
pack.occupied = pack.dataBus.length;
|
|
929
595
|
}
|
|
930
596
|
|
|
931
597
|
/**
|
|
932
598
|
* Copies elements from an `Array` or `TypedArray` to the beginning of the block.
|
|
933
599
|
*
|
|
934
|
-
* The copied
|
|
935
|
-
* updated to the greater
|
|
936
|
-
*
|
|
937
|
-
* occupancy when
|
|
600
|
+
* The copied amount is limited by the block's capacity. Occupancy is
|
|
601
|
+
* updated to the greater of the previous occupancy and the amount
|
|
602
|
+
* actually copied; therefore, a call to `put()` does not reduce
|
|
603
|
+
* existing occupancy when given a shorter sequence.
|
|
938
604
|
*
|
|
939
|
-
* For `Array`, only the first elements that fit are copied.
|
|
940
|
-
* For `TypedArray`, a source
|
|
605
|
+
* For an `Array`, only the first elements that fit are copied.
|
|
606
|
+
* For a `TypedArray`, a subarray of the source is used without an
|
|
607
|
+
* intermediate copy.
|
|
941
608
|
*
|
|
942
609
|
* @param {number} handler - Block handle.
|
|
943
610
|
* @param {Array|TypedArray} data - Sequence of values to copy.
|
|
944
611
|
* @returns {void}
|
|
945
|
-
* @throws {Error} If the handle is invalid
|
|
612
|
+
* @throws {Error} If the handle is invalid or corrupted, or the block is borrowed by withView().
|
|
946
613
|
* @throws {TypeError} If `data` is neither an `Array` nor a `TypedArray`.
|
|
947
614
|
*/
|
|
948
615
|
put(handler, data) {
|
|
949
|
-
|
|
950
|
-
if (meta.borrowed) throw new Error("Block is borrowed");
|
|
616
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
951
617
|
|
|
952
|
-
const
|
|
953
|
-
|
|
618
|
+
const pack = this.bus[handler];
|
|
619
|
+
if (pack.borrowed) throw new Error("Block is borrowed");
|
|
954
620
|
|
|
955
|
-
if (Array.isArray(data)) {
|
|
956
|
-
|
|
957
|
-
} else if (isTypedArray(data)) {
|
|
958
|
-
this.auxiliary.set(data.subarray(0, length), rawAddress);
|
|
959
|
-
} else {
|
|
960
|
-
const actual = data?.constructor?.name ?? (data === null ? 'null' : typeof data);
|
|
621
|
+
if (!Array.isArray(data) && !isTypedArray(data)) {
|
|
622
|
+
const actual = data?.constructor?.name ?? (data === null ? "null" : typeof data);
|
|
961
623
|
throw new TypeError(`data must be Array or TypedArray, but ${actual} was given.`);
|
|
962
624
|
}
|
|
963
625
|
|
|
964
|
-
|
|
626
|
+
const length = Math.min(pack.dataBus.length, data.length);
|
|
627
|
+
|
|
628
|
+
if (Array.isArray(data)) pack.dataBus.set(data.slice(0, length), 0);
|
|
629
|
+
else pack.dataBus.set(data.subarray(0, length), 0);
|
|
630
|
+
|
|
631
|
+
pack.occupied = Math.max(length, pack.occupied);
|
|
965
632
|
}
|
|
966
633
|
|
|
967
634
|
/**
|
|
@@ -970,45 +637,43 @@ export default class Balloc {
|
|
|
970
637
|
* The result is a `TypedArray` of the same type configured by
|
|
971
638
|
* `bucketSize`. Unoccupied physical capacity is not included in the result.
|
|
972
639
|
*
|
|
973
|
-
* The operation does not
|
|
974
|
-
*
|
|
975
|
-
*
|
|
640
|
+
* The operation does not expose the internal storage directly:
|
|
641
|
+
* `TypedArray.prototype.slice()` produces a copy with independent
|
|
642
|
+
* storage.
|
|
976
643
|
*
|
|
977
644
|
* @param {number} handler - Block handle.
|
|
978
|
-
* @returns {TypedArray} Copy of elements occupied by the block. When
|
|
645
|
+
* @returns {TypedArray} Copy of the elements occupied by the block. When
|
|
979
646
|
* the block is empty, returns an empty `TypedArray` of the configured type.
|
|
980
647
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
981
648
|
*/
|
|
982
649
|
pick(handler) {
|
|
983
|
-
|
|
650
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
984
651
|
|
|
985
|
-
const
|
|
986
|
-
|
|
987
|
-
if (meta.occupied === 0) {
|
|
988
|
-
return this.auxiliary.slice(rawAddress, rawAddress);
|
|
989
|
-
}
|
|
990
|
-
|
|
991
|
-
return this.auxiliary.slice(rawAddress, rawAddress + meta.occupied);
|
|
652
|
+
const pack = this.bus[handler];
|
|
653
|
+
return pack.dataBus.slice(0, pack.occupied);
|
|
992
654
|
}
|
|
993
655
|
|
|
994
|
-
|
|
995
|
-
* Executes a callback
|
|
996
|
-
*
|
|
997
|
-
* The
|
|
998
|
-
*
|
|
999
|
-
*
|
|
1000
|
-
*
|
|
1001
|
-
*
|
|
1002
|
-
*
|
|
1003
|
-
*
|
|
1004
|
-
*
|
|
1005
|
-
* @param {...(number|Function)} args
|
|
1006
|
-
* @returns {*}
|
|
1007
|
-
* @throws {TypeError} If
|
|
1008
|
-
*
|
|
656
|
+
/**
|
|
657
|
+
* Executes a synchronous callback with direct subviews of one or more blocks.
|
|
658
|
+
*
|
|
659
|
+
* The arguments contain one or more handles, followed by a function.
|
|
660
|
+
* Each view covers only the occupied elements and shares the block's
|
|
661
|
+
* storage. The blocks are marked as borrowed during
|
|
662
|
+
* execution and released in finally, even if the callback throws
|
|
663
|
+
* an exception. Duplicate handles are not allowed.
|
|
664
|
+
*
|
|
665
|
+
* The views must not be retained or used after the callback returns.
|
|
666
|
+
*
|
|
667
|
+
* @param {...(number|Function)} args Handles followed by the callback.
|
|
668
|
+
* @returns {*} Value returned by the callback.
|
|
669
|
+
* @throws {TypeError} If a handle or callback is missing, or if the last
|
|
670
|
+
* argument is not a function.
|
|
671
|
+
* @throws {Error} If any handle does not exist, is duplicated,
|
|
672
|
+
* or is already borrowed.
|
|
1009
673
|
*/
|
|
1010
674
|
withView(...args) {
|
|
1011
675
|
if (args.length < 2) throw new TypeError("At least one handler and one callback are required");
|
|
676
|
+
|
|
1012
677
|
const lastIndex = args.length - 1;
|
|
1013
678
|
const callback = args[lastIndex];
|
|
1014
679
|
if (typeof callback !== "function") throw new TypeError("callback must be a function");
|
|
@@ -1019,44 +684,41 @@ export default class Balloc {
|
|
|
1019
684
|
|
|
1020
685
|
try {
|
|
1021
686
|
for (let i = 0; i < lastIndex; i++) {
|
|
1022
|
-
const
|
|
1023
|
-
const headerAddr = metadata.headerAddress;
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
if (Object.hasOwn(lookup, headerAddr)) throw new Error("Duplicate handler");
|
|
1027
|
-
if (metadata.borrowed) throw new Error("Block is already borrowed");
|
|
687
|
+
const handler = args[i];
|
|
1028
688
|
|
|
1029
|
-
this.
|
|
689
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
690
|
+
if (Object.hasOwn(lookup, handler)) throw new Error("Duplicate handler");
|
|
1030
691
|
|
|
1031
|
-
const
|
|
1032
|
-
|
|
1033
|
-
acquired.push(headerAddr);
|
|
692
|
+
const pack = this.bus[handler];
|
|
693
|
+
if (pack.borrowed) throw new Error("Block is already borrowed");
|
|
1034
694
|
|
|
1035
|
-
|
|
695
|
+
pack.borrowed = true;
|
|
696
|
+
lookup[handler] = true;
|
|
697
|
+
acquired.push(pack);
|
|
698
|
+
views.push(pack.dataBus.subarray(0, pack.occupied));
|
|
1036
699
|
}
|
|
700
|
+
|
|
1037
701
|
return callback(...views);
|
|
1038
702
|
} finally {
|
|
1039
|
-
for (let i = 0; i < acquired.length; i++)
|
|
1040
|
-
this.controlBus[acquired[i] + FLAGS_OFFSET] &= ~VIEW_LOCK;
|
|
1041
|
-
}
|
|
703
|
+
for (let i = 0; i < acquired.length; i++) acquired[i].borrowed = false;
|
|
1042
704
|
}
|
|
1043
705
|
}
|
|
1044
|
-
|
|
1045
706
|
// ======== CAPACITY IN SLOTS ==========
|
|
1046
707
|
|
|
1047
708
|
/**
|
|
1048
|
-
* Returns the physical capacity
|
|
709
|
+
* Returns the block's physical capacity in bytes.
|
|
1049
710
|
*
|
|
1050
|
-
* Capacity
|
|
1051
|
-
* block, regardless of how many elements are currently
|
|
711
|
+
* Capacity is the space reserved for all slots in the
|
|
712
|
+
* block, regardless of how many elements are currently
|
|
713
|
+
* occupied.
|
|
1052
714
|
*
|
|
1053
715
|
* @param {number} handler - Block handle.
|
|
1054
716
|
* @returns {number} Physical capacity of the block, in bytes.
|
|
1055
717
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
1056
718
|
*/
|
|
1057
719
|
byteSize(handler) {
|
|
1058
|
-
|
|
1059
|
-
return
|
|
720
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
721
|
+
return this.bus[handler].dataBus.byteLength;
|
|
1060
722
|
}
|
|
1061
723
|
|
|
1062
724
|
/**
|
|
@@ -1067,31 +729,30 @@ export default class Balloc {
|
|
|
1067
729
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
1068
730
|
*/
|
|
1069
731
|
occupied(handler) {
|
|
1070
|
-
|
|
1071
|
-
return
|
|
1072
|
-
|
|
732
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
733
|
+
return this.bus[handler].occupied;
|
|
1073
734
|
}
|
|
1074
735
|
|
|
1075
736
|
/**
|
|
1076
|
-
* Returns the block
|
|
737
|
+
* Returns the block capacity as a number of elements.
|
|
1077
738
|
*
|
|
1078
739
|
* The value corresponds to the total number of physically reserved slots,
|
|
1079
|
-
* not the currently occupied
|
|
740
|
+
* not the number currently occupied.
|
|
1080
741
|
*
|
|
1081
742
|
* @param {number} handler - Block handle.
|
|
1082
743
|
* @returns {number} Total number of slots available in the block.
|
|
1083
744
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
1084
745
|
*/
|
|
1085
746
|
length(handler) {
|
|
1086
|
-
|
|
1087
|
-
return
|
|
747
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
748
|
+
return this.bus[handler].dataBus.length;
|
|
1088
749
|
}
|
|
1089
750
|
|
|
1090
751
|
/**
|
|
1091
|
-
* Checks
|
|
752
|
+
* Checks whether the memory block has reached its allocation capacity.
|
|
1092
753
|
*
|
|
1093
754
|
* A block is considered full when the number of occupied elements
|
|
1094
|
-
* equals its total allocated capacity, leaving no positions
|
|
755
|
+
* equals its total allocated capacity, leaving no available positions
|
|
1095
756
|
* to store new elements.
|
|
1096
757
|
*
|
|
1097
758
|
* @param {number} handler - Block handle.
|
|
@@ -1099,51 +760,44 @@ export default class Balloc {
|
|
|
1099
760
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
1100
761
|
*/
|
|
1101
762
|
isFull(handler) {
|
|
1102
|
-
|
|
1103
|
-
|
|
763
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
764
|
+
const pack = this.bus[handler];
|
|
765
|
+
return pack.occupied === pack.dataBus.length;
|
|
1104
766
|
}
|
|
1105
767
|
|
|
1106
|
-
// ==========
|
|
768
|
+
// ==========OPERATIONS BETWEEN BLOCKS=============
|
|
1107
769
|
|
|
1108
770
|
/**
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
* @param {number} destination - Destination block handle.
|
|
1124
|
-
* @param {number} [slots=UNKNOWN] - Maximum number of elements to copy.
|
|
1125
|
-
* `UNKNOWN` represents the sentinel value used not to impose an
|
|
1126
|
-
* additional limit.
|
|
1127
|
-
* @returns {Uint8Array} The `dataBus` after the operation.
|
|
1128
|
-
* @throws {Error} If handles are invalid, corrupted,
|
|
1129
|
-
* or the destination block is borrowed via withView().
|
|
1130
|
-
*/
|
|
771
|
+
* Copies occupied elements from one block to the beginning of another.
|
|
772
|
+
*
|
|
773
|
+
* The copied amount is the minimum of the source occupancy, destination
|
|
774
|
+
* capacity, and slots. The copy uses TypedArray.prototype.set(), converting
|
|
775
|
+
* values according to the destination view type. Destination occupancy
|
|
776
|
+
* becomes the greater of the previous occupancy and the number of
|
|
777
|
+
* copied elements.
|
|
778
|
+
*
|
|
779
|
+
* @param {number} source Handle of the source block.
|
|
780
|
+
* @param {number} destination Handle of the destination block.
|
|
781
|
+
* @param {number} [slots=UNKNOWN] Maximum number of elements to copy.
|
|
782
|
+
* @returns {TypedArray} Typed view of the destination block.
|
|
783
|
+
* @throws {Error} If either handle does not exist or the destination is borrowed.
|
|
784
|
+
*/
|
|
1131
785
|
copy(source, destination, slots = UNKNOWN) {
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
/** @type {Metadata} */
|
|
1135
|
-
const metaB = {};
|
|
786
|
+
if (!Object.hasOwn(this.bus, source)) throw new Error("Invalid or corrupted handle");
|
|
787
|
+
if (!Object.hasOwn(this.bus, destination)) throw new Error("Invalid or corrupted handle");
|
|
1136
788
|
|
|
1137
|
-
this.
|
|
1138
|
-
this.
|
|
1139
|
-
if (metaB.borrowed) throw new Error("Block is borrowed");
|
|
789
|
+
const src = this.bus[source];
|
|
790
|
+
const dest = this.bus[destination];
|
|
1140
791
|
|
|
1141
|
-
|
|
1142
|
-
const slotsToCopy = Math.min(metaA.occupied, maxSlotsDest, slots);
|
|
1143
|
-
const newOccupied = Math.max(metaB.occupied, slotsToCopy);
|
|
792
|
+
if (dest.borrowed) throw new Error("Block is borrowed");
|
|
1144
793
|
|
|
1145
|
-
|
|
1146
|
-
|
|
794
|
+
if (!Number.isSafeInteger(slots) || slots < 0) throw new RangeError("Invalid copy slot count");
|
|
795
|
+
|
|
796
|
+
const count = Math.min(src.occupied, dest.dataBus.length, slots);
|
|
797
|
+
dest.dataBus.set(src.dataBus.subarray(0, count), 0);
|
|
798
|
+
dest.occupied = Math.max(dest.occupied, count);
|
|
799
|
+
|
|
800
|
+
return dest.dataBus;
|
|
1147
801
|
}
|
|
1148
802
|
|
|
1149
803
|
/**
|
|
@@ -1151,29 +805,34 @@ export default class Balloc {
|
|
|
1151
805
|
*
|
|
1152
806
|
* Indices are zero-based and follow the semantics of
|
|
1153
807
|
* `Array.prototype.copyWithin()`: `target` identifies the destination position,
|
|
1154
|
-
* `start` the starting
|
|
808
|
+
* `start` the source starting position, and `length` the maximum number of
|
|
1155
809
|
* elements to copy.
|
|
1156
810
|
*
|
|
1157
|
-
* Negative indices are interpreted
|
|
1158
|
-
* After
|
|
1159
|
-
* The copied
|
|
1160
|
-
* and
|
|
811
|
+
* Negative indices are interpreted relative to the end of the block's
|
|
812
|
+
* capacity. After normalization, the indices are clamped to the physical
|
|
813
|
+
* capacity. The copied amount is also limited by the elements
|
|
814
|
+
* actually occupied and by the space available from `target`.
|
|
1161
815
|
*
|
|
1162
|
-
* The operation supports overlapping regions and can increase block
|
|
1163
|
-
* when the destination range
|
|
816
|
+
* The operation supports overlapping regions and can increase the block's
|
|
817
|
+
* occupancy when the destination range extends beyond the current occupancy.
|
|
1164
818
|
*
|
|
1165
819
|
* @param {number} handler - Block handle.
|
|
1166
|
-
* @param {number} target -
|
|
1167
|
-
* @param {number} start -
|
|
820
|
+
* @param {number} target - Starting destination index.
|
|
821
|
+
* @param {number} start - Starting source index.
|
|
1168
822
|
* @param {number} length - Maximum number of elements to copy.
|
|
1169
823
|
* @returns {Uint8Array} The `dataBus` after the operation.
|
|
1170
|
-
* @throws {Error} If the handle is invalid
|
|
824
|
+
* @throws {Error} If the handle is invalid or corrupted, or the block is borrowed by withView().
|
|
1171
825
|
*/
|
|
1172
826
|
copyWithin(handler, target, start, length) {
|
|
1173
|
-
|
|
1174
|
-
|
|
827
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
828
|
+
|
|
829
|
+
const pack = this.bus[handler];
|
|
830
|
+
if (pack.borrowed) throw new Error("Block is borrowed");
|
|
831
|
+
|
|
832
|
+
const slots = pack.dataBus.length;
|
|
833
|
+
|
|
834
|
+
if (![target, start, length].every(Number.isFinite)) throw new RangeError("Invalid copyWithin range");
|
|
1175
835
|
|
|
1176
|
-
const slots = Math.floor(meta.byteCapacity / this.wordSize);
|
|
1177
836
|
target = Math.trunc(target);
|
|
1178
837
|
start = Math.trunc(start);
|
|
1179
838
|
length = Math.trunc(length);
|
|
@@ -1184,436 +843,229 @@ export default class Balloc {
|
|
|
1184
843
|
target = Math.max(0, Math.min(target, slots));
|
|
1185
844
|
start = Math.max(0, Math.min(start, slots));
|
|
1186
845
|
|
|
1187
|
-
|
|
1188
|
-
const maxWrite = slots - target;
|
|
1189
|
-
length = Math.max(0, Math.min(length, maxRead, maxWrite));
|
|
846
|
+
length = Math.max(0, Math.min(length, pack.occupied - start, slots - target));
|
|
1190
847
|
|
|
1191
|
-
if (length === 0) return
|
|
848
|
+
if (length === 0) return pack.dataBus;
|
|
1192
849
|
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
const newOccupied = Math.max(meta.occupied, target + length);
|
|
850
|
+
pack.dataBus.copyWithin(target, start, start + length);
|
|
851
|
+
pack.occupied = Math.max(pack.occupied, target + length);
|
|
1196
852
|
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
return this.dataBus.copyWithin(targetAddress, sourceAddress, sourceAddress + length * this.wordSize);
|
|
853
|
+
return pack.dataBus;
|
|
1200
854
|
}
|
|
1201
855
|
|
|
1202
856
|
/**
|
|
1203
|
-
* Appends occupied elements of `source` to the end of `target`.
|
|
857
|
+
* Appends the occupied elements of `source` to the end of `target`.
|
|
1204
858
|
*
|
|
1205
|
-
* The copied
|
|
1206
|
-
* source and remaining free space in the destination
|
|
859
|
+
* The copied amount is limited by the number of occupied elements in the
|
|
860
|
+
* source and by the remaining free space in the destination capacity.
|
|
1207
861
|
*
|
|
1208
|
-
* The operation supports `target` and `source`
|
|
1209
|
-
* In this case,
|
|
1210
|
-
* preserves `copyWithin()
|
|
862
|
+
* The operation supports `target` and `source` referring to the same block.
|
|
863
|
+
* In this case, the copy is performed within the block itself and
|
|
864
|
+
* preserves the overlapping-region semantics of `copyWithin()`.
|
|
1211
865
|
*
|
|
1212
|
-
* @param {number} target - Handle of the block that will receive elements.
|
|
866
|
+
* @param {number} target - Handle of the block that will receive the elements.
|
|
1213
867
|
* @param {number} source - Handle of the block whose elements will be
|
|
1214
868
|
* appended.
|
|
1215
|
-
* @returns {number} Number of elements
|
|
1216
|
-
* @throws {Error} If handles are invalid
|
|
1217
|
-
* or the destination block is borrowed
|
|
869
|
+
* @returns {number} Number of elements actually appended.
|
|
870
|
+
* @throws {Error} If the handles are invalid or corrupted
|
|
871
|
+
* or the destination block is borrowed by withView().
|
|
1218
872
|
*/
|
|
1219
873
|
append(target, source) {
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
this.extractMetadata(target, metaTarget);
|
|
1224
|
-
this.extractMetadata(source, metaSource);
|
|
1225
|
-
if (metaTarget.borrowed) throw new Error("Block is borrowed");
|
|
874
|
+
if (!Object.hasOwn(this.bus, target)) throw new Error("Invalid or corrupted handle");
|
|
875
|
+
if (!Object.hasOwn(this.bus, source)) throw new Error("Invalid or corrupted handle");
|
|
1226
876
|
|
|
877
|
+
const dest = this.bus[target];
|
|
878
|
+
const src = this.bus[source];
|
|
1227
879
|
|
|
1228
|
-
|
|
1229
|
-
const count = Math.min(available, metaSource.occupied);
|
|
880
|
+
if (dest.borrowed) throw new Error("Block is borrowed");
|
|
1230
881
|
|
|
882
|
+
const count = Math.min(dest.dataBus.length - dest.occupied, src.occupied);
|
|
1231
883
|
if (count === 0) return 0;
|
|
1232
884
|
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
this.controlAdapter.setUint32(metaTarget.headerAddress + OCCUPIED_OFFSET, metaTarget.occupied + count, IS_LITTLE_ENDIAN);
|
|
885
|
+
if (dest === src) {
|
|
886
|
+
dest.dataBus.copyWithin(dest.occupied, 0, count);
|
|
887
|
+
} else {
|
|
888
|
+
dest.dataBus.set(src.dataBus.subarray(0, count), dest.occupied);
|
|
889
|
+
}
|
|
1239
890
|
|
|
891
|
+
dest.occupied += count;
|
|
1240
892
|
return count;
|
|
1241
893
|
}
|
|
1242
894
|
|
|
1243
895
|
/**
|
|
1244
896
|
* Creates an independent copy of a block.
|
|
1245
897
|
*
|
|
1246
|
-
* The
|
|
1247
|
-
*
|
|
1248
|
-
*
|
|
898
|
+
* The original view size is passed to alloc() as a size in bytes.
|
|
899
|
+
* For types with more than one byte per element, the resulting capacity
|
|
900
|
+
* may be smaller than the original; in that case, set() may throw a RangeError
|
|
901
|
+
* when attempting to copy the entire view. The view's elements are copied,
|
|
902
|
+
* including those outside the occupied region.
|
|
1249
903
|
*
|
|
1250
|
-
* The new block receives
|
|
1251
|
-
* allocation and recycling rules.
|
|
904
|
+
* The new block receives its own handle.
|
|
1252
905
|
*
|
|
1253
|
-
* @param {number} handler - Handle of the block to
|
|
906
|
+
* @param {number} handler - Handle of the block to clone.
|
|
1254
907
|
* @returns {number} Handle of the new block.
|
|
1255
908
|
* @throws {Error} If the handle is invalid or corrupted.
|
|
1256
909
|
*/
|
|
1257
910
|
clone(handler) {
|
|
1258
|
-
|
|
1259
|
-
const cloned = this.alloc(meta.byteCapacity / this.wordSize);
|
|
911
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
1260
912
|
|
|
1261
|
-
const
|
|
1262
|
-
this.
|
|
913
|
+
const pack = this.bus[handler];
|
|
914
|
+
const cloned = this.alloc(pack.dataBus.byteLength, pack.bucketType);
|
|
915
|
+
const clonePack = this.bus[cloned];
|
|
1263
916
|
|
|
1264
|
-
|
|
1265
|
-
|
|
917
|
+
clonePack.dataBus.set(pack.dataBus);
|
|
918
|
+
clonePack.occupied = pack.occupied;
|
|
1266
919
|
|
|
1267
920
|
return cloned;
|
|
1268
921
|
}
|
|
1269
922
|
|
|
1270
923
|
/**
|
|
1271
|
-
* Swaps the
|
|
1272
|
-
*
|
|
1273
|
-
* The operation swaps region addresses, capacities, and occupancies
|
|
1274
|
-
* between headers. Handles remain unchanged:
|
|
1275
|
-
* after the operation, each handle continues identifying its own
|
|
1276
|
-
* header, but that header now describes the region previously
|
|
1277
|
-
* belonging to the other block.
|
|
924
|
+
* Swaps the block objects associated with two handles.
|
|
1278
925
|
*
|
|
1279
|
-
*
|
|
1280
|
-
*
|
|
926
|
+
* The operation swaps the references stored in bus; it does not copy data
|
|
927
|
+
* or change the numeric handles. After the swap, each handle points
|
|
928
|
+
* to the block object previously associated with the other handle.
|
|
1281
929
|
*
|
|
1282
|
-
* @param {number} source
|
|
1283
|
-
* @param {number} target
|
|
930
|
+
* @param {number} source First handle.
|
|
931
|
+
* @param {number} target Second handle.
|
|
1284
932
|
* @returns {void}
|
|
1285
|
-
* @throws {Error} If
|
|
1286
|
-
*
|
|
933
|
+
* @throws {Error} If either handle does not exist or either block is
|
|
934
|
+
* borrowed.
|
|
1287
935
|
*/
|
|
1288
936
|
swap(source, target) {
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
/** @type {Metadata} */
|
|
1292
|
-
const metaB = {};
|
|
1293
|
-
|
|
1294
|
-
this.extractMetadata(source, metaA);
|
|
1295
|
-
this.extractMetadata(target, metaB);
|
|
937
|
+
if (!Object.hasOwn(this.bus, source)) throw new Error("Invalid or corrupted handle");
|
|
938
|
+
if (!Object.hasOwn(this.bus, target)) throw new Error("Invalid or corrupted handle");
|
|
1296
939
|
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
940
|
+
const packA = this.bus[source];
|
|
941
|
+
const packB = this.bus[target];
|
|
1300
942
|
|
|
1301
|
-
|
|
1302
|
-
this.controlAdapter.setUint32(metaB.headerAddress + ADDRESS_OFFSET, metaA.dataAddress, IS_LITTLE_ENDIAN);
|
|
943
|
+
if (packA.borrowed || packB.borrowed) throw new Error("Block is borrowed");
|
|
1303
944
|
|
|
1304
|
-
this.
|
|
1305
|
-
this.
|
|
1306
|
-
|
|
1307
|
-
this.controlAdapter.setUint32(metaA.headerAddress + OCCUPIED_OFFSET, metaB.occupied, IS_LITTLE_ENDIAN);
|
|
1308
|
-
this.controlAdapter.setUint32(metaB.headerAddress + OCCUPIED_OFFSET, metaA.occupied, IS_LITTLE_ENDIAN);
|
|
945
|
+
this.bus[source] = packB;
|
|
946
|
+
this.bus[target] = packA;
|
|
1309
947
|
}
|
|
1310
948
|
|
|
1311
949
|
|
|
1312
950
|
// ============= SERIALIZATION ==============
|
|
1313
951
|
|
|
1314
952
|
/**
|
|
1315
|
-
* Serializes
|
|
953
|
+
* Serializes a block into a CBOR version 2 envelope.
|
|
1316
954
|
*
|
|
1317
|
-
* The
|
|
1318
|
-
*
|
|
1319
|
-
*
|
|
955
|
+
* The envelope contains the bucket type, capacity in elements,
|
|
956
|
+
* occupancy, and a copy of the occupied elements (payload). Encoding
|
|
957
|
+
* is performed by cbor-x's encode().
|
|
1320
958
|
*
|
|
1321
|
-
* @param {number} handler
|
|
1322
|
-
* @returns {Uint8Array}
|
|
1323
|
-
* @throws {Error} If the handle
|
|
959
|
+
* @param {number} handler Block handle.
|
|
960
|
+
* @returns {Uint8Array} Serialized CBOR data.
|
|
961
|
+
* @throws {Error} If the handle does not exist or the capacity
|
|
962
|
+
* or occupancy metadata is inconsistent.
|
|
1324
963
|
*/
|
|
1325
964
|
dump(handler) {
|
|
1326
|
-
|
|
1327
|
-
const capacity = meta.capacityExponent;
|
|
1328
|
-
const capacityElements = 2 ** capacity;
|
|
965
|
+
if (!Object.hasOwn(this.bus, handler)) throw new Error("Invalid or corrupted handle");
|
|
1329
966
|
|
|
1330
|
-
|
|
967
|
+
const pack = this.bus[handler];
|
|
968
|
+
const capacity = pack.dataBus.length;
|
|
969
|
+
|
|
970
|
+
if (!Number.isInteger(capacity) || capacity < 2 || capacity > 2 ** 31 || pack.occupied < 0 || pack.occupied > capacity) {
|
|
1331
971
|
throw new Error("Cannot serialize a block with invalid metadata");
|
|
1332
972
|
}
|
|
1333
973
|
|
|
1334
|
-
const payload =
|
|
1335
|
-
|
|
1336
|
-
if (payload.length !== meta.occupied || payload.constructor !== this.bucketArray) {
|
|
1337
|
-
throw new Error("Cannot serialize a block with an invalid payload");
|
|
1338
|
-
}
|
|
974
|
+
const payload = pack.dataBus.slice(0, pack.occupied);
|
|
1339
975
|
|
|
1340
976
|
return encode({
|
|
1341
|
-
v:
|
|
1342
|
-
|
|
977
|
+
v: 2,
|
|
978
|
+
bucketType: pack.bucketType,
|
|
1343
979
|
capacity,
|
|
1344
|
-
occupied:
|
|
1345
|
-
le: IS_LITTLE_ENDIAN,
|
|
980
|
+
occupied: pack.occupied,
|
|
1346
981
|
payload
|
|
1347
982
|
});
|
|
1348
983
|
}
|
|
1349
984
|
|
|
1350
985
|
/**
|
|
1351
|
-
*
|
|
1352
|
-
*
|
|
1353
|
-
*
|
|
1354
|
-
*
|
|
1355
|
-
*
|
|
1356
|
-
*
|
|
1357
|
-
*
|
|
1358
|
-
*
|
|
1359
|
-
* @
|
|
1360
|
-
* @
|
|
986
|
+
* Decodes a CBOR envelope and recreates the serialized block.
|
|
987
|
+
*
|
|
988
|
+
* Accepts only ArrayBuffer or Uint8Array. Validates the envelope structure,
|
|
989
|
+
* version 2, bucket type, capacity, occupancy, and the type and
|
|
990
|
+
* length of the payload. If the instance has bucketSize fixed in the
|
|
991
|
+
* constructor, the serialized type must match it. The capacity
|
|
992
|
+
* recorded in elements is converted to bytes before allocation.
|
|
993
|
+
*
|
|
994
|
+
* @param {Uint8Array|ArrayBuffer} serialized Buffer containing the CBOR envelope.
|
|
995
|
+
* @returns {number} Handle of the restored block.
|
|
996
|
+
* @throws {TypeError} If the input, envelope, or payload have
|
|
997
|
+
* invalid types or structures.
|
|
998
|
+
* @throws {Error} If the version is not 2 or the bucket type differs
|
|
999
|
+
* from bucketSize when the instance has a fixed default type.
|
|
1000
|
+
* @throws {RangeError} If the capacity or occupancy is invalid.
|
|
1361
1001
|
*/
|
|
1362
1002
|
load(serialized) {
|
|
1363
1003
|
if (!(serialized instanceof ArrayBuffer) && !(serialized instanceof Uint8Array)) {
|
|
1364
1004
|
throw new TypeError("serialized must be an ArrayBuffer or Uint8Array");
|
|
1365
1005
|
}
|
|
1366
1006
|
|
|
1367
|
-
const envelope = decode(serialized);
|
|
1007
|
+
const envelope = decode(serialized instanceof ArrayBuffer ? new Uint8Array(serialized) : serialized);
|
|
1368
1008
|
|
|
1369
1009
|
if (!envelope || typeof envelope !== "object" || Array.isArray(envelope)) {
|
|
1370
1010
|
throw new TypeError("Invalid serialized block envelope");
|
|
1371
1011
|
}
|
|
1372
1012
|
|
|
1373
|
-
if (envelope.v !==
|
|
1374
|
-
throw new Error(`Unsupported serialized block version: ${envelope.v}`);
|
|
1375
|
-
}
|
|
1013
|
+
if (envelope.v !== 2) throw new Error(`Unsupported serialized block version: ${envelope.v}`);
|
|
1376
1014
|
|
|
1377
|
-
if (envelope.
|
|
1378
|
-
|
|
1015
|
+
if (!BUCKET_MAPPING[envelope.bucketType]) throw new TypeError(`Unsupported bucket type: ${envelope.bucketType}`);
|
|
1016
|
+
if (this.bucketSize !== null && envelope.bucketType !== this.bucketSize) {
|
|
1017
|
+
throw new Error(`Can't load data, ${this.bucketSize} expected but ${envelope.bucketType} found.`);
|
|
1379
1018
|
}
|
|
1380
1019
|
|
|
1381
|
-
if (!Number.
|
|
1020
|
+
if (!Number.isSafeInteger(envelope.capacity) || envelope.capacity < 2 || envelope.capacity > 2 ** 31) {
|
|
1382
1021
|
throw new RangeError("Invalid serialized block capacity");
|
|
1383
1022
|
}
|
|
1384
1023
|
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
if (!Number.isSafeInteger(envelope.occupied) || envelope.occupied < 0 || envelope.occupied > capacityElements) {
|
|
1024
|
+
if (!Number.isSafeInteger(envelope.occupied) || envelope.occupied < 0 || envelope.occupied > envelope.capacity) {
|
|
1388
1025
|
throw new RangeError("Invalid serialized block occupancy");
|
|
1389
1026
|
}
|
|
1390
1027
|
|
|
1391
|
-
if (typeof envelope.le !== "boolean") {
|
|
1392
|
-
throw new TypeError("Invalid serialized endianness flag");
|
|
1393
|
-
}
|
|
1394
|
-
|
|
1395
1028
|
const payload = envelope.payload;
|
|
1396
1029
|
|
|
1397
|
-
|
|
1398
|
-
|
|
1030
|
+
const mapping = BUCKET_MAPPING[envelope.bucketType];
|
|
1031
|
+
const TypedArray = globalThis[`${mapping.type}Array`];
|
|
1032
|
+
const bytesPerElement = mapping.bits / 8;
|
|
1033
|
+
if (!isTypedArray(payload) || payload.constructor !== TypedArray || payload.length !== envelope.occupied) {
|
|
1034
|
+
throw new TypeError(`Invalid payload: expected ${mapping.type}Array with ${envelope.occupied} elements`);
|
|
1399
1035
|
}
|
|
1400
1036
|
|
|
1401
|
-
if (payload.BYTES_PER_ELEMENT !==
|
|
1037
|
+
if (payload.BYTES_PER_ELEMENT !== bytesPerElement || payload.byteLength !== envelope.occupied * bytesPerElement) {
|
|
1402
1038
|
throw new TypeError("Serialized payload has an invalid element size");
|
|
1403
1039
|
}
|
|
1404
1040
|
|
|
1405
|
-
const handler = this.alloc(
|
|
1406
|
-
const
|
|
1407
|
-
|
|
1408
|
-
|
|
1041
|
+
const handler = this.alloc(envelope.capacity * bytesPerElement, envelope.bucketType);
|
|
1042
|
+
const pack = this.bus[handler];
|
|
1043
|
+
|
|
1044
|
+
pack.dataBus.set(payload);
|
|
1045
|
+
pack.occupied = envelope.occupied;
|
|
1409
1046
|
|
|
1410
1047
|
return handler;
|
|
1411
1048
|
}
|
|
1412
1049
|
|
|
1413
|
-
// ========== INTEGRATION AND
|
|
1050
|
+
// ========== DATA INTEGRATION AND INGESTION ==========
|
|
1414
1051
|
|
|
1415
1052
|
/**
|
|
1416
|
-
* Loads data
|
|
1053
|
+
* Loads data by delegating the operation to an adapter.
|
|
1417
1054
|
*
|
|
1418
|
-
* The adapter
|
|
1419
|
-
*
|
|
1420
|
-
*
|
|
1421
|
-
* operation and returns the resulting handler.
|
|
1055
|
+
* The adapter must expose a load(allocator) function. It is called
|
|
1056
|
+
* synchronously with the current Balloc instance, and its return value
|
|
1057
|
+
* is passed through unchanged.
|
|
1422
1058
|
*
|
|
1423
|
-
*
|
|
1424
|
-
*
|
|
1425
|
-
*
|
|
1426
|
-
*
|
|
1427
|
-
* @param {FormatAdapter} adapter - Adapter implementing load(allocator).
|
|
1428
|
-
* @returns {number} Handler returned by the adapter.
|
|
1429
|
-
* @throws {TypeError} If the adapter does not implement load().
|
|
1430
|
-
* @throws {Error} If the adapter rejects the subscribed infrastructure.
|
|
1059
|
+
* @param {FormatAdapter} adapter Adapter implementing load(allocator).
|
|
1060
|
+
* @returns {*} Value returned by adapter.load(this).
|
|
1061
|
+
* @throws {TypeError} If the adapter does not exist or load is not a function.
|
|
1431
1062
|
*/
|
|
1432
1063
|
loadData(adapter) {
|
|
1433
1064
|
if (!adapter || typeof adapter.load !== "function") throw new TypeError("adapter must implement load(allocator)");
|
|
1434
1065
|
return adapter.load(this);
|
|
1435
1066
|
}
|
|
1436
|
-
// ============= UTILITIES ==============
|
|
1437
|
-
|
|
1438
|
-
/**
|
|
1439
|
-
* Prevents modification operations on a borrowed block.
|
|
1440
|
-
*
|
|
1441
|
-
* Checks the VIEW_LOCK flag in the header associated with the provided address.
|
|
1442
|
-
*
|
|
1443
|
-
* @param {number} headerAddress - Address of the header in controlBus.
|
|
1444
|
-
* @returns {void}
|
|
1445
|
-
* @throws {Error} If the block is borrowed via withView().
|
|
1446
|
-
* @private
|
|
1447
|
-
* @deprecated
|
|
1448
|
-
*/
|
|
1449
|
-
assertNotBorrowed(headerAddress) {
|
|
1450
|
-
if (this.controlBus[headerAddress + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1451
|
-
throw new Error("Block is borrowed");
|
|
1452
|
-
}
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
/**
|
|
1456
|
-
* Prevents operations that replace or remap the dataBus
|
|
1457
|
-
* while any segment is borrowed.
|
|
1458
|
-
*
|
|
1459
|
-
* @returns {void}
|
|
1460
|
-
* @throws {Error} If any block is borrowed via withView().
|
|
1461
|
-
* @private
|
|
1462
|
-
*/
|
|
1463
|
-
assertNoActiveViews() {
|
|
1464
|
-
for (let i = 0; i < this.controlPointer; i += HEADER_SIZE) {
|
|
1465
|
-
if (this.controlBus[i + COOKIE_OFFSET] === 0) continue;
|
|
1466
|
-
if (this.controlBus[i + FLAGS_OFFSET] & VIEW_LOCK) {
|
|
1467
|
-
throw new Error("Cannot remap memory while a block is borrowed");
|
|
1468
|
-
}
|
|
1469
|
-
}
|
|
1470
|
-
}
|
|
1471
|
-
|
|
1472
|
-
/**
|
|
1473
|
-
* [INTERNAL USE]
|
|
1474
|
-
* Decodes metadata associated with a handle.
|
|
1475
|
-
*
|
|
1476
|
-
* The handle is split into generation and header address. The cookie stored
|
|
1477
|
-
* in the header is then recalculated to validate the combination; only after
|
|
1478
|
-
* this validation are metadata read.
|
|
1479
|
-
*
|
|
1480
|
-
* Decoded fields are written to the `out` object, which is returned
|
|
1481
|
-
* by the function itself. The object can be reused between calls to avoid
|
|
1482
|
-
* new allocations.
|
|
1483
|
-
*
|
|
1484
|
-
* @param {number} handler - Handle to be decoded and validated.
|
|
1485
|
-
* @param {Metadata} out - Object that will receive decoded metadata.
|
|
1486
|
-
* @returns {Metadata} The same reference provided in `out`.
|
|
1487
|
-
* @throws {Error} If the header cookie does not match the handle or
|
|
1488
|
-
* if the handle does not identify a valid header.
|
|
1489
|
-
* @private
|
|
1490
|
-
*/
|
|
1491
|
-
extractMetadata(handler, out) {
|
|
1492
|
-
out.generation = (handler / ROTATE_32BITS) & CLAMP_11BITS;
|
|
1493
|
-
out.headerAddress = handler >>> 0;
|
|
1494
|
-
|
|
1495
|
-
const cookie = this.controlAdapter.getUint8(out.headerAddress + COOKIE_OFFSET);
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
if (cookie !== this.bakeCookie(out.headerAddress, out.generation)) {
|
|
1499
|
-
throw new Error("Invalid or corrupted handle");
|
|
1500
|
-
}
|
|
1501
|
-
|
|
1502
|
-
out.dataAddress = this.controlAdapter.getUint32(out.headerAddress + ADDRESS_OFFSET, IS_LITTLE_ENDIAN);
|
|
1503
|
-
out.capacityExponent = this.controlAdapter.getUint8(out.headerAddress + CAPACITY_OFFSET);
|
|
1504
|
-
out.byteCapacity = (1 << out.capacityExponent >>> 0) * this.wordSize;
|
|
1505
|
-
out.occupied = this.controlAdapter.getUint32(out.headerAddress + OCCUPIED_OFFSET, IS_LITTLE_ENDIAN);
|
|
1506
|
-
out.borrowed = this.controlAdapter.getUint32(out.headerAddress + FLAGS_OFFSET, IS_LITTLE_ENDIAN) & VIEW_LOCK;
|
|
1507
|
-
|
|
1508
|
-
return out;
|
|
1509
|
-
}
|
|
1510
|
-
|
|
1511
|
-
/**
|
|
1512
|
-
* [INTERNAL USE]
|
|
1513
|
-
* Calculates a header's validation cookie.
|
|
1514
|
-
*
|
|
1515
|
-
* The cookie is derived from the header address and the 11 generation bits via
|
|
1516
|
-
* fast hash.
|
|
1517
|
-
* The result is reduced to 8 bits.
|
|
1518
|
-
*
|
|
1519
|
-
* The value zero is reserved as an invalid header indicator; therefore,
|
|
1520
|
-
* when the calculation produces zero, the method returns `1`.
|
|
1521
|
-
*
|
|
1522
|
-
* The cookie does not constitute a cryptographic mechanism. Its purpose is
|
|
1523
|
-
* to detect invalid, stale, or corrupted handles within the allocator's
|
|
1524
|
-
* memory management model.
|
|
1525
|
-
*
|
|
1526
|
-
* @param {number} addr - Header address.
|
|
1527
|
-
* @param {number} gen - Generation associated with the handle. Only the 11
|
|
1528
|
-
* least significant bits are considered.
|
|
1529
|
-
* @returns {number} 8-bit cookie in the range [1, 255].
|
|
1530
|
-
* @private
|
|
1531
|
-
*/
|
|
1532
|
-
bakeCookie(addr, gen) {
|
|
1533
|
-
return (addr ^ (gen & 0x7FF * 0x9e3779b1)) & 0xff || 1;
|
|
1534
|
-
}
|
|
1535
|
-
|
|
1536
|
-
/**
|
|
1537
|
-
* Returns the number of regions currently stored in the recycling list.
|
|
1538
|
-
*
|
|
1539
|
-
* Each region corresponds to a freed block that could not be returned
|
|
1540
|
-
* directly to the top of the `dataBus` and, therefore, remains available for
|
|
1541
|
-
* reuse by a future allocation of identical capacity.
|
|
1542
|
-
*
|
|
1543
|
-
* @returns {number} Number of free regions kept in `recycle`.
|
|
1544
|
-
*/
|
|
1545
|
-
recycleCount() {
|
|
1546
|
-
let count = 0;
|
|
1547
|
-
|
|
1548
|
-
for (const [, page] of this.recycle) {
|
|
1549
|
-
count += page[0];
|
|
1550
|
-
}
|
|
1551
|
-
|
|
1552
|
-
return count;
|
|
1553
|
-
}
|
|
1554
|
-
|
|
1555
|
-
/**
|
|
1556
|
-
* Returns the total bytes belonging to regions currently stored in the
|
|
1557
|
-
* recycling list.
|
|
1558
|
-
*
|
|
1559
|
-
* The value is calculated by summing each free region's capacity multiplied
|
|
1560
|
-
* by the number of regions registered for that capacity.
|
|
1561
|
-
*
|
|
1562
|
-
* @returns {number} Number of bytes occupied by recyclable regions.
|
|
1563
|
-
*/
|
|
1564
|
-
get recycleBytes() {
|
|
1565
|
-
let total = 0;
|
|
1566
|
-
|
|
1567
|
-
for (const [capacity, page] of this.recycle) {
|
|
1568
|
-
total += page[0] * capacity;
|
|
1569
|
-
}
|
|
1570
|
-
|
|
1571
|
-
return total;
|
|
1572
|
-
}
|
|
1573
|
-
|
|
1574
|
-
/**
|
|
1575
|
-
* Returns the quantity of allocated bytes that do not belong to currently
|
|
1576
|
-
* recyclable regions.
|
|
1577
|
-
*
|
|
1578
|
-
* The value is obtained by subtracting the bytes belonging to free regions
|
|
1579
|
-
* kept in `recycle` from the total advance of `dataPointer`.
|
|
1580
|
-
*
|
|
1581
|
-
* @returns {number} Number of bytes associated with non-recyclable regions
|
|
1582
|
-
* within the area already used by `dataBus`.
|
|
1583
|
-
*/
|
|
1584
|
-
get allocatedBytes() {
|
|
1585
|
-
return this.dataPointer - this.recycleBytes;
|
|
1586
|
-
}
|
|
1587
1067
|
|
|
1588
|
-
|
|
1589
|
-
* Returns the quantity of bytes not yet used at the top of the `dataBus`.
|
|
1590
|
-
*
|
|
1591
|
-
* This space corresponds to the region between `dataPointer` and the end of the
|
|
1592
|
-
* capacity currently reserved for the buffer.
|
|
1593
|
-
*
|
|
1594
|
-
* @returns {number} Number of bytes available after `dataPointer`.
|
|
1595
|
-
*/
|
|
1596
|
-
get unallocatedBytes() {
|
|
1597
|
-
return this.size - this.dataPointer;
|
|
1598
|
-
}
|
|
1068
|
+
// ============= UTILITIES ==============
|
|
1599
1069
|
|
|
1600
|
-
/**
|
|
1601
|
-
* Returns the ratio between recyclable memory and the used heap area.
|
|
1602
|
-
*
|
|
1603
|
-
* The metric considers free regions kept in `recycle` as fragmentation
|
|
1604
|
-
* and uses `dataPointer` as the denominator. When no bytes have been used
|
|
1605
|
-
* by the heap, the result is zero.
|
|
1606
|
-
*
|
|
1607
|
-
* The value does not represent the fraction of the entire `dataBus` capacity, but
|
|
1608
|
-
* specifically the proportion of recyclable bytes within the area already traversed
|
|
1609
|
-
* by the bump pointer.
|
|
1610
|
-
*
|
|
1611
|
-
* @returns {number} Fragmentation ratio in the range [0, 1] under normal
|
|
1612
|
-
* operating conditions.
|
|
1613
|
-
*/
|
|
1614
|
-
get fragmentationRatio() {
|
|
1615
|
-
if (this.dataPointer === 0) return 0;
|
|
1616
|
-
return this.recycleBytes / this.dataPointer;
|
|
1617
|
-
}
|
|
1618
1070
|
|
|
1619
1071
|
}
|