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

Last change on this file since 3802 was 3609, checked in by sandervl, 25 years ago

memory map bugfixes + added method for querying image size

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