Adds Runtime.WithCapacityPages to avoid allocations during runtime. (#514)
`Runtime.WithMemoryCapacityPages` is a function that determines memory
capacity in pages (65536 bytes per page). The inputs are the min and
possibly nil max defined by the module, and the default is to return
the min.
Ex. To set capacity to max when exists:
```golang
c.WithMemoryCapacityPages(func(minPages uint32, maxPages *uint32) uint32 {
if maxPages != nil {
return *maxPages
}
return minPages
})
```
Note: This applies at compile time, ModuleBuilder.Build or Runtime.CompileModule.
Fixes #500
Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
@@ -90,6 +90,83 @@ func TestExternTypeName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemory_ValidateCap(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mem *Memory
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "ok",
|
||||
mem: &Memory{Min: 2, Cap: 2},
|
||||
},
|
||||
{
|
||||
name: "cap < min",
|
||||
mem: &Memory{Min: 2, Cap: 1},
|
||||
expectedErr: "capacity 1 pages (64 Ki) less than minimum 2 pages (128 Ki)",
|
||||
},
|
||||
{
|
||||
name: "cap > maxLimit",
|
||||
mem: &Memory{Min: 2, Cap: 4},
|
||||
expectedErr: "capacity 4 pages (256 Ki) over limit of 3 pages (192 Ki)",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tc := tt
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.mem.ValidateCap(3)
|
||||
if tc.expectedErr == "" {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
require.EqualError(t, err, tc.expectedErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemory_ValidateMinMax(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mem *Memory
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "ok",
|
||||
mem: &Memory{Min: 2},
|
||||
},
|
||||
{
|
||||
name: "max < min",
|
||||
mem: &Memory{Min: 2, Max: 0, IsMaxEncoded: true},
|
||||
expectedErr: "min 2 pages (128 Ki) > max 0 pages (0 Ki)",
|
||||
},
|
||||
{
|
||||
name: "min > limit",
|
||||
mem: &Memory{Min: math.MaxUint32},
|
||||
expectedErr: "min 4294967295 pages (3 Ti) over limit of 3 pages (192 Ki)",
|
||||
},
|
||||
{
|
||||
name: "max > limit",
|
||||
mem: &Memory{Max: math.MaxUint32, IsMaxEncoded: true},
|
||||
expectedErr: "max 4294967295 pages (3 Ti) over limit of 3 pages (192 Ki)",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tc := tt
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.mem.ValidateMinMax(3)
|
||||
if tc.expectedErr == "" {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
require.EqualError(t, err, tc.expectedErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestModule_allDeclarations(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
module *Module
|
||||
@@ -730,7 +807,7 @@ func TestModule_buildMemoryInstance(t *testing.T) {
|
||||
t.Run("non-nil", func(t *testing.T) {
|
||||
min := uint32(1)
|
||||
max := uint32(10)
|
||||
m := Module{MemorySection: &Memory{Min: min, Max: max}}
|
||||
m := Module{MemorySection: &Memory{Min: min, Cap: min, Max: max}}
|
||||
mem := m.buildMemory()
|
||||
require.Equal(t, min, mem.Min)
|
||||
require.Equal(t, max, mem.Max)
|
||||
|
||||
Reference in New Issue
Block a user