1. Address space management

$Log: flx_mmap.pak,v $ Revision 1.3 2006/08/02 22:24:15 idadesub make sure we can also deal with a None result from get_stdout Revision 1.2 2006/07/17 17:22:40 skaller Add test case for mmap. Revision 1.1 2006/07/17 16:42:40 skaller Start with config and small binding for mmap.
Start data section to tmp/mmap_test.cxx[1 /1 ]
     1: // NOTE REQUIRES MAP_ANONYMOUS (linux only ..)
     2: #include <sys/mman.h>
     3: #include <stdio.h>
     4: #include <errno.h>
     5: #include <stdlib.h>
     6: 
     7: int main()
     8: {
     9:    size_t n = 10000;
    10:    void *data =
    11:        mmap
    12:        (
    13:           NULL,n,
    14:           PROT_WRITE | PROT_READ,
    15:           MAP_PRIVATE | MAP_ANONYMOUS,
    16:           0,0
    17:        )
    18:     ;
    19:    if (data == MAP_FAILED)
    20:    {
    21:       return 1;
    22:    }
    23:    int res = munmap(data,n);
    24:    if (res != 0)
    25:    {
    26:       return 1;
    27:    }
    28:    return 0;
    29: }
    30: 
End data section to tmp/mmap_test.cxx[1]
Start python section to cpkgs/target/mmap.py[1 /1 ]
     1: #line 44 "./lpsrc/flx_mmap.pak"
     2: execfile("config"+os.sep+"config.py")
     3: try:
     4:   cload(globals(),"mmap")
     5: except:
     6:   #HAVE_MMAP=TARGET_CXX.check_header_exists(xqt,'sys/mman.h')
     7:   try:
     8:     result, lines = TARGET_CXX.run_static_program(xqt, get_stdout, "tmp"+os.sep+"mmap_test")
     9:   except:
    10:     HAVE_MMAP = 0
    11:   else:
    12:     HAVE_MMAP = not result
    13:   f = cwrite("mmap")
    14:   pa(f,locals(),"HAVE_MMAP")
    15:   f.close()
    16:   cload(globals(),"mmap")
    17: 
End python section to cpkgs/target/mmap.py[1]
Start python section to spkgs/mmap.py[1 /1 ]
     1: #line 62 "./lpsrc/flx_mmap.pak"
     2: execfile('cpkgs'+os.sep+'target'+os.sep+'mmap.py')
     3: if HAVE_MMAP:
     4:   unit_tests = ['test'+os.sep+'flx_mmap_test.flx']
     5: 
     6: iscr_source = ['lpsrc/flx_mmap.pak']
     7: weaver_directory = 'doc/mmap/'
     8: 
End python section to spkgs/mmap.py[1]
Start data section to config/mmap.fpc[1 /1 ]
     1: Name: mmap
     2: 
End data section to config/mmap.fpc[1]
Start felix section to lib/mmap.flx[1 /1 ]
     1: #line 75 "./lpsrc/flx_mmap.pak"
     2: #import <flx.flxh>
     3: #import <flx_platform.flxh>
     4: 
     5: module Mmap
     6: {
     7:   header """
     8:     #include <sys/mman.h>
     9:     #include <unistd.h>
    10:   """;
    11: 
    12:   typedef off_t = unsigned long;
    13:   // protection options
    14:   const PROT_EXEC : int;
    15:   const PROT_READ: int;
    16:   const PROT_WRITE : int;
    17:   const PROT_NONE: int;
    18: 
    19:   const MAP_FIXED: int;
    20:   const MAP_SHARED: int;
    21:   const MAP_PRIVATE: int;
    22: 
    23: #if LINUX
    24:   const MAP_NORESERVE: int;
    25:   const MAP_LOCKED: int;
    26:   const MAP_GROWSDOWN: int;
    27:   const MAP_ANONYMOUS: int;
    28:   const MAP_32BIT: int;
    29:   const MAP_POPULATE: int;
    30:   const MAP_NONBLOCK: int;
    31: #endif
    32: 
    33:   const MAP_FAILED : address;
    34: 
    35:   // size of a page
    36:   const _SC_PAGESIZE : long = "sysconf(_SC_PAGESIZE)";
    37: 
    38:   fun mmap:
    39:     address * //< start address
    40:     size *    //< bytes to map
    41:     int *     //< protection
    42:     int *     //< flags
    43:     int *     //< file descriptor
    44:     off_t     //< offset into file, multiple of _SC_PAGESIZE
    45:     -> address; //< start of reserved address space
    46: 
    47:   fun munmap: address * size -> int;
    48: }
    49: 
End felix section to lib/mmap.flx[1]
Start felix section to test/flx_mmap_test.flx[1 /1 ]
     1: #line 125 "./lpsrc/flx_mmap.pak"
     2: #import <flx.flxh>
     3: include "mmap";
     4: open Mmap;
     5: open MixedInt;
     6: open C_hack;
     7: 
     8: val n = cast[size](10000);
     9: data :=
    10:   mmap
    11:     (
    12:       NULL,n,
    13:       PROT_WRITE \| PROT_READ,
    14:       MAP_PRIVATE \| MAP_ANONYMOUS,
    15:       0,cast[off_t]0
    16:     )
    17: ;
    18: 
    19: if data == MAP_FAILED do
    20:   print$ "mmap failed!\n";
    21: else
    22:   res := munmap(data,n);
    23:   if res != 0 do
    24:     print$ "munmap failed!\n";
    25:   else
    26:     print$ "mmap worked!\n";
    27:   done;
    28: done;
    29: 
End felix section to test/flx_mmap_test.flx[1]
Start data section to test/flx_mmap_test.expect[1 /1 ]
     1: mmap worked!
End data section to test/flx_mmap_test.expect[1]