Skip to content

Commit e503be6

Browse files
committed
Refactors.
1 parent 11c03a1 commit e503be6

File tree

13 files changed

+51
-77
lines changed

13 files changed

+51
-77
lines changed

internal/util/mmap.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func CanMap(ctx context.Context) bool {
2929
return s.mmapState.enabled
3030
}
3131

32-
func (s *mmapState) new(ctx context.Context, mod api.Module, size uint32) *MappedRegion {
32+
func (s *mmapState) new(ctx context.Context, mod api.Module, size int32) *MappedRegion {
3333
// Find unused region.
3434
for _, r := range s.regions {
3535
if !r.used && r.size == size {
@@ -65,11 +65,11 @@ func (s *mmapState) new(ctx context.Context, mod api.Module, size uint32) *Mappe
6565
type MappedRegion struct {
6666
addr uintptr
6767
Ptr uint32
68-
size uint32
68+
size int32
6969
used bool
7070
}
7171

72-
func MapRegion(ctx context.Context, mod api.Module, f *os.File, offset int64, size uint32) (*MappedRegion, error) {
72+
func MapRegion(ctx context.Context, mod api.Module, f *os.File, offset int64, size int32) (*MappedRegion, error) {
7373
s := ctx.Value(moduleKey{}).(*moduleState)
7474
r := s.new(ctx, mod, size)
7575
err := r.mmap(f, offset)
@@ -98,8 +98,8 @@ func (r *MappedRegion) mmap(f *os.File, offset int64) error {
9898
return err
9999
}
100100

101+
// We need the low level mmap for MAP_FIXED to work.
102+
// Bind the syscall version hoping that it is more stable.
103+
101104
//go:linkname mmap syscall.mmap
102105
func mmap(addr, length uintptr, prot, flag, fd int, pos int64) (*byte, error)
103-
104-
//go:linkname munmap syscall.munmap
105-
func munmap(addr, length uintptr) error

sqlite3/vfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include "include.h"
66
#include "sqlite3.h"
77

8-
int go_localtime(struct tm *, sqlite3_int64);
98
int go_vfs_find(const char *zVfsName);
9+
int go_localtime(struct tm *, sqlite3_int64);
1010

1111
int go_randomness(sqlite3_vfs *, int nByte, char *zOut);
1212
int go_sleep(sqlite3_vfs *, int microseconds);

util/ioutil/seek.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
// SeekingReaderAt implements [io.ReaderAt]
99
// through an underlying [io.ReadSeeker].
1010
type SeekingReaderAt struct {
11-
l sync.Mutex
1211
r io.ReadSeeker
12+
l sync.Mutex
1313
}
1414

1515
// NewSeekingReaderAt creates a new SeekingReaderAt.

vfs/file.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package vfs
22

33
import (
4+
"context"
45
"errors"
56
"io"
67
"io/fs"
@@ -11,6 +12,7 @@ import (
1112
"syscall"
1213

1314
"github.com/ncruces/go-sqlite3/util/osutil"
15+
"github.com/tetratelabs/wazero/api"
1416
)
1517

1618
type vfsOS struct{}
@@ -125,12 +127,12 @@ func (vfsOS) OpenParams(name string, flags OpenFlag, params url.Values) (File, O
125127

126128
type vfsFile struct {
127129
*os.File
130+
shm vfsShm
128131
lock LockLevel
129132
readOnly bool
130133
keepWAL bool
131134
syncDir bool
132135
psow bool
133-
shm vfsShm
134136
}
135137

136138
var (
@@ -213,3 +215,9 @@ func (f *vfsFile) PowersafeOverwrite() bool { return f.psow }
213215
func (f *vfsFile) PersistentWAL() bool { return f.keepWAL }
214216
func (f *vfsFile) SetPowersafeOverwrite(psow bool) { f.psow = psow }
215217
func (f *vfsFile) SetPersistentWAL(keepWAL bool) { f.keepWAL = keepWAL }
218+
219+
type fileShm interface {
220+
shmMap(context.Context, api.Module, int32, int32, bool) (uint32, error)
221+
shmLock(int32, int32, _ShmFlag) error
222+
shmUnmap(bool)
223+
}

vfs/os_bsd.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,3 @@ func osReadLock(file *os.File, _ /*start*/, _ /*len*/ int64, _ /*timeout*/ time.
3131
func osWriteLock(file *os.File, _ /*start*/, _ /*len*/ int64, _ /*timeout*/ time.Duration) _ErrorCode {
3232
return osLock(file, unix.LOCK_EX|unix.LOCK_NB, _IOERR_LOCK)
3333
}
34-
35-
func osGetLock(file *os.File, start, len int64) (int16, _ErrorCode) {
36-
lock := unix.Flock_t{
37-
Type: unix.F_WRLCK,
38-
Start: start,
39-
Len: len,
40-
}
41-
if unix.FcntlFlock(file.Fd(), unix.F_GETLK, &lock) != nil {
42-
return 0, _IOERR_CHECKRESERVEDLOCK
43-
}
44-
return lock.Type, _OK
45-
}

vfs/os_darwin.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const (
1414
// https://github.com/apple/darwin-xnu/blob/main/bsd/sys/fcntl.h
1515
_F_OFD_SETLK = 90
1616
_F_OFD_SETLKW = 91
17-
_F_OFD_GETLK = 92
1817
_F_OFD_SETLKWTIMEOUT = 93
1918
)
2019

@@ -94,15 +93,3 @@ func osReadLock(file *os.File, start, len int64, timeout time.Duration) _ErrorCo
9493
func osWriteLock(file *os.File, start, len int64, timeout time.Duration) _ErrorCode {
9594
return osLock(file, unix.F_WRLCK, start, len, timeout, _IOERR_LOCK)
9695
}
97-
98-
func osGetLock(file *os.File, start, len int64) (int16, _ErrorCode) {
99-
lock := unix.Flock_t{
100-
Type: unix.F_WRLCK,
101-
Start: start,
102-
Len: len,
103-
}
104-
if unix.FcntlFlock(file.Fd(), _F_OFD_GETLK, &lock) != nil {
105-
return 0, _IOERR_CHECKRESERVEDLOCK
106-
}
107-
return lock.Type, _OK
108-
}

vfs/os_ofd.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func osLock(file *os.File, typ int16, start, len int64, timeout time.Duration, d
4040
if errno, _ := err.(unix.Errno); errno != unix.EAGAIN {
4141
break
4242
}
43-
if timeout <= 0 || timeout < time.Since(before) {
43+
if timeout < time.Since(before) {
4444
break
4545
}
4646
osSleep(time.Millisecond)
@@ -56,15 +56,3 @@ func osReadLock(file *os.File, start, len int64, timeout time.Duration) _ErrorCo
5656
func osWriteLock(file *os.File, start, len int64, timeout time.Duration) _ErrorCode {
5757
return osLock(file, unix.F_WRLCK, start, len, timeout, _IOERR_LOCK)
5858
}
59-
60-
func osGetLock(file *os.File, start, len int64) (int16, _ErrorCode) {
61-
lock := unix.Flock_t{
62-
Type: unix.F_WRLCK,
63-
Start: start,
64-
Len: len,
65-
}
66-
if unix.FcntlFlock(file.Fd(), unix.F_OFD_GETLK, &lock) != nil {
67-
return 0, _IOERR_CHECKRESERVEDLOCK
68-
}
69-
return lock.Type, _OK
70-
}

vfs/os_unix_lock.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ func osCheckReservedLock(file *os.File) (bool, _ErrorCode) {
6868
return lock == unix.F_WRLCK, rc
6969
}
7070

71+
func osGetLock(file *os.File, start, len int64) (int16, _ErrorCode) {
72+
lock := unix.Flock_t{
73+
Type: unix.F_WRLCK,
74+
Start: start,
75+
Len: len,
76+
}
77+
if unix.FcntlFlock(file.Fd(), unix.F_GETLK, &lock) != nil {
78+
return 0, _IOERR_CHECKRESERVEDLOCK
79+
}
80+
return lock.Type, _OK
81+
}
82+
7183
func osLockErrorCode(err error, def _ErrorCode) _ErrorCode {
7284
if err == nil {
7385
return _OK

vfs/os_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func osLock(file *os.File, flags, start, len uint32, timeout time.Duration, def
132132
if errno, _ := err.(windows.Errno); errno != windows.ERROR_LOCK_VIOLATION {
133133
break
134134
}
135-
if timeout <= 0 || timeout < time.Since(before) {
135+
if timeout < time.Since(before) {
136136
break
137137
}
138138
osSleep(time.Millisecond)

vfs/shm.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const (
3333
_SHM_DMS = _SHM_BASE + _SHM_NLOCK
3434
)
3535

36-
func (f *vfsFile) shmMap(ctx context.Context, mod api.Module, id, size uint32, extend bool) (uint32, error) {
36+
func (f *vfsFile) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (uint32, error) {
3737
// Ensure size is a multiple of the OS page size.
3838
if int(size)&(unix.Getpagesize()-1) != 0 {
3939
return 0, _IOERR_SHMMAP
@@ -96,9 +96,9 @@ func (f *vfsFile) shmMap(ctx context.Context, mod api.Module, id, size uint32, e
9696
return r.Ptr, nil
9797
}
9898

99-
func (f *vfsFile) shmLock(offset, n uint32, flags _ShmFlag) error {
99+
func (f *vfsFile) shmLock(offset, n int32, flags _ShmFlag) error {
100100
// Argument check.
101-
if n == 0 || offset+n > _SHM_NLOCK {
101+
if n <= 0 || offset < 0 || offset+n > _SHM_NLOCK {
102102
panic(util.AssertErr())
103103
}
104104
switch flags {

0 commit comments

Comments
 (0)