-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc_std.go
79 lines (64 loc) · 1.63 KB
/
alloc_std.go
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
//go:build !tinygo && !wasm && !wasi && !tinygo.wasm
// +build !tinygo,!wasm,!wasi,!tinygo.wasm
package memory
import (
"github.com/moontrade/memory/alloc/rpmalloc"
)
func Init() {}
func Scope(fn func(a AutoFree)) {
scope(fn)
}
func scope(fn func(a AutoFree)) {
a := NewAuto(32)
defer a.Free()
fn(a)
a.Print()
}
// Alloc calls Alloc on the system allocator
//export alloc
func Alloc(size uintptr) Pointer {
return Pointer(rpmalloc.Malloc(size))
}
//export allocCap
func AllocCap(size uintptr) (Pointer, uintptr) {
p, c := rpmalloc.MallocCap(size)
return Pointer(p), c
}
//export allocZeroed
func AllocZeroed(size uintptr) Pointer {
return Pointer(rpmalloc.MallocZeroed(size))
}
//export allocZeroedCap
func AllocZeroedCap(size uintptr) (Pointer, uintptr) {
p, c := rpmalloc.MallocZeroedCap(size)
return Pointer(p), c
}
// Alloc calls Alloc on the system allocator
//export calloc
func Calloc(num, size uintptr) Pointer {
return Pointer(rpmalloc.Calloc(num, size))
}
//export callocCap
func CallocCap(num, size uintptr) (Pointer, uintptr) {
p, c := rpmalloc.CallocCap(num, size)
return Pointer(p), c
}
// Realloc calls Realloc on the system allocator
//export realloc
func Realloc(p Pointer, size uintptr) Pointer {
return Pointer(rpmalloc.Realloc(uintptr(p), size))
}
//export reallocCap
func ReallocCap(p Pointer, size uintptr) (Pointer, uintptr) {
newPtr, c := rpmalloc.ReallocCap(uintptr(p), size)
return Pointer(newPtr), c
}
// Free calls Free on the system allocator
//export free
func Free(p Pointer) {
rpmalloc.Free(uintptr(p))
}
//export sizeof
func SizeOf(ptr Pointer) uintptr {
return rpmalloc.UsableSize(uintptr(ptr))
}