aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/syscall
diff options
context:
space:
mode:
authorDan Kegel <[email protected]>2022-03-17 09:53:28 -0700
committerRon Evans <[email protected]>2022-03-18 13:44:30 +0100
commitcf8f4d65f2e8b3d9cb52b850106132fae2e70387 (patch)
tree2224c3035baf062ebd84348a27ea069c180b05f4 /src/syscall
parent06b19cde2d39c85eb208e26fb223ecea138cbd67 (diff)
downloadtinygo-cf8f4d65f2e8b3d9cb52b850106132fae2e70387.tar.gz
tinygo-cf8f4d65f2e8b3d9cb52b850106132fae2e70387.zip
Implement getpagesize and munmap. For go 1.18.
Diffstat (limited to 'src/syscall')
-rw-r--r--src/syscall/mmap_unix_test.go23
-rw-r--r--src/syscall/syscall_libc.go20
2 files changed, 43 insertions, 0 deletions
diff --git a/src/syscall/mmap_unix_test.go b/src/syscall/mmap_unix_test.go
new file mode 100644
index 000000000..d13036044
--- /dev/null
+++ b/src/syscall/mmap_unix_test.go
@@ -0,0 +1,23 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build darwin || linux
+// +build darwin linux
+
+package syscall_test
+
+import (
+ "syscall"
+ "testing"
+)
+
+func TestMmap(t *testing.T) {
+ b, err := syscall.Mmap(-1, 0, syscall.Getpagesize(), syscall.PROT_NONE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
+ if err != nil {
+ t.Fatalf("Mmap: %v", err)
+ }
+ if err := syscall.Munmap(b); err != nil {
+ t.Fatalf("Munmap: %v", err)
+ }
+}
diff --git a/src/syscall/syscall_libc.go b/src/syscall/syscall_libc.go
index fd94d8934..c94c29cd1 100644
--- a/src/syscall/syscall_libc.go
+++ b/src/syscall/syscall_libc.go
@@ -226,6 +226,14 @@ func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, e
return (*[1 << 30]byte)(addr)[:length:length], nil
}
+func Munmap(b []byte) (err error) {
+ errCode := libc_munmap(unsafe.Pointer(&b[0]), uintptr(len(b)))
+ if errCode != 0 {
+ err = getErrno()
+ }
+ return err
+}
+
func Mprotect(b []byte, prot int) (err error) {
errCode := libc_mprotect(unsafe.Pointer(&b[0]), uintptr(len(b)), int32(prot))
if errCode != 0 {
@@ -234,6 +242,10 @@ 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.
@@ -343,10 +355,18 @@ func libc_dup(fd int32) int32
//export mmap
func libc_mmap(addr unsafe.Pointer, length uintptr, prot, flags, fd int32, offset uintptr) unsafe.Pointer
+// int munmap(void *addr, size_t length);
+//export munmap
+func libc_munmap(addr unsafe.Pointer, length uintptr) int32
+
// int mprotect(void *addr, size_t len, int prot);
//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