summaryrefslogtreecommitdiff
path: root/vendor/github.com/ncruces/go-sqlite3/internal/alloc/alloc_unix.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/ncruces/go-sqlite3/internal/alloc/alloc_unix.go')
-rw-r--r--vendor/github.com/ncruces/go-sqlite3/internal/alloc/alloc_unix.go32
1 files changed, 20 insertions, 12 deletions
diff --git a/vendor/github.com/ncruces/go-sqlite3/internal/alloc/alloc_unix.go b/vendor/github.com/ncruces/go-sqlite3/internal/alloc/alloc_unix.go
index a00dbbf24..203d8471a 100644
--- a/vendor/github.com/ncruces/go-sqlite3/internal/alloc/alloc_unix.go
+++ b/vendor/github.com/ncruces/go-sqlite3/internal/alloc/alloc_unix.go
@@ -9,24 +9,31 @@ import (
"golang.org/x/sys/unix"
)
-func NewMemory(_, max uint64) experimental.LinearMemory {
+func NewMemory(cap, max uint64) experimental.LinearMemory {
// Round up to the page size.
rnd := uint64(unix.Getpagesize() - 1)
- max = (max + rnd) &^ rnd
+ res := (max + rnd) &^ rnd
- if max > math.MaxInt {
- // This ensures int(max) overflows to a negative value,
+ if res > math.MaxInt {
+ // This ensures int(res) overflows to a negative value,
// and unix.Mmap returns EINVAL.
- max = math.MaxUint64
+ res = math.MaxUint64
}
- // Reserve max bytes of address space, to ensure we won't need to move it.
+ com := res
+ prot := unix.PROT_READ | unix.PROT_WRITE
+ if cap < max { // Commit memory only if cap=max.
+ com = 0
+ prot = unix.PROT_NONE
+ }
+
+ // Reserve res bytes of address space, to ensure we won't need to move it.
// A protected, private, anonymous mapping should not commit memory.
- b, err := unix.Mmap(-1, 0, int(max), unix.PROT_NONE, unix.MAP_PRIVATE|unix.MAP_ANON)
+ b, err := unix.Mmap(-1, 0, int(res), prot, unix.MAP_PRIVATE|unix.MAP_ANON)
if err != nil {
panic(err)
}
- return &mmappedMemory{buf: b[:0]}
+ return &mmappedMemory{buf: b[:com]}
}
// The slice covers the entire mmapped memory:
@@ -40,9 +47,11 @@ func (m *mmappedMemory) Reallocate(size uint64) []byte {
com := uint64(len(m.buf))
res := uint64(cap(m.buf))
if com < size && size <= res {
- // Round up to the page size.
+ // Grow geometrically, round up to the page size.
rnd := uint64(unix.Getpagesize() - 1)
- new := (size + rnd) &^ rnd
+ new := com + com>>3
+ new = min(max(size, new), res)
+ new = (new + rnd) &^ rnd
// Commit additional memory up to new bytes.
err := unix.Mprotect(m.buf[com:new], unix.PROT_READ|unix.PROT_WRITE)
@@ -50,8 +59,7 @@ func (m *mmappedMemory) Reallocate(size uint64) []byte {
return nil
}
- // Update committed memory.
- m.buf = m.buf[:new]
+ m.buf = m.buf[:new] // Update committed memory.
}
// Limit returned capacity because bytes beyond
// len(m.buf) have not yet been committed.