source: trunk/src/kernel32/mmap.h@ 10010

Last change on this file since 10010 was 9971, checked in by sandervl, 22 years ago

PF: Corrected HFILE definition as it is in Wine and in Win2k

File size: 7.6 KB
Line 
1/* $Id: mmap.h,v 1.28 2003-04-02 11:03:32 sandervl Exp $ */
2
3/*
4 * Memory mapped class
5 *
6 * Copyright 1999-2003 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 */
12#ifndef __MMAP_H__
13#define __MMAP_H__
14
15#include <vmutex.h>
16#include "heapshared.h"
17#include "asmutil.h"
18
19
20#ifndef PAGE_SIZE
21#define PAGE_SIZE 4096
22#endif
23#ifndef PAGE_SHIFT
24#define PAGE_SHIFT 12
25#endif
26
27#define MEMMAP_CRITSECTION_NAME "\\SEM32\\ODIN_MMAP.SEM"
28
29//commit 16 pages at once when the app accesses it
30#define NRPAGES_TOCOMMIT 16
31
32#define MEMMAP_ACCESS_INVALID 0
33#define MEMMAP_ACCESS_READ 1
34#define MEMMAP_ACCESS_WRITE 2
35#define MEMMAP_ACCESS_EXECUTE 4
36#define MEMMAP_ACCESS_COPYONWRITE 8
37
38#define MMAP_FLUSHVIEW_ALL 0xFFFFFFFF
39
40typedef enum
41{
42 PAGEVIEW_READONLY,
43 PAGEVIEW_VIEW,
44 PAGEVIEW_GUARD
45} PAGEVIEW;
46
47class Win32MemMapView;
48class Win32PeLdrImage;
49
50//******************************************************************************
51//Memory mapping class
52//******************************************************************************
53class Win32MemMap
54{
55public:
56 Win32MemMap(HANDLE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName);
57 //Use by PE loader image class only:
58 Win32MemMap(Win32PeLdrImage *pImage, ULONG lpImageMem, ULONG size);
59virtual ~Win32MemMap();
60
61virtual BOOL Init(DWORD aMSize=0);
62virtual BOOL flushView(ULONG viewaddr, ULONG offset, ULONG cbFlush);
63virtual LPVOID mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess);
64virtual BOOL unmapViewOfFile(LPVOID addr);
65
66 BOOL updateViewPages(ULONG offset, ULONG size, PAGEVIEW flags);
67 BOOL allocateMap();
68
69 HANDLE getFileHandle() { return hMemFile; };
70 LPSTR getMemName() { return lpszMapName; };
71 DWORD getMapSize() { return mSize; };
72 DWORD getProtFlags() { return mProtFlags; };
73 void setProtFlags(DWORD dwNewFlags) { mProtFlags = dwNewFlags; };
74 LPVOID getMappingAddr() { return pMapping; };
75 DWORD getProcessId() { return mProcessId;};
76Win32PeLdrImage *getImage() { return image; };
77
78 BOOL isImageMap() { return image != NULL; };
79
80 void AddRef() { ++referenced; };
81 int Release();
82
83 void AddView() { ++nrMappings; };
84 void RemoveView() { --nrMappings; };
85
86
87 void markDirtyPages(int startpage, int nrpages);
88 void clearDirtyPages(int startpage, int nrpages);
89 BOOL isDirtyPage(int pagenr) { return test_bit(pagenr, pWriteBitmap) != 0; };
90
91virtual BOOL invalidatePages(ULONG offset, ULONG size);
92virtual BOOL commitPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages = NRPAGES_TOCOMMIT);
93virtual BOOL commitGuardPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess);
94 BOOL commitRange(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages);
95
96static Win32MemMap *findMap(LPSTR lpszName);
97static Win32MemMap *findMapByFile(HANDLE hFile);
98static Win32MemMap *findMap(ULONG address);
99
100//Should only be called in ExitProcess
101static void deleteAll();
102
103#ifdef __DEBUG_ALLOC__
104 void *operator new(size_t size, const char *filename, size_t lineno)
105 {
106 return _smalloc(size);
107 }
108 void operator delete(void *location, const char *filename, size_t lineno)
109 {
110 free(location);
111 }
112#else
113 void *operator new(size_t size)
114 {
115 return _smalloc(size);
116 }
117 void operator delete(void *location)
118 {
119 free(location);
120 }
121#endif
122
123protected:
124 HANDLE hMemFile;
125 HANDLE hOrgMemFile;
126 ULONG mSize;
127 ULONG mProtFlags;
128 ULONG mProcessId;
129 ULONG mMapAccess;
130 LPSTR lpszMapName;
131 void *pMapping;
132
133 char *pWriteBitmap;
134
135 ULONG nrMappings;
136
137 ULONG referenced;
138
139 VMutex mapMutex;
140
141 Win32PeLdrImage *image;
142
143 Win32MemMapView *views;
144
145private:
146 static Win32MemMap *memmaps;
147 Win32MemMap *next;
148};
149//******************************************************************************
150//Duplicate memory mapping class (duplicate map with different protection flags
151//associated with an existing memory map)
152//******************************************************************************
153class Win32MemMapDup : public Win32MemMap
154{
155public:
156 Win32MemMapDup(Win32MemMap *parent, HANDLE hFile, ULONG size, ULONG fdwProtect, LPSTR lpszName);
157 virtual ~Win32MemMapDup();
158
159virtual BOOL Init(DWORD aMSize=0);
160virtual BOOL flushView(ULONG viewaddr, ULONG offset, ULONG cbFlush);
161virtual LPVOID mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess);
162virtual BOOL unmapViewOfFile(LPVOID addr);
163
164virtual BOOL invalidatePages(ULONG offset, ULONG size);
165virtual BOOL commitGuardPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess);
166virtual BOOL commitPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages = NRPAGES_TOCOMMIT);
167
168protected:
169 Win32MemMap *parent;
170
171private:
172};
173//******************************************************************************
174//Memory mapped file View Class
175//******************************************************************************
176class Win32MemMapView
177{
178public:
179 Win32MemMapView(Win32MemMap *parent, ULONG offset, ULONG size, ULONG fdwAccess, Win32MemMap *owner = NULL);
180 ~Win32MemMapView();
181
182 BOOL changePageFlags(ULONG offset, ULONG size, PAGEVIEW flags);
183
184 DWORD getAccessFlags() { return mfAccess; };
185 DWORD getSize() { return mSize; };
186 ULONG getOffset() { return mOffset; };
187 LPVOID getViewAddr() { return (LPVOID)((char *)pMapView + getOffset()); };
188
189 BOOL everythingOk() { return errorState == 0; };
190
191Win32MemMap *getParentMap() { return mParentMap;};
192Win32MemMap *getOwnerMap() { return mOwnerMap; };
193
194 DWORD getProcessId() { return mProcessId;};
195
196 void markCOWPages(int startpage, int nrpages);
197 BOOL isCOWPage(int pagenr) { return (pCOWBitmap) ? (test_bit(pagenr, pCOWBitmap) != 0) : FALSE; };
198
199static void deleteViews(Win32MemMap *map);
200static Win32MemMap *findMapByView(ULONG address, ULONG *offset = NULL,
201 ULONG accessType = MEMMAP_ACCESS_READ);
202static int findViews(Win32MemMap *map, int nrViews, Win32MemMapView *viewarray[]);
203static Win32MemMapView *findView(ULONG address);
204
205#ifdef __DEBUG_ALLOC__
206 void *operator new(size_t size, const char *filename, size_t lineno)
207 {
208 return _smalloc(size);
209 }
210 void operator delete(void *location, const char *filename, size_t lineno)
211 {
212 free(location);
213 }
214#else
215 void *operator new(size_t size)
216 {
217 return _smalloc(size);
218 }
219 void operator delete(void *location)
220 {
221 free(location);
222 }
223#endif
224
225protected:
226 ULONG mSize, errorState;
227 ULONG mProcessId;
228 ULONG mfAccess, mOffset;
229 void *pMapView, *pShareViewAddr;
230
231 char *pCOWBitmap;
232
233 //parent map object; memory map that contains the original memory map
234 Win32MemMap *mParentMap;
235 //owner map object (can be NULL); duplicate memory map that created this view
236 Win32MemMap *mOwnerMap;
237
238private:
239 static Win32MemMapView *mapviews;
240 Win32MemMapView *next;
241
242 friend class Win32MemMap;
243};
244//******************************************************************************
245//******************************************************************************
246
247#pragma data_seg(_GLOBALDATA)
248extern CRITICAL_SECTION_OS2 globalmapcritsect;
249#pragma data_seg()
250
251void InitializeMemMaps();
252
253#endif //__MMAP_H__
Note: See TracBrowser for help on using the repository browser.