1 | /* $Id: mmap.h,v 1.12 1999-11-22 20:35:50 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 |
|
---|
22 | //commit 4 pages at once when the app accesses it
|
---|
23 | #define NRPAGES_TOCOMMIT 200
|
---|
24 |
|
---|
25 | #define MEMMAP_ACCESS_READ 1
|
---|
26 | #define MEMMAP_ACCESS_WRITE 2
|
---|
27 | #define MEMMAP_ACCESS_EXECUTE 4
|
---|
28 |
|
---|
29 | class Win32MemMapView;
|
---|
30 | class Win32PeLdrImage;
|
---|
31 |
|
---|
32 | //******************************************************************************
|
---|
33 | //******************************************************************************
|
---|
34 | class Win32MemMap
|
---|
35 | {
|
---|
36 | public:
|
---|
37 | Win32MemMap(HFILE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName);
|
---|
38 | //Use by PE loader image class only:
|
---|
39 | Win32MemMap(Win32PeLdrImage *pImage, ULONG lpImageMem, ULONG size);
|
---|
40 | ~Win32MemMap();
|
---|
41 |
|
---|
42 | BOOL Init(HANDLE hMemMap);
|
---|
43 | BOOL flushView(ULONG offset, ULONG cbFlush);
|
---|
44 | LPVOID mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess);
|
---|
45 | BOOL unmapViewOfFile(Win32MemMapView *view);
|
---|
46 |
|
---|
47 | HFILE getMapHandle() { return hMemMap; };
|
---|
48 | LPSTR getMemName() { return lpszMapName; };
|
---|
49 | DWORD getProtFlags() { return mProtFlags; };
|
---|
50 | LPVOID getMappingAddr() { return pMapping; };
|
---|
51 | DWORD getProcessId() { return mProcessId;};
|
---|
52 | Win32PeLdrImage *getImage() { return image; };
|
---|
53 |
|
---|
54 | BOOL isImageMap() { return image != NULL; };
|
---|
55 |
|
---|
56 | void AddRef() { ++referenced; };
|
---|
57 | void Release() { if(--referenced == 0) delete this; };
|
---|
58 |
|
---|
59 | BOOL commitPage(ULONG offset, BOOL fWriteAccess, int nrpages = NRPAGES_TOCOMMIT);
|
---|
60 |
|
---|
61 | static Win32MemMap *findMap(LPSTR lpszName);
|
---|
62 | static Win32MemMap *findMap(ULONG address);
|
---|
63 |
|
---|
64 | //Should only be called in ExitProcess
|
---|
65 | static void deleteAll();
|
---|
66 |
|
---|
67 | #ifdef __DEBUG_ALLOC__
|
---|
68 | void *operator new(size_t size, const char *filename, size_t lineno)
|
---|
69 | {
|
---|
70 | return _umalloc(sharedHeap, size);
|
---|
71 | }
|
---|
72 | void operator delete(void *location, const char *filename, size_t lineno)
|
---|
73 | {
|
---|
74 | free(location);
|
---|
75 | }
|
---|
76 | #else
|
---|
77 | void *operator new(size_t size)
|
---|
78 | {
|
---|
79 | return _umalloc(sharedHeap, size);
|
---|
80 | }
|
---|
81 | void operator delete(void *location)
|
---|
82 | {
|
---|
83 | free(location);
|
---|
84 | }
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | protected:
|
---|
88 | HFILE hMemMap, hMemFile;
|
---|
89 | ULONG mSize;
|
---|
90 | ULONG mProtFlags;
|
---|
91 | ULONG mProcessId;
|
---|
92 | ULONG mMapAccess;
|
---|
93 | LPSTR lpszMapName;
|
---|
94 | void *pMapping;
|
---|
95 |
|
---|
96 | ULONG nrMappings;
|
---|
97 |
|
---|
98 | ULONG referenced;
|
---|
99 |
|
---|
100 | VMutex mapMutex;
|
---|
101 |
|
---|
102 | Win32PeLdrImage *image;
|
---|
103 |
|
---|
104 | private:
|
---|
105 | static Win32MemMap *memmaps;
|
---|
106 | Win32MemMap *next;
|
---|
107 | };
|
---|
108 | //******************************************************************************
|
---|
109 | //Memory mapped file View Class
|
---|
110 | //******************************************************************************
|
---|
111 | class Win32MemMapView
|
---|
112 | {
|
---|
113 | public:
|
---|
114 | Win32MemMapView(Win32MemMap *map, ULONG offset, ULONG size, ULONG fdwAccess);
|
---|
115 | ~Win32MemMapView();
|
---|
116 |
|
---|
117 | DWORD getAccessFlags() { return mfAccess; };
|
---|
118 | DWORD getSize() { return mSize; };
|
---|
119 | LPVOID getViewAddr() { return pMapView; };
|
---|
120 | ULONG getOffset() { return mOffset; };
|
---|
121 |
|
---|
122 | BOOL everythingOk() { return errorState == 0; };
|
---|
123 |
|
---|
124 | Win32MemMap *getParentMap() { return mParentMap;};
|
---|
125 | DWORD getProcessId() { return mProcessId;};
|
---|
126 |
|
---|
127 | static void deleteViews(Win32MemMap *map);
|
---|
128 | static Win32MemMap *findMapByView(ULONG address, ULONG *offset, ULONG accessType, Win32MemMapView **pView=NULL);
|
---|
129 | static Win32MemMapView *findView(LPVOID address);
|
---|
130 |
|
---|
131 | #ifdef __DEBUG_ALLOC__
|
---|
132 | void *operator new(size_t size, const char *filename, size_t lineno)
|
---|
133 | {
|
---|
134 | return _umalloc(sharedHeap, size);
|
---|
135 | }
|
---|
136 | void operator delete(void *location, const char *filename, size_t lineno)
|
---|
137 | {
|
---|
138 | free(location);
|
---|
139 | }
|
---|
140 | #else
|
---|
141 | void *operator new(size_t size)
|
---|
142 | {
|
---|
143 | return _umalloc(sharedHeap, size);
|
---|
144 | }
|
---|
145 | void operator delete(void *location)
|
---|
146 | {
|
---|
147 | free(location);
|
---|
148 | }
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | protected:
|
---|
152 | ULONG mSize, errorState;
|
---|
153 | ULONG mProcessId;
|
---|
154 | ULONG mfAccess, mOffset;
|
---|
155 | void *pMapView;
|
---|
156 |
|
---|
157 | Win32MemMap *mParentMap;
|
---|
158 |
|
---|
159 | private:
|
---|
160 | static Win32MemMapView *mapviews;
|
---|
161 | Win32MemMapView *next;
|
---|
162 |
|
---|
163 | friend class Win32MemMap;
|
---|
164 | };
|
---|
165 | //******************************************************************************
|
---|
166 | //******************************************************************************
|
---|
167 |
|
---|
168 | #endif //__MMAP_H__
|
---|