Changeset 21302 for trunk/src/kernel32/oslibmem.cpp
- Timestamp:
- Jun 18, 2009, 11:53:26 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kernel32/oslibmem.cpp
r9945 r21302 32 32 #include <winconst.h> 33 33 #include <win\winioctl.h> 34 #include <misc.h> 34 #include <dbglog.h> 35 #include <vmutex.h> 35 36 #include "initterm.h" 36 37 #include "oslibdos.h" … … 38 39 #include "dosqss.h" 39 40 #include "win32k.h" 41 #include "exceptstackdump.h" 40 42 41 43 #define DBG_LOCALLOG DBG_oslibmem 42 44 #include "dbglocal.h" 43 45 46 #include <_ras.h> 47 48 #ifdef RAS 49 RAS_TRACK_HANDLE rthVirtual = 0; 50 51 void rasInitVirtual (void) 52 { 53 RasRegisterObjectTracking (&rthVirtual, "Virtual* memory allocation", 54 0, RAS_TRACK_FLAG_MEMORY | RAS_TRACK_FLAG_LOG_OBJECTS_AT_EXIT, 55 NULL, NULL); 56 } 57 #endif 58 59 typedef struct _VirtAllocRec { 60 ULONG baseaddr; 61 ULONG size; 62 ULONG attr; 63 64 struct _VirtAllocRec *next; 65 } VirtAllocRec; 66 67 static VirtAllocRec *allocrecords = NULL; 68 static CRITICAL_SECTION_OS2 alloccritsect = {0}; 69 70 //****************************************************************************** 71 //****************************************************************************** 72 void AddAllocRec(ULONG baseaddr, ULONG size, ULONG attr) 73 { 74 VirtAllocRec *rec, *tmp; 75 76 rec = (VirtAllocRec *)malloc(sizeof(VirtAllocRec)); 77 if(!rec) { 78 DebugInt3(); 79 return; 80 } 81 rec->baseaddr = baseaddr; 82 rec->size = size; 83 rec->attr = attr; 84 85 DosEnterCriticalSection(&alloccritsect); 86 if(!allocrecords || allocrecords->baseaddr > baseaddr) { 87 rec->next = allocrecords; 88 allocrecords = rec; 89 } 90 else { 91 tmp = allocrecords; 92 while(tmp->next) { 93 if(tmp->next->baseaddr > baseaddr) { 94 break; 95 } 96 tmp = tmp->next; 97 } 98 99 rec->next = tmp->next; 100 tmp->next = rec; 101 } 102 DosLeaveCriticalSection(&alloccritsect); 103 } 104 //****************************************************************************** 105 //****************************************************************************** 106 void FreeAllocRec(ULONG baseaddr) 107 { 108 VirtAllocRec *rec = NULL, *tmp; 109 110 if(!allocrecords) { 111 DebugInt3(); 112 return; 113 } 114 115 DosEnterCriticalSection(&alloccritsect); 116 if(allocrecords->baseaddr == baseaddr) { 117 rec = allocrecords; 118 allocrecords = allocrecords->next; 119 } 120 else { 121 tmp = allocrecords; 122 while(tmp->next) { 123 if(tmp->next->baseaddr == baseaddr) { 124 break; 125 } 126 tmp = tmp->next; 127 } 128 if(tmp->next) { 129 rec = tmp->next; 130 tmp->next = tmp->next->next; 131 } 132 else dprintf(("ERROR: FreeAllocRec: allocation not found!! (%x)", baseaddr)); 133 } 134 DosLeaveCriticalSection(&alloccritsect); 135 if(rec) free(rec); 136 } 137 //****************************************************************************** 138 //****************************************************************************** 139 BOOL FindAllocRec(ULONG addr, ULONG *lpBase, ULONG *lpSize, ULONG *lpAttr) 140 { 141 VirtAllocRec *rec = NULL; 142 143 DosEnterCriticalSection(&alloccritsect); 144 rec = allocrecords; 145 while(rec) { 146 if(rec->baseaddr <= addr && rec->baseaddr + rec->size > addr) { 147 *lpBase = rec->baseaddr; 148 *lpSize = rec->size; 149 *lpAttr = rec->attr; 150 break; //found it 151 } 152 if(rec->baseaddr > addr) { 153 //sorted list, so no need to search any further 154 rec = NULL; 155 break; 156 } 157 rec = rec->next; 158 } 159 DosLeaveCriticalSection(&alloccritsect); 160 return (rec != NULL); 161 } 44 162 //****************************************************************************** 45 163 //TODO: Check if this works for code aliases... … … 83 201 pAlias += size; 84 202 } 203 AddAllocRec((ULONG)*ppbAlias, cb, fl); 85 204 return 0; 86 205 } … … 99 218 fMemFlags = 0; 100 219 } 220 101 221 /* 102 222 * Let's try use the extended DosAllocMem API of Win32k.sys. … … 105 225 { 106 226 rc = DosAllocMemEx(lplpMemAddr, cbSize, flFlags | fMemFlags | OBJ_ALIGN64K); 227 #ifdef RAS 228 if (rc == NO_ERROR) 229 { 230 RasAddObject (rthVirtual, (ULONG)*lplpMemAddr, NULL, cbSize); 231 } 232 #endif 107 233 if (rc != ERROR_NOT_SUPPORTED) /* This call was stubbed until recently. */ 108 234 return rc; … … 132 258 return rc; 133 259 } 260 261 PVOID baseAddr = (PVOID)addr64kb; // sunlover20040613: save returned address for a possible Free on failure 262 134 263 dprintf(("Allocate aligned memory %x -> %x", addr64kb, (addr64kb + 0xFFFF) & ~0xFFFF)); 135 264 … … 144 273 if(rc) { 145 274 dprintf(("!ERROR!: DosSetMem failed with rc %d", rc)); 275 DosFreeMem (baseAddr); // sunlover20040613: Free allocated memory 146 276 return rc; 147 277 } … … 149 279 } 150 280 151 if(!rc) 281 if(!rc) { 152 282 *lplpMemAddr = pvMemAddr; 153 283 AddAllocRec((ULONG)pvMemAddr, cbSize, flFlags); 284 RasAddObject (rthVirtual, (ULONG)*lplpMemAddr, NULL, cbSize); 285 } 154 286 return rc; 155 287 } … … 157 289 //Locate the base page of a memory allocation (the page with the PAG_BASE attribute) 158 290 //****************************************************************************** 159 PVOID OSLibDosFindMemBase(LPVOID lpMemAddr )160 { 161 ULONG ulAttr, ulSize, ulAddr ;291 PVOID OSLibDosFindMemBase(LPVOID lpMemAddr, DWORD *lpAttr) 292 { 293 ULONG ulAttr, ulSize, ulAddr, ulBase; 162 294 APIRET rc; 295 VirtAllocRec *allocrec; 296 297 *lpAttr = 0; 163 298 299 if(FindAllocRec((ULONG)lpMemAddr, &ulBase, &ulSize, lpAttr) == TRUE) { 300 return (PVOID)ulBase; 301 } 302 303 ulSize = PAGE_SIZE; 164 304 rc = DosQueryMem(lpMemAddr, &ulSize, &ulAttr); 165 305 if(rc != NO_ERROR) { … … 226 366 dprintf(("!ERROR!: OSLibDosFreeMem: Unable to find base of %x", lpMemAddr)); 227 367 DebugInt3(); 228 } 229 else { 230 lpMemAddr = (PVOID)ulAddr; 231 } 232 return DosFreeMem(lpMemAddr); 368 return ERROR_INVALID_PARAMETER; 369 } 370 FreeAllocRec((ULONG)lpMemAddr); 371 372 RasRemoveObject (rthVirtual, (ULONG)lpMemAddr); 373 374 return DosFreeMem((PVOID)ulAddr); 233 375 } 234 376 //******************************************************************************
Note:
See TracChangeset
for help on using the changeset viewer.