Skip to content

Commit

Permalink
Use unsafe.Slice in Memory.UnsafeData (#200)
Browse files Browse the repository at this point in the history
* use unsafe.Slice in Memory.UnsafeData (#199)

* bump bazel build's Go to 1.21.1
  • Loading branch information
guregu authored Oct 31, 2023
1 parent 915992a commit 650f03f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
8 changes: 4 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_go",
sha256 = "685052b498b6ddfe562ca7a97736741d87916fe536623afb7da2824c0211c369",
sha256 = "91585017debb61982f7054c9688857a2ad1fd823fc3f9cb05048b0025c47d023",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.33.0/rules_go-v0.33.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.33.0/rules_go-v0.33.0.zip",
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.42.0/rules_go-v0.42.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.42.0/rules_go-v0.42.0.zip",
],
)

Expand Down Expand Up @@ -63,6 +63,6 @@ go_repository(

go_rules_dependencies()

go_register_toolchains(version = "1.16")
go_register_toolchains(version = "1.21.1")

gazelle_dependencies()
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 650f03f

Please sign in to comment.