Skip to content

Commit

Permalink
starlark: use syscall.Mmap to avoid golang.org/x/sys dependency (goog…
Browse files Browse the repository at this point in the history
…le#287)

Also, enable the optimization on other POSIX platforms.
  • Loading branch information
adonovan authored Jul 7, 2020
1 parent 5993613 commit 474f21a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/chzyer/logex v1.1.10 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
golang.org/x/sys v0.0.0-20191002063906-3421d5a6bb1c
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5O
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
golang.org/x/sys v0.0.0-20191002063906-3421d5a6bb1c h1:Vco5b+cuG5NNfORVxZy6bYZQ7rsigisU1WQFkvQ0L5E=
golang.org/x/sys v0.0.0-20191002063906-3421d5a6bb1c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2 changes: 1 addition & 1 deletion starlark/int_generic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//+build !linux,!darwin darwin,arm64 !amd64,!arm64,!mips64x,!ppc64x
//+build !linux,!darwin,!dragonfly,!freebsd,!netbsd,!openbsd,!solaris darwin,arm64 !amd64,!arm64,!mips64x,!ppc64x

package starlark

Expand Down
16 changes: 12 additions & 4 deletions starlark/int_posix64.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//+build linux darwin
//+build linux darwin dragonfly freebsd netbsd openbsd solaris
//+build amd64 arm64,!darwin mips64x ppc64x

package starlark
Expand All @@ -22,9 +22,9 @@ import (
"log"
"math"
"math/big"
"runtime"
"syscall"
"unsafe"

"golang.org/x/sys/unix"
)

// intImpl represents a union of (int32, *big.Int) in a single pointer,
Expand Down Expand Up @@ -59,7 +59,15 @@ func makeBigInt(x *big.Int) Int { return Int{intImpl(x)} }
var smallints = reserveAddresses(1 << 32)

func reserveAddresses(len int) uintptr {
b, err := unix.Mmap(-1, 0, len, unix.PROT_READ, unix.MAP_PRIVATE|unix.MAP_ANONYMOUS)
// Use syscall to avoid golang.org/x/sys/unix dependency.
MAP_ANON := 0x1000 // darwin (and all BSDs)
switch runtime.GOOS {
case "linux":
MAP_ANON = 0x20
case "solaris":
MAP_ANON = 0x100
}
b, err := syscall.Mmap(-1, 0, len, syscall.PROT_READ, syscall.MAP_PRIVATE|MAP_ANON)
if err != nil {
log.Fatalf("mmap: %v", err)
}
Expand Down

0 comments on commit 474f21a

Please sign in to comment.