1 | /* $Id: mmap.h,v 1.9 1999-08-27 16:51:00 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 |
|
---|
17 | #ifndef PAGE_SIZE
|
---|
18 | #define PAGE_SIZE 4096
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | //commit 4 pages at once when the app accesses it
|
---|
22 | #define NRPAGES_TOCOMMIT 200
|
---|
23 |
|
---|
24 | #define MEMMAP_ACCESS_READ 1
|
---|
25 | #define MEMMAP_ACCESS_WRITE 2
|
---|
26 | #define MEMMAP_ACCESS_EXECUTE 4
|
---|
27 |
|
---|
28 | class Win32MemMapView;
|
---|
29 |
|
---|
30 | //******************************************************************************
|
---|
31 | //******************************************************************************
|
---|
32 | class Win32MemMap
|
---|
33 | {
|
---|
34 | public:
|
---|
35 | Win32MemMap(HFILE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName);
|
---|
36 | ~Win32MemMap();
|
---|
37 |
|
---|
38 | BOOL Init(HANDLE hMemMap);
|
---|
39 | BOOL flushView(ULONG offset, ULONG cbFlush);
|
---|
40 | LPVOID mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess);
|
---|
41 | BOOL unmapViewOfFile(Win32MemMapView *view);
|
---|
42 |
|
---|
43 | HFILE getMapHandle() { return hMemMap; };
|
---|
44 | LPSTR getMemName() { return lpszMapName; };
|
---|
45 | DWORD getProtFlags() { return mProtFlags; };
|
---|
46 | LPVOID getMappingAddr() { return pMapping; };
|
---|
47 |
|
---|
48 | void AddRef() { ++referenced; };
|
---|
49 | void Release() { if(--referenced == 0) delete this; };
|
---|
50 |
|
---|
51 | BOOL commitPage(ULONG offset, BOOL fWriteAccess);
|
---|
52 |
|
---|
53 | static Win32MemMap *findMap(LPSTR lpszName);
|
---|
54 | static Win32MemMap *findMap(ULONG address);
|
---|
55 |
|
---|
56 | //Should only be called in ExitProcess
|
---|
57 | static void deleteAll();
|
---|
58 |
|
---|
59 | protected:
|
---|
60 | HFILE hMemMap, hMemFile;
|
---|
61 | ULONG mSize;
|
---|
62 | ULONG mProtFlags;
|
---|
63 | ULONG mMapAccess;
|
---|
64 | LPSTR lpszMapName;
|
---|
65 | void *pMapping;
|
---|
66 |
|
---|
67 | ULONG nrMappings;
|
---|
68 |
|
---|
69 | ULONG referenced;
|
---|
70 |
|
---|
71 | VMutex mapMutex;
|
---|
72 |
|
---|
73 | private:
|
---|
74 | static Win32MemMap *memmaps;
|
---|
75 | Win32MemMap *next;
|
---|
76 | };
|
---|
77 | //******************************************************************************
|
---|
78 | //Memory mapped file View Class
|
---|
79 | //******************************************************************************
|
---|
80 | class Win32MemMapView
|
---|
81 | {
|
---|
82 | public:
|
---|
83 | Win32MemMapView(Win32MemMap *map, ULONG offset, ULONG size, ULONG fdwAccess);
|
---|
84 | ~Win32MemMapView();
|
---|
85 |
|
---|
86 | DWORD getAccessFlags() { return mfAccess; };
|
---|
87 | DWORD getSize() { return mSize; };
|
---|
88 | LPVOID getViewAddr() { return pMapView; };
|
---|
89 | ULONG getOffset() { return mOffset; };
|
---|
90 |
|
---|
91 | BOOL everythingOk() { return errorState == 0; };
|
---|
92 |
|
---|
93 | Win32MemMap *getParentMap() { return mParentMap;};
|
---|
94 |
|
---|
95 | static void deleteView(Win32MemMap *map);
|
---|
96 | static Win32MemMap *findMapByView(ULONG address, ULONG *offset, ULONG accessType, Win32MemMapView **pView=NULL);
|
---|
97 | static Win32MemMapView *findView(LPVOID address);
|
---|
98 |
|
---|
99 | protected:
|
---|
100 | ULONG mSize, errorState;
|
---|
101 | ULONG mfAccess, mOffset;
|
---|
102 | void *pMapView;
|
---|
103 |
|
---|
104 | Win32MemMap *mParentMap;
|
---|
105 |
|
---|
106 | private:
|
---|
107 | static Win32MemMapView *mapviews;
|
---|
108 | Win32MemMapView *next;
|
---|
109 |
|
---|
110 | friend class Win32MemMap;
|
---|
111 | };
|
---|
112 | //******************************************************************************
|
---|
113 | //******************************************************************************
|
---|
114 |
|
---|
115 | #endif //__MMAP_H__
|
---|