balloc 1.3.1 → 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 +44 -0
- package/README.MD +171 -318
- package/docs/API /342/200/224 balloc.en.md" +472 -1156
- 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 +515 -933
- package/src/en/balloc.js +1 -1
- package/src/en/balloc.mjs +561 -1064
- package/types/README.MD +0 -0
- package/types/en/balloc.d.mts +279 -554
- package/README.ar.md +0 -409
- package/README.en.md +0 -437
- package/README.es.md +0 -409
- package/README.jp.md +0 -408
- package/README.pt.md +0 -433
- package/README.zh.md +0 -408
- package/docs/API /342/200/224 balloc.ar.md" +0 -1453
- package/docs/API /342/200/224 balloc.es.md" +0 -1269
- package/docs/API /342/200/224 balloc.jp.md" +0 -1208
- package/docs/API /342/200/224 balloc.pt.md" +0 -1339
- package/docs/API /342/200/224 balloc.zh.md" +0 -1268
- package/src/ar/balloc.cjs +0 -1273
- package/src/ar/balloc.js +0 -2
- package/src/ar/balloc.mjs +0 -1548
- package/src/es/balloc.cjs +0 -1288
- package/src/es/balloc.js +0 -2
- package/src/es/balloc.mjs +0 -1563
- package/src/jp/balloc.cjs +0 -1228
- package/src/jp/balloc.js +0 -2
- package/src/jp/balloc.mjs +0 -1503
- package/src/pt/balloc.cjs +0 -1293
- package/src/pt/balloc.js +0 -2
- package/src/pt/balloc.mjs +0 -1568
- package/src/zh/balloc.cjs +0 -1175
- package/src/zh/balloc.js +0 -2
- package/src/zh/balloc.mjs +0 -1450
- package/types/ar/balloc.d.mts +0 -762
- package/types/es/balloc.d.mts +0 -777
- package/types/jp/balloc.d.mts +0 -717
- package/types/pt/balloc.d.mts +0 -781
- package/types/zh/balloc.d.mts +0 -664
|
@@ -1,1402 +1,718 @@
|
|
|
1
|
-
|
|
2
|
-
| Language | Link |
|
|
3
|
-
|---|---|
|
|
4
|
-
| Arabic | [API — balloc.ar.md](./API%20—%20balloc.ar.md) |
|
|
5
|
-
| Chinese | [API — balloc.zh.md](./API%20—%20balloc.zh.md) |
|
|
6
|
-
| Spanish | [API — balloc.es.md](./API%20—%20balloc.es.md) |
|
|
7
|
-
| English | [API — balloc.en.md](./API%20—%20balloc.en.md) |
|
|
8
|
-
| Japanese | [API — balloc.jp.md](./API%20—%20balloc.jp.md) |
|
|
9
|
-
| Portuguese | [API — balloc.pt.md](./API%20—%20balloc.pt.md) |
|
|
10
|
-
|
|
11
|
-
# Balloc.js
|
|
12
|
-
|
|
13
|
-
`Balloc` is a memory allocator based on **bump allocation**, featuring two independent buses:
|
|
14
|
-
|
|
15
|
-
* `dataBus` — storage for block data.
|
|
16
|
-
* `controlBus` — storage for headers and metadata.
|
|
17
|
-
|
|
18
|
-
Blocks are externally identified by a **43-bit logical handler**, composed of an 11-bit generation and a 32-bit header address. The physical capacity of the blocks is always a power of two in number of elements.
|
|
19
|
-
|
|
20
|
-
## API Ontology
|
|
21
|
-
|
|
22
|
-
```text
|
|
23
|
-
Balloc
|
|
24
|
-
├── Instantiation
|
|
25
|
-
│ └── constructor()
|
|
26
|
-
│
|
|
27
|
-
├── Block Lifecycle
|
|
28
|
-
│ ├── alloc()
|
|
29
|
-
│ ├── realloc()
|
|
30
|
-
│ ├── shrink()
|
|
31
|
-
│ ├── free()
|
|
32
|
-
│ └── gc()
|
|
33
|
-
│
|
|
34
|
-
├── Linear Structures
|
|
35
|
-
│ ├── Stack
|
|
36
|
-
│ │ ├── push()
|
|
37
|
-
│ │ ├── pop()
|
|
38
|
-
│ │ └── peek()
|
|
39
|
-
│ │
|
|
40
|
-
│ └── Queue
|
|
41
|
-
│ ├── nudge()
|
|
42
|
-
│ ├── pull()
|
|
43
|
-
│ └── head()
|
|
44
|
-
│
|
|
45
|
-
├── Content Access
|
|
46
|
-
│ ├── has()
|
|
47
|
-
│ ├── get()
|
|
48
|
-
│ ├── set()
|
|
49
|
-
│ ├── clear()
|
|
50
|
-
│ ├── fill()
|
|
51
|
-
│ ├── fillAll()
|
|
52
|
-
│ ├── put()
|
|
53
|
-
│ ├── pick()
|
|
54
|
-
│ ├── indexOf()
|
|
55
|
-
│ ├── lastIndexOf()
|
|
56
|
-
│ └── withView()
|
|
57
|
-
│
|
|
58
|
-
├── Capacity and Occupancy
|
|
59
|
-
│ ├── byteSize()
|
|
60
|
-
│ ├── occupied()
|
|
61
|
-
│ ├── length()
|
|
62
|
-
│ └── isFull()
|
|
63
|
-
│
|
|
64
|
-
├── Inter-block Operations
|
|
65
|
-
│ ├── copy()
|
|
66
|
-
│ ├── copyWithin()
|
|
67
|
-
│ ├── append()
|
|
68
|
-
│ └── clone()
|
|
69
|
-
│ └── swap()
|
|
70
|
-
│
|
|
71
|
-
├── Serialization
|
|
72
|
-
│ ├── dump()
|
|
73
|
-
│ └── load()
|
|
74
|
-
│
|
|
75
|
-
├── Diagnostics and Utilization
|
|
76
|
-
│ ├── recycleCount()
|
|
77
|
-
│ ├── recycleBytes
|
|
78
|
-
│ ├── allocatedBytes
|
|
79
|
-
│ ├── unallocatedBytes
|
|
80
|
-
│ └── fragmentationRatio
|
|
81
|
-
│
|
|
82
|
-
└── Internal Usage
|
|
83
|
-
├── Recycling
|
|
84
|
-
│ ├── recyclePush()
|
|
85
|
-
│ └── recyclePop()
|
|
86
|
-
│
|
|
87
|
-
├── Scaling
|
|
88
|
-
│ ├── grow()
|
|
89
|
-
│ └── headerGrow()
|
|
90
|
-
│
|
|
91
|
-
└── Utilities
|
|
92
|
-
├── extractMetadata()
|
|
93
|
-
└── bakeCookie()
|
|
94
|
-
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
---
|
|
98
|
-
|
|
99
|
-
## Capacity Semantics
|
|
100
|
-
|
|
101
|
-
The logical capacity unit depends on `bucketSize`.
|
|
102
|
-
|
|
103
|
-
```text
|
|
104
|
-
bucketSize = "8i" or bucketSize = "8u" → 1 byte per element
|
|
105
|
-
bucketSize = "16i" or bucketSize = "16u" or bucketSize = "16f" → 2 bytes per element
|
|
106
|
-
bucketSize = "32i" or bucketSize = "32u" or bucketSize = "32f" → 4 bytes per element
|
|
107
|
-
bucketSize = "64i" or bucketSize = "64u" or bucketSize = "64f" → 8 bytes per element
|
|
108
|
-
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
The allocator computes capacity in **elements** as a power of two and subsequently converts this capacity into bytes.
|
|
112
|
-
|
|
113
|
-
For example, with `bucketSize = "32u"`:
|
|
114
|
-
|
|
115
|
-
```text
|
|
116
|
-
capacity = 8 elements
|
|
117
|
-
wordSize = 4 bytes
|
|
118
|
-
|
|
119
|
-
byteCapacity = 8 × 4 = 32 bytes
|
|
120
|
-
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
Physical capacity and occupancy are distinct concepts:
|
|
124
|
-
|
|
125
|
-
```text
|
|
126
|
-
length(handler) → physical slots
|
|
127
|
-
occupied(handler) → occupied slots
|
|
128
|
-
byteSize(handler) → reserved physical bytes
|
|
129
|
-
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
---
|
|
133
|
-
|
|
134
|
-
## Memory Borrowing
|
|
135
|
-
|
|
136
|
-
Balloc supports **scoped, zero-copy access** to the occupied data of one or more blocks. This is a borrowing mechanism: a callback receives typed-array views over Balloc's internal storage, allowing direct reads and writes without allocating a copy.
|
|
1
|
+
# Balloc 2.0.0 — Public API Reference
|
|
137
2
|
|
|
138
|
-
|
|
3
|
+
This document is the method-level reference for the public API of Balloc 2.0.0. It describes the behavior implemented by `src/en/balloc.mjs`, including parameter units, return values, validation, mutation rules, borrowing restrictions, and serialization format.
|
|
139
4
|
|
|
140
|
-
|
|
5
|
+
Balloc manages independent typed memory blocks. Each block owns an `ArrayBuffer`, has a numeric handle, and tracks physical capacity separately from logical occupancy.
|
|
141
6
|
|
|
142
|
-
|
|
7
|
+
## API map
|
|
143
8
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
```js
|
|
158
|
-
memory.withView(source, target, (sourceView, targetView) => {
|
|
159
|
-
targetView.set(sourceView.subarray(0, targetView.length));
|
|
160
|
-
});
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
Each handler must be valid, distinct, and not already borrowed. Balloc acquires the locks before invoking the callback and releases all acquired locks when it exits.
|
|
164
|
-
|
|
165
|
-
### Borrowing constraints
|
|
166
|
-
|
|
167
|
-
A borrowed view, or any subview derived from it, must not be retained, returned, or used after the callback finishes. If the callback returns a Promise, the borrow ends immediately when that Promise is returned; it is not held until the Promise settles. The callback must therefore complete all view access synchronously.
|
|
168
|
-
|
|
169
|
-
While a block is borrowed, operations that modify that block are rejected with `Error`. This includes `push()`, `pop()`, `nudge()`, `pull()`, `set()`, `clear()`, `fill()`, `fillAll()`, `put()`, `realloc()`, `shrink()`, `free()`, and `copyWithin()`. `copy()` and `append()` are rejected when the borrowed block is the destination; a borrowed source is allowed. `swap()` is rejected if either block is borrowed. Read-only operations remain available.
|
|
170
|
-
|
|
171
|
-
Operations that replace or remap `dataBus` are rejected while any view is active. This includes `gc()` and growth of `dataBus` triggered by `alloc()` or `realloc()`.
|
|
172
|
-
|
|
173
|
-
# 1. Instantiation
|
|
174
|
-
|
|
175
|
-
## `new Balloc(size, bucketSize, metadataSize)`
|
|
9
|
+
| Area | Public members |
|
|
10
|
+
|---|---|
|
|
11
|
+
| Construction | `Balloc()` |
|
|
12
|
+
| Block lifecycle | `alloc()`, `realloc()`, `shrink()`, `free()` |
|
|
13
|
+
| Capacity and occupancy | `length()`, `occupied()`, `byteSize()`, `isFull()` |
|
|
14
|
+
| Stack | `push()`, `pop()`, `peek()` |
|
|
15
|
+
| Queue | `nudge()`, `pull()`, `head()` |
|
|
16
|
+
| Indexed access and search | `get()`, `set()`, `has()`, `indexOf()`, `lastIndexOf()` |
|
|
17
|
+
| Bulk data operations | `clear()`, `fill()`, `fillAll()`, `put()`, `pick()` |
|
|
18
|
+
| Scoped views | `withView()` |
|
|
19
|
+
| Inter-block operations | `copy()`, `copyWithin()`, `append()`, `clone()`, `swap()` |
|
|
20
|
+
| Serialization | `dump()`, `load()` |
|
|
21
|
+
| Data adapters | `loadData()` |
|
|
176
22
|
|
|
177
|
-
|
|
23
|
+
## 1. Construction
|
|
178
24
|
|
|
179
|
-
|
|
25
|
+
### `new Balloc(size = null, bucketSize = null)`
|
|
180
26
|
|
|
181
|
-
|
|
27
|
+
Creates an allocator instance. The constructor stores the allocation-size limit and an optional default element type. It does not allocate a shared heap or preallocate block storage; each call to `alloc()` creates a separate `ArrayBuffer`.
|
|
182
28
|
|
|
183
29
|
| Parameter | Type | Default | Description |
|
|
184
|
-
|
|
185
|
-
| `size` | `number` | `
|
|
186
|
-
| `bucketSize` | `BucketType` | `
|
|
187
|
-
| `metadataSize` | `number` | `1024` | Initial capacity of `controlBus`, in bytes. |
|
|
30
|
+
|---|---|---:|---|
|
|
31
|
+
| `size` | `number \| null` | `null` | Maximum permitted allocation size, in bytes. `null` is normalized to `0x80000000`. |
|
|
32
|
+
| `bucketSize` | `BucketType \| null` | `null` | Optional default type for all blocks allocated by this instance. |
|
|
188
33
|
|
|
189
|
-
|
|
34
|
+
Validation:
|
|
190
35
|
|
|
191
|
-
|
|
36
|
+
- `size` must be a safe integer in the inclusive range `[2, 0x80000000]`.
|
|
37
|
+
- A non-null `bucketSize` must be one of the supported bucket identifiers.
|
|
38
|
+
- Invalid values throw `RangeError`.
|
|
192
39
|
|
|
193
|
-
|
|
194
|
-
| --- | --- | --- |
|
|
195
|
-
| `8i` | `8u` | |
|
|
196
|
-
| `16i` | `16u` | `16f` |
|
|
197
|
-
| `32i` | `32u` | `32f` |
|
|
198
|
-
| `64i` | `64u` | `64f` |
|
|
40
|
+
The configured `size` is stored as `maxByteSize`. It is an upper bound used by allocation methods; it is not the size of a preallocated heap. Allocation requests must be strictly smaller than this limit.
|
|
199
41
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
### Exceptions
|
|
203
|
-
|
|
204
|
-
* `RangeError` — `size` out of range.
|
|
205
|
-
* `RangeError` — `bucketSize` different from those defined by `BucketType`.
|
|
206
|
-
* `RangeError` — `metadataSize` out of the permitted range.
|
|
207
|
-
|
|
208
|
-
### Example
|
|
42
|
+
Example:
|
|
209
43
|
|
|
210
44
|
```js
|
|
211
|
-
|
|
45
|
+
import Balloc from "balloc";
|
|
212
46
|
|
|
213
47
|
const memory = new Balloc();
|
|
214
|
-
|
|
48
|
+
const smallMemory = new Balloc(1024, "16u");
|
|
215
49
|
```
|
|
216
50
|
|
|
217
|
-
|
|
51
|
+
## 2. Element types and capacity units
|
|
218
52
|
|
|
219
|
-
|
|
220
|
-
const memory = new Balloc(65535, "32u");
|
|
221
|
-
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
The constructor internally instantiates the `TypedArray` corresponding to `bucketSize`, alongside the `DataView`s used for reading and writing.
|
|
53
|
+
A bucket identifier uses the format `[bit width][type suffix]`.
|
|
225
54
|
|
|
226
|
-
|
|
55
|
+
| Category | Identifiers | JavaScript typed-array constructor |
|
|
56
|
+
|---|---|---|
|
|
57
|
+
| Unsigned integer | `8u`, `16u`, `32u`, `64u` | `Uint8Array`, `Uint16Array`, `Uint32Array`, `BigUint64Array` |
|
|
58
|
+
| Signed integer | `8i`, `16i`, `32i`, `64i` | `Int8Array`, `Int16Array`, `Int32Array`, `BigInt64Array` |
|
|
59
|
+
| Floating point | `16f`, `32f`, `64f` | `Float16Array`, `Float32Array`, `Float64Array` |
|
|
227
60
|
|
|
228
|
-
|
|
61
|
+
The selected typed-array constructor must exist in the JavaScript runtime. In particular, `Float16Array` requires runtime support for that constructor.
|
|
229
62
|
|
|
230
|
-
|
|
63
|
+
| Bucket width | Bytes per element |
|
|
64
|
+
|---:|---:|
|
|
65
|
+
| 8 bits | 1 |
|
|
66
|
+
| 16 bits | 2 |
|
|
67
|
+
| 32 bits | 4 |
|
|
68
|
+
| 64 bits | 8 |
|
|
231
69
|
|
|
232
|
-
|
|
233
|
-
alloc()
|
|
234
|
-
│
|
|
235
|
-
├── block operations
|
|
236
|
-
│
|
|
237
|
-
├── realloc()
|
|
238
|
-
│
|
|
239
|
-
└── free()
|
|
240
|
-
│
|
|
241
|
-
└── recycling
|
|
242
|
-
|
|
243
|
-
```
|
|
70
|
+
The 64-bit integer types read and write JavaScript `bigint` values. The other listed types use JavaScript `number` values.
|
|
244
71
|
|
|
245
|
-
|
|
72
|
+
Capacity is expressed in **elements** by `length()` and in **bytes** by `byteSize()`. The `alloc()` size argument is in bytes, whereas the `realloc()` size argument is in elements.
|
|
246
73
|
|
|
247
|
-
##
|
|
74
|
+
## 3. Block lifecycle
|
|
248
75
|
|
|
249
|
-
|
|
76
|
+
### `alloc(size = 16, bucketType = "32u")`
|
|
250
77
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
### Parameters
|
|
78
|
+
Allocates a block with its own `ArrayBuffer` and returns a numeric handle.
|
|
254
79
|
|
|
255
80
|
| Parameter | Type | Default | Description |
|
|
256
|
-
|
|
257
|
-
| `size` | `number` | `16` |
|
|
258
|
-
|
|
259
|
-
`size` must be within the range `[2, 0x80000000]`.
|
|
260
|
-
|
|
261
|
-
### Return
|
|
262
|
-
|
|
263
|
-
```text
|
|
264
|
-
number
|
|
265
|
-
|
|
266
|
-
```
|
|
81
|
+
|---|---|---:|---|
|
|
82
|
+
| `size` | `number` | `16` | Buffer size in bytes. |
|
|
83
|
+
| `bucketType` | `BucketType` | `"32u"` | Element type for the block. Overridden by the constructor's non-null `bucketSize`. |
|
|
267
84
|
|
|
268
|
-
|
|
85
|
+
Validation and allocation rules:
|
|
269
86
|
|
|
270
|
-
The
|
|
87
|
+
- The effective bucket type must be supported.
|
|
88
|
+
- `size` must be a safe integer, at least 2, and strictly less than the instance's `maxByteSize`.
|
|
89
|
+
- `size` must be divisible by the element width in bytes. For example, a `"32u"` allocation must have a byte size divisible by 4.
|
|
90
|
+
- The typed-array capacity is `size / bytesPerElement`; no power-of-two rounding is performed.
|
|
91
|
+
- Invalid type or size throws `RangeError`.
|
|
92
|
+
- If the unsigned 32-bit handle space is exhausted, allocation throws `RangeError`.
|
|
271
93
|
|
|
272
|
-
|
|
273
|
-
[ 11 generation bits ][ 32 header address bits ]
|
|
94
|
+
The first successful allocation receives handle `1`. Handles increase monotonically. A handle is not reused after its block is freed. The handle is an identifier, not a byte offset or encoded header address.
|
|
274
95
|
|
|
275
|
-
|
|
96
|
+
Returns: `number`, the new handle.
|
|
276
97
|
|
|
277
|
-
|
|
98
|
+
Example:
|
|
278
99
|
|
|
279
100
|
```js
|
|
280
|
-
const handler = memory.alloc(
|
|
281
|
-
|
|
282
|
-
memory.push(handler, 42);
|
|
283
|
-
|
|
101
|
+
const handler = memory.alloc(64, "16u");
|
|
102
|
+
// 32 elements, each 2 bytes
|
|
284
103
|
```
|
|
285
104
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
---
|
|
289
|
-
|
|
290
|
-
## `realloc(handler)`
|
|
291
|
-
|
|
292
|
-
Increases block capacity to the next power of two.
|
|
293
|
-
|
|
294
|
-
If the block is located at the current top of the `dataBus`, expansion occurs **in-place**. Otherwise, a new region is obtained from the recycling system or the heap top, occupied elements are copied, and the previous region is recycled.
|
|
295
|
-
|
|
296
|
-
The handler does not change, as its header is preserved.
|
|
105
|
+
### `realloc(handler, newSize = null)`
|
|
297
106
|
|
|
298
|
-
|
|
107
|
+
Replaces the block's buffer with a larger buffer and copies the occupied elements into it. The numeric handle and occupancy remain unchanged.
|
|
299
108
|
|
|
300
|
-
| Parameter | Type | Description |
|
|
301
|
-
|
|
302
|
-
| `handler` | `number` |
|
|
109
|
+
| Parameter | Type | Default | Description |
|
|
110
|
+
|---|---|---:|---|
|
|
111
|
+
| `handler` | `number` | — | Handle of the block to resize. |
|
|
112
|
+
| `newSize` | `number \| null` | `null` | Requested new capacity in **elements**. If null, the current capacity is doubled. |
|
|
303
113
|
|
|
304
|
-
|
|
114
|
+
The requested capacity must be a safe integer greater than the current capacity when explicitly supplied. The effective capacity is capped at the largest element count whose byte size is strictly less than `maxByteSize`:
|
|
305
115
|
|
|
306
116
|
```text
|
|
307
|
-
|
|
308
|
-
|
|
117
|
+
maxCapacity = floor((maxByteSize - 1) / bytesPerElement)
|
|
309
118
|
```
|
|
310
119
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
* `Error` — invalid or corrupted handler.
|
|
314
|
-
* `AggregateError` — capacity has reached the maximum limit.
|
|
315
|
-
|
|
316
|
-
### Example
|
|
317
|
-
|
|
318
|
-
```js
|
|
319
|
-
const handler = memory.alloc(8);
|
|
320
|
-
|
|
321
|
-
memory.realloc(handler);
|
|
322
|
-
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
The operation doubles physical capacity, moving to the next power of two.
|
|
326
|
-
|
|
327
|
-
---
|
|
328
|
-
|
|
329
|
-
## `shrink(handler)`
|
|
330
|
-
|
|
331
|
-
Reduces a block's physical capacity to the smallest supported power of two that can contain its current occupied elements. The minimum capacity is two elements. If the current capacity is already sufficient, the method does nothing.
|
|
332
|
-
|
|
333
|
-
The block's data remains at the same address, and its handler remains valid because the header is preserved. Only the capacity recorded in the header changes. The released tail is reclaimed according to the block's position:
|
|
120
|
+
If the capped capacity is not greater than the current capacity, `RangeError` is thrown. An invalid handle or a borrowed block causes `Error`. The method returns `undefined`.
|
|
334
121
|
|
|
335
|
-
|
|
336
|
-
* Otherwise, the released tail is split into power-of-two regions and each region is added to the recycling list for later reuse.
|
|
122
|
+
Only occupied elements are copied. Values in unoccupied slots are not preserved.
|
|
337
123
|
|
|
338
|
-
`shrink()`
|
|
124
|
+
### `shrink(handler)`
|
|
339
125
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
| Parameter | Type | Description |
|
|
343
|
-
| --- | --- | --- |
|
|
344
|
-
| `handler` | `number` | Block handler. |
|
|
345
|
-
|
|
346
|
-
### Return
|
|
126
|
+
Reduces the block capacity to the smallest power of two that can contain its occupied elements, with a minimum capacity of two elements.
|
|
347
127
|
|
|
348
128
|
```text
|
|
349
|
-
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
### Exceptions
|
|
353
|
-
|
|
354
|
-
* `Error` — invalid or corrupted handler, or the block is currently borrowed through `withView()`.
|
|
355
|
-
|
|
356
|
-
### Example
|
|
357
|
-
|
|
358
|
-
```js
|
|
359
|
-
const handler = memory.alloc(128);
|
|
360
|
-
memory.put(handler, [10, 20, 30]);
|
|
361
|
-
memory.shrink(handler);
|
|
362
|
-
|
|
363
|
-
console.log(memory.length(handler)); // 4
|
|
364
|
-
console.log(memory.occupied(handler)); // 3
|
|
129
|
+
newCapacity = 2 ** ceil(log2(max(2, occupied)))
|
|
365
130
|
```
|
|
366
131
|
|
|
367
|
-
The
|
|
132
|
+
The operation allocates a new buffer and copies the occupied elements. The handle and occupancy remain unchanged. Unoccupied slots are discarded.
|
|
368
133
|
|
|
369
|
-
|
|
134
|
+
If the calculated capacity is greater than or equal to the current capacity, the method returns without changing the block. An invalid handle or borrowed block causes `Error`. The method returns `undefined`.
|
|
370
135
|
|
|
371
|
-
|
|
136
|
+
### `free(handler)`
|
|
372
137
|
|
|
373
|
-
|
|
138
|
+
Removes the block associated with `handler` from the allocator registry. The handle becomes invalid and is never reused.
|
|
374
139
|
|
|
375
|
-
|
|
140
|
+
An invalid handle or borrowed block causes `Error`. The method returns `undefined`.
|
|
376
141
|
|
|
377
|
-
|
|
378
|
-
* removed directly from the top when it is the most recent region on the heap.
|
|
142
|
+
Freeing a block does not add its buffer to a recycling pool; blocks own independent buffers.
|
|
379
143
|
|
|
380
|
-
|
|
144
|
+
## 4. Capacity and occupancy queries
|
|
381
145
|
|
|
382
|
-
|
|
383
|
-
| --- | --- | --- |
|
|
384
|
-
| `handler` | `number` | Block handler. |
|
|
146
|
+
A block has two independent dimensions:
|
|
385
147
|
|
|
386
|
-
|
|
148
|
+
- **Capacity** is the number of elements physically available in its typed array.
|
|
149
|
+
- **Occupancy** is the number of elements currently considered part of its logical sequence.
|
|
387
150
|
|
|
388
|
-
|
|
389
|
-
void
|
|
151
|
+
The invariant maintained by the public operations is `0 <= occupied <= capacity`.
|
|
390
152
|
|
|
391
|
-
|
|
153
|
+
### `length(handler)`
|
|
392
154
|
|
|
393
|
-
|
|
155
|
+
Returns the physical capacity in elements.
|
|
394
156
|
|
|
395
|
-
|
|
157
|
+
- Return: `number`.
|
|
158
|
+
- Throws `Error` if the handle is invalid.
|
|
159
|
+
- Does not change the block.
|
|
396
160
|
|
|
397
|
-
|
|
398
|
-
memory.free(handler);
|
|
161
|
+
### `occupied(handler)`
|
|
399
162
|
|
|
400
|
-
|
|
163
|
+
Returns the number of occupied elements.
|
|
401
164
|
|
|
402
|
-
|
|
165
|
+
- Return: `number`.
|
|
166
|
+
- Throws `Error` if the handle is invalid.
|
|
167
|
+
- Does not change the block.
|
|
403
168
|
|
|
404
|
-
|
|
169
|
+
### `byteSize(handler)`
|
|
405
170
|
|
|
406
|
-
|
|
171
|
+
Returns the physical buffer size in bytes. This is the full capacity, not the byte length of the occupied sequence.
|
|
407
172
|
|
|
408
|
-
|
|
173
|
+
- Return: `number`.
|
|
174
|
+
- Throws `Error` if the handle is invalid.
|
|
175
|
+
- Does not change the block.
|
|
409
176
|
|
|
410
|
-
|
|
177
|
+
### `isFull(handler)`
|
|
411
178
|
|
|
412
|
-
|
|
179
|
+
Returns whether occupancy is exactly equal to physical capacity.
|
|
413
180
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
* preserves physical block capacity;
|
|
418
|
-
* preserves occupancy;
|
|
419
|
-
* updates block addresses;
|
|
420
|
-
* repositions `dataPointer` after the last live block.
|
|
181
|
+
- Return: `boolean`.
|
|
182
|
+
- Throws `Error` if the handle is invalid.
|
|
183
|
+
- Does not change the block.
|
|
421
184
|
|
|
422
|
-
|
|
185
|
+
## 5. Stack operations
|
|
423
186
|
|
|
424
|
-
|
|
425
|
-
void
|
|
426
|
-
|
|
427
|
-
```
|
|
428
|
-
|
|
429
|
-
### Complexity
|
|
430
|
-
|
|
431
|
-
Header traversal is `O(n)`, where `n` is the number of headers examined. Total cost also depends on the volume of data moved.
|
|
432
|
-
|
|
433
|
-
```js
|
|
434
|
-
memory.gc();
|
|
435
|
-
|
|
436
|
-
```
|
|
437
|
-
|
|
438
|
-
`gc()` does not alter handlers, as headers remain at the same addresses; only data regions are compacted.
|
|
439
|
-
|
|
440
|
-
---
|
|
441
|
-
|
|
442
|
-
# 3. Linear Structures
|
|
443
|
-
|
|
444
|
-
Blocks can be used as linear structures accessed from the extremities.
|
|
445
|
-
|
|
446
|
-
## 3.1 Stack
|
|
187
|
+
Stack operations use the end of the occupied sequence.
|
|
447
188
|
|
|
448
189
|
### `push(handler, value)`
|
|
449
190
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
If space is unavailable, no element is inserted and the method returns `false`.
|
|
453
|
-
|
|
454
|
-
```js
|
|
455
|
-
memory.push(handler, 10);
|
|
456
|
-
memory.push(handler, 20);
|
|
457
|
-
memory.push(handler, 30);
|
|
458
|
-
|
|
459
|
-
```
|
|
460
|
-
|
|
461
|
-
Return:
|
|
191
|
+
Writes `value` at index `occupied` and increments occupancy by one.
|
|
462
192
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
193
|
+
| Parameter | Type | Description |
|
|
194
|
+
|---|---|---|
|
|
195
|
+
| `handler` | `number` | Target block handle. |
|
|
196
|
+
| `value` | `number \| bigint` | Value converted by the block's typed array. |
|
|
467
197
|
|
|
468
|
-
|
|
469
|
-
|
|
198
|
+
- Returns `true` when the value is inserted.
|
|
199
|
+
- Returns `false` when the block is full; in that case, no write or occupancy change occurs.
|
|
200
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
470
201
|
|
|
471
|
-
|
|
202
|
+
The value is subject to the conversion and range semantics of the selected typed-array element type.
|
|
472
203
|
|
|
473
204
|
### `pop(handler)`
|
|
474
205
|
|
|
475
|
-
Removes and returns the
|
|
476
|
-
|
|
477
|
-
Occupancy is decremented before element reading. Old bytes are not cleared.
|
|
478
|
-
|
|
479
|
-
Return:
|
|
480
|
-
|
|
481
|
-
```text
|
|
482
|
-
number | undefined
|
|
483
|
-
|
|
484
|
-
```
|
|
485
|
-
|
|
486
|
-
`undefined` indicates an empty block.
|
|
487
|
-
|
|
488
|
-
```js
|
|
489
|
-
const value = memory.pop(handler);
|
|
490
|
-
|
|
491
|
-
```
|
|
206
|
+
Removes the last occupied element and returns its value. Occupancy is decremented before the element is read. The bytes in the vacated slot are not cleared.
|
|
492
207
|
|
|
493
|
-
|
|
208
|
+
- Return: `number | bigint | undefined`.
|
|
209
|
+
- Returns `undefined` when occupancy is zero; occupancy remains zero.
|
|
210
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
494
211
|
|
|
495
212
|
### `peek(handler)`
|
|
496
213
|
|
|
497
|
-
Returns the
|
|
498
|
-
|
|
499
|
-
Return:
|
|
214
|
+
Returns the element at index `occupied - 1` without changing occupancy.
|
|
500
215
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
```
|
|
216
|
+
- Return: `number | bigint | undefined`.
|
|
217
|
+
- Returns `undefined` for an empty block because the indexed read at `-1` produces `undefined`.
|
|
218
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
505
219
|
|
|
506
|
-
|
|
220
|
+
## 6. Queue operations
|
|
507
221
|
|
|
508
|
-
|
|
222
|
+
Queue operations use the beginning of the occupied sequence.
|
|
509
223
|
|
|
510
224
|
### `nudge(handler, value)`
|
|
511
225
|
|
|
512
|
-
Inserts
|
|
513
|
-
|
|
514
|
-
Existing elements are shifted forward by one position. The operation is proportional to the number of occupied elements.
|
|
226
|
+
Inserts `value` at index zero. Existing occupied elements are shifted one position toward the end, and occupancy is incremented.
|
|
515
227
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
```
|
|
228
|
+
| Parameter | Type | Description |
|
|
229
|
+
|---|---|---|
|
|
230
|
+
| `handler` | `number` | Target block handle. |
|
|
231
|
+
| `value` | `number \| bigint` | Value written to the first slot. |
|
|
522
232
|
|
|
523
|
-
|
|
524
|
-
|
|
233
|
+
- Returns `true` when insertion succeeds.
|
|
234
|
+
- Returns `false` when the block is full; no elements are shifted in that case.
|
|
235
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
525
236
|
|
|
526
|
-
|
|
237
|
+
The operation shifts the occupied range using the typed array's `copyWithin()` method. Its work is proportional to the number of elements shifted.
|
|
527
238
|
|
|
528
239
|
### `pull(handler)`
|
|
529
240
|
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
Remaining elements are shifted one position toward the beginning.
|
|
241
|
+
Returns the element at index zero, shifts the remaining occupied elements one position toward the beginning, and decrements occupancy.
|
|
533
242
|
|
|
534
|
-
Return:
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
```
|
|
540
|
-
|
|
541
|
-
---
|
|
243
|
+
- Return: `number | bigint | undefined`.
|
|
244
|
+
- Returns `undefined` without mutation when the block is empty.
|
|
245
|
+
- The vacated final slot is not cleared.
|
|
246
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
542
247
|
|
|
543
248
|
### `head(handler)`
|
|
544
249
|
|
|
545
|
-
Returns the first
|
|
546
|
-
|
|
547
|
-
Return:
|
|
548
|
-
|
|
549
|
-
```text
|
|
550
|
-
number | undefined
|
|
551
|
-
|
|
552
|
-
```
|
|
553
|
-
|
|
554
|
-
---
|
|
555
|
-
|
|
556
|
-
# 4. Content Access
|
|
557
|
-
|
|
558
|
-
## `has(handler, value)`
|
|
559
|
-
|
|
560
|
-
Checks whether `value` is among the occupied elements.
|
|
250
|
+
Returns the value stored in the first physical slot without changing occupancy.
|
|
561
251
|
|
|
562
|
-
|
|
252
|
+
- Return: `number | bigint | undefined`.
|
|
253
|
+
- Throws `Error` for an invalid handle.
|
|
254
|
+
- Unlike most read operations, `head()` does not reject a borrowed block.
|
|
255
|
+
- It does not check occupancy. On an empty block it reads the first physical slot, which is the typed array's initial zero value (`0` for number-based arrays or `0n` for BigInt arrays), rather than returning `undefined`.
|
|
563
256
|
|
|
564
|
-
|
|
565
|
-
if (memory.has(handler, 42)) {
|
|
566
|
-
// ...
|
|
567
|
-
}
|
|
257
|
+
## 7. Indexed access and search
|
|
568
258
|
|
|
569
|
-
|
|
259
|
+
All indexed access uses zero-based indices. The logical sequence is the occupied range `[0, occupied)`, not the entire physical capacity.
|
|
570
260
|
|
|
571
|
-
|
|
261
|
+
### `get(handler, index)`
|
|
572
262
|
|
|
573
|
-
|
|
574
|
-
boolean
|
|
575
|
-
|
|
576
|
-
```
|
|
577
|
-
|
|
578
|
-
---
|
|
579
|
-
|
|
580
|
-
## `indexOf(handler, searchElement, fromIndex = 0)`
|
|
581
|
-
|
|
582
|
-
Finds the first occurrence of a value, searching in ascending index order. Only occupied elements are searched; unused block capacity is ignored.
|
|
583
|
-
|
|
584
|
-
## `lastIndexOf(handler, searchElement, fromIndex = -1)`
|
|
585
|
-
|
|
586
|
-
Finds the last occurrence of a value, searching in descending index order. Only occupied elements are searched; unused block capacity is ignored.
|
|
587
|
-
|
|
588
|
-
Both search methods return the matching zero-based index, or `-1` when the value is not found. They throw `Error` if the handler is invalid or corrupted.
|
|
589
|
-
|
|
590
|
-
## `withView(...args)`
|
|
591
|
-
|
|
592
|
-
Borrows one or more blocks for the duration of a synchronous callback. Supply one or more block handlers followed by the callback; the callback receives the corresponding typed-array views in handler order. The full borrowing model, examples, and restrictions are described in [Memory Borrowing](#memory-borrowing).
|
|
593
|
-
|
|
594
|
-
### Parameters
|
|
263
|
+
Returns the element at `index` if the index is within the occupied range.
|
|
595
264
|
|
|
596
265
|
| Parameter | Type | Description |
|
|
597
|
-
|
|
598
|
-
|
|
|
599
|
-
|
|
600
|
-
### Return
|
|
601
|
-
|
|
602
|
-
The value returned by the callback.
|
|
603
|
-
|
|
604
|
-
### Exceptions
|
|
605
|
-
|
|
606
|
-
* `TypeError` — no handler is provided or the final argument is not a function.
|
|
607
|
-
* `Error` — a handler is invalid, duplicated, or already borrowed.
|
|
608
|
-
|
|
609
|
-
---
|
|
610
|
-
|
|
611
|
-
## `get(handler, index)`
|
|
612
|
-
|
|
613
|
-
Gets the element at the specified index.
|
|
614
|
-
|
|
615
|
-
Zero-based index. Indices outside `[0, occupied)` return `undefined`.
|
|
616
|
-
|
|
617
|
-
```js
|
|
618
|
-
const value = memory.get(handler, 3);
|
|
619
|
-
|
|
620
|
-
```
|
|
621
|
-
|
|
622
|
-
Return:
|
|
623
|
-
|
|
624
|
-
```text
|
|
625
|
-
number | undefined
|
|
626
|
-
|
|
627
|
-
```
|
|
628
|
-
|
|
629
|
-
---
|
|
630
|
-
|
|
631
|
-
## `set(handler, index, value)`
|
|
632
|
-
|
|
633
|
-
Replaces the element at an occupied index.
|
|
634
|
-
|
|
635
|
-
Occupancy is not modified.
|
|
636
|
-
|
|
637
|
-
Indices outside `[0, occupied)` are ignored and cause the method to return `false`.
|
|
638
|
-
|
|
639
|
-
```js
|
|
640
|
-
memory.set(handler, 3, 99);
|
|
641
|
-
|
|
642
|
-
```
|
|
643
|
-
|
|
644
|
-
Return:
|
|
645
|
-
|
|
646
|
-
```text
|
|
647
|
-
boolean
|
|
648
|
-
|
|
649
|
-
```
|
|
650
|
-
|
|
651
|
-
* `true` — value written.
|
|
652
|
-
* `false` — invalid index for the occupied region.
|
|
653
|
-
|
|
654
|
-
---
|
|
655
|
-
|
|
656
|
-
## `clear(handler, value = 0)`
|
|
657
|
-
|
|
658
|
-
Zeros occupancy and fills **all physical slots** of the block with `value`.
|
|
659
|
-
|
|
660
|
-
This includes slots that were not part of the occupied sequence.
|
|
661
|
-
|
|
662
|
-
```js
|
|
663
|
-
memory.clear(handler);
|
|
664
|
-
|
|
665
|
-
```
|
|
666
|
-
|
|
667
|
-
After the operation:
|
|
668
|
-
|
|
669
|
-
```text
|
|
670
|
-
occupied = 0
|
|
671
|
-
|
|
672
|
-
```
|
|
673
|
-
|
|
674
|
-
Physical capacity remains unchanged.
|
|
675
|
-
|
|
676
|
-
---
|
|
677
|
-
|
|
678
|
-
## `fill(handler, value, start = 0, end = null)`
|
|
679
|
-
|
|
680
|
-
Fills a range of slots with `value`.
|
|
681
|
-
|
|
682
|
-
The range follows the convention:
|
|
683
|
-
|
|
684
|
-
```text
|
|
685
|
-
[start, end)
|
|
686
|
-
|
|
687
|
-
```
|
|
688
|
-
|
|
689
|
-
`start` is inclusive and `end` is exclusive. When `end` is `null`, the limit is the total physical capacity. Occupancy becomes `end`.
|
|
690
|
-
|
|
691
|
-
```js
|
|
692
|
-
memory.fill(handler, 0, 2, 6);
|
|
693
|
-
|
|
694
|
-
```
|
|
695
|
-
|
|
696
|
-
Fills:
|
|
697
|
-
|
|
698
|
-
```text
|
|
699
|
-
2, 3, 4, 5
|
|
700
|
-
|
|
701
|
-
```
|
|
702
|
-
|
|
703
|
-
### Exceptions
|
|
704
|
-
|
|
705
|
-
`RangeError` when:
|
|
266
|
+
|---|---|---|
|
|
267
|
+
| `handler` | `number` | Block handle. |
|
|
268
|
+
| `index` | `number` | Zero-based index. |
|
|
706
269
|
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
* `start > end`.
|
|
270
|
+
- Return: `number | bigint | undefined`.
|
|
271
|
+
- Returns `undefined` when `index < 0` or `index >= occupied`.
|
|
272
|
+
- Throws `Error` for an invalid handle.
|
|
273
|
+
- Does not reject a borrowed block.
|
|
712
274
|
|
|
713
|
-
|
|
275
|
+
### `set(handler, index, value)`
|
|
714
276
|
|
|
715
|
-
|
|
277
|
+
Replaces the element at an occupied index. Occupancy is not changed.
|
|
716
278
|
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
occupied = capacity
|
|
723
|
-
|
|
724
|
-
```
|
|
725
|
-
|
|
726
|
-
Unlike `clear()`, the block ends up fully occupied.
|
|
727
|
-
|
|
728
|
-
```js
|
|
729
|
-
memory.fillAll(handler, 0);
|
|
730
|
-
|
|
731
|
-
```
|
|
732
|
-
|
|
733
|
-
---
|
|
734
|
-
|
|
735
|
-
## `put(handler, data)`
|
|
736
|
-
|
|
737
|
-
Copies an `Array` or `TypedArray` to the beginning of the block.
|
|
738
|
-
|
|
739
|
-
The amount copied is limited to block capacity. Occupancy becomes the greater value between prior occupancy and the amount actually copied. Therefore, `put()` does not reduce existing occupancy when receiving a smaller sequence.
|
|
740
|
-
|
|
741
|
-
```js
|
|
742
|
-
memory.put(handler, [10, 20, 30, 40]);
|
|
743
|
-
|
|
744
|
-
```
|
|
745
|
-
|
|
746
|
-
Also accepts `TypedArray`:
|
|
747
|
-
|
|
748
|
-
```js
|
|
749
|
-
memory.put(handler, new Uint32Array([10, 20, 30]));
|
|
750
|
-
|
|
751
|
-
```
|
|
752
|
-
|
|
753
|
-
### Accepted Types
|
|
754
|
-
|
|
755
|
-
```text
|
|
756
|
-
Array
|
|
757
|
-
TypedArray
|
|
758
|
-
|
|
759
|
-
```
|
|
760
|
-
|
|
761
|
-
### Exceptions
|
|
762
|
-
|
|
763
|
-
`TypeError` when `data` is neither `Array` nor `TypedArray`.
|
|
764
|
-
|
|
765
|
-
---
|
|
766
|
-
|
|
767
|
-
## `pick(handler)`
|
|
768
|
-
|
|
769
|
-
Returns a copy of currently occupied elements.
|
|
770
|
-
|
|
771
|
-
The type of the returned `TypedArray` matches the `bucketSize` configured in the allocator. Unoccupied physical capacity is not included.
|
|
772
|
-
|
|
773
|
-
```js
|
|
774
|
-
const values = memory.pick(handler);
|
|
775
|
-
|
|
776
|
-
```
|
|
777
|
-
|
|
778
|
-
The result has storage independent of `dataBus`.
|
|
779
|
-
|
|
780
|
-
For a `bucketSize = "32u"` allocator, for instance, the `Uint32Array` type will store the result.
|
|
781
|
-
|
|
782
|
-
---
|
|
783
|
-
|
|
784
|
-
# 5. Capacity and Occupancy
|
|
785
|
-
|
|
786
|
-
## `byteSize(handler)`
|
|
787
|
-
|
|
788
|
-
Returns the physical capacity of the block in bytes.
|
|
789
|
-
|
|
790
|
-
Does not represent the number of bytes currently occupied by elements; it represents the space reserved for all slots in the block.
|
|
791
|
-
|
|
792
|
-
```js
|
|
793
|
-
const bytes = memory.byteSize(handler);
|
|
794
|
-
|
|
795
|
-
```
|
|
796
|
-
|
|
797
|
-
---
|
|
798
|
-
|
|
799
|
-
## `occupied(handler)`
|
|
800
|
-
|
|
801
|
-
Returns the number of currently occupied elements.
|
|
802
|
-
|
|
803
|
-
```js
|
|
804
|
-
const count = memory.occupied(handler);
|
|
805
|
-
|
|
806
|
-
```
|
|
807
|
-
|
|
808
|
-
---
|
|
279
|
+
| Parameter | Type | Description |
|
|
280
|
+
|---|---|---|
|
|
281
|
+
| `handler` | `number` | Block handle. |
|
|
282
|
+
| `index` | `number` | Zero-based index in the occupied range. |
|
|
283
|
+
| `value` | `number \| bigint` | Replacement value. |
|
|
809
284
|
|
|
810
|
-
|
|
285
|
+
- Returns `true` when the index passes the occupied-range check and the assignment is performed.
|
|
286
|
+
- Returns `false` when `index < 0` or `index >= occupied`.
|
|
287
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
288
|
+
- The assignment uses typed-array conversion semantics.
|
|
811
289
|
|
|
812
|
-
|
|
290
|
+
### `has(handler, value)`
|
|
813
291
|
|
|
814
|
-
|
|
292
|
+
Tests whether `value` occurs in the occupied range.
|
|
815
293
|
|
|
816
|
-
|
|
817
|
-
|
|
294
|
+
- Return: `boolean`.
|
|
295
|
+
- Searches only `[0, occupied)`.
|
|
296
|
+
- Uses `TypedArray.prototype.includes()` semantics (SameValueZero comparison).
|
|
297
|
+
- Throws `Error` for an invalid handle.
|
|
298
|
+
- Does not reject a borrowed block.
|
|
818
299
|
|
|
819
|
-
|
|
300
|
+
### `indexOf(handler, searchElement, fromIndex = 0)`
|
|
820
301
|
|
|
821
|
-
|
|
822
|
-
const capacity = memory.length(handler);
|
|
823
|
-
|
|
824
|
-
```
|
|
302
|
+
Returns the first index of `searchElement` in the occupied range, searching in ascending index order.
|
|
825
303
|
|
|
826
|
-
|
|
304
|
+
| Parameter | Type | Default | Description |
|
|
305
|
+
|---|---|---:|---|
|
|
306
|
+
| `handler` | `number` | — | Block handle. |
|
|
307
|
+
| `searchElement` | `number \| bigint` | — | Value to find. |
|
|
308
|
+
| `fromIndex` | `number` | `0` | Starting index, interpreted according to typed-array `indexOf()` semantics. |
|
|
827
309
|
|
|
828
|
-
|
|
310
|
+
- Return: `number`; returns `-1` when no match exists.
|
|
311
|
+
- Searches only the occupied range.
|
|
312
|
+
- Uses strict equality semantics; for floating-point arrays, `NaN` does not match itself.
|
|
313
|
+
- Throws `Error` for an invalid handle.
|
|
829
314
|
|
|
830
|
-
|
|
315
|
+
### `lastIndexOf(handler, searchElement, fromIndex = -1)`
|
|
831
316
|
|
|
832
|
-
|
|
317
|
+
Returns the last index of `searchElement` in the occupied range, searching in descending index order.
|
|
833
318
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
319
|
+
| Parameter | Type | Default | Description |
|
|
320
|
+
|---|---|---:|---|
|
|
321
|
+
| `handler` | `number` | — | Block handle. |
|
|
322
|
+
| `searchElement` | `number \| bigint` | — | Value to find. |
|
|
323
|
+
| `fromIndex` | `number` | `-1` | Starting index, interpreted according to typed-array `lastIndexOf()` semantics. |
|
|
837
324
|
|
|
838
|
-
|
|
325
|
+
- Return: `number`; returns `-1` when no match exists.
|
|
326
|
+
- The default `-1` starts at the last occupied element.
|
|
327
|
+
- Searches only the occupied range.
|
|
328
|
+
- Uses strict equality semantics.
|
|
329
|
+
- Throws `Error` for an invalid handle.
|
|
839
330
|
|
|
840
|
-
|
|
331
|
+
## 8. Bulk data operations
|
|
841
332
|
|
|
842
|
-
|
|
333
|
+
### `clear(handler, value = 0)`
|
|
843
334
|
|
|
844
|
-
|
|
335
|
+
Fills every physical slot with `value` and then sets occupancy to zero.
|
|
845
336
|
|
|
846
|
-
|
|
337
|
+
| Parameter | Type | Default | Description |
|
|
338
|
+
|---|---|---:|---|
|
|
339
|
+
| `handler` | `number` | — | Block handle. |
|
|
340
|
+
| `value` | `number \| bigint` | `0` | Fill value. |
|
|
847
341
|
|
|
848
|
-
|
|
342
|
+
The operation affects both occupied and unoccupied slots. It does not change capacity.
|
|
849
343
|
|
|
850
|
-
|
|
344
|
+
- Return: `undefined`.
|
|
345
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
851
346
|
|
|
852
|
-
|
|
853
|
-
2. `destiny` capacity;
|
|
854
|
-
3. `slots`.
|
|
347
|
+
### `fill(handler, value, start = 0, end = undefined)`
|
|
855
348
|
|
|
856
|
-
`
|
|
349
|
+
Fills the half-open range `[start, end)` with `value`. If `end` is `undefined`, it defaults to the physical capacity.
|
|
857
350
|
|
|
858
|
-
|
|
859
|
-
|
|
351
|
+
| Parameter | Type | Default | Description |
|
|
352
|
+
|---|---|---:|---|
|
|
353
|
+
| `handler` | `number` | — | Block handle. |
|
|
354
|
+
| `value` | `number \| bigint` | — | Fill value. |
|
|
355
|
+
| `start` | `number` | `0` | Inclusive start index. |
|
|
356
|
+
| `end` | `number \| undefined` | capacity | Exclusive end index. |
|
|
860
357
|
|
|
861
|
-
|
|
358
|
+
Validation:
|
|
862
359
|
|
|
863
|
-
|
|
360
|
+
- `start` and the effective `end` must be safe integers.
|
|
361
|
+
- The range must satisfy `0 <= start <= end <= capacity`.
|
|
362
|
+
- An invalid range throws `RangeError`.
|
|
363
|
+
- An invalid handle or borrowed block throws `Error`.
|
|
864
364
|
|
|
865
|
-
|
|
866
|
-
memory.copy(source, destiny, 8);
|
|
365
|
+
After the operation, occupancy becomes `max(previousOccupancy, end)`, even when the range is empty and no elements are written. Therefore, an empty range whose `end` is greater than the previous occupancy still increases occupancy to `end`. Filling an earlier range does not reduce occupancy.
|
|
867
366
|
|
|
868
|
-
|
|
367
|
+
Return: `undefined`.
|
|
869
368
|
|
|
870
|
-
###
|
|
369
|
+
### `fillAll(handler, value)`
|
|
871
370
|
|
|
872
|
-
|
|
873
|
-
Uint8Array
|
|
371
|
+
Fills every physical slot with `value` and sets occupancy to the full capacity.
|
|
874
372
|
|
|
875
|
-
|
|
373
|
+
- Return: `undefined`.
|
|
374
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
876
375
|
|
|
877
|
-
|
|
376
|
+
Unlike `clear()`, the block is full after this operation.
|
|
878
377
|
|
|
879
|
-
|
|
378
|
+
### `put(handler, data)`
|
|
880
379
|
|
|
881
|
-
|
|
380
|
+
Copies values from an `Array` or a typed-array instance to the beginning of the block. The number of copied elements is limited to the destination capacity.
|
|
882
381
|
|
|
883
|
-
|
|
382
|
+
| Parameter | Type | Description |
|
|
383
|
+
|---|---|---|
|
|
384
|
+
| `handler` | `number` | Destination block handle. |
|
|
385
|
+
| `data` | `Array \| TypedArray` | Source sequence. `DataView` is not accepted as a typed array. |
|
|
884
386
|
|
|
885
|
-
|
|
387
|
+
For an ordinary array, the implementation copies a slice containing the values that fit. For a typed array, it passes a bounded `subarray()` to the destination typed array's `set()` method.
|
|
886
388
|
|
|
887
|
-
|
|
888
|
-
memory.copyWithin(handler, 4, 0, 4);
|
|
389
|
+
Occupancy becomes `max(previousOccupancy, copiedElementCount)`. Consequently, writing a shorter sequence does not reduce prior occupancy, and slots after the copied sequence are not cleared.
|
|
889
390
|
|
|
890
|
-
|
|
391
|
+
- Return: `undefined`.
|
|
392
|
+
- Throws `TypeError` if `data` is neither an ordinary array nor a typed array.
|
|
393
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
394
|
+
- Native typed-array conversion errors, including incompatible Number/BigInt element conversions, propagate.
|
|
891
395
|
|
|
892
|
-
|
|
396
|
+
### `pick(handler)`
|
|
893
397
|
|
|
894
|
-
|
|
398
|
+
Returns a new typed array containing a copy of the occupied elements only.
|
|
895
399
|
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
400
|
+
- Return: a typed array with the same concrete constructor as the block.
|
|
401
|
+
- The returned array has independent storage; changing it does not change the block.
|
|
402
|
+
- An empty block produces an empty typed array of the block's element type.
|
|
403
|
+
- Throws `Error` for an invalid handle.
|
|
404
|
+
- Does not reject a borrowed block.
|
|
901
405
|
|
|
902
|
-
|
|
406
|
+
## 9. Scoped memory views
|
|
903
407
|
|
|
904
|
-
|
|
905
|
-
Uint8Array
|
|
408
|
+
### `withView(handler, callback)`
|
|
906
409
|
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
The `dataBus`.
|
|
410
|
+
### `withView(handler1, handler2, ..., callback)`
|
|
910
411
|
|
|
911
|
-
|
|
412
|
+
Runs a synchronous callback with zero-copy typed-array views over one or more blocks. The callback receives one view per handle, in the same order as the handles supplied.
|
|
912
413
|
|
|
913
|
-
|
|
414
|
+
Each view covers only the occupied elements, not the unused capacity. The view shares storage with its block: writes through the view immediately modify the block.
|
|
914
415
|
|
|
915
|
-
|
|
416
|
+
| Argument | Type | Description |
|
|
417
|
+
|---|---|---|
|
|
418
|
+
| `handler...` | `number` | One or more distinct block handles. |
|
|
419
|
+
| `callback` | `Function` | Final argument. Receives one typed-array view per handle. |
|
|
916
420
|
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
```js
|
|
920
|
-
const count = memory.append(target, source);
|
|
421
|
+
Return: exactly the value returned by the callback.
|
|
921
422
|
|
|
922
|
-
|
|
423
|
+
Validation and lifetime:
|
|
923
424
|
|
|
924
|
-
|
|
425
|
+
- At least one handle and a callback are required. Otherwise, `TypeError` is thrown.
|
|
426
|
+
- The final argument must be a function; otherwise, `TypeError` is thrown.
|
|
427
|
+
- Each handle must exist, must not be repeated, and must not already be borrowed. Violations throw `Error`.
|
|
428
|
+
- Blocks are marked borrowed before the callback is invoked.
|
|
429
|
+
- All successfully acquired borrow flags are released in a `finally` block, including when validation of a later handle fails or the callback throws.
|
|
430
|
+
- Exceptions thrown by the callback propagate unchanged.
|
|
431
|
+
- If the callback returns a Promise, the borrow is released as soon as the callback returns that Promise. The borrow is not extended until the Promise settles.
|
|
925
432
|
|
|
926
|
-
|
|
927
|
-
number
|
|
433
|
+
Do not retain, return, or use a borrowed view (or a subview derived from it) after the callback finishes.
|
|
928
434
|
|
|
929
|
-
|
|
435
|
+
While a block is borrowed, methods that mutate, resize, free, or exchange that block reject the operation with `Error`. This includes `push()`, `pop()`, `peek()`, `nudge()`, `pull()`, `set()`, `clear()`, `fill()`, `fillAll()`, `put()`, `realloc()`, `shrink()`, `free()`, and `copyWithin()`. `copy()` and `append()` reject a borrowed destination but permit a borrowed source. `swap()` rejects either borrowed block.
|
|
930
436
|
|
|
931
|
-
|
|
437
|
+
Read-only operations such as `get()`, `has()`, `indexOf()`, `lastIndexOf()`, `length()`, `occupied()`, `byteSize()`, `isFull()`, `pick()`, `dump()`, and `head()` do not all share the same borrow check; see each method's individual behavior. In particular, `peek()` rejects a borrowed block, whereas `head()` does not.
|
|
932
438
|
|
|
933
|
-
|
|
439
|
+
Example with one block:
|
|
934
440
|
|
|
935
441
|
```js
|
|
936
|
-
memory.
|
|
937
|
-
|
|
442
|
+
const result = memory.withView(handler, view => {
|
|
443
|
+
view[0] = 42;
|
|
444
|
+
return view.length;
|
|
445
|
+
});
|
|
938
446
|
```
|
|
939
447
|
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
---
|
|
943
|
-
|
|
944
|
-
## `clone(handler)`
|
|
945
|
-
|
|
946
|
-
Creates an independent copy of the block.
|
|
947
|
-
|
|
948
|
-
The clone possesses:
|
|
949
|
-
|
|
950
|
-
* same physical capacity;
|
|
951
|
-
* same occupancy;
|
|
952
|
-
* same bytes across the entire reserved region;
|
|
953
|
-
* new handler.
|
|
448
|
+
Example with two blocks:
|
|
954
449
|
|
|
955
450
|
```js
|
|
956
|
-
|
|
957
|
-
|
|
451
|
+
memory.withView(source, destination, (sourceView, destinationView) => {
|
|
452
|
+
destinationView.set(sourceView.subarray(0, destinationView.length));
|
|
453
|
+
});
|
|
958
454
|
```
|
|
959
455
|
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
---
|
|
963
|
-
|
|
964
|
-
## `swap(source, target)`
|
|
965
|
-
|
|
966
|
-
Swaps the data regions associated with two handlers.
|
|
967
|
-
|
|
968
|
-
The operation swaps in headers:
|
|
969
|
-
|
|
970
|
-
* address;
|
|
971
|
-
* capacity;
|
|
972
|
-
* occupancy.
|
|
456
|
+
## 10. Operations between blocks
|
|
973
457
|
|
|
974
|
-
|
|
458
|
+
### `copy(source, destination, slots = 0xFFFFFFFF)`
|
|
975
459
|
|
|
976
|
-
|
|
977
|
-
memory.swap(a, b);
|
|
460
|
+
Copies occupied elements from the source block to the beginning of the destination block.
|
|
978
461
|
|
|
979
|
-
|
|
462
|
+
| Parameter | Type | Default | Description |
|
|
463
|
+
|---|---|---:|---|
|
|
464
|
+
| `source` | `number` | — | Source block handle. |
|
|
465
|
+
| `destination` | `number` | — | Destination block handle. |
|
|
466
|
+
| `slots` | `number` | `0xFFFFFFFF` | Maximum number of elements to copy. |
|
|
980
467
|
|
|
981
|
-
|
|
468
|
+
The copied count is:
|
|
982
469
|
|
|
983
470
|
```text
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
handler A → region A
|
|
987
|
-
handler B → region B
|
|
988
|
-
|
|
989
|
-
after:
|
|
990
|
-
|
|
991
|
-
handler A → region B
|
|
992
|
-
handler B → region A
|
|
993
|
-
|
|
471
|
+
min(source.occupied, destination.capacity, slots)
|
|
994
472
|
```
|
|
995
473
|
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
# 7. Serialization
|
|
474
|
+
The destination typed array's `set()` method performs element conversion. Destination occupancy becomes `max(previousDestinationOccupancy, copiedCount)`; it is not reduced if fewer elements are copied than were previously occupied.
|
|
999
475
|
|
|
1000
|
-
|
|
476
|
+
- Return: the destination block's full typed-array view, not a copy.
|
|
477
|
+
- `slots` must be a non-negative safe integer; otherwise, `RangeError` is thrown.
|
|
478
|
+
- Invalid source or destination handles throw `Error`.
|
|
479
|
+
- A borrowed destination throws `Error`; a borrowed source is allowed.
|
|
480
|
+
- Source and destination may be the same handle.
|
|
1001
481
|
|
|
1002
|
-
|
|
482
|
+
### `copyWithin(handler, target, start, length)`
|
|
1003
483
|
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
The method extracts the occupied data from the block, groups it alongside capacity and word type information into a data envelope, and encodes it using the CBOR format.
|
|
1007
|
-
|
|
1008
|
-
### Return
|
|
1009
|
-
|
|
1010
|
-
```text
|
|
1011
|
-
Uint8Array
|
|
484
|
+
Copies a range within one block, supporting overlapping source and destination ranges.
|
|
1012
485
|
|
|
486
|
+
| Parameter | Type | Description |
|
|
487
|
+
|---|---|---|
|
|
488
|
+
| `handler` | `number` | Block handle. |
|
|
489
|
+
| `target` | `number` | Destination start index. |
|
|
490
|
+
| `start` | `number` | Source start index. |
|
|
491
|
+
| `length` | `number` | Maximum number of elements to copy. |
|
|
1013
492
|
|
|
1014
|
-
|
|
493
|
+
Normalization and bounds:
|
|
1015
494
|
|
|
1016
|
-
|
|
495
|
+
1. All three numeric arguments must be finite; otherwise, `RangeError` is thrown.
|
|
496
|
+
2. Values are truncated toward zero.
|
|
497
|
+
3. Negative `target` and `start` values are interpreted relative to physical capacity.
|
|
498
|
+
4. `target` and `start` are clamped to the inclusive range from zero to capacity.
|
|
499
|
+
5. The effective copy length is clamped to zero or greater, the number of occupied source elements available after `start`, and the destination space available after `target`.
|
|
1017
500
|
|
|
1018
|
-
|
|
501
|
+
The operation updates occupancy to `max(previousOccupancy, target + effectiveLength)`. If the effective length is zero, it returns the block's typed-array view without changing occupancy.
|
|
1019
502
|
|
|
1020
|
-
|
|
503
|
+
- Return: the block's full typed-array view.
|
|
504
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
505
|
+
- Throws `RangeError` if any argument is not finite.
|
|
1021
506
|
|
|
1022
|
-
|
|
507
|
+
This method operates on physical-capacity indices but never reads beyond the occupied source range.
|
|
1023
508
|
|
|
1024
|
-
|
|
509
|
+
### `append(target, source)`
|
|
1025
510
|
|
|
1026
|
-
|
|
511
|
+
Appends occupied source elements after the target's occupied sequence.
|
|
1027
512
|
|
|
1028
|
-
|
|
513
|
+
| Parameter | Type | Description |
|
|
514
|
+
|---|---|---|
|
|
515
|
+
| `target` | `number` | Destination block handle. |
|
|
516
|
+
| `source` | `number` | Source block handle. |
|
|
1029
517
|
|
|
1030
|
-
|
|
518
|
+
The appended count is the smaller of source occupancy and the free capacity remaining in the destination:
|
|
1031
519
|
|
|
1032
520
|
```text
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
521
|
+
min(source.occupied, target.capacity - target.occupied)
|
|
1036
522
|
```
|
|
1037
523
|
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
### Exceptions
|
|
1041
|
-
|
|
1042
|
-
`Error` — if the word type of the decoded data diverges from the one configured in the allocator.
|
|
1043
|
-
|
|
1044
|
-
---
|
|
1045
|
-
|
|
1046
|
-
# 8. Diagnostics and Utilization
|
|
1047
|
-
|
|
1048
|
-
## `recycleCount()`
|
|
1049
|
-
|
|
1050
|
-
Returns the quantity of regions currently stored in the recycling system.
|
|
524
|
+
The destination occupancy increases by the appended count.
|
|
1051
525
|
|
|
1052
|
-
|
|
526
|
+
- Return: `number`, the number of elements actually appended.
|
|
527
|
+
- Returns `0` if no elements can be appended.
|
|
528
|
+
- Invalid handles throw `Error`.
|
|
529
|
+
- A borrowed destination throws `Error`; a borrowed source is allowed.
|
|
530
|
+
- The same handle may be supplied for both arguments. In this case, the occupied prefix is copied to the free region at the end of the same block.
|
|
1053
531
|
|
|
1054
|
-
|
|
1055
|
-
const count = memory.recycleCount();
|
|
1056
|
-
|
|
1057
|
-
```
|
|
1058
|
-
|
|
1059
|
-
---
|
|
532
|
+
### `clone(handler)`
|
|
1060
533
|
|
|
1061
|
-
|
|
534
|
+
Creates a new block with independent storage and copies the source block's entire typed-array contents, including unoccupied slots. The clone preserves the source occupancy and bucket type.
|
|
1062
535
|
|
|
1063
|
-
|
|
536
|
+
- Parameter: `handler`, source block handle (`number`).
|
|
537
|
+
- Return: `number`, the new block's handle.
|
|
538
|
+
- Invalid source handle throws `Error`.
|
|
539
|
+
- Allocation constraints still apply. If the source byte size is not a valid allocation size under this allocator's `maxByteSize`, or the handle space is exhausted, the allocation error propagates.
|
|
1064
540
|
|
|
1065
|
-
|
|
1066
|
-
const bytes = memory.recycleBytes;
|
|
1067
|
-
|
|
1068
|
-
```
|
|
541
|
+
### `swap(source, target)`
|
|
1069
542
|
|
|
1070
|
-
|
|
543
|
+
Exchanges the block objects associated with two handles in the allocator registry. It does not copy element data and does not change the numeric handle values. After the operation, each handle refers to the block object previously associated with the other handle.
|
|
1071
544
|
|
|
1072
|
-
|
|
1073
|
-
|
|
545
|
+
| Parameter | Type | Description |
|
|
546
|
+
|---|---|---|
|
|
547
|
+
| `source` | `number` | First block handle. |
|
|
548
|
+
| `target` | `number` | Second block handle. |
|
|
1074
549
|
|
|
1075
|
-
|
|
550
|
+
- Return: `undefined`.
|
|
551
|
+
- Invalid handles throw `Error`.
|
|
552
|
+
- If either block is borrowed, throws `Error`.
|
|
553
|
+
- Passing the same handle twice is a no-op.
|
|
1076
554
|
|
|
1077
|
-
|
|
555
|
+
## 11. Serialization
|
|
1078
556
|
|
|
1079
|
-
|
|
557
|
+
Balloc serializes blocks as CBOR version 2 envelopes. The encoded envelope contains the bucket identifier, physical capacity in elements, occupancy, and a typed-array payload containing only occupied elements.
|
|
1080
558
|
|
|
1081
|
-
|
|
559
|
+
### `dump(handler)`
|
|
1082
560
|
|
|
1083
|
-
|
|
561
|
+
Serializes a block.
|
|
1084
562
|
|
|
1085
|
-
|
|
1086
|
-
|
|
563
|
+
- Parameter: `handler`, block handle (`number`).
|
|
564
|
+
- Return: `Uint8Array`, CBOR-encoded envelope.
|
|
565
|
+
- The payload is a copy of the occupied prefix; unused capacity is not serialized.
|
|
566
|
+
- Throws `Error` for an invalid handle or inconsistent capacity/occupancy metadata.
|
|
567
|
+
- Does not reject a borrowed block.
|
|
1087
568
|
|
|
1088
|
-
|
|
569
|
+
Envelope structure:
|
|
1089
570
|
|
|
1090
571
|
```js
|
|
1091
|
-
|
|
1092
|
-
|
|
572
|
+
{
|
|
573
|
+
v: 2,
|
|
574
|
+
bucketType: "32u",
|
|
575
|
+
capacity: 16, // elements, not bytes
|
|
576
|
+
occupied: 3,
|
|
577
|
+
payload: Uint32Array([10, 20, 30])
|
|
578
|
+
}
|
|
1093
579
|
```
|
|
1094
580
|
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
## `unallocatedBytes`
|
|
1098
|
-
|
|
1099
|
-
Getter that returns space still available at the top of the `dataBus`.
|
|
581
|
+
The object above illustrates the logical fields before CBOR encoding; `dump()` returns the encoded bytes, not this JavaScript object.
|
|
1100
582
|
|
|
1101
|
-
|
|
583
|
+
### `load(serialized)`
|
|
1102
584
|
|
|
1103
|
-
|
|
1104
|
-
size - dataPointer
|
|
1105
|
-
|
|
1106
|
-
```
|
|
1107
|
-
|
|
1108
|
-
```js
|
|
1109
|
-
const bytes = memory.unallocatedBytes;
|
|
1110
|
-
|
|
1111
|
-
```
|
|
585
|
+
Decodes a CBOR envelope and allocates a new block from it.
|
|
1112
586
|
|
|
1113
|
-
|
|
587
|
+
| Parameter | Type | Description |
|
|
588
|
+
|---|---|---|
|
|
589
|
+
| `serialized` | `ArrayBuffer \| Uint8Array` | CBOR bytes produced from a compatible version 2 envelope. |
|
|
1114
590
|
|
|
1115
|
-
|
|
591
|
+
Validation is performed in this order:
|
|
1116
592
|
|
|
1117
|
-
|
|
593
|
+
1. The input must be an `ArrayBuffer` or `Uint8Array`; otherwise, `TypeError` is thrown.
|
|
594
|
+
2. The decoded value must be a non-null object and not an array; otherwise, `TypeError` is thrown.
|
|
595
|
+
3. The envelope version must equal `2`; otherwise, `Error` is thrown.
|
|
596
|
+
4. The bucket identifier must be supported; otherwise, `TypeError` is thrown.
|
|
597
|
+
5. If the allocator was constructed with a fixed `bucketSize`, the envelope bucket type must match it; otherwise, `Error` is thrown.
|
|
598
|
+
6. Capacity must be a safe integer in `[2, 2 ** 31]`; otherwise, `RangeError` is thrown.
|
|
599
|
+
7. Occupancy must be a safe integer in `[0, capacity]`; otherwise, `RangeError` is thrown.
|
|
600
|
+
8. Payload must be a typed array of the exact constructor corresponding to the bucket type, with length equal to occupancy; otherwise, `TypeError` is thrown.
|
|
601
|
+
9. Payload element width and byte length must match the selected bucket type; otherwise, `TypeError` is thrown.
|
|
1118
602
|
|
|
1119
|
-
|
|
1120
|
-
recycleBytes / dataPointer
|
|
603
|
+
After validation, capacity in elements is converted to bytes and passed to `alloc()`. The payload is copied into the new block and occupancy is restored.
|
|
1121
604
|
|
|
1122
|
-
|
|
605
|
+
- Return: `number`, handle of the newly allocated block.
|
|
606
|
+
- CBOR decoder errors propagate.
|
|
607
|
+
- Allocation errors (including `RangeError` when the capacity exceeds this allocator's configured limit) propagate.
|
|
608
|
+
- The serialized block's original handle is not restored; the new block receives a new handle.
|
|
1123
609
|
|
|
1124
|
-
|
|
610
|
+
Example:
|
|
1125
611
|
|
|
1126
612
|
```js
|
|
1127
|
-
const
|
|
1128
|
-
|
|
613
|
+
const serialized = memory.dump(handler);
|
|
614
|
+
const restoredHandler = memory.load(serialized);
|
|
1129
615
|
```
|
|
1130
616
|
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
---
|
|
617
|
+
## 12. Data-ingestion adapters
|
|
1134
618
|
|
|
1135
|
-
|
|
619
|
+
### `loadData(adapter)`
|
|
1136
620
|
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
```text
|
|
1140
|
-
Internal usage
|
|
1141
|
-
├── Recycling
|
|
1142
|
-
│ ├── recyclePush()
|
|
1143
|
-
│ └── recyclePop()
|
|
1144
|
-
│
|
|
1145
|
-
├── Scaling
|
|
1146
|
-
│ ├── grow()
|
|
1147
|
-
│ └── headerGrow()
|
|
1148
|
-
│
|
|
1149
|
-
└── Validation / metadata
|
|
1150
|
-
├── extractMetadata()
|
|
1151
|
-
└── bakeCookie()
|
|
1152
|
-
|
|
1153
|
-
```
|
|
621
|
+
Delegates synchronous ingestion to an adapter object. Balloc calls `adapter.load(this)` and returns its result unchanged.
|
|
1154
622
|
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
### `recyclePush(capacity, address)`
|
|
1158
|
-
|
|
1159
|
-
**Private.**
|
|
1160
|
-
|
|
1161
|
-
Inserts a free region into the recycling list associated with its capacity in bytes.
|
|
1162
|
-
|
|
1163
|
-
Each capacity has a page represented by `Uint32Array`:
|
|
1164
|
-
|
|
1165
|
-
```text
|
|
1166
|
-
[count, address1, address2, ...]
|
|
1167
|
-
|
|
1168
|
-
```
|
|
1169
|
-
|
|
1170
|
-
The structure operates as a LIFO stack. When the page becomes full, its buffer expands to double.
|
|
1171
|
-
|
|
1172
|
-
---
|
|
1173
|
-
|
|
1174
|
-
### `recyclePop(capacity)`
|
|
1175
|
-
|
|
1176
|
-
**Private.**
|
|
1177
|
-
|
|
1178
|
-
Removes and returns a recyclable region of exact capacity.
|
|
1179
|
-
|
|
1180
|
-
Removal occurs from the end of the page, characterizing LIFO behavior.
|
|
1181
|
-
|
|
1182
|
-
Returns:
|
|
1183
|
-
|
|
1184
|
-
```text
|
|
1185
|
-
address
|
|
1186
|
-
|
|
1187
|
-
```
|
|
1188
|
-
|
|
1189
|
-
or:
|
|
1190
|
-
|
|
1191
|
-
```js
|
|
1192
|
-
UNKNOWN // 0xFFFFFFFF
|
|
623
|
+
Adapter contract:
|
|
1193
624
|
|
|
625
|
+
```ts
|
|
626
|
+
interface FormatAdapter<Result = unknown> {
|
|
627
|
+
load(allocator: Balloc): Result;
|
|
628
|
+
}
|
|
1194
629
|
```
|
|
1195
630
|
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
## 9.2 Scaling
|
|
1201
|
-
|
|
1202
|
-
### `grow()`
|
|
1203
|
-
|
|
1204
|
-
**Private. Do not use directly.**
|
|
1205
|
-
|
|
1206
|
-
Doubles `dataBus` capacity.
|
|
1207
|
-
|
|
1208
|
-
The underlying `ArrayBuffer` is transferred to a larger buffer and associated views are recreated.
|
|
1209
|
-
|
|
1210
|
-
Triggered automatically when a new region does not fit in remaining `dataBus` space.
|
|
1211
|
-
|
|
1212
|
-
---
|
|
1213
|
-
|
|
1214
|
-
### `headerGrow()`
|
|
1215
|
-
|
|
1216
|
-
**Private. Do not use directly.**
|
|
1217
|
-
|
|
1218
|
-
Doubles `controlBus` capacity.
|
|
1219
|
-
|
|
1220
|
-
Existing headers are preserved during `ArrayBuffer` transfer, and `controlAdapter` is recreated over the new storage.
|
|
1221
|
-
|
|
1222
|
-
Triggered automatically when there is no space for a new header.
|
|
1223
|
-
|
|
1224
|
-
---
|
|
1225
|
-
|
|
1226
|
-
## 9.3 Validation and Metadata
|
|
1227
|
-
|
|
1228
|
-
### `extractMetadata(handler, out)`
|
|
1229
|
-
|
|
1230
|
-
**Private.**
|
|
1231
|
-
|
|
1232
|
-
Decodes and validates a handler.
|
|
1233
|
-
|
|
1234
|
-
The method extracts:
|
|
1235
|
-
|
|
1236
|
-
```text
|
|
1237
|
-
generation
|
|
1238
|
-
headerAddress
|
|
1239
|
-
dataAddress
|
|
1240
|
-
capacityExponent
|
|
1241
|
-
byteCapacity
|
|
1242
|
-
occupied
|
|
1243
|
-
|
|
1244
|
-
```
|
|
631
|
+
| Parameter | Type | Description |
|
|
632
|
+
|---|---|---|
|
|
633
|
+
| `adapter` | `FormatAdapter` | Object with a callable `load(allocator)` method. |
|
|
1245
634
|
|
|
1246
|
-
|
|
635
|
+
- Return: exactly the value returned by `adapter.load(this)`; Balloc does not validate the result.
|
|
636
|
+
- Throws `TypeError` if the adapter is falsy or `adapter.load` is not a function.
|
|
637
|
+
- Exceptions thrown by the adapter propagate unchanged.
|
|
638
|
+
- The method is synchronous. If the adapter returns a Promise, that Promise is returned as-is and is not awaited.
|
|
639
|
+
- Balloc does not provide transaction or rollback behavior. The adapter is responsible for cleaning up blocks it allocated if ingestion fails.
|
|
1247
640
|
|
|
1248
|
-
|
|
641
|
+
Example:
|
|
1249
642
|
|
|
1250
643
|
```js
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
Metadata
|
|
1259
|
-
|
|
1260
|
-
```
|
|
1261
|
-
|
|
1262
|
-
The return is the reference provided in `out` itself.
|
|
1263
|
-
|
|
1264
|
-
---
|
|
1265
|
-
|
|
1266
|
-
### `bakeCookie(addr, gen)`
|
|
1267
|
-
|
|
1268
|
-
**Private.**
|
|
1269
|
-
|
|
1270
|
-
Calculates the validation cookie used to verify handlers.
|
|
1271
|
-
|
|
1272
|
-
The cookie combines:
|
|
1273
|
-
|
|
1274
|
-
* header address;
|
|
1275
|
-
* lower 11 bits of generation.
|
|
1276
|
-
|
|
1277
|
-
The result is reduced to 8 bits. The value `0` is reserved to indicate an invalid header, so the method replaces `0` with `1`.
|
|
1278
|
-
|
|
1279
|
-
The cookie is not cryptographic. Its function is to detect invalid, stale, or corrupted handlers within the allocator management model.
|
|
1280
|
-
|
|
1281
|
-
---
|
|
1282
|
-
|
|
1283
|
-
# 10. Storage Model
|
|
1284
|
-
|
|
1285
|
-
Each block features a fixed **16-byte** header:
|
|
1286
|
-
|
|
1287
|
-
```text
|
|
1288
|
-
offset size field
|
|
1289
|
-
────── ─────── ─────────────
|
|
1290
|
-
0 4 bytes address
|
|
1291
|
-
4 4 bytes occupied
|
|
1292
|
-
8 1 byte capacity
|
|
1293
|
-
9 1 byte cookie
|
|
1294
|
-
10 6 bytes reserved
|
|
1295
|
-
|
|
1296
|
-
```
|
|
1297
|
-
|
|
1298
|
-
Relevant fields are:
|
|
1299
|
-
|
|
1300
|
-
### `address`
|
|
1301
|
-
|
|
1302
|
-
Initial address of the data region inside `dataBus`.
|
|
1303
|
-
|
|
1304
|
-
### `occupied`
|
|
1305
|
-
|
|
1306
|
-
Number of currently occupied elements.
|
|
1307
|
-
|
|
1308
|
-
### `capacity`
|
|
1309
|
-
|
|
1310
|
-
Power of two exponent representing physical capacity:
|
|
1311
|
-
|
|
1312
|
-
```text
|
|
1313
|
-
capacityElements = 2 ** capacity
|
|
1314
|
-
|
|
1315
|
-
```
|
|
1316
|
-
|
|
1317
|
-
Capacity in bytes is:
|
|
1318
|
-
|
|
1319
|
-
```text
|
|
1320
|
-
capacityBytes = 2 ** capacity × wordSize
|
|
1321
|
-
|
|
644
|
+
const handler = memory.loadData({
|
|
645
|
+
load(allocator) {
|
|
646
|
+
const block = allocator.alloc(4, "8u");
|
|
647
|
+
allocator.put(block, new Uint8Array([255, 0, 0, 255]));
|
|
648
|
+
return block;
|
|
649
|
+
}
|
|
650
|
+
});
|
|
1322
651
|
```
|
|
1323
652
|
|
|
1324
|
-
|
|
653
|
+
## 13. Error behavior and borrowing matrix
|
|
1325
654
|
|
|
1326
|
-
|
|
655
|
+
Unless a method documents a different exception, an unknown or freed handle causes `Error`.
|
|
1327
656
|
|
|
1328
|
-
|
|
657
|
+
| Method group | Invalid handle | Borrowed block |
|
|
658
|
+
|---|---|---|
|
|
659
|
+
| `realloc()`, `shrink()`, `free()` | `Error` | `Error` |
|
|
660
|
+
| `push()`, `pop()`, `peek()`, `nudge()`, `pull()` | `Error` | `Error` |
|
|
661
|
+
| `set()`, `clear()`, `fill()`, `fillAll()`, `put()`, `copyWithin()` | `Error` | `Error` |
|
|
662
|
+
| `copy()`, `append()` | `Error` for either handle | `Error` when destination is borrowed; borrowed source is permitted |
|
|
663
|
+
| `swap()` | `Error` for either handle | `Error` if either block is borrowed |
|
|
664
|
+
| `get()`, `has()`, `indexOf()`, `lastIndexOf()` | `Error` | Permitted |
|
|
665
|
+
| `length()`, `occupied()`, `byteSize()`, `isFull()`, `pick()`, `dump()`, `head()` | `Error` | Permitted |
|
|
666
|
+
| `withView()` | `Error` for invalid, duplicate, or already borrowed handles | Cannot borrow an already borrowed block |
|
|
1329
667
|
|
|
1330
|
-
|
|
668
|
+
Additional validation exceptions:
|
|
1331
669
|
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
|
1352
|
-
|
|
|
1353
|
-
|
|
|
1354
|
-
|
|
|
1355
|
-
|
|
|
1356
|
-
|
|
|
1357
|
-
|
|
|
1358
|
-
|
|
|
1359
|
-
|
|
|
1360
|
-
|
|
|
1361
|
-
|
|
|
1362
|
-
|
|
|
1363
|
-
|
|
|
1364
|
-
|
|
|
1365
|
-
|
|
|
1366
|
-
|
|
|
1367
|
-
|
|
|
1368
|
-
|
|
|
1369
|
-
|
|
|
1370
|
-
|
|
|
1371
|
-
|
|
|
1372
|
-
|
|
|
1373
|
-
|
|
|
1374
|
-
|
|
|
1375
|
-
|
|
|
1376
|
-
|
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
| Blocks | `copyWithin()` | `Uint8Array`|
|
|
1382
|
-
| Blocks | `append()` | `number`|
|
|
1383
|
-
| Blocks | `clone()` | `number`|
|
|
1384
|
-
| Blocks | `swap()` | `void`|
|
|
1385
|
-
| Serialization | `dump()` | `Uint8Array`|
|
|
1386
|
-
| Serialization | `load()` | `number`|
|
|
1387
|
-
| Diagnostics | `recycleCount()` | `number`|
|
|
1388
|
-
| Diagnostics | `recycleBytes` | `number`|
|
|
1389
|
-
| Diagnostics | `allocatedBytes` | `number`|
|
|
1390
|
-
| Diagnostics | `unallocatedBytes` | `number`|
|
|
1391
|
-
| Diagnostics | `fragmentationRatio` | `number`|
|
|
1392
|
-
|
|
1393
|
-
## Internal Methods
|
|
1394
|
-
|
|
1395
|
-
| Group | Method | Visibility |
|
|
1396
|
-
| --- | --- | --- |
|
|
1397
|
-
| Recycling | `recyclePush()` | private |
|
|
1398
|
-
| Recycling | `recyclePop()` | private |
|
|
1399
|
-
| Scaling | `grow()` | private |
|
|
1400
|
-
| Scaling | `headerGrow()` | private |
|
|
1401
|
-
| Metadata | `extractMetadata()` | private |
|
|
1402
|
-
| Validation | `bakeCookie()` | private |
|
|
670
|
+
| Exception | Conditions |
|
|
671
|
+
|---|---|
|
|
672
|
+
| `RangeError` | Invalid constructor size or bucket type; invalid `alloc()` type/size; invalid `realloc()` capacity; invalid `fill()` range; invalid `copy()` slot count; non-finite `copyWithin()` arguments; invalid serialized capacity or occupancy; allocation limit exceeded. |
|
|
673
|
+
| `TypeError` | Invalid `put()` source; invalid `withView()` argument structure; invalid serialized input, envelope, bucket payload type, or payload length; invalid `loadData()` adapter. |
|
|
674
|
+
| `Error` | Invalid/freed handle; borrowed-block restrictions; unsupported serialization version; fixed bucket type mismatch. |
|
|
675
|
+
|
|
676
|
+
Native JavaScript errors caused by typed-array conversions or CBOR decoding are not wrapped by these methods unless explicitly stated.
|
|
677
|
+
|
|
678
|
+
## 14. Public API summary
|
|
679
|
+
|
|
680
|
+
| Method | Return type | Mutates block state |
|
|
681
|
+
|---|---|---|
|
|
682
|
+
| `alloc(size?, bucketType?)` | `number` | Creates a block |
|
|
683
|
+
| `realloc(handler, newSize?)` | `void` | Replaces storage; preserves occupied values |
|
|
684
|
+
| `shrink(handler)` | `void` | May replace storage and reduce capacity |
|
|
685
|
+
| `free(handler)` | `void` | Removes block |
|
|
686
|
+
| `length(handler)` | `number` | No |
|
|
687
|
+
| `occupied(handler)` | `number` | No |
|
|
688
|
+
| `byteSize(handler)` | `number` | No |
|
|
689
|
+
| `isFull(handler)` | `boolean` | No |
|
|
690
|
+
| `push(handler, value)` | `boolean` | Yes, when successful |
|
|
691
|
+
| `pop(handler)` | `number \| bigint \| undefined` | Yes, when non-empty |
|
|
692
|
+
| `peek(handler)` | `number \| bigint \| undefined` | No |
|
|
693
|
+
| `nudge(handler, value)` | `boolean` | Yes, when successful |
|
|
694
|
+
| `pull(handler)` | `number \| bigint \| undefined` | Yes, when non-empty |
|
|
695
|
+
| `head(handler)` | `number \| bigint \| undefined` | No |
|
|
696
|
+
| `get(handler, index)` | `number \| bigint \| undefined` | No |
|
|
697
|
+
| `set(handler, index, value)` | `boolean` | Yes, when index is occupied |
|
|
698
|
+
| `has(handler, value)` | `boolean` | No |
|
|
699
|
+
| `indexOf(handler, searchElement, fromIndex?)` | `number` | No |
|
|
700
|
+
| `lastIndexOf(handler, searchElement, fromIndex?)` | `number` | No |
|
|
701
|
+
| `clear(handler, value?)` | `void` | Yes |
|
|
702
|
+
| `fill(handler, value, start?, end?)` | `void` | Yes |
|
|
703
|
+
| `fillAll(handler, value)` | `void` | Yes |
|
|
704
|
+
| `put(handler, data)` | `void` | Yes |
|
|
705
|
+
| `pick(handler)` | `TypedArray` | No; returns a copy |
|
|
706
|
+
| `withView(...handlers, callback)` | Callback result | Callback can mutate borrowed storage |
|
|
707
|
+
| `copy(source, destination, slots?)` | `TypedArray` | Yes, destination |
|
|
708
|
+
| `copyWithin(handler, target, start, length)` | `TypedArray` | Yes |
|
|
709
|
+
| `append(target, source)` | `number` | Yes, target |
|
|
710
|
+
| `clone(handler)` | `number` | Creates an independent block |
|
|
711
|
+
| `swap(source, target)` | `void` | Exchanges block references |
|
|
712
|
+
| `dump(handler)` | `Uint8Array` | No; returns serialized copy |
|
|
713
|
+
| `load(serialized)` | `number` | Creates a restored block |
|
|
714
|
+
| `loadData(adapter)` | Adapter result | Depends on adapter |
|
|
715
|
+
|
|
716
|
+
## 15. Scope
|
|
717
|
+
|
|
718
|
+
This reference documents the consumer-facing constructor and methods implemented by Balloc 2.0.0. It intentionally excludes implementation-only fields and helpers. The allocator does not expose the former shared bump-allocation heap, generation-encoded handlers, region recycling, garbage collection, or diagnostic counters described by the pre-2.0 API.
|