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

feat: fix mremap on darwin #96

Merged
merged 4 commits into from
Feb 8, 2024
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
7 changes: 5 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ on:

jobs:
go-test:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
Expand All @@ -20,7 +23,7 @@ jobs:
- name: Use Node.js 18
uses: actions/setup-node@v3
with:
node-version: '18'
node-version: "18"
- run: npm ci
- run: npm run build
- run: npm test
11 changes: 7 additions & 4 deletions pkg/mmap/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ type MemoryMappedFile struct {
file *os.File
bytes []byte
seek int64

// parameters used for remapping.
prot, flags int
}

var _ io.ReadWriteSeeker = &MemoryMappedFile{}
Expand All @@ -35,13 +38,13 @@ func NewMemoryMappedFile(f *os.File, prot int) (*MemoryMappedFile, error) {
return nil, fmt.Errorf("stat: %v", err)
}
if fi.Size() == 0 {
return &MemoryMappedFile{file: f, bytes: nil, seek: 0}, nil
return &MemoryMappedFile{file: f, bytes: nil, seek: 0, prot: prot, flags: unix.MAP_SHARED}, nil
}
b, err := unix.Mmap(int(fd), 0, int(fi.Size()), prot, unix.MAP_SHARED)
if err != nil {
return nil, fmt.Errorf("mmap: %v", err)
}
return &MemoryMappedFile{file: f, bytes: b, seek: 0}, nil
return &MemoryMappedFile{file: f, bytes: b, seek: 0, prot: prot, flags: unix.MAP_SHARED}, nil
}

// Open is a convenience function to open a file and memory map it.
Expand Down Expand Up @@ -141,13 +144,13 @@ func (m *MemoryMappedFile) WriteAt(b []byte, off int64) (int, error) {
return 0, err
}
if m.bytes == nil {
m.bytes, err = unix.Mmap(int(m.file.Fd()), 0, int(fi.Size()), unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED)
m.bytes, err = unix.Mmap(int(m.file.Fd()), 0, int(fi.Size()), m.prot, m.flags)
if err != nil {
return 0, fmt.Errorf("mmap: %v", err)
}
return len(b), nil
}
b, err := unix.Mremap(m.bytes, int(fi.Size()), unix.MREMAP_MAYMOVE)
b, err := mremap(m.bytes, int(m.file.Fd()), int(fi.Size()), m.prot, m.flags)
if err != nil {
return 0, fmt.Errorf("mmap: %v", err)
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/mmap/mremap_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mmap

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

func mremap(oldAddress []byte, fd, newSize, prot, flags int) ([]byte, error) {
// darwin doesn't have mremap, so we have to munmap and mmap the new size

// unmap the old address
if err := unix.Munmap(oldAddress); err != nil {
return nil, err
}
return unix.Mmap(fd, 0, newSize, prot, flags)
}
7 changes: 7 additions & 0 deletions pkg/mmap/mremap_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mmap

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

func mremap(oldAddress []byte, fd, newSize, prot, flags int) ([]byte, error) {
return unix.Mremap(oldAddress, newSize, unix.MREMAP_MAYMOVE)
}
Loading