Skip to content

Commit

Permalink
use unsafe.Slice in Memory.UnsafeData (bytecodealliance#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Oct 31, 2023
1 parent 915992a commit 4407535
Showing 1 changed file with 1 addition and 6 deletions.
7 changes: 1 addition & 6 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,8 @@ func (mem *Memory) Data(store Storelike) unsafe.Pointer {
// `m` alive for long enough while you're using the `[]byte` slice. If the
// `[]byte` slice is used after `m` is GC'd then that is undefined behavior.
func (mem *Memory) UnsafeData(store Storelike) []byte {
// see https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
const MaxLen = 1 << 32
length := mem.DataSize(store)
if length >= MaxLen {
panic("memory is too big")
}
return (*[MaxLen]byte)(mem.Data(store))[:length:length]
return unsafe.Slice((*byte)(mem.Data(store)), length)
}

// DataSize returns the size, in bytes, that `Data()` is valid for
Expand Down

0 comments on commit 4407535

Please sign in to comment.