]> gitweb.factorcode.org Git - factor.git/blob - basis/io/windows/mmap/mmap.factor
Fix permission bits
[factor.git] / basis / io / windows / mmap / mmap.factor
1 USING: alien alien.c-types arrays destructors generic io.mmap
2 io.ports io.windows io.windows.files io.windows.privileges
3 kernel libc math math.bitwise namespaces quotations sequences
4 windows windows.advapi32 windows.kernel32 io.backend system
5 accessors locals ;
6 IN: io.windows.mmap
7
8 : create-file-mapping ( hFile lpAttributes flProtect dwMaximumSizeHigh dwMaximumSizeLow lpName -- HANDLE )
9     CreateFileMapping [ win32-error=0/f ] keep <win32-handle> ;
10
11 : map-view-of-file ( hFileMappingObject dwDesiredAccess dwFileOffsetHigh dwFileOffsetLow dwNumberOfBytesToMap -- HANDLE )
12     MapViewOfFile [ win32-error=0/f ] keep ;
13
14 :: mmap-open ( path length access-mode create-mode protect access -- handle handle address )
15     [let | lo [ length HEX: ffffffff bitand ]
16            hi [ length -32 shift HEX: ffffffff bitand ] |
17         { "SeCreateGlobalPrivilege" "SeLockMemoryPrivilege" } [
18             path access-mode create-mode 0 open-file |dispose
19             dup handle>> f protect hi lo f create-file-mapping |dispose
20             dup handle>> access 0 0 0 map-view-of-file
21         ] with-privileges
22     ] ;
23
24 TUPLE: win32-mapped-file file mapping ;
25
26 M: win32-mapped-file dispose
27     [ file>> dispose ] [ mapping>> dispose ] bi ;
28
29 C: <win32-mapped-file> win32-mapped-file
30
31 M: windows (mapped-file)
32     [
33         { GENERIC_WRITE GENERIC_READ } flags
34         OPEN_ALWAYS
35         { PAGE_READWRITE SEC_COMMIT } flags
36         FILE_MAP_ALL_ACCESS mmap-open
37         -rot <win32-mapped-file>
38     ] with-destructors ;
39
40 M: windows close-mapped-file ( mapped-file -- )
41     [
42         [ handle>> &dispose drop ]
43         [ address>> UnmapViewOfFile win32-error=0/f ] bi
44     ] with-destructors ;