Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Nov 5, 2024
1 parent ee15dec commit 04af9fe
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 28 deletions.
10 changes: 7 additions & 3 deletions internal/util/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ type MappedRegion struct {
used bool
}

func MapRegion(ctx context.Context, mod api.Module, f *os.File, offset int64, size int32, prot int) (*MappedRegion, error) {
func MapRegion(ctx context.Context, mod api.Module, f *os.File, offset int64, size int32, readOnly bool) (*MappedRegion, error) {
s := ctx.Value(moduleKey{}).(*moduleState)
r := s.new(ctx, mod, size)
err := r.mmap(f, offset, prot)
err := r.mmap(f, offset, readOnly)
if err != nil {
return nil, err
}
Expand All @@ -75,7 +75,11 @@ func (r *MappedRegion) Unmap() error {
return err
}

func (r *MappedRegion) mmap(f *os.File, offset int64, prot int) error {
func (r *MappedRegion) mmap(f *os.File, offset int64, readOnly bool) error {
prot := unix.PROT_READ
if !readOnly {
prot |= unix.PROT_WRITE
}
_, err := unix.MmapPtr(int(f.Fd()), offset, r.addr, uintptr(r.size),
prot, unix.MAP_SHARED|unix.MAP_FIXED)
r.used = err == nil
Expand Down
2 changes: 1 addition & 1 deletion vfs/shm.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build ((darwin || linux || freebsd || openbsd || netbsd || dragonfly || illumos) && (386 || arm || amd64 || arm64 || riscv64 || ppc64le) && !sqlite3_nosys) || sqlite3_flock || sqlite3_dotlk
//go:build ((linux || darwin || freebsd || openbsd || netbsd || dragonfly || illumos) && (386 || arm || amd64 || arm64 || riscv64 || ppc64le) && !sqlite3_nosys) || sqlite3_flock || sqlite3_dotlk

package vfs

Expand Down
3 changes: 1 addition & 2 deletions vfs/shm_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, ext
}
}

r, err := util.MapRegion(ctx, mod, s.File, int64(id)*int64(size), size,
unix.PROT_READ|unix.PROT_WRITE)
r, err := util.MapRegion(ctx, mod, s.File, int64(id)*int64(size), size, false)
if err != nil {
return 0, _IOERR_SHMMAP
}
Expand Down
6 changes: 3 additions & 3 deletions vfs/shm_dotlk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"github.com/tetratelabs/wazero/api"
)

const _WALINDEX_PGSZ = 32768

type vfsShmBuffer struct {
shared []byte // +checklocks:Mutex
refs int // +checklocks:vfsShmBuffersMtx
Expand Down Expand Up @@ -151,7 +149,7 @@ func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) _ErrorCode {

switch {
case flags&_SHM_LOCK != 0:
s.shmAcquire()
defer s.shmAcquire()
case flags&_SHM_EXCLUSIVE != 0:
s.shmRelease()
}
Expand Down Expand Up @@ -248,6 +246,8 @@ func (s *vfsShm) shmBarrier() {
//
// https://sqlite.org/walformat.html#the_wal_index_file_format

const _WALINDEX_PGSZ = 32768

// +checklocks:s.Mutex
func (s *vfsShm) shmAcquire() {
// Copies modified words from shared to private memory.
Expand Down
26 changes: 8 additions & 18 deletions vfs/shm_ofd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build (darwin || linux) && (386 || arm || amd64 || arm64 || riscv64 || ppc64le) && !(sqlite3_flock || sqlite3_dotlk || sqlite3_nosys)
//go:build (linux || darwin) && (386 || arm || amd64 || arm64 || riscv64 || ppc64le) && !(sqlite3_flock || sqlite3_dotlk || sqlite3_nosys)

package vfs

Expand All @@ -21,7 +21,7 @@ type vfsShm struct {
regions []*util.MappedRegion
readOnly bool
blocking bool
barrier sync.Mutex
sync.Mutex
}

var _ blockingSharedMemory = &vfsShm{}
Expand All @@ -32,7 +32,7 @@ func (s *vfsShm) shmOpen() _ErrorCode {
unix.O_RDWR|unix.O_CREAT|unix.O_NOFOLLOW, 0666)
if err != nil {
f, err = os.OpenFile(s.path,
unix.O_CREAT|unix.O_NOFOLLOW, 0666)
unix.O_RDONLY|unix.O_CREAT|unix.O_NOFOLLOW, 0666)
s.readOnly = true
}
if err != nil {
Expand Down Expand Up @@ -64,10 +64,7 @@ func (s *vfsShm) shmOpen() _ErrorCode {
return _IOERR_SHMOPEN
}
}
if rc := osReadLock(s.File, _SHM_DMS, 1, time.Millisecond); rc != _OK {
return rc
}
return _OK
return osReadLock(s.File, _SHM_DMS, 1, time.Millisecond)
}

func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (uint32, _ErrorCode) {
Expand All @@ -94,13 +91,7 @@ func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, ext
}
}

var prot int
if s.readOnly {
prot = unix.PROT_READ
} else {
prot = unix.PROT_READ | unix.PROT_WRITE
}
r, err := util.MapRegion(ctx, mod, s.File, int64(id)*int64(size), size, prot)
r, err := util.MapRegion(ctx, mod, s.File, int64(id)*int64(size), size, s.readOnly)
if err != nil {
return 0, _IOERR_SHMMAP
}
Expand Down Expand Up @@ -156,8 +147,7 @@ func (s *vfsShm) shmUnmap(delete bool) {
for _, r := range s.regions {
r.Unmap()
}
clear(s.regions)
s.regions = s.regions[:0]
s.regions = nil

// Close the file.
if delete {
Expand All @@ -168,9 +158,9 @@ func (s *vfsShm) shmUnmap(delete bool) {
}

func (s *vfsShm) shmBarrier() {
s.barrier.Lock()
s.Lock()
//lint:ignore SA2001 memory barrier.
s.barrier.Unlock()
s.Unlock()
}

func (s *vfsShm) shmEnableBlocking(block bool) {
Expand Down
2 changes: 1 addition & 1 deletion vfs/shm_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !(((darwin || linux || freebsd || openbsd || netbsd || dragonfly || illumos) && (386 || arm || amd64 || arm64 || riscv64 || ppc64le) && !sqlite3_nosys) || sqlite3_flock || sqlite3_dotlk)
//go:build !(((linux || darwin || freebsd || openbsd || netbsd || dragonfly || illumos) && (386 || arm || amd64 || arm64 || riscv64 || ppc64le) && !sqlite3_nosys) || sqlite3_flock || sqlite3_dotlk)

package vfs

Expand Down

0 comments on commit 04af9fe

Please sign in to comment.