github.com/chenzhuoyu/iasm@v0.9.1/repl/mem.go (about) 1 package repl 2 3 import ( 4 `os` 5 `reflect` 6 `syscall` 7 `unsafe` 8 ) 9 10 type _Memory struct { 11 size uint64 12 addr uintptr 13 } 14 15 func (self _Memory) p() (v unsafe.Pointer) { 16 *(*uintptr)(unsafe.Pointer(&v)) = self.addr 17 return 18 } 19 20 func (self _Memory) buf() (v []byte) { 21 p := (*reflect.SliceHeader)(unsafe.Pointer(&v)) 22 p.Cap = int(self.size) 23 p.Len = int(self.size) 24 p.Data = self.addr 25 return 26 } 27 28 const ( 29 _AP = syscall.MAP_ANON | syscall.MAP_PRIVATE 30 _RWX = syscall.PROT_READ | syscall.PROT_WRITE | syscall.PROT_EXEC 31 ) 32 33 var ( 34 _PageSize = uint64(os.Getpagesize()) 35 ) 36 37 func mmap(nb uint64) (_Memory, error) { 38 nb = (((nb - 1) / _PageSize) + 1) * _PageSize 39 mm, _, err := syscall.RawSyscall6(syscall.SYS_MMAP, 0, uintptr(nb), _RWX, _AP, 0, 0) 40 41 /* check for errors */ 42 if err != 0 { 43 return _Memory{}, err 44 } else { 45 return _Memory { addr: mm, size: nb }, nil 46 } 47 } 48 49 func munmap(m _Memory) error { 50 if _, _, err := syscall.RawSyscall(syscall.SYS_MUNMAP, m.addr, uintptr(m.size), 0); err != 0 { 51 return err 52 } else { 53 return nil 54 } 55 }