From 3d30a561f0e0145cbb607cc2fe4fd1f856817dec Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 17 May 2024 17:23:54 +0100 Subject: [PATCH] Custom allocator. --- internal/util/alloc_other.go | 9 --------- internal/util/alloc_unix.go | 13 +++---------- internal/util/alloc_windows.go | 19 ++++++------------- internal/util/mmap.go | 8 +++++++- internal/util/mmap_other.go | 8 +++++++- internal/util/mmap_windows.go | 16 ++++++++++++++++ 6 files changed, 39 insertions(+), 34 deletions(-) delete mode 100644 internal/util/alloc_other.go create mode 100644 internal/util/mmap_windows.go diff --git a/internal/util/alloc_other.go b/internal/util/alloc_other.go deleted file mode 100644 index 95672ba..0000000 --- a/internal/util/alloc_other.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build !(unix || windows) || sqlite3_nosys - -package util - -import "context" - -func withAllocator(ctx context.Context) context.Context { - return ctx -} diff --git a/internal/util/alloc_unix.go b/internal/util/alloc_unix.go index 673d130..f7a1c03 100644 --- a/internal/util/alloc_unix.go +++ b/internal/util/alloc_unix.go @@ -3,21 +3,12 @@ package util import ( - "context" "math" "github.com/tetratelabs/wazero/experimental" "golang.org/x/sys/unix" ) -func withAllocator(ctx context.Context) context.Context { - if math.MaxInt != math.MaxInt64 { - return ctx - } - return experimental.WithMemoryAllocator(ctx, - experimental.MemoryAllocatorFunc(newAllocator)) -} - func newAllocator(cap, max uint64) experimental.LinearMemory { // Round up to the page size. rnd := uint64(unix.Getpagesize() - 1) @@ -52,7 +43,9 @@ type mmappedMemory struct { } func (m *mmappedMemory) Reallocate(size uint64) []byte { - if com := uint64(len(m.buf)); com < size { + com := uint64(len(m.buf)) + res := uint64(cap(m.buf)) + if com < size && size < res { // Round up to the page size. rnd := uint64(unix.Getpagesize() - 1) new := (size + rnd) &^ rnd diff --git a/internal/util/alloc_windows.go b/internal/util/alloc_windows.go index 76863ea..482689f 100644 --- a/internal/util/alloc_windows.go +++ b/internal/util/alloc_windows.go @@ -3,7 +3,6 @@ package util import ( - "context" "math" "reflect" "unsafe" @@ -12,14 +11,6 @@ import ( "golang.org/x/sys/windows" ) -func withAllocator(ctx context.Context) context.Context { - if math.MaxInt != math.MaxInt64 { - return ctx - } - return experimental.WithMemoryAllocator(ctx, - experimental.MemoryAllocatorFunc(newAllocator)) -} - func newAllocator(cap, max uint64) experimental.LinearMemory { // Round up to the page size. rnd := uint64(windows.Getpagesize() - 1) @@ -27,12 +18,12 @@ func newAllocator(cap, max uint64) experimental.LinearMemory { cap = (cap + rnd) &^ rnd if max > math.MaxInt { - // This ensures int(max) overflows to a negative value, - // and unix.Mmap returns EINVAL. + // This ensures uintptr(max) overflows to a large value, + // and windows.VirtualAlloc returns an error. max = math.MaxUint64 } // Reserve max bytes of address space, to ensure we won't need to move it. - // A protected, private, anonymous mapping should not commit memory. + // This does not commit memory. r, err := windows.VirtualAlloc(0, uintptr(max), windows.MEM_RESERVE, windows.PAGE_READWRITE) if err != nil { panic(err) @@ -61,7 +52,9 @@ type virtualMemory struct { } func (m *virtualMemory) Reallocate(size uint64) []byte { - if com := uint64(len(m.buf)); com < size { + com := uint64(len(m.buf)) + res := uint64(cap(m.buf)) + if com < size && size < res { // Round up to the page size. rnd := uint64(windows.Getpagesize() - 1) new := (size + rnd) &^ rnd diff --git a/internal/util/mmap.go b/internal/util/mmap.go index f7de238..de89a47 100644 --- a/internal/util/mmap.go +++ b/internal/util/mmap.go @@ -1,4 +1,4 @@ -//go:build (darwin || linux) && (amd64 || arm64 || riscv64) && !(sqlite3_flock || sqlite3_noshm || sqlite3_nosys) +//go:build (darwin || linux) && (amd64 || arm64 || riscv64) && !(sqlite3_noshm || sqlite3_nosys) package util @@ -8,9 +8,15 @@ import ( "unsafe" "github.com/tetratelabs/wazero/api" + "github.com/tetratelabs/wazero/experimental" "golang.org/x/sys/unix" ) +func withAllocator(ctx context.Context) context.Context { + return experimental.WithMemoryAllocator(ctx, + experimental.MemoryAllocatorFunc(newAllocator)) +} + type mmapState struct { regions []*MappedRegion } diff --git a/internal/util/mmap_other.go b/internal/util/mmap_other.go index d16dade..c0bb15c 100644 --- a/internal/util/mmap_other.go +++ b/internal/util/mmap_other.go @@ -1,5 +1,11 @@ -//go:build !(darwin || linux) || !(amd64 || arm64 || riscv64) || sqlite3_flock || sqlite3_noshm || sqlite3_nosys +//go:build !(darwin || linux || windows) || !(amd64 || arm64 || riscv64) || sqlite3_noshm || sqlite3_nosys package util +import "context" + type mmapState struct{} + +func withAllocator(ctx context.Context) context.Context { + return ctx +} diff --git a/internal/util/mmap_windows.go b/internal/util/mmap_windows.go new file mode 100644 index 0000000..f8dcbf5 --- /dev/null +++ b/internal/util/mmap_windows.go @@ -0,0 +1,16 @@ +//go:build (amd64 || arm64) && !(sqlite3_noshm || sqlite3_nosys) + +package util + +import ( + "context" + + "github.com/tetratelabs/wazero/experimental" +) + +type mmapState struct{} + +func withAllocator(ctx context.Context) context.Context { + return experimental.WithMemoryAllocator(ctx, + experimental.MemoryAllocatorFunc(newAllocator)) +}