source: trunk/src/kernel32/mmapnotify.cpp@ 10376

Last change on this file since 10376 was 10376, checked in by sandervl, 22 years ago

memory map functions for dib section code

File size: 7.0 KB
Line 
1/* $Id: mmapnotify.cpp,v 1.1 2004-01-11 11:52:18 sandervl Exp $ */
2
3/*
4 * Win32 Memory mapped notification class
5 *
6 * Calls notification handler when an exception occurs in the memory range of
7 * the map.
8 *
9 * Copyright 2003 Sander van Leeuwen (sandervl@innotek.de)
10 *
11 * Project Odin Software License can be found in LICENSE.TXT
12 *
13 */
14#include <os2win.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <win\virtual.h>
19#include <odincrt.h>
20#include <handlemanager.h>
21#include "mmap.h"
22#include "oslibdos.h"
23#include "oslibmem.h"
24#include <winimagepeldr.h>
25#include <custombuild.h>
26#include <memmap.h>
27
28#define DBG_LOCALLOG DBG_mmapnotify
29#include "dbglocal.h"
30
31Win32MemMapNotify *Win32MemMapNotify::mapviews = NULL;
32
33//******************************************************************************
34//******************************************************************************
35BOOL WIN32API MMAP_RegisterMemoryRange(PFNEXCEPTIONNOTIFY pfnNotify, LPVOID lpViewAddr, DWORD size, DWORD dwUserData)
36{
37 Win32MemMapNotify *map;
38
39 dprintf(("MMAP_RegisterMemoryRange %x %x %d %x", pfnNotify, lpViewAddr, size, dwUserData));
40
41 map = new Win32MemMapNotify(pfnNotify, lpViewAddr, size, dwUserData);
42 if(map == NULL) DebugInt3();
43
44 return (map != NULL);
45}
46//******************************************************************************
47//******************************************************************************
48BOOL WIN32API MMAP_UnregisterMemoryRange(LPVOID lpViewAddr)
49{
50 Win32MemMapNotify *map;
51
52 dprintf(("MMAP_UnregisterMemoryRange %x", lpViewAddr));
53
54 map = Win32MemMapNotify::findView((ULONG)lpViewAddr);
55 if(map) delete map;
56
57 return (map != NULL);
58}
59//******************************************************************************
60
61//******************************************************************************
62// Class Win32MemMapNotify
63//
64//
65//
66//******************************************************************************
67Win32MemMapNotify::Win32MemMapNotify(PFNEXCEPTIONNOTIFY pfnNotify, LPVOID lpViewAddr, ULONG size, ULONG dwUserData)
68{
69 Win32MemMapNotify *tmpview = mapviews;
70
71 dprintf(("Win32MemMapNotify::Win32MemMapNotify: created %x, size %d", lpViewAddr, size));
72
73 pMapView = lpViewAddr;
74 mSize = size;
75 this->pfnNotify = pfnNotify;
76 this->dwUserData = dwUserData;
77
78 DosEnterCriticalSection(&globalmapcritsect);
79 if(tmpview == NULL || tmpview->getViewAddr() > pMapView) {
80 next = mapviews;
81 mapviews = this;
82 }
83 else {
84 while(tmpview->next) {
85 if(tmpview->next->getViewAddr() > pMapView) {
86 break;
87 }
88 tmpview = tmpview->next;
89 }
90 next = tmpview->next;
91 tmpview->next = this;
92 }
93 DosLeaveCriticalSection(&globalmapcritsect);
94}
95//******************************************************************************
96//******************************************************************************
97Win32MemMapNotify::~Win32MemMapNotify()
98{
99 dprintf(("Win32MemMapNotify dtor: deleting view %x %x", pMapView, mSize));
100
101 DosEnterCriticalSection(&globalmapcritsect);
102 Win32MemMapNotify *view = mapviews;
103
104 if(view == this) {
105 mapviews = next;
106 }
107 else {
108 while(view->next) {
109 if(view->next == this)
110 break;
111 view = view->next;
112 }
113 if(view->next) {
114 view->next = next;
115 }
116 else dprintf(("Win32MemMapNotify::~Win32MemMapNotify: map not found!! (%x)", this));
117 }
118 DosLeaveCriticalSection(&globalmapcritsect);
119}
120//******************************************************************************
121//******************************************************************************
122BOOL Win32MemMapNotify::notify(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess)
123{
124 return pfnNotify(pMapView, offset, fWriteAccess, mSize, dwUserData);
125}
126//******************************************************************************
127//******************************************************************************
128// Win32MemMapNotify::findMapByView
129//
130// Find the map of the view that contains the specified starting address
131// and has the specified access type
132//
133// Parameters:
134//
135// ULONG address - view address
136// ULONG *offset - address of ULONG that receives the offset
137// in the returned memory map
138// ULONG accessType - access type:
139// MEMMAP_ACCESS_READ
140// MEMMAP_ACCESS_WRITE
141// MEMMAP_ACCESS_EXECUTE
142//
143// Returns:
144// <> NULL - success, address of parent map object
145// NULL - failure
146//
147//******************************************************************************
148//******************************************************************************
149Win32MemMapNotify *Win32MemMapNotify::findMapByView(ULONG address,
150 ULONG *offset,
151 ULONG accessType)
152{
153 ULONG ulOffset;
154
155 if(mapviews == NULL) return NULL;
156
157 DosEnterCriticalSection(&globalmapcritsect);
158 Win32MemMapNotify *view = mapviews;
159 ULONG ulViewAddr;
160
161 if(!offset) offset = &ulOffset;
162
163 *offset = 0;
164
165 if(view != NULL)
166 {
167 do
168 {
169 ulViewAddr = (ULONG)view->getViewAddr();
170
171 // if ulViewAddr is > address, we've exhausted
172 // the sorted list already and can abort search.
173 if(ulViewAddr <= address)
174 {
175 if(ulViewAddr + view->getSize() > address)
176 {
177 *offset = (address - ulViewAddr);
178 goto success;
179 }
180
181 // Not found yet, continue search with next map
182 view = view->next;
183 }
184 else
185 {
186 // list is exhausted, abort loop
187 view = NULL;
188 }
189 }
190 while(view);
191
192 //failure if we get here
193 view = NULL;
194 }
195success:
196
197 DosLeaveCriticalSection(&globalmapcritsect);
198
199 return view;
200}
201//******************************************************************************
202// Win32MemMap::findView
203//
204// Find the view that contains the specified starting address
205//
206// Parameters:
207//
208// LPVOID address - view address
209//
210// Returns:
211// <> NULL - success, address view object
212// NULL - failure
213//
214//******************************************************************************
215Win32MemMapNotify *Win32MemMapNotify::findView(ULONG address)
216{
217 ULONG ulViewAddr;
218
219 DosEnterCriticalSection(&globalmapcritsect);
220 Win32MemMapNotify *view = mapviews;
221
222 if(view != NULL) {
223 while(view) {
224 ulViewAddr = (ULONG)view->getViewAddr();
225 if(ulViewAddr <= address && ulViewAddr + view->getSize() > address)
226 {
227 break;
228 }
229 view = view->next;
230 }
231 }
232 DosLeaveCriticalSection(&globalmapcritsect);
233 return view;
234}
235//******************************************************************************
236//******************************************************************************
Note: See TracBrowser for help on using the repository browser.