Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unsafe.Slice in Memory.UnsafeData #200

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading