summary refs log tree commit diff stats
path: root/tests/mmaptest.nim
diff options
context:
space:
mode:
authorJacek Sieka <arnetheduck@gmail.com>2019-07-08 09:14:35 +0200
committerMiran <narimiran@disroot.org>2019-07-08 09:14:35 +0200
commit9e7d885f3509a25ee804f2837e24b49a378a1fe1 (patch)
treebd57b9f05acab60dbd21643488d7c1ba13e457c0 /tests/mmaptest.nim
parentd0616ed115840053f705a8bf45fa5cf566d3e2b1 (diff)
downloadNim-9e7d885f3509a25ee804f2837e24b49a378a1fe1.tar.gz
dynlib: use posix module (#11623)
Diffstat (limited to 'tests/mmaptest.nim')
-rw-r--r--tests/mmaptest.nim33
1 files changed, 9 insertions, 24 deletions
diff --git a/tests/mmaptest.nim b/tests/mmaptest.nim
index 84036cbf0..7abdab45f 100644
--- a/tests/mmaptest.nim
+++ b/tests/mmaptest.nim
@@ -1,25 +1,7 @@
 # Small test program to test for mmap() weirdnesses
 
-include "lib/system/ansi_c"
-
-const
-  PageSize = 4096
-  PROT_READ  = 1             # page can be read
-  PROT_WRITE = 2             # page can be written
-  MAP_PRIVATE = 2            # Changes are private
-
-when defined(macosx) or defined(bsd):
-  const MAP_ANONYMOUS = 0x1000
-elif defined(solaris):
-  const MAP_ANONYMOUS = 0x100
-else:
-  var
-    MAP_ANONYMOUS {.importc: "MAP_ANONYMOUS", header: "<sys/mman.h>".}: cint
-
-proc mmap(adr: pointer, len: int, prot, flags, fildes: cint,
-          off: int): pointer {.header: "<sys/mman.h>".}
-
-proc munmap(adr: pointer, len: int) {.header: "<sys/mman.h>".}
+import system/ansi_c
+import posix
 
 proc osAllocPages(size: int): pointer {.inline.} =
   result = mmap(nil, size, PROT_READ or PROT_WRITE,
@@ -31,18 +13,21 @@ proc osAllocPages(size: int): pointer {.inline.} =
 
 proc osDeallocPages(p: pointer, size: int) {.inline} =
   cfprintf(c_stdout, "freed pages %p..%p\n", p, cast[int](p) + size)
-  munmap(p, size-1)
+  discard munmap(p, size-1)
 
 proc `+!!`(p: pointer, size: int): pointer {.inline.} =
   result = cast[pointer](cast[int](p) + size)
 
+const
+  PageShift = when defined(cpu16): 8 else: 12 # \
+    # my tests showed no improvments for using larger page sizes.
+  PageSize = 1 shl PageShift
+
 var p = osAllocPages(3 * PageSize)
 
 osDeallocPages(p, PageSize)
 # If this fails the OS has freed the whole block starting at 'p':
-echo(cast[ptr int](p +!! (pageSize*2))[])
+echo(cast[ptr int](p +!! (PageSize*2))[])
 
 osDeallocPages(p +!! PageSize*2, PageSize)
 osDeallocPages(p +!! PageSize, PageSize)
-
-