Skip to content

Commit

Permalink
Use madvise syscall wrapper from golang.org/x/sys/unix
Browse files Browse the repository at this point in the history
Direct syscalls using syscall.Syscall(SYS_*, ...) should no longer be
used on darwin, see [1]. Instead, use the madvise syscall wrapper
provided by the golang.org/x/sys/unix package for all unix platforms.
This implement the same functionality.

[1] https://golang.org/doc/go1.12#darwin

As suggested by @ptabor in etcd-io/etcd#12316 (comment)
  • Loading branch information
tklauser committed Sep 24, 2020
1 parent f6be823 commit 74e833b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 13 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -7,6 +7,7 @@ go:
- 1.12

before_install:
- go get -v golang.org/x/sys/unix
- go get -v honnef.co/go/tools/...
- go get -v github.com/kisielk/errcheck

Expand Down
13 changes: 3 additions & 10 deletions bolt_unix.go
Expand Up @@ -7,6 +7,8 @@ import (
"syscall"
"time"
"unsafe"

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

// flock acquires an advisory lock on a file descriptor.
Expand Down Expand Up @@ -55,7 +57,7 @@ func mmap(db *DB, sz int) error {
}

// Advise the kernel that the mmap is accessed randomly.
err = madvise(b, syscall.MADV_RANDOM)
err = unix.Madvise(b, syscall.MADV_RANDOM)
if err != nil && err != syscall.ENOSYS {
// Ignore not implemented error in kernel because it still works.
return fmt.Errorf("madvise: %s", err)
Expand All @@ -82,12 +84,3 @@ func munmap(db *DB) error {
db.datasz = 0
return err
}

// NOTE: This function is copied from stdlib because it is not available on darwin.
func madvise(b []byte, advice int) (err error) {
_, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice))
if e1 != 0 {
err = e1
}
return
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -2,4 +2,4 @@ module go.etcd.io/bbolt

go 1.12

require golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5
require golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d
4 changes: 2 additions & 2 deletions go.sum
@@ -1,2 +1,2 @@
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d h1:L/IKR6COd7ubZrs2oTnTi73IhgqJ71c9s80WsQnh0Es=
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 comments on commit 74e833b

Please sign in to comment.