@@ -9,29 +9,36 @@ import (
99 "golang.org/x/sys/windows"
1010)
1111
12- func NewMemory (_ , max uint64 ) experimental.LinearMemory {
12+ func NewMemory (cap , max uint64 ) experimental.LinearMemory {
1313 // Round up to the page size.
1414 rnd := uint64 (windows .Getpagesize () - 1 )
15- max = (max + rnd ) &^ rnd
15+ res : = (max + rnd ) &^ rnd
1616
17- if max > math .MaxInt {
18- // This ensures uintptr(max ) overflows to a large value,
17+ if res > math .MaxInt {
18+ // This ensures uintptr(res ) overflows to a large value,
1919 // and windows.VirtualAlloc returns an error.
20- max = math .MaxUint64
20+ res = math .MaxUint64
2121 }
2222
23- // Reserve max bytes of address space, to ensure we won't need to move it.
24- // This does not commit memory.
25- r , err := windows .VirtualAlloc (0 , uintptr (max ), windows .MEM_RESERVE , windows .PAGE_READWRITE )
23+ com := res
24+ kind := windows .MEM_COMMIT
25+ if cap < max { // Commit memory only if cap=max.
26+ com = 0
27+ kind = windows .MEM_RESERVE
28+ }
29+
30+ // Reserve res bytes of address space, to ensure we won't need to move it.
31+ r , err := windows .VirtualAlloc (0 , uintptr (res ), uint32 (kind ), windows .PAGE_READWRITE )
2632 if err != nil {
2733 panic (err )
2834 }
2935
3036 mem := virtualMemory {addr : r }
3137 // SliceHeader, although deprecated, avoids a go vet warning.
3238 sh := (* reflect .SliceHeader )(unsafe .Pointer (& mem .buf ))
33- sh .Cap = int (max )
3439 sh .Data = r
40+ sh .Len = int (com )
41+ sh .Cap = int (res )
3542 return & mem
3643}
3744
@@ -59,8 +66,7 @@ func (m *virtualMemory) Reallocate(size uint64) []byte {
5966 return nil
6067 }
6168
62- // Update committed memory.
63- m .buf = m .buf [:new ]
69+ m .buf = m .buf [:new ] // Update committed memory.
6470 }
6571 // Limit returned capacity because bytes beyond
6672 // len(m.buf) have not yet been committed.
0 commit comments