source: trunk/src/kernel32/mmap.h@ 9752

Last change on this file since 9752 was 8913, checked in by sandervl, 23 years ago

Enhanced PE loader class to support files with PE image starting at an offset ..= 0 (custom build) & Fixes for memory map view with offset

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