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