-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐳 mmap: migrate to unix.MmapPtr & unix.MunmapPtr
While here, improve the API and add a fallback path for files on unsupported filesystems.
- Loading branch information
1 parent
7143719
commit d88ced6
Showing
15 changed files
with
147 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package mmap | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"os" | ||
"unsafe" | ||
) | ||
|
||
// ReadFile maps the named file into memory for reading. | ||
// On success, it returns the mapped data as a byte slice or a string, | ||
// and a function that unmaps the data. | ||
func ReadFile[T ~[]byte | ~string](name string) (data T, close func() error, err error) { | ||
f, err := os.Open(name) | ||
if err != nil { | ||
return | ||
} | ||
defer f.Close() | ||
|
||
fs, err := f.Stat() | ||
if err != nil { | ||
return | ||
} | ||
|
||
size := fs.Size() | ||
if size == 0 { | ||
return | ||
} | ||
|
||
addr, close, err := readFile(f, uintptr(size)) | ||
if err != nil { | ||
if errors.Is(err, errors.ErrUnsupported) { | ||
return readFileFallback[T](f, size) | ||
} | ||
return | ||
} | ||
|
||
b := unsafe.Slice((*byte)(addr), size) | ||
return *(*T)(unsafe.Pointer(&b)), close, nil | ||
} | ||
|
||
func readFileFallback[T ~[]byte | ~string](f *os.File, size int64) (data T, close func() error, err error) { | ||
b := make([]byte, size) | ||
if _, err = io.ReadFull(f, b); err != nil { | ||
return | ||
} | ||
return *(*T)(unsafe.Pointer(&b)), func() error { return nil }, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build !unix && !windows | ||
|
||
package mmap | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"unsafe" | ||
) | ||
|
||
type mmapUnsupportError struct{} | ||
|
||
func (mmapUnsupportError) Error() string { | ||
return "mmap is not supported on this platform" | ||
} | ||
|
||
func (mmapUnsupportError) Is(target error) bool { | ||
return target == errors.ErrUnsupported | ||
} | ||
|
||
func readFile(_ *os.File, _ uintptr) (addr unsafe.Pointer, close func() error, err error) { | ||
return nil, nil, mmapUnsupportError{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build unix | ||
|
||
package mmap | ||
|
||
import ( | ||
"os" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func readFile(f *os.File, size uintptr) (addr unsafe.Pointer, close func() error, err error) { | ||
rawConn, err := f.SyscallConn() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if cerr := rawConn.Control(func(fd uintptr) { | ||
addr, err = unix.MmapPtr(int(fd), 0, nil, size, unix.PROT_READ, unix.MAP_SHARED) | ||
}); cerr != nil { | ||
return nil, nil, cerr | ||
} | ||
|
||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return addr, func() error { | ||
return unix.MunmapPtr(addr, size) | ||
}, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.