-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathtlsf-st.ts
28 lines (24 loc) · 960 Bytes
/
tlsf-st.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
import {BLOCK_OVERHEAD} from "./common";
import {allocateBlock, freeBlock, reallocateBlock, ROOT, ROOT_INIT, TLSFinitialize, checkUsedBlock, moveBlock} from "./tlsf-base";
// @ts-ignore: decorator
@global @unsafe
export function __alloc(size: usize): usize {
if (!load<i32>(ROOT_INIT)) TLSFinitialize();
return changetype<usize>(allocateBlock(ROOT, size)) + BLOCK_OVERHEAD;
}
// @ts-ignore: decorator
@global @unsafe
export function __realloc(ptr: usize, size: usize): usize {
if (!load<i32>(ROOT_INIT)) TLSFinitialize();
return (ptr < __heap_base
? changetype<usize>(moveBlock(ROOT, checkUsedBlock(ptr), size))
: changetype<usize>(reallocateBlock(ROOT, checkUsedBlock(ptr), size))
) + BLOCK_OVERHEAD;
}
// @ts-ignore: decorator
@global @unsafe
export function __free(ptr: usize): void {
if (ptr < __heap_base) return;
if (!load<i32>(ROOT_INIT)) TLSFinitialize();
freeBlock(ROOT, checkUsedBlock(ptr));
}