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