syscall.Getpagesize(): add test, implement for Linux and Windows

Этот коммит содержится в:
Dan Kegel 2022-06-10 17:20:54 -07:00 коммит произвёл Ayke
родитель caf405b01d
коммит 8754f64f3b
10 изменённых файлов: 77 добавлений и 8 удалений

Просмотреть файл

@ -115,6 +115,7 @@ var Musl = Library{
"internal/libc.c",
"internal/syscall_ret.c",
"internal/vdso.c",
"legacy/*.c",
"malloc/*.c",
"mman/*.c",
"signal/*.c",

17
src/os/getpagesize_test.go Обычный файл
Просмотреть файл

@ -0,0 +1,17 @@
//go:build windows || darwin || (linux && !baremetal)
// +build windows darwin linux,!baremetal
package os_test
import (
"os"
"testing"
)
func TestGetpagesize(t *testing.T) {
pagesize := os.Getpagesize()
if pagesize == 0x1000 || pagesize == 0x4000 || pagesize == 0x10000 {
return
}
t.Errorf("os.Getpagesize() returns strange value %d", pagesize)
}

Просмотреть файл

@ -7,6 +7,11 @@
package os
import "syscall"
// Getpagesize returns the underlying system's memory page size.
func Getpagesize() int { return syscall.Getpagesize() }
func (fs *fileStat) Name() string { return fs.name }
func (fs *fileStat) IsDir() bool { return fs.Mode().IsDir() }

Просмотреть файл

@ -40,3 +40,11 @@ func markGlobals() {
start = (start + unsafe.Alignof(uintptr(0)) - 1) &^ (unsafe.Alignof(uintptr(0)) - 1) // align on word boundary
markRoots(start, end)
}
//export getpagesize
func libc_getpagesize() int
//go:linkname syscall_Getpagesize syscall.Getpagesize
func syscall_Getpagesize() int {
return libc_getpagesize()
}

Просмотреть файл

@ -90,3 +90,26 @@ func markGlobals() {
section = (*peSection)(unsafe.Pointer(uintptr(unsafe.Pointer(section)) + unsafe.Sizeof(peSection{})))
}
}
type systeminfo struct {
anon0 [4]byte
dwpagesize uint32
lpminimumapplicationaddress *byte
lpmaximumapplicationaddress *byte
dwactiveprocessormask uintptr
dwnumberofprocessors uint32
dwprocessortype uint32
dwallocationgranularity uint32
wprocessorlevel uint16
wprocessorrevision uint16
}
//export GetSystemInfo
func _GetSystemInfo(lpSystemInfo unsafe.Pointer)
//go:linkname syscall_Getpagesize syscall.Getpagesize
func syscall_Getpagesize() int {
var info systeminfo
_GetSystemInfo(unsafe.Pointer(&info))
return int(info.dwpagesize)
}

Просмотреть файл

@ -251,10 +251,6 @@ func Mprotect(b []byte, prot int) (err error) {
return
}
func Getpagesize() int {
return int(libc_getpagesize())
}
func Environ() []string {
// This function combines all the environment into a single allocation.
@ -372,10 +368,6 @@ func libc_munmap(addr unsafe.Pointer, length uintptr) int32
//export mprotect
func libc_mprotect(addr unsafe.Pointer, len uintptr, prot int32) int32
// int getpagesize();
//export getpagesize
func libc_getpagesize() int32
// int chdir(const char *pathname, mode_t mode);
//export chdir
func libc_chdir(pathname *byte) int32

Просмотреть файл

@ -267,6 +267,14 @@ func readdir_r(dir uintptr, entry *Dirent, result **Dirent) (err error) {
return
}
func Getpagesize() int {
return int(libc_getpagesize())
}
// int pipe(int32 *fds);
//export pipe
func libc_pipe(fds *int32) int32
// int getpagesize();
//export getpagesize
func libc_getpagesize() int32

Просмотреть файл

@ -67,3 +67,7 @@ func getErrno() error {
func Pipe2(p []int, flags int) (err error) {
return ENOSYS // TODO
}
func Getpagesize() int {
return 4096 // TODO
}

Просмотреть файл

@ -296,6 +296,11 @@ func Pipe2(p []int, flags int) (err error) {
return ENOSYS // TODO
}
func Getpagesize() int {
// per upstream
return 65536
}
// int stat(const char *path, struct stat * buf);
//export stat
func libc_stat(pathname *byte, ptr unsafe.Pointer) int32

Просмотреть файл

@ -205,3 +205,9 @@ type Timeval struct {
Sec int64
Usec int64
}
func Getpagesize() int {
// There is no right value to return here, but 4096 is a
// common assumption when pagesize is unknown
return 4096
}