Skip to content

Commit 53eef15

Browse files
committed
Fixed capacity virtual memory.
1 parent d23bdcd commit 53eef15

File tree

8 files changed

+53
-23
lines changed

8 files changed

+53
-23
lines changed

internal/util/alloc_other.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !(unix || windows) || sqlite3_nosys
2+
3+
package util
4+
5+
import "github.com/tetratelabs/wazero/experimental"
6+
7+
func virtualAlloc(cap, max uint64) experimental.LinearMemory {
8+
return sliceAlloc(cap, max)
9+
}

internal/util/alloc_slice.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//go:build !(darwin || linux) || !(amd64 || arm64 || riscv64) || sqlite3_noshm || sqlite3_nosys
2+
3+
package util
4+
5+
import "github.com/tetratelabs/wazero/experimental"
6+
7+
func sliceAlloc(cap, max uint64) experimental.LinearMemory {
8+
return &sliceBuffer{make([]byte, cap), max}
9+
}
10+
11+
type sliceBuffer struct {
12+
buf []byte
13+
max uint64
14+
}
15+
16+
func (b *sliceBuffer) Free() {}
17+
18+
func (b *sliceBuffer) Reallocate(size uint64) []byte {
19+
if cap := uint64(cap(b.buf)); size > cap {
20+
b.buf = append(b.buf[:cap], make([]byte, size-cap)...)
21+
} else {
22+
b.buf = b.buf[:size]
23+
}
24+
return b.buf
25+
}

internal/util/alloc_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"golang.org/x/sys/unix"
1010
)
1111

12-
func newAllocator(cap, max uint64) experimental.LinearMemory {
12+
func virtualAlloc(cap, max uint64) experimental.LinearMemory {
1313
// Round up to the page size.
1414
rnd := uint64(unix.Getpagesize() - 1)
1515
max = (max + rnd) &^ rnd

internal/util/alloc_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"golang.org/x/sys/windows"
1212
)
1313

14-
func newAllocator(cap, max uint64) experimental.LinearMemory {
14+
func virtualAlloc(cap, max uint64) experimental.LinearMemory {
1515
// Round up to the page size.
1616
rnd := uint64(windows.Getpagesize() - 1)
1717
max = (max + rnd) &^ rnd

internal/util/mmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
func withAllocator(ctx context.Context) context.Context {
1616
return experimental.WithMemoryAllocator(ctx,
17-
experimental.MemoryAllocatorFunc(newAllocator))
17+
experimental.MemoryAllocatorFunc(virtualAlloc))
1818
}
1919

2020
type mmapState struct {

internal/util/mmap_other.go

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

33
package util
44

5-
import "context"
5+
import (
6+
"context"
7+
8+
"github.com/tetratelabs/wazero/experimental"
9+
)
610

711
type mmapState struct{}
812

913
func withAllocator(ctx context.Context) context.Context {
10-
return ctx
14+
return experimental.WithMemoryAllocator(ctx,
15+
experimental.MemoryAllocatorFunc(func(cap, max uint64) experimental.LinearMemory {
16+
if cap == max {
17+
return virtualAlloc(cap, max)
18+
}
19+
return sliceAlloc(cap, max)
20+
}))
1121
}

internal/util/mmap_windows.go

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

tests/testcfg/testcfg.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
)
1010

1111
func init() {
12-
sqlite3.RuntimeConfig = wazero.NewRuntimeConfig().WithMemoryLimitPages(1024)
12+
sqlite3.RuntimeConfig = wazero.NewRuntimeConfig().
13+
WithMemoryCapacityFromMax(true).
14+
WithMemoryLimitPages(1024)
1315

1416
if os.Getenv("CI") != "" {
1517
path := filepath.Join(os.TempDir(), "wazero")

0 commit comments

Comments
 (0)