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