github.com/tencent/goom@v1.0.1/internal/bytecode/stub/space.go (about) 1 package stub 2 3 import ( 4 "fmt" 5 6 "github.com/tencent/goom/internal/bytecode/memory" 7 ) 8 9 const ( 10 // TypeHolder place holder 方式获取可执行内存 11 TypeHolder = 1 12 // TypeMMap mmap 方式获取可执行内存 13 TypeMMap = 2 14 ) 15 16 // Space 可执行空间 17 type Space struct { 18 Addr uintptr 19 Space *[]byte 20 typ int 21 } 22 23 // Acquire enough executable space 24 // nolint 25 func Acquire(spaceLen int) (*Space, error) { 26 if addr, space, err := acquireFromMMap(spaceLen); err == nil { 27 return &Space{ 28 Addr: addr, 29 Space: space, 30 typ: TypeMMap, 31 }, nil 32 } 33 if addr, space, err := acquireFromHolder(spaceLen); err == nil { 34 return &Space{ 35 Addr: addr, 36 Space: space, 37 typ: TypeHolder, 38 }, nil 39 } else { 40 return nil, err 41 } 42 } 43 44 // Write 写入数据 45 func Write(s *Space, data []byte) error { 46 switch s.typ { 47 case TypeMMap: 48 copy(*s.Space, data[:]) 49 return nil 50 case TypeHolder: 51 return memory.WriteTo(s.Addr, data) 52 default: 53 return fmt.Errorf("stub write fail, illegal type: %d", s.typ) 54 } 55 }