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
|
@@ -1,1457 +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
|
-
├── Data Ingestion
|
|
35
|
-
│ └── loadData()
|
|
36
|
-
│
|
|
37
|
-
├── Linear Structures
|
|
38
|
-
│ ├── Stack
|
|
39
|
-
│ │ ├── push()
|
|
40
|
-
│ │ ├── pop()
|
|
41
|
-
│ │ └── peek()
|
|
42
|
-
│ │
|
|
43
|
-
│ └── Queue
|
|
44
|
-
│ ├── nudge()
|
|
45
|
-
│ ├── pull()
|
|
46
|
-
│ └── head()
|
|
47
|
-
│
|
|
48
|
-
├── Content Access
|
|
49
|
-
│ ├── has()
|
|
50
|
-
│ ├── get()
|
|
51
|
-
│ ├── set()
|
|
52
|
-
│ ├── clear()
|
|
53
|
-
│ ├── fill()
|
|
54
|
-
│ ├── fillAll()
|
|
55
|
-
│ ├── put()
|
|
56
|
-
│ ├── pick()
|
|
57
|
-
│ ├── indexOf()
|
|
58
|
-
│ ├── lastIndexOf()
|
|
59
|
-
│ └── withView()
|
|
60
|
-
│
|
|
61
|
-
├── Capacity and Occupancy
|
|
62
|
-
│ ├── byteSize()
|
|
63
|
-
│ ├── occupied()
|
|
64
|
-
│ ├── length()
|
|
65
|
-
│ └── isFull()
|
|
66
|
-
│
|
|
67
|
-
├── Inter-block Operations
|
|
68
|
-
│ ├── copy()
|
|
69
|
-
│ ├── copyWithin()
|
|
70
|
-
│ ├── append()
|
|
71
|
-
│ └── clone()
|
|
72
|
-
│ └── swap()
|
|
73
|
-
│
|
|
74
|
-
├── Serialization
|
|
75
|
-
│ ├── dump()
|
|
76
|
-
│ └── load()
|
|
77
|
-
│
|
|
78
|
-
├── Diagnostics and Utilization
|
|
79
|
-
│ ├── recycleCount()
|
|
80
|
-
│ ├── recycleBytes
|
|
81
|
-
│ ├── allocatedBytes
|
|
82
|
-
│ ├── unallocatedBytes
|
|
83
|
-
│ └── fragmentationRatio
|
|
84
|
-
│
|
|
85
|
-
└── Internal Usage
|
|
86
|
-
├── Recycling
|
|
87
|
-
│ ├── recyclePush()
|
|
88
|
-
│ └── recyclePop()
|
|
89
|
-
│
|
|
90
|
-
├── Scaling
|
|
91
|
-
│ ├── grow()
|
|
92
|
-
│ └── headerGrow()
|
|
93
|
-
│
|
|
94
|
-
└── Utilities
|
|
95
|
-
├── extractMetadata()
|
|
96
|
-
└── bakeCookie()
|
|
97
|
-
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
---
|
|
101
|
-
|
|
102
|
-
## Capacity Semantics
|
|
103
|
-
|
|
104
|
-
The logical capacity unit depends on `bucketSize`.
|
|
105
|
-
|
|
106
|
-
```text
|
|
107
|
-
bucketSize = "8i" or bucketSize = "8u" → 1 byte per element
|
|
108
|
-
bucketSize = "16i" or bucketSize = "16u" or bucketSize = "16f" → 2 bytes per element
|
|
109
|
-
bucketSize = "32i" or bucketSize = "32u" or bucketSize = "32f" → 4 bytes per element
|
|
110
|
-
bucketSize = "64i" or bucketSize = "64u" or bucketSize = "64f" → 8 bytes per element
|
|
111
|
-
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
The allocator computes capacity in **elements** as a power of two and subsequently converts this capacity into bytes.
|
|
1
|
+
# Balloc 2.0.0 — Public API Reference
|
|
115
2
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
```text
|
|
119
|
-
capacity = 8 elements
|
|
120
|
-
wordSize = 4 bytes
|
|
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.
|
|
121
4
|
|
|
122
|
-
|
|
5
|
+
Balloc manages independent typed memory blocks. Each block owns an `ArrayBuffer`, has a numeric handle, and tracks physical capacity separately from logical occupancy.
|
|
123
6
|
|
|
124
|
-
|
|
7
|
+
## API map
|
|
125
8
|
|
|
126
|
-
|
|
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()` |
|
|
127
22
|
|
|
128
|
-
|
|
129
|
-
length(handler) → physical slots
|
|
130
|
-
occupied(handler) → occupied slots
|
|
131
|
-
byteSize(handler) → reserved physical bytes
|
|
23
|
+
## 1. Construction
|
|
132
24
|
|
|
133
|
-
|
|
25
|
+
### `new Balloc(size = null, bucketSize = null)`
|
|
134
26
|
|
|
135
|
-
|
|
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`.
|
|
136
28
|
|
|
137
|
-
|
|
29
|
+
| Parameter | Type | Default | Description |
|
|
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. |
|
|
138
33
|
|
|
139
|
-
|
|
34
|
+
Validation:
|
|
140
35
|
|
|
141
|
-
|
|
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`.
|
|
142
39
|
|
|
143
|
-
|
|
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.
|
|
144
41
|
|
|
145
|
-
|
|
42
|
+
Example:
|
|
146
43
|
|
|
147
44
|
```js
|
|
148
|
-
|
|
149
|
-
view[0] = 42;
|
|
150
|
-
return view.length;
|
|
151
|
-
});
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
Writes through the view immediately affect the block. No copy is made, and the callback's return value is returned by `withView()`.
|
|
155
|
-
|
|
156
|
-
### Multiple blocks
|
|
157
|
-
|
|
158
|
-
Pass two or more handlers before the callback. The callback receives one view per handler, in the same order:
|
|
45
|
+
import Balloc from "balloc";
|
|
159
46
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
targetView.set(sourceView.subarray(0, targetView.length));
|
|
163
|
-
});
|
|
47
|
+
const memory = new Balloc();
|
|
48
|
+
const smallMemory = new Balloc(1024, "16u");
|
|
164
49
|
```
|
|
165
50
|
|
|
166
|
-
|
|
51
|
+
## 2. Element types and capacity units
|
|
167
52
|
|
|
168
|
-
|
|
53
|
+
A bucket identifier uses the format `[bit width][type suffix]`.
|
|
169
54
|
|
|
170
|
-
|
|
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` |
|
|
171
60
|
|
|
172
|
-
|
|
61
|
+
The selected typed-array constructor must exist in the JavaScript runtime. In particular, `Float16Array` requires runtime support for that constructor.
|
|
173
62
|
|
|
174
|
-
|
|
63
|
+
| Bucket width | Bytes per element |
|
|
64
|
+
|---:|---:|
|
|
65
|
+
| 8 bits | 1 |
|
|
66
|
+
| 16 bits | 2 |
|
|
67
|
+
| 32 bits | 4 |
|
|
68
|
+
| 64 bits | 8 |
|
|
175
69
|
|
|
176
|
-
|
|
70
|
+
The 64-bit integer types read and write JavaScript `bigint` values. The other listed types use JavaScript `number` values.
|
|
177
71
|
|
|
178
|
-
|
|
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.
|
|
179
73
|
|
|
180
|
-
|
|
74
|
+
## 3. Block lifecycle
|
|
181
75
|
|
|
182
|
-
|
|
76
|
+
### `alloc(size = 16, bucketType = "32u")`
|
|
183
77
|
|
|
184
|
-
|
|
78
|
+
Allocates a block with its own `ArrayBuffer` and returns a numeric handle.
|
|
185
79
|
|
|
186
80
|
| Parameter | Type | Default | Description |
|
|
187
|
-
|
|
188
|
-
| `size` | `number` | `
|
|
189
|
-
| `
|
|
190
|
-
| `metadataSize` | `number` | `1024` | Initial capacity of `controlBus`, in bytes. |
|
|
191
|
-
|
|
192
|
-
`size` must be a safe integer within the range `[2, 0x80000000]`.
|
|
193
|
-
|
|
194
|
-
`bucketSize` accepts a `BucketType` value which is a string in the format `[Bit width][data type]`, accepting only values from the table:
|
|
195
|
-
|
|
196
|
-
| Signed Integer | Unsigned Integer | Float |
|
|
197
|
-
| --- | --- | --- |
|
|
198
|
-
| `8i` | `8u` | |
|
|
199
|
-
| `16i` | `16u` | `16f` |
|
|
200
|
-
| `32i` | `32u` | `32f` |
|
|
201
|
-
| `64i` | `64u` | `64f` |
|
|
202
|
-
|
|
203
|
-
`metadataSize` must be a safe integer within the range `[16, 0x80000000]`.
|
|
204
|
-
|
|
205
|
-
### Exceptions
|
|
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`. |
|
|
206
84
|
|
|
207
|
-
|
|
208
|
-
* `RangeError` — `bucketSize` different from those defined by `BucketType`.
|
|
209
|
-
* `RangeError` — `metadataSize` out of the permitted range.
|
|
85
|
+
Validation and allocation rules:
|
|
210
86
|
|
|
211
|
-
|
|
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`.
|
|
212
93
|
|
|
213
|
-
|
|
214
|
-
const { Balloc } = require("Balloc");
|
|
215
|
-
|
|
216
|
-
const memory = new Balloc();
|
|
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.
|
|
217
95
|
|
|
218
|
-
|
|
96
|
+
Returns: `number`, the new handle.
|
|
219
97
|
|
|
220
|
-
|
|
98
|
+
Example:
|
|
221
99
|
|
|
222
100
|
```js
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
The constructor internally instantiates the `TypedArray` corresponding to `bucketSize`, alongside the `DataView`s used for reading and writing.
|
|
228
|
-
|
|
229
|
-
---
|
|
230
|
-
|
|
231
|
-
# 2. Block Lifecycle
|
|
232
|
-
|
|
233
|
-
The fundamental cycle of a block is:
|
|
234
|
-
|
|
235
|
-
```text
|
|
236
|
-
alloc()
|
|
237
|
-
│
|
|
238
|
-
├── block operations
|
|
239
|
-
│
|
|
240
|
-
├── realloc()
|
|
241
|
-
│
|
|
242
|
-
└── free()
|
|
243
|
-
│
|
|
244
|
-
└── recycling
|
|
245
|
-
|
|
101
|
+
const handler = memory.alloc(64, "16u");
|
|
102
|
+
// 32 elements, each 2 bytes
|
|
246
103
|
```
|
|
247
104
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
## `alloc(size = 16)`
|
|
251
|
-
|
|
252
|
-
Allocates a new block and returns its handler.
|
|
253
|
-
|
|
254
|
-
Capacity is rounded up to the smallest power of two that can accommodate `size` elements. The block is searched first in the list of recyclable regions; if no compatible region exists, space is obtained from the top of the `dataBus`.
|
|
105
|
+
### `realloc(handler, newSize = null)`
|
|
255
106
|
|
|
256
|
-
|
|
107
|
+
Replaces the block's buffer with a larger buffer and copies the occupied elements into it. The numeric handle and occupancy remain unchanged.
|
|
257
108
|
|
|
258
109
|
| Parameter | Type | Default | Description |
|
|
259
|
-
|
|
260
|
-
| `
|
|
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. |
|
|
261
113
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
### Return
|
|
265
|
-
|
|
266
|
-
```text
|
|
267
|
-
number
|
|
268
|
-
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
Block handler.
|
|
272
|
-
|
|
273
|
-
The handler has the logical form:
|
|
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`:
|
|
274
115
|
|
|
275
116
|
```text
|
|
276
|
-
|
|
277
|
-
|
|
117
|
+
maxCapacity = floor((maxByteSize - 1) / bytesPerElement)
|
|
278
118
|
```
|
|
279
119
|
|
|
280
|
-
|
|
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`.
|
|
281
121
|
|
|
282
|
-
|
|
283
|
-
const handler = memory.alloc(100);
|
|
284
|
-
|
|
285
|
-
memory.push(handler, 42);
|
|
286
|
-
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
A request for 100 elements does not reserve exactly 100 slots: capacity will be the next power of two.
|
|
290
|
-
|
|
291
|
-
---
|
|
292
|
-
|
|
293
|
-
## `realloc(handler)`
|
|
294
|
-
|
|
295
|
-
Increases block capacity to the next power of two.
|
|
122
|
+
Only occupied elements are copied. Values in unoccupied slots are not preserved.
|
|
296
123
|
|
|
297
|
-
|
|
124
|
+
### `shrink(handler)`
|
|
298
125
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
### Parameters
|
|
302
|
-
|
|
303
|
-
| Parameter | Type | Description |
|
|
304
|
-
| --- | --- | --- |
|
|
305
|
-
| `handler` | `number` | Block handler. |
|
|
306
|
-
|
|
307
|
-
### 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.
|
|
308
127
|
|
|
309
128
|
```text
|
|
310
|
-
|
|
311
|
-
|
|
129
|
+
newCapacity = 2 ** ceil(log2(max(2, occupied)))
|
|
312
130
|
```
|
|
313
131
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
* `Error` — invalid or corrupted handler.
|
|
317
|
-
* `AggregateError` — capacity has reached the maximum limit.
|
|
318
|
-
|
|
319
|
-
### Example
|
|
320
|
-
|
|
321
|
-
```js
|
|
322
|
-
const handler = memory.alloc(8);
|
|
132
|
+
The operation allocates a new buffer and copies the occupied elements. The handle and occupancy remain unchanged. Unoccupied slots are discarded.
|
|
323
133
|
|
|
324
|
-
|
|
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`.
|
|
325
135
|
|
|
326
|
-
|
|
136
|
+
### `free(handler)`
|
|
327
137
|
|
|
328
|
-
|
|
138
|
+
Removes the block associated with `handler` from the allocator registry. The handle becomes invalid and is never reused.
|
|
329
139
|
|
|
330
|
-
|
|
140
|
+
An invalid handle or borrowed block causes `Error`. The method returns `undefined`.
|
|
331
141
|
|
|
332
|
-
|
|
142
|
+
Freeing a block does not add its buffer to a recycling pool; blocks own independent buffers.
|
|
333
143
|
|
|
334
|
-
|
|
144
|
+
## 4. Capacity and occupancy queries
|
|
335
145
|
|
|
336
|
-
|
|
146
|
+
A block has two independent dimensions:
|
|
337
147
|
|
|
338
|
-
|
|
339
|
-
|
|
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.
|
|
340
150
|
|
|
341
|
-
|
|
151
|
+
The invariant maintained by the public operations is `0 <= occupied <= capacity`.
|
|
342
152
|
|
|
343
|
-
###
|
|
153
|
+
### `length(handler)`
|
|
344
154
|
|
|
345
|
-
|
|
346
|
-
| --- | --- | --- |
|
|
347
|
-
| `handler` | `number` | Block handler. |
|
|
155
|
+
Returns the physical capacity in elements.
|
|
348
156
|
|
|
349
|
-
|
|
157
|
+
- Return: `number`.
|
|
158
|
+
- Throws `Error` if the handle is invalid.
|
|
159
|
+
- Does not change the block.
|
|
350
160
|
|
|
351
|
-
|
|
352
|
-
void
|
|
353
|
-
```
|
|
161
|
+
### `occupied(handler)`
|
|
354
162
|
|
|
355
|
-
|
|
163
|
+
Returns the number of occupied elements.
|
|
356
164
|
|
|
357
|
-
|
|
165
|
+
- Return: `number`.
|
|
166
|
+
- Throws `Error` if the handle is invalid.
|
|
167
|
+
- Does not change the block.
|
|
358
168
|
|
|
359
|
-
###
|
|
169
|
+
### `byteSize(handler)`
|
|
360
170
|
|
|
361
|
-
|
|
362
|
-
const handler = memory.alloc(128);
|
|
363
|
-
memory.put(handler, [10, 20, 30]);
|
|
364
|
-
memory.shrink(handler);
|
|
171
|
+
Returns the physical buffer size in bytes. This is the full capacity, not the byte length of the occupied sequence.
|
|
365
172
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
173
|
+
- Return: `number`.
|
|
174
|
+
- Throws `Error` if the handle is invalid.
|
|
175
|
+
- Does not change the block.
|
|
369
176
|
|
|
370
|
-
|
|
177
|
+
### `isFull(handler)`
|
|
371
178
|
|
|
372
|
-
|
|
179
|
+
Returns whether occupancy is exactly equal to physical capacity.
|
|
373
180
|
|
|
374
|
-
|
|
181
|
+
- Return: `boolean`.
|
|
182
|
+
- Throws `Error` if the handle is invalid.
|
|
183
|
+
- Does not change the block.
|
|
375
184
|
|
|
376
|
-
|
|
185
|
+
## 5. Stack operations
|
|
377
186
|
|
|
378
|
-
|
|
187
|
+
Stack operations use the end of the occupied sequence.
|
|
379
188
|
|
|
380
|
-
|
|
381
|
-
* removed directly from the top when it is the most recent region on the heap.
|
|
189
|
+
### `push(handler, value)`
|
|
382
190
|
|
|
383
|
-
|
|
191
|
+
Writes `value` at index `occupied` and increments occupancy by one.
|
|
384
192
|
|
|
385
193
|
| Parameter | Type | Description |
|
|
386
|
-
|
|
387
|
-
| `handler` | `number` |
|
|
194
|
+
|---|---|---|
|
|
195
|
+
| `handler` | `number` | Target block handle. |
|
|
196
|
+
| `value` | `number \| bigint` | Value converted by the block's typed array. |
|
|
388
197
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
void
|
|
393
|
-
|
|
394
|
-
```
|
|
395
|
-
|
|
396
|
-
### Exceptions
|
|
397
|
-
|
|
398
|
-
`Error` when the handler is invalid or corrupted.
|
|
399
|
-
|
|
400
|
-
```js
|
|
401
|
-
memory.free(handler);
|
|
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.
|
|
402
201
|
|
|
403
|
-
|
|
202
|
+
The value is subject to the conversion and range semantics of the selected typed-array element type.
|
|
404
203
|
|
|
405
|
-
|
|
204
|
+
### `pop(handler)`
|
|
406
205
|
|
|
407
|
-
|
|
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.
|
|
408
207
|
|
|
409
|
-
|
|
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.
|
|
410
211
|
|
|
411
|
-
|
|
212
|
+
### `peek(handler)`
|
|
412
213
|
|
|
413
|
-
|
|
214
|
+
Returns the element at index `occupied - 1` without changing occupancy.
|
|
414
215
|
|
|
415
|
-
|
|
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.
|
|
416
219
|
|
|
417
|
-
|
|
418
|
-
* compacts live blocks;
|
|
419
|
-
* preserves header order;
|
|
420
|
-
* preserves physical block capacity;
|
|
421
|
-
* preserves occupancy;
|
|
422
|
-
* updates block addresses;
|
|
423
|
-
* repositions `dataPointer` after the last live block.
|
|
220
|
+
## 6. Queue operations
|
|
424
221
|
|
|
425
|
-
|
|
222
|
+
Queue operations use the beginning of the occupied sequence.
|
|
426
223
|
|
|
427
|
-
|
|
428
|
-
void
|
|
224
|
+
### `nudge(handler, value)`
|
|
429
225
|
|
|
430
|
-
|
|
226
|
+
Inserts `value` at index zero. Existing occupied elements are shifted one position toward the end, and occupancy is incremented.
|
|
431
227
|
|
|
432
|
-
|
|
228
|
+
| Parameter | Type | Description |
|
|
229
|
+
|---|---|---|
|
|
230
|
+
| `handler` | `number` | Target block handle. |
|
|
231
|
+
| `value` | `number \| bigint` | Value written to the first slot. |
|
|
433
232
|
|
|
434
|
-
|
|
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.
|
|
435
236
|
|
|
436
|
-
|
|
437
|
-
memory.gc();
|
|
237
|
+
The operation shifts the occupied range using the typed array's `copyWithin()` method. Its work is proportional to the number of elements shifted.
|
|
438
238
|
|
|
439
|
-
|
|
239
|
+
### `pull(handler)`
|
|
440
240
|
|
|
441
|
-
|
|
241
|
+
Returns the element at index zero, shifts the remaining occupied elements one position toward the beginning, and decrements occupancy.
|
|
442
242
|
|
|
443
|
-
|
|
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.
|
|
444
247
|
|
|
445
|
-
|
|
248
|
+
### `head(handler)`
|
|
446
249
|
|
|
447
|
-
|
|
250
|
+
Returns the value stored in the first physical slot without changing occupancy.
|
|
448
251
|
|
|
449
|
-
|
|
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`.
|
|
450
256
|
|
|
451
|
-
|
|
257
|
+
## 7. Indexed access and search
|
|
452
258
|
|
|
453
|
-
|
|
454
|
-
interface FormatAdapter {
|
|
455
|
-
load(allocator: Balloc): number;
|
|
456
|
-
}
|
|
457
|
-
```
|
|
259
|
+
All indexed access uses zero-based indices. The logical sequence is the occupied range `[0, occupied)`, not the entire physical capacity.
|
|
458
260
|
|
|
459
|
-
|
|
261
|
+
### `get(handler, index)`
|
|
460
262
|
|
|
461
|
-
|
|
263
|
+
Returns the element at `index` if the index is within the occupied range.
|
|
462
264
|
|
|
463
265
|
| Parameter | Type | Description |
|
|
464
|
-
|
|
465
|
-
| `
|
|
466
|
-
|
|
467
|
-
### Return
|
|
468
|
-
|
|
469
|
-
```text
|
|
470
|
-
number
|
|
471
|
-
```
|
|
472
|
-
|
|
473
|
-
The handler returned by the adapter, identifying the primary block loaded.
|
|
474
|
-
|
|
475
|
-
### Exceptions
|
|
476
|
-
|
|
477
|
-
* `TypeError` — `adapter` is missing or does not provide a callable `load()` method.
|
|
478
|
-
* Any exception thrown by the adapter's `load()` method is propagated unchanged.
|
|
479
|
-
|
|
480
|
-
### Example
|
|
481
|
-
|
|
482
|
-
```js
|
|
483
|
-
const handler = memory.loadData({
|
|
484
|
-
load(allocator) {
|
|
485
|
-
const handler = allocator.alloc(4);
|
|
486
|
-
allocator.put(handler, new Uint8Array([255, 0, 0, 255]));
|
|
487
|
-
return handler;
|
|
488
|
-
}
|
|
489
|
-
});
|
|
490
|
-
```
|
|
491
|
-
|
|
492
|
-
Format-specific adapters can be distributed as independent packages. For example, `imagedata-adapter` loads RGBA8 `ImageData` instances into a `Balloc` allocator.
|
|
266
|
+
|---|---|---|
|
|
267
|
+
| `handler` | `number` | Block handle. |
|
|
268
|
+
| `index` | `number` | Zero-based index. |
|
|
493
269
|
|
|
494
|
-
|
|
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.
|
|
495
274
|
|
|
496
|
-
|
|
275
|
+
### `set(handler, index, value)`
|
|
497
276
|
|
|
498
|
-
|
|
277
|
+
Replaces the element at an occupied index. Occupancy is not changed.
|
|
499
278
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
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. |
|
|
503
284
|
|
|
504
|
-
|
|
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.
|
|
505
289
|
|
|
506
|
-
|
|
290
|
+
### `has(handler, value)`
|
|
507
291
|
|
|
508
|
-
|
|
509
|
-
memory.push(handler, 10);
|
|
510
|
-
memory.push(handler, 20);
|
|
511
|
-
memory.push(handler, 30);
|
|
292
|
+
Tests whether `value` occurs in the occupied range.
|
|
512
293
|
|
|
513
|
-
|
|
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.
|
|
514
299
|
|
|
515
|
-
|
|
300
|
+
### `indexOf(handler, searchElement, fromIndex = 0)`
|
|
516
301
|
|
|
517
|
-
|
|
518
|
-
boolean
|
|
302
|
+
Returns the first index of `searchElement` in the occupied range, searching in ascending index order.
|
|
519
303
|
|
|
520
|
-
|
|
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. |
|
|
521
309
|
|
|
522
|
-
|
|
523
|
-
|
|
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.
|
|
524
314
|
|
|
525
|
-
|
|
315
|
+
### `lastIndexOf(handler, searchElement, fromIndex = -1)`
|
|
526
316
|
|
|
527
|
-
|
|
317
|
+
Returns the last index of `searchElement` in the occupied range, searching in descending index order.
|
|
528
318
|
|
|
529
|
-
|
|
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. |
|
|
530
324
|
|
|
531
|
-
|
|
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.
|
|
532
330
|
|
|
533
|
-
|
|
331
|
+
## 8. Bulk data operations
|
|
534
332
|
|
|
535
|
-
|
|
536
|
-
number | undefined
|
|
333
|
+
### `clear(handler, value = 0)`
|
|
537
334
|
|
|
538
|
-
|
|
335
|
+
Fills every physical slot with `value` and then sets occupancy to zero.
|
|
539
336
|
|
|
540
|
-
|
|
337
|
+
| Parameter | Type | Default | Description |
|
|
338
|
+
|---|---|---:|---|
|
|
339
|
+
| `handler` | `number` | — | Block handle. |
|
|
340
|
+
| `value` | `number \| bigint` | `0` | Fill value. |
|
|
541
341
|
|
|
542
|
-
|
|
543
|
-
const value = memory.pop(handler);
|
|
342
|
+
The operation affects both occupied and unoccupied slots. It does not change capacity.
|
|
544
343
|
|
|
545
|
-
|
|
344
|
+
- Return: `undefined`.
|
|
345
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
546
346
|
|
|
547
|
-
|
|
347
|
+
### `fill(handler, value, start = 0, end = undefined)`
|
|
548
348
|
|
|
549
|
-
|
|
349
|
+
Fills the half-open range `[start, end)` with `value`. If `end` is `undefined`, it defaults to the physical capacity.
|
|
550
350
|
|
|
551
|
-
|
|
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. |
|
|
552
357
|
|
|
553
|
-
|
|
358
|
+
Validation:
|
|
554
359
|
|
|
555
|
-
|
|
556
|
-
|
|
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`.
|
|
557
364
|
|
|
558
|
-
|
|
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.
|
|
559
366
|
|
|
560
|
-
|
|
367
|
+
Return: `undefined`.
|
|
561
368
|
|
|
562
|
-
|
|
369
|
+
### `fillAll(handler, value)`
|
|
563
370
|
|
|
564
|
-
|
|
371
|
+
Fills every physical slot with `value` and sets occupancy to the full capacity.
|
|
565
372
|
|
|
566
|
-
|
|
373
|
+
- Return: `undefined`.
|
|
374
|
+
- Throws `Error` for an invalid handle or borrowed block.
|
|
567
375
|
|
|
568
|
-
|
|
376
|
+
Unlike `clear()`, the block is full after this operation.
|
|
569
377
|
|
|
570
|
-
|
|
378
|
+
### `put(handler, data)`
|
|
571
379
|
|
|
572
|
-
|
|
573
|
-
boolean
|
|
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.
|
|
574
381
|
|
|
575
|
-
|
|
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. |
|
|
576
386
|
|
|
577
|
-
|
|
578
|
-
* `false` — block full.
|
|
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.
|
|
579
388
|
|
|
580
|
-
|
|
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.
|
|
581
390
|
|
|
582
|
-
|
|
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.
|
|
583
395
|
|
|
584
|
-
|
|
396
|
+
### `pick(handler)`
|
|
585
397
|
|
|
586
|
-
|
|
398
|
+
Returns a new typed array containing a copy of the occupied elements only.
|
|
587
399
|
|
|
588
|
-
Return:
|
|
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.
|
|
589
405
|
|
|
590
|
-
|
|
591
|
-
number | undefined
|
|
406
|
+
## 9. Scoped memory views
|
|
592
407
|
|
|
593
|
-
|
|
408
|
+
### `withView(handler, callback)`
|
|
594
409
|
|
|
595
|
-
|
|
410
|
+
### `withView(handler1, handler2, ..., callback)`
|
|
596
411
|
|
|
597
|
-
|
|
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.
|
|
598
413
|
|
|
599
|
-
|
|
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.
|
|
600
415
|
|
|
601
|
-
|
|
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. |
|
|
602
420
|
|
|
603
|
-
|
|
604
|
-
number | undefined
|
|
421
|
+
Return: exactly the value returned by the callback.
|
|
605
422
|
|
|
606
|
-
|
|
423
|
+
Validation and lifetime:
|
|
607
424
|
|
|
608
|
-
|
|
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.
|
|
609
432
|
|
|
610
|
-
|
|
433
|
+
Do not retain, return, or use a borrowed view (or a subview derived from it) after the callback finishes.
|
|
611
434
|
|
|
612
|
-
|
|
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.
|
|
613
436
|
|
|
614
|
-
|
|
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.
|
|
615
438
|
|
|
616
|
-
|
|
439
|
+
Example with one block:
|
|
617
440
|
|
|
618
441
|
```js
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
```
|
|
624
|
-
|
|
625
|
-
Return:
|
|
626
|
-
|
|
627
|
-
```text
|
|
628
|
-
boolean
|
|
629
|
-
|
|
442
|
+
const result = memory.withView(handler, view => {
|
|
443
|
+
view[0] = 42;
|
|
444
|
+
return view.length;
|
|
445
|
+
});
|
|
630
446
|
```
|
|
631
447
|
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
## `indexOf(handler, searchElement, fromIndex = 0)`
|
|
635
|
-
|
|
636
|
-
Finds the first occurrence of a value, searching in ascending index order. Only occupied elements are searched; unused block capacity is ignored.
|
|
637
|
-
|
|
638
|
-
## `lastIndexOf(handler, searchElement, fromIndex = -1)`
|
|
639
|
-
|
|
640
|
-
Finds the last occurrence of a value, searching in descending index order. Only occupied elements are searched; unused block capacity is ignored.
|
|
641
|
-
|
|
642
|
-
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.
|
|
643
|
-
|
|
644
|
-
## `withView(...args)`
|
|
645
|
-
|
|
646
|
-
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).
|
|
647
|
-
|
|
648
|
-
### Parameters
|
|
649
|
-
|
|
650
|
-
| Parameter | Type | Description |
|
|
651
|
-
| --- | --- | --- |
|
|
652
|
-
| `...args` | `number[] + callback` | One or more block handlers followed by the callback. |
|
|
653
|
-
|
|
654
|
-
### Return
|
|
655
|
-
|
|
656
|
-
The value returned by the callback.
|
|
657
|
-
|
|
658
|
-
### Exceptions
|
|
659
|
-
|
|
660
|
-
* `TypeError` — no handler is provided or the final argument is not a function.
|
|
661
|
-
* `Error` — a handler is invalid, duplicated, or already borrowed.
|
|
662
|
-
|
|
663
|
-
---
|
|
664
|
-
|
|
665
|
-
## `get(handler, index)`
|
|
666
|
-
|
|
667
|
-
Gets the element at the specified index.
|
|
668
|
-
|
|
669
|
-
Zero-based index. Indices outside `[0, occupied)` return `undefined`.
|
|
448
|
+
Example with two blocks:
|
|
670
449
|
|
|
671
450
|
```js
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
Return:
|
|
677
|
-
|
|
678
|
-
```text
|
|
679
|
-
number | undefined
|
|
680
|
-
|
|
451
|
+
memory.withView(source, destination, (sourceView, destinationView) => {
|
|
452
|
+
destinationView.set(sourceView.subarray(0, destinationView.length));
|
|
453
|
+
});
|
|
681
454
|
```
|
|
682
455
|
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
## `set(handler, index, value)`
|
|
686
|
-
|
|
687
|
-
Replaces the element at an occupied index.
|
|
456
|
+
## 10. Operations between blocks
|
|
688
457
|
|
|
689
|
-
|
|
458
|
+
### `copy(source, destination, slots = 0xFFFFFFFF)`
|
|
690
459
|
|
|
691
|
-
|
|
460
|
+
Copies occupied elements from the source block to the beginning of the destination block.
|
|
692
461
|
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
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. |
|
|
697
467
|
|
|
698
|
-
|
|
468
|
+
The copied count is:
|
|
699
469
|
|
|
700
470
|
```text
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
```
|
|
704
|
-
|
|
705
|
-
* `true` — value written.
|
|
706
|
-
* `false` — invalid index for the occupied region.
|
|
707
|
-
|
|
708
|
-
---
|
|
709
|
-
|
|
710
|
-
## `clear(handler, value = 0)`
|
|
711
|
-
|
|
712
|
-
Zeros occupancy and fills **all physical slots** of the block with `value`.
|
|
713
|
-
|
|
714
|
-
This includes slots that were not part of the occupied sequence.
|
|
715
|
-
|
|
716
|
-
```js
|
|
717
|
-
memory.clear(handler);
|
|
718
|
-
|
|
471
|
+
min(source.occupied, destination.capacity, slots)
|
|
719
472
|
```
|
|
720
473
|
|
|
721
|
-
|
|
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.
|
|
722
475
|
|
|
723
|
-
|
|
724
|
-
|
|
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.
|
|
725
481
|
|
|
726
|
-
|
|
482
|
+
### `copyWithin(handler, target, start, length)`
|
|
727
483
|
|
|
728
|
-
|
|
484
|
+
Copies a range within one block, supporting overlapping source and destination ranges.
|
|
729
485
|
|
|
730
|
-
|
|
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. |
|
|
731
492
|
|
|
732
|
-
|
|
493
|
+
Normalization and bounds:
|
|
733
494
|
|
|
734
|
-
|
|
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`.
|
|
735
500
|
|
|
736
|
-
The
|
|
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.
|
|
737
502
|
|
|
738
|
-
|
|
739
|
-
|
|
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.
|
|
740
506
|
|
|
741
|
-
|
|
507
|
+
This method operates on physical-capacity indices but never reads beyond the occupied source range.
|
|
742
508
|
|
|
743
|
-
|
|
509
|
+
### `append(target, source)`
|
|
744
510
|
|
|
745
|
-
|
|
746
|
-
memory.fill(handler, 0, 2, 6);
|
|
511
|
+
Appends occupied source elements after the target's occupied sequence.
|
|
747
512
|
|
|
748
|
-
|
|
513
|
+
| Parameter | Type | Description |
|
|
514
|
+
|---|---|---|
|
|
515
|
+
| `target` | `number` | Destination block handle. |
|
|
516
|
+
| `source` | `number` | Source block handle. |
|
|
749
517
|
|
|
750
|
-
|
|
518
|
+
The appended count is the smaller of source occupancy and the free capacity remaining in the destination:
|
|
751
519
|
|
|
752
520
|
```text
|
|
753
|
-
|
|
754
|
-
|
|
521
|
+
min(source.occupied, target.capacity - target.occupied)
|
|
755
522
|
```
|
|
756
523
|
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
`RangeError` when:
|
|
524
|
+
The destination occupancy increases by the appended count.
|
|
760
525
|
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
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.
|
|
766
531
|
|
|
767
|
-
|
|
532
|
+
### `clone(handler)`
|
|
768
533
|
|
|
769
|
-
|
|
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.
|
|
770
535
|
|
|
771
|
-
|
|
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.
|
|
772
540
|
|
|
773
|
-
|
|
541
|
+
### `swap(source, target)`
|
|
774
542
|
|
|
775
|
-
|
|
776
|
-
occupied = capacity
|
|
777
|
-
|
|
778
|
-
```
|
|
779
|
-
|
|
780
|
-
Unlike `clear()`, the block ends up fully occupied.
|
|
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.
|
|
781
544
|
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
545
|
+
| Parameter | Type | Description |
|
|
546
|
+
|---|---|---|
|
|
547
|
+
| `source` | `number` | First block handle. |
|
|
548
|
+
| `target` | `number` | Second block handle. |
|
|
786
549
|
|
|
787
|
-
|
|
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.
|
|
788
554
|
|
|
789
|
-
##
|
|
555
|
+
## 11. Serialization
|
|
790
556
|
|
|
791
|
-
|
|
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.
|
|
792
558
|
|
|
793
|
-
|
|
559
|
+
### `dump(handler)`
|
|
794
560
|
|
|
795
|
-
|
|
796
|
-
memory.put(handler, [10, 20, 30, 40]);
|
|
561
|
+
Serializes a block.
|
|
797
562
|
|
|
798
|
-
|
|
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.
|
|
799
568
|
|
|
800
|
-
|
|
569
|
+
Envelope structure:
|
|
801
570
|
|
|
802
571
|
```js
|
|
803
|
-
|
|
804
|
-
|
|
572
|
+
{
|
|
573
|
+
v: 2,
|
|
574
|
+
bucketType: "32u",
|
|
575
|
+
capacity: 16, // elements, not bytes
|
|
576
|
+
occupied: 3,
|
|
577
|
+
payload: Uint32Array([10, 20, 30])
|
|
578
|
+
}
|
|
805
579
|
```
|
|
806
580
|
|
|
807
|
-
|
|
581
|
+
The object above illustrates the logical fields before CBOR encoding; `dump()` returns the encoded bytes, not this JavaScript object.
|
|
808
582
|
|
|
809
|
-
|
|
810
|
-
Array
|
|
811
|
-
TypedArray
|
|
583
|
+
### `load(serialized)`
|
|
812
584
|
|
|
813
|
-
|
|
585
|
+
Decodes a CBOR envelope and allocates a new block from it.
|
|
814
586
|
|
|
815
|
-
|
|
587
|
+
| Parameter | Type | Description |
|
|
588
|
+
|---|---|---|
|
|
589
|
+
| `serialized` | `ArrayBuffer \| Uint8Array` | CBOR bytes produced from a compatible version 2 envelope. |
|
|
816
590
|
|
|
817
|
-
|
|
591
|
+
Validation is performed in this order:
|
|
818
592
|
|
|
819
|
-
|
|
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.
|
|
820
602
|
|
|
821
|
-
|
|
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.
|
|
822
604
|
|
|
823
|
-
|
|
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.
|
|
824
609
|
|
|
825
|
-
|
|
610
|
+
Example:
|
|
826
611
|
|
|
827
612
|
```js
|
|
828
|
-
const
|
|
829
|
-
|
|
613
|
+
const serialized = memory.dump(handler);
|
|
614
|
+
const restoredHandler = memory.load(serialized);
|
|
830
615
|
```
|
|
831
616
|
|
|
832
|
-
|
|
617
|
+
## 12. Data-ingestion adapters
|
|
833
618
|
|
|
834
|
-
|
|
619
|
+
### `loadData(adapter)`
|
|
835
620
|
|
|
836
|
-
|
|
621
|
+
Delegates synchronous ingestion to an adapter object. Balloc calls `adapter.load(this)` and returns its result unchanged.
|
|
837
622
|
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
## `byteSize(handler)`
|
|
841
|
-
|
|
842
|
-
Returns the physical capacity of the block in bytes.
|
|
843
|
-
|
|
844
|
-
Does not represent the number of bytes currently occupied by elements; it represents the space reserved for all slots in the block.
|
|
845
|
-
|
|
846
|
-
```js
|
|
847
|
-
const bytes = memory.byteSize(handler);
|
|
623
|
+
Adapter contract:
|
|
848
624
|
|
|
625
|
+
```ts
|
|
626
|
+
interface FormatAdapter<Result = unknown> {
|
|
627
|
+
load(allocator: Balloc): Result;
|
|
628
|
+
}
|
|
849
629
|
```
|
|
850
630
|
|
|
851
|
-
|
|
631
|
+
| Parameter | Type | Description |
|
|
632
|
+
|---|---|---|
|
|
633
|
+
| `adapter` | `FormatAdapter` | Object with a callable `load(allocator)` method. |
|
|
852
634
|
|
|
853
|
-
|
|
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.
|
|
854
640
|
|
|
855
|
-
|
|
641
|
+
Example:
|
|
856
642
|
|
|
857
643
|
```js
|
|
858
|
-
const
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
Returns the total quantity of available physical slots.
|
|
867
|
-
|
|
868
|
-
Capacity is independent of occupancy:
|
|
869
|
-
|
|
870
|
-
```text
|
|
871
|
-
length >= occupied
|
|
872
|
-
|
|
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
|
+
});
|
|
873
651
|
```
|
|
874
652
|
|
|
875
|
-
|
|
876
|
-
const capacity = memory.length(handler);
|
|
877
|
-
|
|
878
|
-
```
|
|
653
|
+
## 13. Error behavior and borrowing matrix
|
|
879
654
|
|
|
880
|
-
|
|
655
|
+
Unless a method documents a different exception, an unknown or freed handle causes `Error`.
|
|
881
656
|
|
|
882
|
-
|
|
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 |
|
|
883
667
|
|
|
884
|
-
|
|
668
|
+
Additional validation exceptions:
|
|
885
669
|
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
`
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
## `copyWithin(handler, target, start, length)`
|
|
936
|
-
|
|
937
|
-
Copies a region within the block itself.
|
|
938
|
-
|
|
939
|
-
Semantics follow the concept of `Array.prototype.copyWithin()`, but applied over physical block capacity and limited by source occupancy available.
|
|
940
|
-
|
|
941
|
-
```js
|
|
942
|
-
memory.copyWithin(handler, 4, 0, 4);
|
|
943
|
-
|
|
944
|
-
```
|
|
945
|
-
|
|
946
|
-
Negative indices are interpreted relative to capacity end.
|
|
947
|
-
|
|
948
|
-
The operation:
|
|
949
|
-
|
|
950
|
-
* supports overlapping regions;
|
|
951
|
-
* limits indices to capacity;
|
|
952
|
-
* limits reading to the occupied region;
|
|
953
|
-
* limits writing to capacity;
|
|
954
|
-
* may increase `occupied`.
|
|
955
|
-
|
|
956
|
-
Return:
|
|
957
|
-
|
|
958
|
-
```text
|
|
959
|
-
Uint8Array
|
|
960
|
-
|
|
961
|
-
```
|
|
962
|
-
|
|
963
|
-
The `dataBus`.
|
|
964
|
-
|
|
965
|
-
---
|
|
966
|
-
|
|
967
|
-
## `append(target, source)`
|
|
968
|
-
|
|
969
|
-
Appends occupied elements of `source` to the end of occupied elements of `target`.
|
|
970
|
-
|
|
971
|
-
The amount is limited by source occupancy and available space in destination.
|
|
972
|
-
|
|
973
|
-
```js
|
|
974
|
-
const count = memory.append(target, source);
|
|
975
|
-
|
|
976
|
-
```
|
|
977
|
-
|
|
978
|
-
### Return
|
|
979
|
-
|
|
980
|
-
```text
|
|
981
|
-
number
|
|
982
|
-
|
|
983
|
-
```
|
|
984
|
-
|
|
985
|
-
Number of elements effectively appended.
|
|
986
|
-
|
|
987
|
-
The operation also accepts:
|
|
988
|
-
|
|
989
|
-
```js
|
|
990
|
-
memory.append(handler, handler);
|
|
991
|
-
|
|
992
|
-
```
|
|
993
|
-
|
|
994
|
-
when `target` and `source` are the same block.
|
|
995
|
-
|
|
996
|
-
---
|
|
997
|
-
|
|
998
|
-
## `clone(handler)`
|
|
999
|
-
|
|
1000
|
-
Creates an independent copy of the block.
|
|
1001
|
-
|
|
1002
|
-
The clone possesses:
|
|
1003
|
-
|
|
1004
|
-
* same physical capacity;
|
|
1005
|
-
* same occupancy;
|
|
1006
|
-
* same bytes across the entire reserved region;
|
|
1007
|
-
* new handler.
|
|
1008
|
-
|
|
1009
|
-
```js
|
|
1010
|
-
const copy = memory.clone(handler);
|
|
1011
|
-
|
|
1012
|
-
```
|
|
1013
|
-
|
|
1014
|
-
Unoccupied content is also copied.
|
|
1015
|
-
|
|
1016
|
-
---
|
|
1017
|
-
|
|
1018
|
-
## `swap(source, target)`
|
|
1019
|
-
|
|
1020
|
-
Swaps the data regions associated with two handlers.
|
|
1021
|
-
|
|
1022
|
-
The operation swaps in headers:
|
|
1023
|
-
|
|
1024
|
-
* address;
|
|
1025
|
-
* capacity;
|
|
1026
|
-
* occupancy.
|
|
1027
|
-
|
|
1028
|
-
No bytes are copied in `dataBus`. Handlers continue pointing to their respective headers.
|
|
1029
|
-
|
|
1030
|
-
```js
|
|
1031
|
-
memory.swap(a, b);
|
|
1032
|
-
|
|
1033
|
-
```
|
|
1034
|
-
|
|
1035
|
-
Conceptually:
|
|
1036
|
-
|
|
1037
|
-
```text
|
|
1038
|
-
before:
|
|
1039
|
-
|
|
1040
|
-
handler A → region A
|
|
1041
|
-
handler B → region B
|
|
1042
|
-
|
|
1043
|
-
after:
|
|
1044
|
-
|
|
1045
|
-
handler A → region B
|
|
1046
|
-
handler B → region A
|
|
1047
|
-
|
|
1048
|
-
```
|
|
1049
|
-
|
|
1050
|
-
---
|
|
1051
|
-
|
|
1052
|
-
# 7. Serialization
|
|
1053
|
-
|
|
1054
|
-
Methods specialized in supporting block state export and import using the CBOR format.
|
|
1055
|
-
|
|
1056
|
-
## `dump(handler)`
|
|
1057
|
-
|
|
1058
|
-
Serializes the contents of a memory block identified by `handler`.
|
|
1059
|
-
|
|
1060
|
-
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.
|
|
1061
|
-
|
|
1062
|
-
### Return
|
|
1063
|
-
|
|
1064
|
-
```text
|
|
1065
|
-
Uint8Array
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
```
|
|
1069
|
-
|
|
1070
|
-
A buffer containing the serialized block data.
|
|
1071
|
-
|
|
1072
|
-
### Exceptions
|
|
1073
|
-
|
|
1074
|
-
`Error` — if the handler is invalid or corrupted.
|
|
1075
|
-
|
|
1076
|
-
---
|
|
1077
|
-
|
|
1078
|
-
## `load(serialized)`
|
|
1079
|
-
|
|
1080
|
-
Restores a memory block from serialized data.
|
|
1081
|
-
|
|
1082
|
-
Decodes the CBOR envelope passed in `serialized`, validates whether the word type of the serialized data is compatible with the current allocator instance, and allocates a new block with the necessary capacity to accommodate the content. Then, original data and occupancy are restored in the new block.
|
|
1083
|
-
|
|
1084
|
-
### Return
|
|
1085
|
-
|
|
1086
|
-
```text
|
|
1087
|
-
number
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
```
|
|
1091
|
-
|
|
1092
|
-
Handler of the newly recreated block.
|
|
1093
|
-
|
|
1094
|
-
### Exceptions
|
|
1095
|
-
|
|
1096
|
-
`Error` — if the word type of the decoded data diverges from the one configured in the allocator.
|
|
1097
|
-
|
|
1098
|
-
---
|
|
1099
|
-
|
|
1100
|
-
# 8. Diagnostics and Utilization
|
|
1101
|
-
|
|
1102
|
-
## `recycleCount()`
|
|
1103
|
-
|
|
1104
|
-
Returns the quantity of regions currently stored in the recycling system.
|
|
1105
|
-
|
|
1106
|
-
A region corresponds to a freed area that could not simply be removed from the top of the `dataBus`.
|
|
1107
|
-
|
|
1108
|
-
```js
|
|
1109
|
-
const count = memory.recycleCount();
|
|
1110
|
-
|
|
1111
|
-
```
|
|
1112
|
-
|
|
1113
|
-
---
|
|
1114
|
-
|
|
1115
|
-
## `recycleBytes`
|
|
1116
|
-
|
|
1117
|
-
Getter that returns the total bytes belonging to currently recyclable regions.
|
|
1118
|
-
|
|
1119
|
-
```js
|
|
1120
|
-
const bytes = memory.recycleBytes;
|
|
1121
|
-
|
|
1122
|
-
```
|
|
1123
|
-
|
|
1124
|
-
The value is calculated as:
|
|
1125
|
-
|
|
1126
|
-
```text
|
|
1127
|
-
Σ(capacity × region_quantity)
|
|
1128
|
-
|
|
1129
|
-
```
|
|
1130
|
-
|
|
1131
|
-
---
|
|
1132
|
-
|
|
1133
|
-
## `allocatedBytes`
|
|
1134
|
-
|
|
1135
|
-
Getter that returns bytes associated with regions not currently in the recycling list.
|
|
1136
|
-
|
|
1137
|
-
Calculated by:
|
|
1138
|
-
|
|
1139
|
-
```text
|
|
1140
|
-
dataPointer - recycleBytes
|
|
1141
|
-
|
|
1142
|
-
```
|
|
1143
|
-
|
|
1144
|
-
```js
|
|
1145
|
-
const bytes = memory.allocatedBytes;
|
|
1146
|
-
|
|
1147
|
-
```
|
|
1148
|
-
|
|
1149
|
-
---
|
|
1150
|
-
|
|
1151
|
-
## `unallocatedBytes`
|
|
1152
|
-
|
|
1153
|
-
Getter that returns space still available at the top of the `dataBus`.
|
|
1154
|
-
|
|
1155
|
-
This is the region between `dataPointer` and the end of reserved capacity:
|
|
1156
|
-
|
|
1157
|
-
```text
|
|
1158
|
-
size - dataPointer
|
|
1159
|
-
|
|
1160
|
-
```
|
|
1161
|
-
|
|
1162
|
-
```js
|
|
1163
|
-
const bytes = memory.unallocatedBytes;
|
|
1164
|
-
|
|
1165
|
-
```
|
|
1166
|
-
|
|
1167
|
-
---
|
|
1168
|
-
|
|
1169
|
-
## `fragmentationRatio`
|
|
1170
|
-
|
|
1171
|
-
Getter that calculates the ratio between recyclable bytes and the heap area already traversed by the bump pointer.
|
|
1172
|
-
|
|
1173
|
-
```text
|
|
1174
|
-
recycleBytes / dataPointer
|
|
1175
|
-
|
|
1176
|
-
```
|
|
1177
|
-
|
|
1178
|
-
When `dataPointer` is zero, the result is `0`.
|
|
1179
|
-
|
|
1180
|
-
```js
|
|
1181
|
-
const ratio = memory.fragmentationRatio;
|
|
1182
|
-
|
|
1183
|
-
```
|
|
1184
|
-
|
|
1185
|
-
The value measures fragmentation **within the already used area of the heap**, rather than relative to total `dataBus` capacity.
|
|
1186
|
-
|
|
1187
|
-
---
|
|
1188
|
-
|
|
1189
|
-
# 9. Internal Usage
|
|
1190
|
-
|
|
1191
|
-
Methods in this section are part of `Balloc` implementation and do not constitute the interface intended for consumer code.
|
|
1192
|
-
|
|
1193
|
-
```text
|
|
1194
|
-
Internal usage
|
|
1195
|
-
├── Recycling
|
|
1196
|
-
│ ├── recyclePush()
|
|
1197
|
-
│ └── recyclePop()
|
|
1198
|
-
│
|
|
1199
|
-
├── Scaling
|
|
1200
|
-
│ ├── grow()
|
|
1201
|
-
│ └── headerGrow()
|
|
1202
|
-
│
|
|
1203
|
-
└── Validation / metadata
|
|
1204
|
-
├── extractMetadata()
|
|
1205
|
-
└── bakeCookie()
|
|
1206
|
-
|
|
1207
|
-
```
|
|
1208
|
-
|
|
1209
|
-
## 9.1 Recycling
|
|
1210
|
-
|
|
1211
|
-
### `recyclePush(capacity, address)`
|
|
1212
|
-
|
|
1213
|
-
**Private.**
|
|
1214
|
-
|
|
1215
|
-
Inserts a free region into the recycling list associated with its capacity in bytes.
|
|
1216
|
-
|
|
1217
|
-
Each capacity has a page represented by `Uint32Array`:
|
|
1218
|
-
|
|
1219
|
-
```text
|
|
1220
|
-
[count, address1, address2, ...]
|
|
1221
|
-
|
|
1222
|
-
```
|
|
1223
|
-
|
|
1224
|
-
The structure operates as a LIFO stack. When the page becomes full, its buffer expands to double.
|
|
1225
|
-
|
|
1226
|
-
---
|
|
1227
|
-
|
|
1228
|
-
### `recyclePop(capacity)`
|
|
1229
|
-
|
|
1230
|
-
**Private.**
|
|
1231
|
-
|
|
1232
|
-
Removes and returns a recyclable region of exact capacity.
|
|
1233
|
-
|
|
1234
|
-
Removal occurs from the end of the page, characterizing LIFO behavior.
|
|
1235
|
-
|
|
1236
|
-
Returns:
|
|
1237
|
-
|
|
1238
|
-
```text
|
|
1239
|
-
address
|
|
1240
|
-
|
|
1241
|
-
```
|
|
1242
|
-
|
|
1243
|
-
or:
|
|
1244
|
-
|
|
1245
|
-
```js
|
|
1246
|
-
UNKNOWN // 0xFFFFFFFF
|
|
1247
|
-
|
|
1248
|
-
```
|
|
1249
|
-
|
|
1250
|
-
when no region exists available for that capacity.
|
|
1251
|
-
|
|
1252
|
-
---
|
|
1253
|
-
|
|
1254
|
-
## 9.2 Scaling
|
|
1255
|
-
|
|
1256
|
-
### `grow()`
|
|
1257
|
-
|
|
1258
|
-
**Private. Do not use directly.**
|
|
1259
|
-
|
|
1260
|
-
Doubles `dataBus` capacity.
|
|
1261
|
-
|
|
1262
|
-
The underlying `ArrayBuffer` is transferred to a larger buffer and associated views are recreated.
|
|
1263
|
-
|
|
1264
|
-
Triggered automatically when a new region does not fit in remaining `dataBus` space.
|
|
1265
|
-
|
|
1266
|
-
---
|
|
1267
|
-
|
|
1268
|
-
### `headerGrow()`
|
|
1269
|
-
|
|
1270
|
-
**Private. Do not use directly.**
|
|
1271
|
-
|
|
1272
|
-
Doubles `controlBus` capacity.
|
|
1273
|
-
|
|
1274
|
-
Existing headers are preserved during `ArrayBuffer` transfer, and `controlAdapter` is recreated over the new storage.
|
|
1275
|
-
|
|
1276
|
-
Triggered automatically when there is no space for a new header.
|
|
1277
|
-
|
|
1278
|
-
---
|
|
1279
|
-
|
|
1280
|
-
## 9.3 Validation and Metadata
|
|
1281
|
-
|
|
1282
|
-
### `extractMetadata(handler, out)`
|
|
1283
|
-
|
|
1284
|
-
**Private.**
|
|
1285
|
-
|
|
1286
|
-
Decodes and validates a handler.
|
|
1287
|
-
|
|
1288
|
-
The method extracts:
|
|
1289
|
-
|
|
1290
|
-
```text
|
|
1291
|
-
generation
|
|
1292
|
-
headerAddress
|
|
1293
|
-
dataAddress
|
|
1294
|
-
capacityExponent
|
|
1295
|
-
byteCapacity
|
|
1296
|
-
occupied
|
|
1297
|
-
|
|
1298
|
-
```
|
|
1299
|
-
|
|
1300
|
-
Handler validity is verified by recalculating the cookie stored in the header. The `out` object is reusable, avoiding new allocations.
|
|
1301
|
-
|
|
1302
|
-
Signature:
|
|
1303
|
-
|
|
1304
|
-
```js
|
|
1305
|
-
extractMetadata(handler, out)
|
|
1306
|
-
|
|
1307
|
-
```
|
|
1308
|
-
|
|
1309
|
-
Return:
|
|
1310
|
-
|
|
1311
|
-
```text
|
|
1312
|
-
Metadata
|
|
1313
|
-
|
|
1314
|
-
```
|
|
1315
|
-
|
|
1316
|
-
The return is the reference provided in `out` itself.
|
|
1317
|
-
|
|
1318
|
-
---
|
|
1319
|
-
|
|
1320
|
-
### `bakeCookie(addr, gen)`
|
|
1321
|
-
|
|
1322
|
-
**Private.**
|
|
1323
|
-
|
|
1324
|
-
Calculates the validation cookie used to verify handlers.
|
|
1325
|
-
|
|
1326
|
-
The cookie combines:
|
|
1327
|
-
|
|
1328
|
-
* header address;
|
|
1329
|
-
* lower 11 bits of generation.
|
|
1330
|
-
|
|
1331
|
-
The result is reduced to 8 bits. The value `0` is reserved to indicate an invalid header, so the method replaces `0` with `1`.
|
|
1332
|
-
|
|
1333
|
-
The cookie is not cryptographic. Its function is to detect invalid, stale, or corrupted handlers within the allocator management model.
|
|
1334
|
-
|
|
1335
|
-
---
|
|
1336
|
-
|
|
1337
|
-
# 10. Storage Model
|
|
1338
|
-
|
|
1339
|
-
Each block features a fixed **16-byte** header:
|
|
1340
|
-
|
|
1341
|
-
```text
|
|
1342
|
-
offset size field
|
|
1343
|
-
────── ─────── ─────────────
|
|
1344
|
-
0 4 bytes address
|
|
1345
|
-
4 4 bytes occupied
|
|
1346
|
-
8 1 byte capacity
|
|
1347
|
-
9 1 byte cookie
|
|
1348
|
-
10 6 bytes reserved
|
|
1349
|
-
|
|
1350
|
-
```
|
|
1351
|
-
|
|
1352
|
-
Relevant fields are:
|
|
1353
|
-
|
|
1354
|
-
### `address`
|
|
1355
|
-
|
|
1356
|
-
Initial address of the data region inside `dataBus`.
|
|
1357
|
-
|
|
1358
|
-
### `occupied`
|
|
1359
|
-
|
|
1360
|
-
Number of currently occupied elements.
|
|
1361
|
-
|
|
1362
|
-
### `capacity`
|
|
1363
|
-
|
|
1364
|
-
Power of two exponent representing physical capacity:
|
|
1365
|
-
|
|
1366
|
-
```text
|
|
1367
|
-
capacityElements = 2 ** capacity
|
|
1368
|
-
|
|
1369
|
-
```
|
|
1370
|
-
|
|
1371
|
-
Capacity in bytes is:
|
|
1372
|
-
|
|
1373
|
-
```text
|
|
1374
|
-
capacityBytes = 2 ** capacity × wordSize
|
|
1375
|
-
|
|
1376
|
-
```
|
|
1377
|
-
|
|
1378
|
-
### `cookie`
|
|
1379
|
-
|
|
1380
|
-
Validation value associated with the header address and handler generation.
|
|
1381
|
-
|
|
1382
|
-
---
|
|
1383
|
-
|
|
1384
|
-
# 11. Handlers
|
|
1385
|
-
|
|
1386
|
-
The handler is the external identifier used by practically all block operations.
|
|
1387
|
-
|
|
1388
|
-
Its logical composition is:
|
|
1389
|
-
|
|
1390
|
-
```text
|
|
1391
|
-
┌───────────────────────┬───────────────────────────────┐
|
|
1392
|
-
│ generation — 11 bits │ header address — 32 bits │
|
|
1393
|
-
└───────────────────────┴───────────────────────────────┘
|
|
1394
|
-
|
|
1395
|
-
```
|
|
1396
|
-
|
|
1397
|
-
The lower part corresponds directly to the header address; the upper part represents generation.
|
|
1398
|
-
|
|
1399
|
-
Generation participates in cookie validation. Consequently, an invalid handler or one whose header has been invalidated causes an error in operations using it.
|
|
1400
|
-
|
|
1401
|
-
---
|
|
1402
|
-
|
|
1403
|
-
# 12. Public API Summary
|
|
1404
|
-
|
|
1405
|
-
| Category | Method / property | Return |
|
|
1406
|
-
| --- | --- | --- |
|
|
1407
|
-
| Instantiation | `constructor()` | `Balloc`|
|
|
1408
|
-
| Lifecycle | `alloc()` | `number`|
|
|
1409
|
-
| Lifecycle | `realloc()` | `void`|
|
|
1410
|
-
| Lifecycle | `shrink()` | `void`|
|
|
1411
|
-
| Lifecycle | `free()` | `void`|
|
|
1412
|
-
| Lifecycle | `gc()` | `void`|
|
|
1413
|
-
| Data ingestion | `loadData()` | `number`|
|
|
1414
|
-
| Stack | `push()` | `boolean`|
|
|
1415
|
-
| Stack | `pop()` | `number \| undefined` |
|
|
1416
|
-
| Stack | `peek()` | `number \| undefined` |
|
|
1417
|
-
| Queue | `nudge()` | `boolean`|
|
|
1418
|
-
| Queue | `pull()` | `number \| undefined` |
|
|
1419
|
-
| Queue | `head()` | `number \| undefined` |
|
|
1420
|
-
| Access | `has()` | `boolean`|
|
|
1421
|
-
| Access | `get()` | `number \| undefined` |
|
|
1422
|
-
| Access | `set()` | `boolean`|
|
|
1423
|
-
| Access | `clear()` | `void`|
|
|
1424
|
-
| Access | `fill()` | `void`|
|
|
1425
|
-
| Access | `fillAll()` | `void`|
|
|
1426
|
-
| Access | `put()` | `void`|
|
|
1427
|
-
| Access | `pick()` | `TypedArray`|
|
|
1428
|
-
| Access | `indexOf()` | `number`|
|
|
1429
|
-
| Access | `lastIndexOf()` | `number`|
|
|
1430
|
-
| Access | `withView()` | `any`|
|
|
1431
|
-
| Capacity | `byteSize()` | `number`|
|
|
1432
|
-
| Capacity | `occupied()` | `number`|
|
|
1433
|
-
| Capacity | `length()` | `number`|
|
|
1434
|
-
| Capacity | `isFull()` | `boolean`|
|
|
1435
|
-
| Blocks | `copy()` | `Uint8Array`|
|
|
1436
|
-
| Blocks | `copyWithin()` | `Uint8Array`|
|
|
1437
|
-
| Blocks | `append()` | `number`|
|
|
1438
|
-
| Blocks | `clone()` | `number`|
|
|
1439
|
-
| Blocks | `swap()` | `void`|
|
|
1440
|
-
| Serialization | `dump()` | `Uint8Array`|
|
|
1441
|
-
| Serialization | `load()` | `number`|
|
|
1442
|
-
| Diagnostics | `recycleCount()` | `number`|
|
|
1443
|
-
| Diagnostics | `recycleBytes` | `number`|
|
|
1444
|
-
| Diagnostics | `allocatedBytes` | `number`|
|
|
1445
|
-
| Diagnostics | `unallocatedBytes` | `number`|
|
|
1446
|
-
| Diagnostics | `fragmentationRatio` | `number`|
|
|
1447
|
-
|
|
1448
|
-
## Internal Methods
|
|
1449
|
-
|
|
1450
|
-
| Group | Method | Visibility |
|
|
1451
|
-
| --- | --- | --- |
|
|
1452
|
-
| Recycling | `recyclePush()` | private |
|
|
1453
|
-
| Recycling | `recyclePop()` | private |
|
|
1454
|
-
| Scaling | `grow()` | private |
|
|
1455
|
-
| Scaling | `headerGrow()` | private |
|
|
1456
|
-
| Metadata | `extractMetadata()` | private |
|
|
1457
|
-
| 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.
|