Skip to content

Commit 3d30a56

Browse files
committed
Custom allocator.
1 parent bdaf77a commit 3d30a56

File tree

6 files changed

+39
-34
lines changed

6 files changed

+39
-34
lines changed

internal/util/alloc_other.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

internal/util/alloc_unix.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,12 @@
33
package util
44

55
import (
6-
"context"
76
"math"
87

98
"github.com/tetratelabs/wazero/experimental"
109
"golang.org/x/sys/unix"
1110
)
1211

13-
func withAllocator(ctx context.Context) context.Context {
14-
if math.MaxInt != math.MaxInt64 {
15-
return ctx
16-
}
17-
return experimental.WithMemoryAllocator(ctx,
18-
experimental.MemoryAllocatorFunc(newAllocator))
19-
}
20-
2112
func newAllocator(cap, max uint64) experimental.LinearMemory {
2213
// Round up to the page size.
2314
rnd := uint64(unix.Getpagesize() - 1)
@@ -52,7 +43,9 @@ type mmappedMemory struct {
5243
}
5344

5445
func (m *mmappedMemory) Reallocate(size uint64) []byte {
55-
if com := uint64(len(m.buf)); com < size {
46+
com := uint64(len(m.buf))
47+
res := uint64(cap(m.buf))
48+
if com < size && size < res {
5649
// Round up to the page size.
5750
rnd := uint64(unix.Getpagesize() - 1)
5851
new := (size + rnd) &^ rnd

internal/util/alloc_windows.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package util
44

55
import (
6-
"context"
76
"math"
87
"reflect"
98
"unsafe"
@@ -12,27 +11,19 @@ import (
1211
"golang.org/x/sys/windows"
1312
)
1413

15-
func withAllocator(ctx context.Context) context.Context {
16-
if math.MaxInt != math.MaxInt64 {
17-
return ctx
18-
}
19-
return experimental.WithMemoryAllocator(ctx,
20-
experimental.MemoryAllocatorFunc(newAllocator))
21-
}
22-
2314
func newAllocator(cap, max uint64) experimental.LinearMemory {
2415
// Round up to the page size.
2516
rnd := uint64(windows.Getpagesize() - 1)
2617
max = (max + rnd) &^ rnd
2718
cap = (cap + rnd) &^ rnd
2819

2920
if max > math.MaxInt {
30-
// This ensures int(max) overflows to a negative value,
31-
// and unix.Mmap returns EINVAL.
21+
// This ensures uintptr(max) overflows to a large value,
22+
// and windows.VirtualAlloc returns an error.
3223
max = math.MaxUint64
3324
}
3425
// Reserve max bytes of address space, to ensure we won't need to move it.
35-
// A protected, private, anonymous mapping should not commit memory.
26+
// This does not commit memory.
3627
r, err := windows.VirtualAlloc(0, uintptr(max), windows.MEM_RESERVE, windows.PAGE_READWRITE)
3728
if err != nil {
3829
panic(err)
@@ -61,7 +52,9 @@ type virtualMemory struct {
6152
}
6253

6354
func (m *virtualMemory) Reallocate(size uint64) []byte {
64-
if com := uint64(len(m.buf)); com < size {
55+
com := uint64(len(m.buf))
56+
res := uint64(cap(m.buf))
57+
if com < size && size < res {
6558
// Round up to the page size.
6659
rnd := uint64(windows.Getpagesize() - 1)
6760
new := (size + rnd) &^ rnd

internal/util/mmap.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build (darwin || linux) && (amd64 || arm64 || riscv64) && !(sqlite3_flock || sqlite3_noshm || sqlite3_nosys)
1+
//go:build (darwin || linux) && (amd64 || arm64 || riscv64) && !(sqlite3_noshm || sqlite3_nosys)
22

33
package util
44

@@ -8,9 +8,15 @@ import (
88
"unsafe"
99

1010
"github.com/tetratelabs/wazero/api"
11+
"github.com/tetratelabs/wazero/experimental"
1112
"golang.org/x/sys/unix"
1213
)
1314

15+
func withAllocator(ctx context.Context) context.Context {
16+
return experimental.WithMemoryAllocator(ctx,
17+
experimental.MemoryAllocatorFunc(newAllocator))
18+
}
19+
1420
type mmapState struct {
1521
regions []*MappedRegion
1622
}

internal/util/mmap_other.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
//go:build !(darwin || linux) || !(amd64 || arm64 || riscv64) || sqlite3_flock || sqlite3_noshm || sqlite3_nosys
1+
//go:build !(darwin || linux || windows) || !(amd64 || arm64 || riscv64) || sqlite3_noshm || sqlite3_nosys
22

33
package util
44

5+
import "context"
6+
57
type mmapState struct{}
8+
9+
func withAllocator(ctx context.Context) context.Context {
10+
return ctx
11+
}

internal/util/mmap_windows.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build (amd64 || arm64) && !(sqlite3_noshm || sqlite3_nosys)
2+
3+
package util
4+
5+
import (
6+
"context"
7+
8+
"github.com/tetratelabs/wazero/experimental"
9+
)
10+
11+
type mmapState struct{}
12+
13+
func withAllocator(ctx context.Context) context.Context {
14+
return experimental.WithMemoryAllocator(ctx,
15+
experimental.MemoryAllocatorFunc(newAllocator))
16+
}

0 commit comments

Comments
 (0)