]> git.donarmstrong.com Git - lilypond.git/blob - lib/windhoos-suck-suck-suck-thank-you-cygnus.cc
release: 0.0.39-1
[lilypond.git] / lib / windhoos-suck-suck-suck-thank-you-cygnus.cc
1 //
2 // windhoos.cc
3 //
4 #ifdef _WIN32
5
6 #include <sys/types.h>
7 #include <sys/mman.h>
8 #include <winbase.h>
9 #include "windhoos-suck-suck-suck-thank-you-cygnus.hh"
10
11 /* 
12 HANDLE CreateFileMapping(
13     HANDLE hFile,       // handle to file to map 
14     LPSECURITY_ATTRIBUTES lpFileMappingAttributes,      // optional security attributes 
15     DWORD flProtect,    // protection for mapping object 
16     DWORD dwMaximumSizeHigh,    // high-order 32 bits of object size  
17     DWORD dwMaximumSizeLow,     // low-order 32 bits of object size  
18     LPCTSTR lpName      // name of file-mapping object 
19    );   
20  
21
22 LPVOID MapViewOfFile(
23     HANDLE hFileMappingObject,  // file-mapping object to map into address space  
24     DWORD dwDesiredAccess,      // access mode 
25     DWORD dwFileOffsetHigh,     // high-order 32 bits of file offset 
26     DWORD dwFileOffsetLow,      // low-order 32 bits of file offset 
27     DWORD dwNumberOfBytesToMap  // number of bytes to map 
28    );   
29  
30
31 io.h:
32 long _get_osfhandle( int filehandle );
33 */
34
35 // cygnus's gnu-win32-b17.1 does not have _get_osfhandle
36 // however, after some hacking, it turns out that:
37
38 static const int OSF_OFFSET_i = 72;  
39 static const int OSF_BASE_i = -3;
40 static const int OSF_FACTOR_i = 8;  
41 // let-s hope bill doesn-t change his mind any time soon :-)
42
43 // so that, while waiting for cygnus's mmap, we can write:
44
45 // #define HAVE_GET_OSFHANDLE  // no we still cannot; works only with cl.exe
46 long
47 _get_osfhandle( int filedes_i )
48 {
49     return (long)( OSF_OFFSET_i + ( filedes_i + OSF_BASE_i ) * OSF_FACTOR_i );
50 }
51
52 #ifdef HAVE_GET_OSFHANDLE
53
54 #include <iostream.h>
55
56 caddr_t
57 mmap(caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
58 {
59     (void)flags;
60     (void)prot;
61     (void)addr;
62     HANDLE osf = (HANDLE)_get_osfhandle( fd );
63     HANDLE file_handle = CreateFileMapping( osf, (void*)0, PAGE_READONLY,
64         0, len, 0 ); 
65     return (caddr_t)MapViewOfFile( file_handle, FILE_MAP_READ, 0, offset, len );
66 }
67
68
69 int
70 munmap(caddr_t addr, size_t len)
71 {
72     (void)len;
73     return UnmapViewOfFile( addr );
74 }
75
76 #else // ! HAVE_GET_OSFHANDLE //
77
78 caddr_t
79 mmap(caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
80 {
81     (void)flags;
82     (void)prot;
83     (void)addr;
84     (void)offset;
85     char* ch_p = new char[ len ];
86     if ( ch_p )
87         read( fd, (void*)ch_p, len );
88     return ch_p;
89 }
90
91
92 int
93 munmap(caddr_t addr, size_t len)
94 {
95     (void)len;
96     delete (char*)addr;
97     return 0;
98 }
99
100 #endif // !HAVE_GET_OSFHANDLE //
101
102
103 #endif // _WIN32 //