github.com/sunvim/utils@v0.1.0/mmap/mmap.go (about) 1 package mmap 2 3 import ( 4 "reflect" 5 "syscall" 6 ) 7 8 func Alloc(eltsize uintptr, size int) (reflect.SliceHeader, error) { 9 var errno syscall.Errno 10 var slice reflect.SliceHeader 11 fd := -1 12 slice.Data, _, errno = syscall.Syscall6( 13 syscall.SYS_MMAP, 14 0, // address 15 eltsize*uintptr(size), 16 syscall.PROT_READ|syscall.PROT_WRITE, 17 syscall.MAP_ANON|syscall.MAP_PRIVATE, 18 uintptr(fd), // No file descriptor 19 0, // offset 20 ) 21 slice.Cap = size 22 23 var err error 24 if errno != 0 { 25 err = errno 26 } 27 return slice, err 28 } 29 30 // Free releases resources allocated via Alloc 31 func Free(slice reflect.SliceHeader, eltsize uintptr) error { 32 _, _, errno := syscall.Syscall(syscall.SYS_MUNMAP, slice.Data, eltsize*uintptr(slice.Cap), 0) 33 if errno != 0 { 34 return errno 35 } 36 return nil 37 }