@ebowwa/bun-native-page 0.2.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/README.md +64 -0
- package/dist/allocator.d.ts +103 -0
- package/dist/allocator.d.ts.map +1 -0
- package/dist/allocator.js +272 -0
- package/dist/allocator.js.map +1 -0
- package/dist/allocator.ts +334 -0
- package/dist/errors.d.ts +24 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +44 -0
- package/dist/errors.js.map +1 -0
- package/dist/errors.ts +51 -0
- package/dist/ffi.d.ts +104 -0
- package/dist/ffi.d.ts.map +1 -0
- package/dist/ffi.js +102 -0
- package/dist/ffi.js.map +1 -0
- package/dist/ffi.ts +119 -0
- package/dist/index.d.ts +89 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +294 -0
- package/dist/index.js.map +1 -0
- package/dist/index.ts +433 -0
- package/dist/oom.d.ts +36 -0
- package/dist/oom.d.ts.map +1 -0
- package/dist/oom.js +73 -0
- package/dist/oom.js.map +1 -0
- package/dist/oom.ts +96 -0
- package/native/darwin-arm64/libpage_native.dylib +0 -0
- package/package.json +69 -0
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# @ebowwa/bun-native-page
|
|
2
|
+
|
|
3
|
+
Cross-platform page-size aware memory operations for Bun.
|
|
4
|
+
|
|
5
|
+
## Why?
|
|
6
|
+
|
|
7
|
+
Different systems have different memory page sizes:
|
|
8
|
+
- **macOS Apple Silicon (M1/M2/M3/M4)**: 16 KB
|
|
9
|
+
- **macOS Intel**: 4 KB
|
|
10
|
+
- **Linux x86_64**: 4 KB
|
|
11
|
+
- **Linux ARM64 (Asahi, future)**: 16 KB
|
|
12
|
+
- **Windows**: 4 KB
|
|
13
|
+
|
|
14
|
+
Hardcoding `4096` wastes memory on 16 KB systems and can cause alignment issues with `mmap`, Direct I/O, and GPU interop.
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- Runtime page size detection
|
|
19
|
+
- Page-aligned memory allocation
|
|
20
|
+
- `mmap`/`munmap`/`madvise` wrappers
|
|
21
|
+
- Huge page support (Linux)
|
|
22
|
+
- `PageArena` bump allocator
|
|
23
|
+
- `BufferPool` fixed-size buffer pool
|
|
24
|
+
- Full TypeScript types
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
bun add @ebowwa/bun-native-page
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { getPageSize, allocPageAligned, mmapAnonymous } from '@ebowwa/bun-native-page';
|
|
36
|
+
|
|
37
|
+
// Get system page size
|
|
38
|
+
const info = getPageSize();
|
|
39
|
+
console.log(`Page size: ${info.size} bytes`); // 4096 or 16384
|
|
40
|
+
console.log(`Platform: ${info.platform}`); // darwin-arm64, linux-x64, etc.
|
|
41
|
+
|
|
42
|
+
// Allocate page-aligned memory
|
|
43
|
+
const buf = allocPageAligned(1024);
|
|
44
|
+
console.log(`Aligned to: ${buf.size} bytes`); // Rounded to page boundary
|
|
45
|
+
buf.free();
|
|
46
|
+
|
|
47
|
+
// Memory-mapped anonymous region
|
|
48
|
+
const region = mmapAnonymous(1024 * 1024); // 1MB
|
|
49
|
+
const view = new Uint8Array(region.buffer);
|
|
50
|
+
view[0] = 0x42;
|
|
51
|
+
region.unmap();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
See [SPEC.md](./SPEC.md) for full API documentation.
|
|
57
|
+
|
|
58
|
+
## Status
|
|
59
|
+
|
|
60
|
+
**Specification Phase** - See [SPEC.md](./SPEC.md) for design review.
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level allocator classes: PageArena and BufferPool
|
|
3
|
+
*/
|
|
4
|
+
import { type AllocatedBuffer } from './index.js';
|
|
5
|
+
export interface ArenaOptions {
|
|
6
|
+
/** Initial size in bytes */
|
|
7
|
+
initialSize?: number;
|
|
8
|
+
/** Growth factor when arena is full (default: 2) */
|
|
9
|
+
growthFactor?: number;
|
|
10
|
+
/** Maximum size in bytes (default: 4GB) */
|
|
11
|
+
maxSize?: number;
|
|
12
|
+
/** Use huge pages if available (Linux only) */
|
|
13
|
+
useHugePages?: boolean;
|
|
14
|
+
/** Lock pages in memory (prevent swap) */
|
|
15
|
+
lockMemory?: boolean;
|
|
16
|
+
/** Use mmap instead of malloc (for large arenas) */
|
|
17
|
+
useMmap?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A bump-allocator arena backed by page-aligned memory
|
|
21
|
+
*/
|
|
22
|
+
export declare class PageArena {
|
|
23
|
+
private _capacity;
|
|
24
|
+
private _used;
|
|
25
|
+
private _growthFactor;
|
|
26
|
+
private _maxSize;
|
|
27
|
+
private _buffer;
|
|
28
|
+
private _useMmap;
|
|
29
|
+
private _destroyed;
|
|
30
|
+
private _allocations;
|
|
31
|
+
constructor(options?: ArenaOptions);
|
|
32
|
+
/** Current capacity in bytes */
|
|
33
|
+
get capacity(): number;
|
|
34
|
+
/** Bytes used */
|
|
35
|
+
get used(): number;
|
|
36
|
+
/** Bytes available */
|
|
37
|
+
get available(): number;
|
|
38
|
+
/** Get ArrayBuffer view of entire arena */
|
|
39
|
+
get buffer(): ArrayBuffer;
|
|
40
|
+
/**
|
|
41
|
+
* Allocate bytes, returns offset from arena start
|
|
42
|
+
*/
|
|
43
|
+
alloc(size: number, alignment?: number): number;
|
|
44
|
+
/**
|
|
45
|
+
* Get slice at offset
|
|
46
|
+
*/
|
|
47
|
+
slice(offset: number, length: number): ArrayBuffer;
|
|
48
|
+
/**
|
|
49
|
+
* Reset arena (does not free memory, allows reuse)
|
|
50
|
+
*/
|
|
51
|
+
reset(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Grow arena by at least additionalBytes
|
|
54
|
+
*/
|
|
55
|
+
grow(additionalBytes: number): void;
|
|
56
|
+
private _grow;
|
|
57
|
+
/**
|
|
58
|
+
* Free all memory
|
|
59
|
+
*/
|
|
60
|
+
destroy(): void;
|
|
61
|
+
private _checkDestroyed;
|
|
62
|
+
/**
|
|
63
|
+
* Create arena from existing options
|
|
64
|
+
*/
|
|
65
|
+
static from(options: ArenaOptions): PageArena;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* A pool of fixed-size page-aligned buffers
|
|
69
|
+
*/
|
|
70
|
+
export declare class BufferPool {
|
|
71
|
+
private _bufferSize;
|
|
72
|
+
private _buffers;
|
|
73
|
+
private _available;
|
|
74
|
+
private _inUse;
|
|
75
|
+
private _destroyed;
|
|
76
|
+
constructor(bufferSize: number, initialCount?: number);
|
|
77
|
+
/** Individual buffer size */
|
|
78
|
+
get bufferSize(): number;
|
|
79
|
+
/** Total buffers in pool */
|
|
80
|
+
get totalCount(): number;
|
|
81
|
+
/** Available buffers */
|
|
82
|
+
get availableCount(): number;
|
|
83
|
+
/** In-use buffers */
|
|
84
|
+
get inUseCount(): number;
|
|
85
|
+
/**
|
|
86
|
+
* Acquire a buffer from the pool
|
|
87
|
+
*/
|
|
88
|
+
acquire(): AllocatedBuffer;
|
|
89
|
+
/**
|
|
90
|
+
* Return a buffer to the pool
|
|
91
|
+
*/
|
|
92
|
+
release(buffer: AllocatedBuffer): void;
|
|
93
|
+
/**
|
|
94
|
+
* Grow pool by count
|
|
95
|
+
*/
|
|
96
|
+
grow(count: number): void;
|
|
97
|
+
/**
|
|
98
|
+
* Destroy pool and free all memory
|
|
99
|
+
*/
|
|
100
|
+
destroy(): void;
|
|
101
|
+
private _checkDestroyed;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=allocator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allocator.d.ts","sourceRoot":"","sources":["allocator.ts"],"names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cb;;OAEG;IACH,OAAO,IAAI,IAAI;IAcf,OAAO,CAAC,eAAe;IAMvB;;OAEG;IACH,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,YAAY,GAAG,SAAS;CAG9C;AAMD;;GAEG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,QAAQ,CAAoB;IACpC,OAAO,CAAC,UAAU,CAAoB;IACtC,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,UAAU,CAAU;gBAEhB,UAAU,EAAE,MAAM,EAAE,YAAY,GAAE,MAAU;IAkBxD,6BAA6B;IAC7B,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,4BAA4B;IAC5B,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,wBAAwB;IACxB,IAAI,cAAc,IAAI,MAAM,CAE3B;IAED,qBAAqB;IACrB,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED;;OAEG;IACH,OAAO,IAAI,eAAe;IAoB1B;;OAEG;IACH,OAAO,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI;IAWtC;;OAEG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAYzB;;OAEG;IACH,OAAO,IAAI,IAAI;IAaf,OAAO,CAAC,eAAe;CAKxB"}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level allocator classes: PageArena and BufferPool
|
|
3
|
+
*/
|
|
4
|
+
import { getPageSize, alignToPage, allocPageAligned, mmapAnonymous } from './index.js';
|
|
5
|
+
import { PageSizeError } from './errors.js';
|
|
6
|
+
/**
|
|
7
|
+
* A bump-allocator arena backed by page-aligned memory
|
|
8
|
+
*/
|
|
9
|
+
export class PageArena {
|
|
10
|
+
_capacity;
|
|
11
|
+
_used;
|
|
12
|
+
_growthFactor;
|
|
13
|
+
_maxSize;
|
|
14
|
+
_buffer;
|
|
15
|
+
_useMmap;
|
|
16
|
+
_destroyed;
|
|
17
|
+
_allocations;
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
const pageSize = getPageSize();
|
|
20
|
+
const initialSize = alignToPage(options.initialSize ?? pageSize.size * 4);
|
|
21
|
+
this._growthFactor = options.growthFactor ?? 2;
|
|
22
|
+
this._maxSize = options.maxSize ?? 4 * 1024 * 1024 * 1024; // 4GB
|
|
23
|
+
this._useMmap = options.useMmap ?? initialSize >= 1024 * 1024; // mmap for >= 1MB
|
|
24
|
+
this._capacity = initialSize;
|
|
25
|
+
this._used = 0;
|
|
26
|
+
this._destroyed = false;
|
|
27
|
+
this._allocations = [];
|
|
28
|
+
// Allocate initial buffer
|
|
29
|
+
if (this._useMmap) {
|
|
30
|
+
this._buffer = mmapAnonymous(initialSize, 'read-write');
|
|
31
|
+
if (options.lockMemory) {
|
|
32
|
+
this._buffer.lock();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this._buffer = allocPageAligned(initialSize);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Current capacity in bytes */
|
|
40
|
+
get capacity() {
|
|
41
|
+
return this._capacity;
|
|
42
|
+
}
|
|
43
|
+
/** Bytes used */
|
|
44
|
+
get used() {
|
|
45
|
+
return this._used;
|
|
46
|
+
}
|
|
47
|
+
/** Bytes available */
|
|
48
|
+
get available() {
|
|
49
|
+
return this._capacity - this._used;
|
|
50
|
+
}
|
|
51
|
+
/** Get ArrayBuffer view of entire arena */
|
|
52
|
+
get buffer() {
|
|
53
|
+
this._checkDestroyed();
|
|
54
|
+
return this._buffer.buffer;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Allocate bytes, returns offset from arena start
|
|
58
|
+
*/
|
|
59
|
+
alloc(size, alignment = 8) {
|
|
60
|
+
this._checkDestroyed();
|
|
61
|
+
if (size <= 0) {
|
|
62
|
+
throw new PageSizeError('Allocation size must be positive');
|
|
63
|
+
}
|
|
64
|
+
// Align offset to requested alignment (not page size - arena is already page-aligned)
|
|
65
|
+
const currentOffset = this._used;
|
|
66
|
+
const alignedOffset = Math.ceil(currentOffset / alignment) * alignment;
|
|
67
|
+
// Check if we need to grow
|
|
68
|
+
if (alignedOffset + size > this._capacity) {
|
|
69
|
+
this._grow(alignedOffset + size);
|
|
70
|
+
}
|
|
71
|
+
this._used = alignedOffset + size;
|
|
72
|
+
this._allocations.push({ offset: alignedOffset, size });
|
|
73
|
+
return alignedOffset;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Get slice at offset
|
|
77
|
+
*/
|
|
78
|
+
slice(offset, length) {
|
|
79
|
+
this._checkDestroyed();
|
|
80
|
+
return this._buffer.buffer.slice(offset, offset + length);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Reset arena (does not free memory, allows reuse)
|
|
84
|
+
*/
|
|
85
|
+
reset() {
|
|
86
|
+
this._checkDestroyed();
|
|
87
|
+
this._used = 0;
|
|
88
|
+
this._allocations = [];
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Grow arena by at least additionalBytes
|
|
92
|
+
*/
|
|
93
|
+
grow(additionalBytes) {
|
|
94
|
+
this._checkDestroyed();
|
|
95
|
+
this._grow(this._used + additionalBytes);
|
|
96
|
+
}
|
|
97
|
+
_grow(requiredSize) {
|
|
98
|
+
if (requiredSize > this._maxSize) {
|
|
99
|
+
throw new PageSizeError(`Arena would exceed max size of ${this._maxSize} bytes`);
|
|
100
|
+
}
|
|
101
|
+
// Calculate new size with growth factor
|
|
102
|
+
const alignedRequired = alignToPage(requiredSize);
|
|
103
|
+
const newSize = Math.min(Math.max(alignedRequired, alignToPage(this._capacity * this._growthFactor)), this._maxSize);
|
|
104
|
+
// Allocate new buffer
|
|
105
|
+
let newBuffer;
|
|
106
|
+
if (this._useMmap) {
|
|
107
|
+
newBuffer = mmapAnonymous(newSize, 'read-write');
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const buf = allocPageAligned(newSize);
|
|
111
|
+
if (!buf) {
|
|
112
|
+
throw new PageSizeError(`Failed to grow arena to ${newSize} bytes`);
|
|
113
|
+
}
|
|
114
|
+
newBuffer = buf;
|
|
115
|
+
}
|
|
116
|
+
// Copy existing data to new buffer
|
|
117
|
+
if (this._used > 0 && this._buffer) {
|
|
118
|
+
const oldView = new Uint8Array(this._buffer.buffer);
|
|
119
|
+
const newView = new Uint8Array(newBuffer.buffer);
|
|
120
|
+
newView.set(oldView.subarray(0, this._used));
|
|
121
|
+
}
|
|
122
|
+
// Free old buffer
|
|
123
|
+
if (this._buffer) {
|
|
124
|
+
if ('unmap' in this._buffer) {
|
|
125
|
+
this._buffer.unmap();
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this._buffer.free();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Update references
|
|
132
|
+
this._buffer = newBuffer;
|
|
133
|
+
this._capacity = newSize;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Free all memory
|
|
137
|
+
*/
|
|
138
|
+
destroy() {
|
|
139
|
+
if (this._destroyed)
|
|
140
|
+
return;
|
|
141
|
+
this._destroyed = true;
|
|
142
|
+
if (this._buffer) {
|
|
143
|
+
if ('unmap' in this._buffer) {
|
|
144
|
+
this._buffer.unmap();
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
this._buffer.free();
|
|
148
|
+
}
|
|
149
|
+
this._buffer = null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
_checkDestroyed() {
|
|
153
|
+
if (this._destroyed) {
|
|
154
|
+
throw new PageSizeError('Arena has been destroyed');
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Create arena from existing options
|
|
159
|
+
*/
|
|
160
|
+
static from(options) {
|
|
161
|
+
return new PageArena(options);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// ============================================================================
|
|
165
|
+
// BufferPool
|
|
166
|
+
// ============================================================================
|
|
167
|
+
/**
|
|
168
|
+
* A pool of fixed-size page-aligned buffers
|
|
169
|
+
*/
|
|
170
|
+
export class BufferPool {
|
|
171
|
+
_bufferSize;
|
|
172
|
+
_buffers;
|
|
173
|
+
_available;
|
|
174
|
+
_inUse;
|
|
175
|
+
_destroyed;
|
|
176
|
+
constructor(bufferSize, initialCount = 4) {
|
|
177
|
+
const pageSize = getPageSize();
|
|
178
|
+
this._bufferSize = alignToPage(bufferSize);
|
|
179
|
+
this._buffers = [];
|
|
180
|
+
this._available = [];
|
|
181
|
+
this._inUse = new Set();
|
|
182
|
+
this._destroyed = false;
|
|
183
|
+
// Pre-allocate initial buffers
|
|
184
|
+
for (let i = 0; i < initialCount; i++) {
|
|
185
|
+
const buffer = allocPageAligned(this._bufferSize);
|
|
186
|
+
if (buffer) {
|
|
187
|
+
this._buffers.push(buffer);
|
|
188
|
+
this._available.push(buffer);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/** Individual buffer size */
|
|
193
|
+
get bufferSize() {
|
|
194
|
+
return this._bufferSize;
|
|
195
|
+
}
|
|
196
|
+
/** Total buffers in pool */
|
|
197
|
+
get totalCount() {
|
|
198
|
+
return this._buffers.length;
|
|
199
|
+
}
|
|
200
|
+
/** Available buffers */
|
|
201
|
+
get availableCount() {
|
|
202
|
+
return this._available.length;
|
|
203
|
+
}
|
|
204
|
+
/** In-use buffers */
|
|
205
|
+
get inUseCount() {
|
|
206
|
+
return this._inUse.size;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Acquire a buffer from the pool
|
|
210
|
+
*/
|
|
211
|
+
acquire() {
|
|
212
|
+
this._checkDestroyed();
|
|
213
|
+
let buffer;
|
|
214
|
+
if (this._available.length > 0) {
|
|
215
|
+
buffer = this._available.pop();
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
// Grow pool
|
|
219
|
+
buffer = allocPageAligned(this._bufferSize);
|
|
220
|
+
if (!buffer) {
|
|
221
|
+
throw new PageSizeError('Failed to allocate buffer from pool');
|
|
222
|
+
}
|
|
223
|
+
this._buffers.push(buffer);
|
|
224
|
+
}
|
|
225
|
+
this._inUse.add(buffer);
|
|
226
|
+
return buffer;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Return a buffer to the pool
|
|
230
|
+
*/
|
|
231
|
+
release(buffer) {
|
|
232
|
+
this._checkDestroyed();
|
|
233
|
+
if (!this._inUse.has(buffer)) {
|
|
234
|
+
throw new PageSizeError('Buffer does not belong to this pool or already released');
|
|
235
|
+
}
|
|
236
|
+
this._inUse.delete(buffer);
|
|
237
|
+
this._available.push(buffer);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Grow pool by count
|
|
241
|
+
*/
|
|
242
|
+
grow(count) {
|
|
243
|
+
this._checkDestroyed();
|
|
244
|
+
for (let i = 0; i < count; i++) {
|
|
245
|
+
const buffer = allocPageAligned(this._bufferSize);
|
|
246
|
+
if (buffer) {
|
|
247
|
+
this._buffers.push(buffer);
|
|
248
|
+
this._available.push(buffer);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Destroy pool and free all memory
|
|
254
|
+
*/
|
|
255
|
+
destroy() {
|
|
256
|
+
if (this._destroyed)
|
|
257
|
+
return;
|
|
258
|
+
this._destroyed = true;
|
|
259
|
+
for (const buffer of this._buffers) {
|
|
260
|
+
buffer.free();
|
|
261
|
+
}
|
|
262
|
+
this._buffers = [];
|
|
263
|
+
this._available = [];
|
|
264
|
+
this._inUse.clear();
|
|
265
|
+
}
|
|
266
|
+
_checkDestroyed() {
|
|
267
|
+
if (this._destroyed) {
|
|
268
|
+
throw new PageSizeError('BufferPool has been destroyed');
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
//# sourceMappingURL=allocator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allocator.js","sourceRoot":"","sources":["allocator.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