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

Last change on this file since 9901 was 9824, checked in by sandervl, 23 years ago

Cleaned up memory map code

File size: 4.8 KB
Line 
1/* $Id: mmap.h,v 1.25 2003-02-18 18:48:55 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(LPVOID addr);
51
52 HFILE getFileHandle() { return hMemFile; };
53 LPSTR getMemName() { return lpszMapName; };
54 DWORD getProtFlags() { return mProtFlags; };
55 BOOL setProtFlags(DWORD dwNewProtect);
56 LPVOID getMappingAddr() { return pMapping; };
57 DWORD getProcessId() { return mProcessId;};
58Win32PeLdrImage *getImage() { return image; };
59
60 BOOL isImageMap() { return image != NULL; };
61
62 void AddRef() { ++referenced; };
63 void Release();
64
65 BOOL commitPage(ULONG offset, BOOL fWriteAccess, int nrpages = NRPAGES_TOCOMMIT);
66
67static Win32MemMap *findMap(LPSTR lpszName);
68static Win32MemMap *findMapByFile(HANDLE hFile);
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 _smalloc(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 _smalloc(size);
87 }
88 void operator delete(void *location)
89 {
90 free(location);
91 }
92#endif
93
94protected:
95 HFILE hMemFile;
96 HFILE hOrgMemFile;
97 ULONG mSize;
98 ULONG mProtFlags;
99 ULONG mProcessId;
100 ULONG mMapAccess;
101 LPSTR lpszMapName;
102 void *pMapping;
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 ULONG getOffset() { return mOffset; };
128 LPVOID getViewAddr() { return (LPVOID)((char *)pMapView + getOffset()); };
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 = NULL,
137 ULONG accessType = MEMMAP_ACCESS_READ);
138static Win32MemMapView *findView(ULONG address);
139
140#ifdef __DEBUG_ALLOC__
141 void *operator new(size_t size, const char *filename, size_t lineno)
142 {
143 return _smalloc(size);
144 }
145 void operator delete(void *location, const char *filename, size_t lineno)
146 {
147 free(location);
148 }
149#else
150 void *operator new(size_t size)
151 {
152 return _smalloc(size);
153 }
154 void operator delete(void *location)
155 {
156 free(location);
157 }
158#endif
159
160protected:
161 ULONG mSize, errorState;
162 ULONG mProcessId;
163 ULONG mfAccess, mOffset;
164 void *pMapView, *pShareViewAddr;
165
166 Win32MemMap *mParentMap;
167
168private:
169 static Win32MemMapView *mapviews;
170 Win32MemMapView *next;
171
172 friend class Win32MemMap;
173};
174//******************************************************************************
175//******************************************************************************
176
177void InitializeMemMaps();
178
179#endif //__MMAP_H__
Note: See TracBrowser for help on using the repository browser.