Skip to content

Commit d23bdcd

Browse files
committed
Commiting memory not needed.
1 parent 321d359 commit d23bdcd

File tree

2 files changed

+4
-16
lines changed

2 files changed

+4
-16
lines changed

internal/util/alloc_unix.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,20 @@ func newAllocator(cap, max uint64) experimental.LinearMemory {
1313
// Round up to the page size.
1414
rnd := uint64(unix.Getpagesize() - 1)
1515
max = (max + rnd) &^ rnd
16-
cap = (cap + rnd) &^ rnd
1716

1817
if max > math.MaxInt {
1918
// This ensures int(max) overflows to a negative value,
2019
// and unix.Mmap returns EINVAL.
2120
max = math.MaxUint64
2221
}
22+
2323
// Reserve max bytes of address space, to ensure we won't need to move it.
2424
// A protected, private, anonymous mapping should not commit memory.
2525
b, err := unix.Mmap(-1, 0, int(max), unix.PROT_NONE, unix.MAP_PRIVATE|unix.MAP_ANON)
2626
if err != nil {
2727
panic(err)
2828
}
29-
// Commit the initial cap bytes of memory.
30-
err = unix.Mprotect(b[:cap], unix.PROT_READ|unix.PROT_WRITE)
31-
if err != nil {
32-
unix.Munmap(b)
33-
panic(err)
34-
}
35-
return &mmappedMemory{buf: b[:cap]}
29+
return &mmappedMemory{buf: b[:0]}
3630
}
3731

3832
// The slice covers the entire mmapped memory:

internal/util/alloc_windows.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,23 @@ func newAllocator(cap, max uint64) experimental.LinearMemory {
1515
// Round up to the page size.
1616
rnd := uint64(windows.Getpagesize() - 1)
1717
max = (max + rnd) &^ rnd
18-
cap = (cap + rnd) &^ rnd
1918

2019
if max > math.MaxInt {
2120
// This ensures uintptr(max) overflows to a large value,
2221
// and windows.VirtualAlloc returns an error.
2322
max = math.MaxUint64
2423
}
24+
2525
// Reserve max bytes of address space, to ensure we won't need to move it.
2626
// This does not commit memory.
2727
r, err := windows.VirtualAlloc(0, uintptr(max), windows.MEM_RESERVE, windows.PAGE_READWRITE)
2828
if err != nil {
2929
panic(err)
3030
}
31-
// Commit the initial cap bytes of memory.
32-
_, err = windows.VirtualAlloc(r, uintptr(cap), windows.MEM_COMMIT, windows.PAGE_READWRITE)
33-
if err != nil {
34-
windows.VirtualFree(r, 0, windows.MEM_RELEASE)
35-
panic(err)
36-
}
31+
3732
mem := virtualMemory{addr: r}
3833
// SliceHeader, although deprecated, avoids a go vet warning.
3934
sh := (*reflect.SliceHeader)(unsafe.Pointer(&mem.buf))
40-
sh.Len = int(cap) // Not a bug.
4135
sh.Cap = int(max) // Not a bug.
4236
sh.Data = r
4337
return &mem

0 commit comments

Comments
 (0)