| 1 |
/* sys/mman.h |
| 2 |
|
| 3 |
Copyright 1996, 1997, 1998, 2000, 2001 Red Hat, Inc. |
| 4 |
|
| 5 |
This file is part of Cygwin. |
| 6 |
|
| 7 |
This software is a copyrighted work licensed under the terms of the |
| 8 |
Cygwin license. Please consult the file "CYGWIN_LICENSE" for |
| 9 |
details. */ |
| 10 |
|
| 11 |
#ifndef _SYS_MMAN_H_ |
| 12 |
#define _SYS_MMAN_H_ |
| 13 |
#include "windows.h" |
| 14 |
#include "io.h" |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#ifdef __cplusplus |
| 18 |
extern "C" { |
| 19 |
#endif /* __cplusplus */ |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
#include <sys/types.h> |
| 23 |
|
| 24 |
#define PROT_NONE 0 |
| 25 |
#define PROT_READ 1 |
| 26 |
#define PROT_WRITE 2 |
| 27 |
#define PROT_EXEC 4 |
| 28 |
|
| 29 |
#define MAP_FILE 0 |
| 30 |
#define MAP_SHARED 1 |
| 31 |
#define MAP_PRIVATE 2 |
| 32 |
#define MAP_TYPE 0xF |
| 33 |
#define MAP_FIXED 0x10 |
| 34 |
#define MAP_ANONYMOUS 0x20 |
| 35 |
#define MAP_ANON MAP_ANONYMOUS |
| 36 |
/* Non-standard flag */ |
| 37 |
#define MAP_NORESERVE 0x4000 /* Don't reserve swap space for this mapping. |
| 38 |
Page protection must be set explicitely |
| 39 |
to access page. Only supported for anonymous |
| 40 |
private mappings. */ |
| 41 |
#define MAP_AUTOGROW 0x8000 /* Grow underlying object to mapping size. |
| 42 |
File must be opened for writing. */ |
| 43 |
|
| 44 |
#define MAP_FAILED ((void *)0) |
| 45 |
|
| 46 |
/* |
| 47 |
* Flags for msync. |
| 48 |
*/ |
| 49 |
#define MS_ASYNC 1 |
| 50 |
#define MS_SYNC 2 |
| 51 |
#define MS_INVALIDATE 4 |
| 52 |
#include <unistd.h> |
| 53 |
#include <stdio.h> |
| 54 |
|
| 55 |
#define L 10 |
| 56 |
|
| 57 |
struct ah { void *addr; HANDLE h; }; |
| 58 |
static struct ah ahs[L]; |
| 59 |
void breakit() {} |
| 60 |
static void *mmap (void *__addr, size_t __len, int __prot, int __flags, int __fd, off64_t __off) |
| 61 |
{ |
| 62 |
HANDLE FILE=(HANDLE)_get_osfhandle(__fd); |
| 63 |
HANDLE fm=CreateFileMapping(FILE,NULL,(__prot==PROT_READ ? PAGE_READONLY : PAGE_READWRITE),0,0,NULL); |
| 64 |
if(fm==0) |
| 65 |
{ |
| 66 |
DWORD error=GetLastError(); |
| 67 |
return MAP_FAILED; |
| 68 |
} |
| 69 |
void *d=MapViewOfFile( fm,(__prot==PROT_READ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS),__off>>32,__off&0xFFFFFFFF,__len); |
| 70 |
|
| 71 |
for(int i=0;i<L;++i) |
| 72 |
{ |
| 73 |
if(ahs[i].addr==0) |
| 74 |
{ |
| 75 |
ahs[i].addr=d; |
| 76 |
ahs[i].h=fm; |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
if(d==0) |
| 81 |
{ |
| 82 |
breakit(); |
| 83 |
DWORD error=GetLastError(); |
| 84 |
d=(void*)malloc(__len); |
| 85 |
lseek(__fd,__off,SEEK_SET); |
| 86 |
read(__fd,d,__len); |
| 87 |
// fprintf(stderr,"MALLOC pos %d len %d at %x: error %x\n",(long)__off & 0xFFFFFFFF,__len,d,GetLastError()); |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
// fprintf(stderr,"MAP pos %d len %d at %x: error %x\n",(long)__off & 0xFFFFFFFF,__len,d,GetLastError()); |
| 92 |
} |
| 93 |
return d; |
| 94 |
} |
| 95 |
static int munmap (void *__addr, size_t __len) |
| 96 |
{ |
| 97 |
// fprintf(stderr,"UNMAP %x\n",__addr); |
| 98 |
if(UnmapViewOfFile(__addr)==0) free(__addr); |
| 99 |
for(int i=0;i<L;++i) |
| 100 |
{ |
| 101 |
if(ahs[i].addr==__addr) |
| 102 |
{ |
| 103 |
CloseHandle(ahs[i].h); |
| 104 |
DWORD dw=GetLastError(); |
| 105 |
ahs[i].addr=0; |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
extern int mprotect (void *__addr, size_t __len, int __prot); |
| 113 |
extern int msync (void *__addr, size_t __len, int __flags); |
| 114 |
extern int mlock (const void *__addr, size_t __len); |
| 115 |
extern int munlock (const void *__addr, size_t __len); |
| 116 |
|
| 117 |
static long sysconf(int name) |
| 118 |
{ |
| 119 |
SYSTEM_INFO info; |
| 120 |
GetSystemInfo(&info); |
| 121 |
return info.dwAllocationGranularity; |
| 122 |
} |
| 123 |
|
| 124 |
#define _SC_PAGESIZE 4096 |
| 125 |
|
| 126 |
#ifdef __cplusplus |
| 127 |
}; |
| 128 |
#endif /* __cplusplus */ |
| 129 |
|
| 130 |
#endif /* _SYS_MMAN_H_ */ |