From 9830f3c5d492f20906c1bad94ddce6bf44857ea1 Mon Sep 17 00:00:00 2001 From: Lou Knauer Date: Mon, 25 Jul 2022 14:28:49 +0200 Subject: [PATCH] Fix mmap based allocator --- internal/memstore/allocator.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/memstore/allocator.go b/internal/memstore/allocator.go index b202726..4a0ab00 100644 --- a/internal/memstore/allocator.go +++ b/internal/memstore/allocator.go @@ -18,6 +18,7 @@ var allocatorLock sync.Mutex var allocatorPool [][]byte func RequestBytes(size int) []byte { + requested := size size = (size + bufferSizeInBytes - 1) / bufferSizeInBytes * bufferSizeInBytes if size == bufferSizeInBytes { allocatorLock.Lock() @@ -31,16 +32,20 @@ func RequestBytes(size int) []byte { } pagesize := os.Getpagesize() - if size < pagesize || pagesize%size != 0 { + if size < pagesize || size%pagesize != 0 { panic("page size and buffer size do not go with each other") } - bytes, err := unix.Mmap(-1, 0, size, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_ANONYMOUS) + bytes, err := unix.Mmap(-1, 0, size, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_ANONYMOUS|unix.MAP_SHARED) if err != nil { - panic(err) + panic("unix.Mmap failed: " + err.Error()) } - return bytes + if cap(bytes) != size { + panic("whoops?") + } + + return bytes[:requested] } func ReleaseBytes(bytes []byte) {