1 | /* $Id: mmap.h,v 1.26 2003-03-06 10:44:34 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Memory mapped class
|
---|
5 | *
|
---|
6 | * Copyright 1999 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 |
|
---|
18 | #ifndef PAGE_SIZE
|
---|
19 | #define PAGE_SIZE 4096
|
---|
20 | #endif
|
---|
21 | #ifndef PAGE_SHIFT
|
---|
22 | #define PAGE_SHIFT 12
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | #define MEMMAP_CRITSECTION_NAME "\\SEM32\\ODIN_MMAP.SEM"
|
---|
26 |
|
---|
27 | //commit 4 pages at once when the app accesses it
|
---|
28 | #define NRPAGES_TOCOMMIT 16
|
---|
29 |
|
---|
30 | #define MEMMAP_ACCESS_INVALID 0
|
---|
31 | #define MEMMAP_ACCESS_READ 1
|
---|
32 | #define MEMMAP_ACCESS_WRITE 2
|
---|
33 | #define MEMMAP_ACCESS_EXECUTE 4
|
---|
34 | #define MEMMAP_ACCESS_COPYONWRITE 8
|
---|
35 |
|
---|
36 | #define MMAP_FLUSHVIEW_ALL 0xFFFFFFFF
|
---|
37 |
|
---|
38 | class Win32MemMapView;
|
---|
39 | class Win32PeLdrImage;
|
---|
40 |
|
---|
41 | //******************************************************************************
|
---|
42 | //******************************************************************************
|
---|
43 | class Win32MemMap
|
---|
44 | {
|
---|
45 | public:
|
---|
46 | Win32MemMap(HFILE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName);
|
---|
47 | //Use by PE loader image class only:
|
---|
48 | Win32MemMap(Win32PeLdrImage *pImage, ULONG lpImageMem, ULONG size);
|
---|
49 | ~Win32MemMap();
|
---|
50 |
|
---|
51 | virtual BOOL Init(DWORD aMSize=0);
|
---|
52 | virtual BOOL flushView(ULONG viewaddr, ULONG offset, ULONG cbFlush);
|
---|
53 | virtual LPVOID mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess);
|
---|
54 | virtual BOOL unmapViewOfFile(LPVOID addr);
|
---|
55 |
|
---|
56 | HFILE getFileHandle() { return hMemFile; };
|
---|
57 | LPSTR getMemName() { return lpszMapName; };
|
---|
58 | DWORD getProtFlags() { return mProtFlags; };
|
---|
59 | BOOL setProtFlags(DWORD dwNewProtect);
|
---|
60 | LPVOID getMappingAddr() { return pMapping; };
|
---|
61 | DWORD getProcessId() { return mProcessId;};
|
---|
62 | Win32PeLdrImage *getImage() { return image; };
|
---|
63 |
|
---|
64 | BOOL isImageMap() { return image != NULL; };
|
---|
65 |
|
---|
66 | void AddRef() { ++referenced; };
|
---|
67 | void Release();
|
---|
68 |
|
---|
69 | virtual BOOL invalidatePages(ULONG offset, ULONG size);
|
---|
70 | virtual BOOL commitPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages = NRPAGES_TOCOMMIT);
|
---|
71 | virtual BOOL commitGuardPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess);
|
---|
72 | BOOL commitRange(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages);
|
---|
73 |
|
---|
74 | static Win32MemMap *findMap(LPSTR lpszName);
|
---|
75 | static Win32MemMap *findMapByFile(HANDLE hFile);
|
---|
76 | static Win32MemMap *findMap(ULONG address);
|
---|
77 |
|
---|
78 | //Should only be called in ExitProcess
|
---|
79 | static void deleteAll();
|
---|
80 |
|
---|
81 | #ifdef __DEBUG_ALLOC__
|
---|
82 | void *operator new(size_t size, const char *filename, size_t lineno)
|
---|
83 | {
|
---|
84 | return _smalloc(size);
|
---|
85 | }
|
---|
86 | void operator delete(void *location, const char *filename, size_t lineno)
|
---|
87 | {
|
---|
88 | free(location);
|
---|
89 | }
|
---|
90 | #else
|
---|
91 | void *operator new(size_t size)
|
---|
92 | {
|
---|
93 | return _smalloc(size);
|
---|
94 | }
|
---|
95 | void operator delete(void *location)
|
---|
96 | {
|
---|
97 | free(location);
|
---|
98 | }
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | protected:
|
---|
102 | HFILE hMemFile;
|
---|
103 | HFILE hOrgMemFile;
|
---|
104 | ULONG mSize;
|
---|
105 | ULONG mProtFlags;
|
---|
106 | ULONG mProcessId;
|
---|
107 | ULONG mMapAccess;
|
---|
108 | LPSTR lpszMapName;
|
---|
109 | void *pMapping;
|
---|
110 |
|
---|
111 | ULONG nrMappings;
|
---|
112 |
|
---|
113 | ULONG referenced;
|
---|
114 |
|
---|
115 | VMutex mapMutex;
|
---|
116 |
|
---|
117 | Win32PeLdrImage *image;
|
---|
118 |
|
---|
119 | private:
|
---|
120 | static Win32MemMap *memmaps;
|
---|
121 | Win32MemMap *next;
|
---|
122 | };
|
---|
123 | //******************************************************************************
|
---|
124 | //Memory mapped file View Class
|
---|
125 | //******************************************************************************
|
---|
126 | class Win32MemMapView
|
---|
127 | {
|
---|
128 | public:
|
---|
129 | Win32MemMapView(Win32MemMap *map, ULONG offset, ULONG size, ULONG fdwAccess);
|
---|
130 | ~Win32MemMapView();
|
---|
131 |
|
---|
132 | DWORD getAccessFlags() { return mfAccess; };
|
---|
133 | DWORD getSize() { return mSize; };
|
---|
134 | ULONG getOffset() { return mOffset; };
|
---|
135 | LPVOID getViewAddr() { return (LPVOID)((char *)pMapView + getOffset()); };
|
---|
136 |
|
---|
137 | BOOL everythingOk() { return errorState == 0; };
|
---|
138 |
|
---|
139 | Win32MemMap *getParentMap() { return mParentMap;};
|
---|
140 | DWORD getProcessId() { return mProcessId;};
|
---|
141 |
|
---|
142 | static void deleteViews(Win32MemMap *map);
|
---|
143 | static Win32MemMap *findMapByView(ULONG address, ULONG *offset = NULL,
|
---|
144 | ULONG accessType = MEMMAP_ACCESS_READ);
|
---|
145 | static Win32MemMapView *findView(ULONG address);
|
---|
146 |
|
---|
147 | #ifdef __DEBUG_ALLOC__
|
---|
148 | void *operator new(size_t size, const char *filename, size_t lineno)
|
---|
149 | {
|
---|
150 | return _smalloc(size);
|
---|
151 | }
|
---|
152 | void operator delete(void *location, const char *filename, size_t lineno)
|
---|
153 | {
|
---|
154 | free(location);
|
---|
155 | }
|
---|
156 | #else
|
---|
157 | void *operator new(size_t size)
|
---|
158 | {
|
---|
159 | return _smalloc(size);
|
---|
160 | }
|
---|
161 | void operator delete(void *location)
|
---|
162 | {
|
---|
163 | free(location);
|
---|
164 | }
|
---|
165 | #endif
|
---|
166 |
|
---|
167 | protected:
|
---|
168 | ULONG mSize, errorState;
|
---|
169 | ULONG mProcessId;
|
---|
170 | ULONG mfAccess, mOffset;
|
---|
171 | void *pMapView, *pShareViewAddr;
|
---|
172 |
|
---|
173 | Win32MemMap *mParentMap;
|
---|
174 |
|
---|
175 | private:
|
---|
176 | static Win32MemMapView *mapviews;
|
---|
177 | Win32MemMapView *next;
|
---|
178 |
|
---|
179 | friend class Win32MemMap;
|
---|
180 | };
|
---|
181 | //******************************************************************************
|
---|
182 | //******************************************************************************
|
---|
183 |
|
---|
184 | void InitializeMemMaps();
|
---|
185 |
|
---|
186 | #endif //__MMAP_H__
|
---|