-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathtlsf-mt.ts
45 lines (34 loc) · 1.27 KB
/
tlsf-mt.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
import {BLOCK_OVERHEAD} from "./common";
import {allocateBlock, freeBlock, reallocateBlock, ROOT, ROOT_INIT, TLSFinitialize, checkUsedBlock, moveBlock} from "./tlsf-base";
import {TlsfMutex_lock, TlsfMutex_unlock} from './tlsf-mutex'
const mutex_ptr = memory.data(4, 16);
// @ts-ignore: decorator
@global @unsafe
export function __alloc(size: usize): usize {
TlsfMutex_lock(mutex_ptr);
if (!load<i32>(ROOT_INIT)) TLSFinitialize();
let r: usize = changetype<usize>(allocateBlock(ROOT, size)) + BLOCK_OVERHEAD;
TlsfMutex_unlock(mutex_ptr);
return r;
}
// @ts-ignore: decorator
@global @unsafe
export function __realloc(ptr: usize, size: usize): usize {
TlsfMutex_lock(mutex_ptr);
if (!load<i32>(ROOT_INIT)) TLSFinitialize();
let r: usize = (ptr < __heap_base
? changetype<usize>(moveBlock(ROOT, checkUsedBlock(ptr), size))
: changetype<usize>(reallocateBlock(ROOT, checkUsedBlock(ptr), size))
) + BLOCK_OVERHEAD;
TlsfMutex_unlock(mutex_ptr);
return r;
}
// @ts-ignore: decorator
@global @unsafe
export function __free(ptr: usize): void {
if (ptr < __heap_base) return;
TlsfMutex_lock(mutex_ptr);
if (!load<i32>(ROOT_INIT)) TLSFinitialize();
freeBlock(ROOT, checkUsedBlock(ptr));
TlsfMutex_unlock(mutex_ptr);
}