-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathpool.ts
515 lines (464 loc) · 11.7 KB
/
pool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// SPDX-License-Identifier: Apache-2.0
import type { Maybe } from "@thi.ng/api";
import {
SIZEOF,
typedArray,
type Type,
type TypedArray,
} from "@thi.ng/api/typedarray";
import type { Pow2 } from "@thi.ng/binary";
import { align } from "@thi.ng/binary/align";
import { isNumber } from "@thi.ng/checks/is-number";
import { assert } from "@thi.ng/errors/assert";
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
import type { IMemPool, MemPoolOpts, MemPoolStats } from "./api.js";
const STATE_FREE = 0;
const STATE_USED = 1;
const STATE_TOP = 2;
const STATE_END = 3;
const STATE_ALIGN = 4;
const STATE_FLAGS = 5;
const STATE_MIN_SPLIT = 6;
const MASK_COMPACT = 1;
const MASK_SPLIT = 2;
const SIZEOF_STATE = 7 * 4;
const MEM_BLOCK_SIZE = 0;
const MEM_BLOCK_NEXT = 1;
const SIZEOF_MEM_BLOCK = 2 * 4;
export class MemPool implements IMemPool {
buf: ArrayBufferLike;
protected readonly start: number;
protected u8: Uint8Array;
protected u32: Uint32Array;
protected state: Uint32Array;
constructor(opts: Partial<MemPoolOpts> = {}) {
this.buf = opts.buf ? opts.buf : new ArrayBuffer(opts.size || 0x1000);
this.start = opts.start != null ? align(Math.max(opts.start, 0), 4) : 0;
this.u8 = new Uint8Array(this.buf);
this.u32 = new Uint32Array(this.buf);
this.state = new Uint32Array(this.buf, this.start, SIZEOF_STATE / 4);
if (!opts.skipInitialization) {
const _align = opts.align || 8;
assert(
_align >= 8,
`invalid alignment: ${_align}, must be a pow2 and >= 8`
);
const top = this.initialTop(_align);
const resolvedEnd =
opts.end != null
? Math.min(opts.end, this.buf.byteLength)
: this.buf.byteLength;
if (top >= resolvedEnd) {
illegalArgs(
`insufficient address range (0x${this.start.toString(
16
)} - 0x${resolvedEnd.toString(16)})`
);
}
this.align = _align;
this.doCompact = opts.compact !== false;
this.doSplit = opts.split !== false;
this.minSplit = opts.minSplit || 16;
this.end = resolvedEnd;
this.top = top;
this._free = 0;
this._used = 0;
}
}
stats(): Readonly<MemPoolStats> {
const listStats = (block: number) => {
let count = 0;
let size = 0;
while (block) {
count++;
size += this.blockSize(block);
block = this.blockNext(block);
}
return { count, size };
};
const free = listStats(this._free);
return {
free,
used: listStats(this._used),
top: this.top,
available: this.end - this.top + free.size,
total: this.buf.byteLength,
};
}
callocAs<T extends Type>(type: T, num: number, fill = 0) {
const block = this.mallocAs(type, num);
block?.fill(fill);
return block;
}
mallocAs<T extends Type>(type: T, num: number) {
const addr = this.malloc(num * SIZEOF[type]);
return addr ? typedArray(type, this.buf, addr, num) : undefined;
}
calloc(bytes: number, fill = 0) {
const addr = this.malloc(bytes);
addr && this.u8.fill(fill, addr, addr + bytes);
return addr;
}
malloc(bytes: number) {
if (bytes <= 0) {
return 0;
}
const paddedSize = align(bytes + SIZEOF_MEM_BLOCK, this.align);
const end = this.end;
let top = this.top;
let block = this._free;
let prev = 0;
while (block) {
const blockSize = this.blockSize(block);
const isTop = block + blockSize >= top;
if (isTop || blockSize >= paddedSize) {
return this.mallocTop(
block,
prev,
blockSize,
paddedSize,
isTop
);
}
prev = block;
block = this.blockNext(block);
}
block = top;
top = block + paddedSize;
if (top <= end) {
this.initBlock(block, paddedSize, this._used);
this._used = block;
this.top = top;
return __blockDataAddress(block);
}
return 0;
}
private mallocTop(
block: number,
prev: number,
blockSize: number,
paddedSize: number,
isTop: boolean
) {
if (isTop && block + paddedSize > this.end) return 0;
if (prev) {
this.unlinkBlock(prev, block);
} else {
this._free = this.blockNext(block);
}
this.setBlockNext(block, this._used);
this._used = block;
if (isTop) {
this.top = block + this.setBlockSize(block, paddedSize);
} else if (this.doSplit) {
const excess = blockSize - paddedSize;
excess >= this.minSplit &&
this.splitBlock(block, paddedSize, excess);
}
return __blockDataAddress(block);
}
realloc(ptr: number, bytes: number) {
if (bytes <= 0) {
return 0;
}
const oldAddr = __blockSelfAddress(ptr);
let newAddr = 0;
let block = this._used;
let blockEnd = 0;
while (block) {
if (block === oldAddr) {
[newAddr, blockEnd] = this.reallocBlock(block, bytes);
break;
}
block = this.blockNext(block);
}
// copy old block contents to new addr
if (newAddr && newAddr !== oldAddr) {
this.u8.copyWithin(
__blockDataAddress(newAddr),
__blockDataAddress(oldAddr),
blockEnd
);
}
return __blockDataAddress(newAddr);
}
private reallocBlock(block: number, bytes: number) {
const blockSize = this.blockSize(block);
const blockEnd = block + blockSize;
const isTop = blockEnd >= this.top;
const paddedSize = align(bytes + SIZEOF_MEM_BLOCK, this.align);
// shrink & possibly split existing block
if (paddedSize <= blockSize) {
if (this.doSplit) {
const excess = blockSize - paddedSize;
if (excess >= this.minSplit) {
this.splitBlock(block, paddedSize, excess);
} else if (isTop) {
this.top = block + paddedSize;
}
} else if (isTop) {
this.top = block + paddedSize;
}
return [block, blockEnd];
}
// try to enlarge block if current top
if (isTop && block + paddedSize < this.end) {
this.top = block + this.setBlockSize(block, paddedSize);
return [block, blockEnd];
}
// fallback to free & malloc
this.free(block);
return [__blockSelfAddress(this.malloc(bytes)), blockEnd];
}
reallocArray<T extends TypedArray>(array: T, num: number): Maybe<T> {
if (array.buffer !== this.buf) {
return;
}
const addr = this.realloc(
array.byteOffset,
num * array.BYTES_PER_ELEMENT
);
return addr
? new (<any>array.constructor)(this.buf, addr, num)
: undefined;
}
free(ptrOrArray: number | TypedArray) {
let addr: number;
if (!isNumber(ptrOrArray)) {
if (ptrOrArray.buffer !== this.buf) {
return false;
}
addr = ptrOrArray.byteOffset;
} else {
addr = ptrOrArray;
}
addr = __blockSelfAddress(addr);
let block = this._used;
let prev = 0;
while (block) {
if (block === addr) {
if (prev) {
this.unlinkBlock(prev, block);
} else {
this._used = this.blockNext(block);
}
this.insert(block);
this.doCompact && this.compact();
return true;
}
prev = block;
block = this.blockNext(block);
}
return false;
}
freeAll() {
this._free = 0;
this._used = 0;
this.top = this.initialTop();
}
release() {
delete (<any>this).u8;
delete (<any>this).u32;
delete (<any>this).state;
delete (<any>this).buf;
return true;
}
protected get align() {
return <Pow2>this.state[STATE_ALIGN];
}
protected set align(x: Pow2) {
this.state[STATE_ALIGN] = x;
}
protected get end() {
return this.state[STATE_END];
}
protected set end(x: number) {
this.state[STATE_END] = x;
}
protected get top() {
return this.state[STATE_TOP];
}
protected set top(x: number) {
this.state[STATE_TOP] = x;
}
protected get _free() {
return this.state[STATE_FREE];
}
protected set _free(block: number) {
this.state[STATE_FREE] = block;
}
protected get _used() {
return this.state[STATE_USED];
}
protected set _used(block: number) {
this.state[STATE_USED] = block;
}
protected get doCompact() {
return !!(this.state[STATE_FLAGS] & MASK_COMPACT);
}
protected set doCompact(flag: boolean) {
flag
? (this.state[STATE_FLAGS] |= 1 << (MASK_COMPACT - 1))
: (this.state[STATE_FLAGS] &= ~MASK_COMPACT);
}
protected get doSplit() {
return !!(this.state[STATE_FLAGS] & MASK_SPLIT);
}
protected set doSplit(flag: boolean) {
flag
? (this.state[STATE_FLAGS] |= 1 << (MASK_SPLIT - 1))
: (this.state[STATE_FLAGS] &= ~MASK_SPLIT);
}
protected get minSplit() {
return this.state[STATE_MIN_SPLIT];
}
protected set minSplit(x: number) {
assert(
x > SIZEOF_MEM_BLOCK,
`illegal min split threshold: ${x}, require at least ${
SIZEOF_MEM_BLOCK + 1
}`
);
this.state[STATE_MIN_SPLIT] = x;
}
protected blockSize(block: number) {
return this.u32[(block >> 2) + MEM_BLOCK_SIZE];
}
/**
* Sets & returns given block size.
*
* @param block -
* @param size -
*/
protected setBlockSize(block: number, size: number) {
this.u32[(block >> 2) + MEM_BLOCK_SIZE] = size;
return size;
}
protected blockNext(block: number) {
return this.u32[(block >> 2) + MEM_BLOCK_NEXT];
}
/**
* Sets block next pointer to `next`. Use zero to indicate list end.
*
* @param block -
*/
protected setBlockNext(block: number, next: number) {
this.u32[(block >> 2) + MEM_BLOCK_NEXT] = next;
}
/**
* Initializes block header with given `size` and `next` pointer. Returns `block`.
*
* @param block -
* @param size -
* @param next -
*/
protected initBlock(block: number, size: number, next: number) {
const idx = block >>> 2;
this.u32[idx + MEM_BLOCK_SIZE] = size;
this.u32[idx + MEM_BLOCK_NEXT] = next;
return block;
}
protected unlinkBlock(prev: number, block: number) {
this.setBlockNext(prev, this.blockNext(block));
}
protected splitBlock(block: number, blockSize: number, excess: number) {
this.insert(
this.initBlock(
block + this.setBlockSize(block, blockSize),
excess,
0
)
);
this.doCompact && this.compact();
}
protected initialTop(_align = this.align) {
return (
align(this.start + SIZEOF_STATE + SIZEOF_MEM_BLOCK, _align) -
SIZEOF_MEM_BLOCK
);
}
/**
* Traverses free list and attempts to recursively merge blocks
* occupying consecutive memory regions. Returns true if any blocks
* have been merged. Only called if `compact` option is enabled.
*/
protected compact() {
let block = this._free;
let prev = 0;
let scan = 0;
let scanPrev: number;
let res = false;
while (block) {
scanPrev = block;
scan = this.blockNext(block);
while (scan && scanPrev + this.blockSize(scanPrev) === scan) {
// console.log("merge:", scan.addr, scan.size);
scanPrev = scan;
scan = this.blockNext(scan);
}
if (scanPrev !== block) {
const newSize = scanPrev - block + this.blockSize(scanPrev);
// console.log("merged size:", newSize);
this.setBlockSize(block, newSize);
const next = this.blockNext(scanPrev);
let tmp = this.blockNext(block);
while (tmp && tmp !== next) {
// console.log("release:", tmp.addr);
const tn = this.blockNext(tmp);
this.setBlockNext(tmp, 0);
tmp = tn;
}
this.setBlockNext(block, next);
res = true;
}
// re-adjust top if poss
if (block + this.blockSize(block) >= this.top) {
this.top = block;
prev
? this.unlinkBlock(prev, block)
: (this._free = this.blockNext(block));
}
prev = block;
block = this.blockNext(block);
}
return res;
}
/**
* Inserts given block into list of free blocks, sorted by address.
*
* @param block -
*/
protected insert(block: number) {
let ptr = this._free;
let prev = 0;
while (ptr) {
if (block <= ptr) break;
prev = ptr;
ptr = this.blockNext(ptr);
}
if (prev) {
this.setBlockNext(prev, block);
} else {
this._free = block;
}
this.setBlockNext(block, ptr);
}
}
/**
* Returns a block's data address, based on given alignment.
*
* @param blockAddress -
*
* @internal
*/
const __blockDataAddress = (blockAddress: number) =>
blockAddress > 0 ? blockAddress + SIZEOF_MEM_BLOCK : 0;
/**
* Returns block start address for given data address and alignment.
*
* @param dataAddress -
*
* @internal
*/
const __blockSelfAddress = (dataAddress: number) =>
dataAddress > 0 ? dataAddress - SIZEOF_MEM_BLOCK : 0;