Changeset 678 for trunk/src/kernel32/mmap.cpp
- Timestamp:
- Aug 25, 1999, 12:28:41 PM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kernel32/mmap.cpp
r664 r678 1 /* $Id: mmap.cpp,v 1. 8 1999-08-24 18:46:40 sandervl Exp $ */1 /* $Id: mmap.cpp,v 1.9 1999-08-25 10:28:40 sandervl Exp $ */ 2 2 3 3 /* … … 18 18 * 19 19 * TODO: Handles returned should be usable by all apis that accept file handles 20 * TODO: Sharing memory mapped files between multiple processes 20 21 * 21 22 * Project Odin Software License can be found in LICENSE.TXT … … 115 116 //****************************************************************************** 116 117 //****************************************************************************** 118 BOOL Win32MemMap::hasReadAccess() 119 { 120 return TRUE; //should have at least this 121 } 122 //****************************************************************************** 123 //****************************************************************************** 124 BOOL Win32MemMap::hasWriteAccess() 125 { 126 return !(mProtFlags & PAGE_READONLY); 127 } 128 //****************************************************************************** 129 //Might want to add this feature for memory mapping executable & dll files in 130 //the loader (done in Win32 with the SEC_IMAGE flag?) 131 //****************************************************************************** 132 BOOL Win32MemMap::hasExecuteAccess() 133 { 134 return FALSE; 135 } 136 //****************************************************************************** 137 //****************************************************************************** 138 BOOL Win32MemMap::commitPage(LPVOID lpPageFaultAddr, ULONG nrpages) 139 { 140 DWORD pageAddr = (DWORD)lpPageFaultAddr & ~0xFFF; 141 DWORD oldProt, newProt, nrBytesRead, offset, size; 142 143 // mapMutex.enter(); 144 newProt = mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY); 145 newProt |= MEM_COMMIT; 146 147 dprintf(("Win32MemMap::commitPage %x (faultaddr %x), nr of pages %d", pageAddr, lpPageFaultAddr, nrpages)); 148 if(VirtualProtect((LPVOID)pageAddr, nrpages*PAGE_SIZE, newProt, &oldProt) == FALSE) { 149 goto fail; 150 } 151 if(hMemFile != -1) { 152 offset = pageAddr - (ULONG)pMapping; 153 size = nrpages*PAGE_SIZE; 154 if(offset + size > mSize) { 155 size = mSize - offset; 156 } 157 if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) { 158 dprintf(("Win32MemMap::commitPage: SetFilePointer failed to set pos to %x", offset)); 159 goto fail; 160 } 161 if(ReadFile(hMemFile, (LPSTR)pageAddr, size, &nrBytesRead, NULL) == FALSE) { 162 dprintf(("Win32MemMap::commitPage: ReadFile failed for %x", pageAddr)); 163 goto fail; 164 } 165 if(nrBytesRead != size) { 166 dprintf(("Win32MemMap::commitPage: ReadFile didn't read all bytes for %x", pageAddr)); 167 goto fail; 168 } 169 } 170 171 // mapMutex.leave(); 172 return TRUE; 173 fail: 174 // mapMutex.leave(); 175 return FALSE; 176 } 177 //****************************************************************************** 178 //****************************************************************************** 117 179 BOOL Win32MemMap::unmapViewOfFile() 118 180 { … … 145 207 if(fdwAccess & FILE_MAP_COPY && !(mProtFlags & PAGE_WRITECOPY)) 146 208 goto parmfail; 147 209 210 //TODO: If committed, read file into memory 211 #if 0 148 212 if(mProtFlags & SEC_COMMIT) 149 213 fAlloc |= MEM_COMMIT; 214 else 150 215 if(mProtFlags & SEC_RESERVE) 151 216 fAlloc |= MEM_RESERVE; 217 #else 218 fAlloc = MEM_RESERVE; 219 #endif 152 220 153 221 if(fMapped == FALSE) {//if not mapped, reserve/commit entire view … … 179 247 { 180 248 MEMORY_BASIC_INFORMATION memInfo; 181 ULONG nrpages, nrBytesWritten ;249 ULONG nrpages, nrBytesWritten, offset, size; 182 250 int i; 183 251 184 mapMutex.enter();252 // mapMutex.enter(); 185 253 if(fMapped == FALSE) 186 254 goto parmfail; … … 204 272 memInfo.AllocationProtect & (PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY)) 205 273 {//committed and allowed for writing? 206 if(WriteFile(hMemFile, (LPSTR)lpvBase+i*PAGE_SIZE, PAGE_SIZE, &nrBytesWritten, NULL) == FALSE) { 274 offset = (ULONG)lpvBase+i*PAGE_SIZE - (ULONG)pMapping; 275 size = PAGE_SIZE; 276 if(offset + size > mSize) { 277 size = mSize - offset; 278 } 279 dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size)); 280 281 if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) { 282 dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset)); 283 goto fail; 284 } 285 if(WriteFile(hMemFile, (LPSTR)lpvBase+i*PAGE_SIZE, size, &nrBytesWritten, NULL) == FALSE) { 207 286 dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)lpvBase+i*PAGE_SIZE)); 208 287 goto fail; 209 288 } 210 if(nrBytesWritten != PAGE_SIZE) {289 if(nrBytesWritten != size) { 211 290 dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)lpvBase+i*PAGE_SIZE)); 212 291 goto fail; … … 214 293 } 215 294 } 216 mapMutex.leave();295 // mapMutex.leave(); 217 296 return TRUE; 218 297 parmfail: 219 298 SetLastError(ERROR_INVALID_PARAMETER); 220 mapMutex.leave();299 // mapMutex.leave(); 221 300 return FALSE; 222 301 fail: 223 mapMutex.leave();302 // mapMutex.leave(); 224 303 return FALSE; 225 304 } … … 264 343 //****************************************************************************** 265 344 //****************************************************************************** 345 void Win32MemMap::deleteAll() 346 { 347 while(memmaps) { 348 CloseHandle(memmaps->hMemMap); 349 } 350 } 351 //****************************************************************************** 352 //****************************************************************************** 266 353 Win32MemMap *Win32MemMap::memmaps = NULL;
Note:
See TracChangeset
for help on using the changeset viewer.