16 static void __mem_FatalHeapError(
CSTR Where )
19 _T(
"[%s] Heap validation failed.\nTry to save your work, then terminate the program!"),
28 #ifndef ERROR_INVALID_CRUNTIME_PARAMETER 29 #define ERROR_INVALID_CRUNTIME_PARAMETER 1288L 43 void* pBlk = HeapAlloc(
app_Heap, HEAP_ZERO_MEMORY, Bytes );
47 SetLastError( ERROR_OUTOFMEMORY );
50 pBlk = HeapAlloc(
app_Heap, HEAP_ZERO_MEMORY, Bytes );
54 SetLastError( ERROR_OUTOFMEMORY );
55 if (!HeapValidate(
app_Heap, 0, NULL ))
58 SetLastError( ERROR_ARENA_TRASHED );
60 if (IsDebuggerPresent())
_BREAK;
61 else __mem_FatalHeapError( _T(
"mem_Alloc") );
63 RaiseException( STATUS_NO_MEMORY, 0,0,NULL );
67 else ExitProcess( ERROR_ARENA_TRASHED );
86 void* pRea = HeapReAlloc(
app_Heap, HEAP_ZERO_MEMORY, pBlk, Bytes );
90 SetLastError( ERROR_OUTOFMEMORY );
93 pRea = HeapReAlloc(
app_Heap, HEAP_ZERO_MEMORY, pBlk, Bytes );
97 if (!HeapValidate(
app_Heap, 0, pBlk ))
99 SetLastError( ERROR_ARENA_TRASHED );
100 if (IsDebuggerPresent())
_BREAK;
101 else __mem_FatalHeapError( _T(
"mem_Realloc") );
109 size_t oldSize = HeapSize(
app_Heap, 0, pBlk );
110 memcpy( pRea, pBlk, min_<size_t>( Bytes, oldSize ));
129 #ifdef HAVE_STRUCTURED_EH 130 __try {
if (HeapFree(
app_Heap, 0, pBlk )) pBlk = NULL; }
133 DWORD xptCode = GetExceptionCode();
138 #else // No SEH.. May crash... 139 if (HeapFree(
app_Heap, 0, pBlk )) pBlk = 0;
152 size_t Largest = HeapCompact(
app_Heap, 0 );
153 bool ok = (Largest > 0);
159 if (Largest == 0 && GetLastError() == 0)
162 SetLastError( ERROR_OUTOFMEMORY );
168 else DPrint(
DP_INFO,
_F(
"HeapCompact returned largest block = %lu\n"), Largest );
180 DWORD cbPages = pgSize * nPages;
182 PVOID pBlk = VirtualAlloc( NULL, cbPages, MEM_COMMIT | MEM_RESERVE, Access );
187 if (pBlk) VirtualLock( pBlk, cbPages );
195 DWORD err = ERROR_INVALID_PARAMETER;
200 BOOL ok = VirtualUnlock( pBlk, cbRegion );
201 if (ok) ok = VirtualFree( pBlk, 0, MEM_RELEASE );
202 if (!ok) err = GetLastError();
209 if (err) SetLastError( err );
217 SYSTEM_INFO si;
IF_DEBUG( memset( &si, 0,
sizeof(si) ));
219 return si.dwPageSize;
225 MEMORY_BASIC_INFORMATION mi;
IF_DEBUG( memset( &mi, 0,
sizeof(mi) ));
226 if (VirtualQuery( pBlk, &mi,
sizeof(mi) ))
228 if (pBlk == mi.AllocationBase) cbRegion = (UINT) mi.RegionSize;
229 else SetLastError( ERROR_INVALID_PARAMETER );
237 MEMORY_BASIC_INFORMATION mi;
IF_DEBUG( memset( &mi, 0,
sizeof(mi) ));
238 if (VirtualQuery( pBlk, &mi,
sizeof(mi) )) pBase = mi.BaseAddress;
245 MEMORY_BASIC_INFORMATION mi;
IF_DEBUG( memset( &mi, 0,
sizeof(mi) ));
246 if (VirtualQuery( pBlk, &mi,
sizeof(mi) )) pBase = mi.AllocationBase;
256 _hmem = CreateFileMapping(
259 _pmem = _hmem ? MapViewOfFile( _hmem,
FILE_MAP_RW, 0,0,0 ) : NULL;
264 _hmem = OpenFileMapping(
FILE_MAP_RW, TRUE, Name );
265 _pmem = _hmem ? MapViewOfFile( _hmem,
FILE_MAP_RW, 0,0,0 ) : NULL;
270 if (_pmem) UnmapViewOfFile( _pmem );
271 if (_hmem) CloseHandle( _hmem );
281 MEMORY_BASIC_INFORMATION mbi;
283 size_t cb = VirtualQuery( _pmem, &mbi,
sizeof(mbi) );
284 if (cb >=
sizeof(mbi)) size = mbi.RegionSize;
291 return bool_cast( FlushViewOfFile( Addr, Range ));
307 typedef struct _physicalMem* _PPhys;
316 #ifdef _DEBUG // This kludge is to be removed once the code has matured.. 317 #define _IF_ERR( cond, fmt, ... ) if (cond) { \ 318 DPrint( DP_ERROR, fmt, __VA_ARGS__ ); \ 319 if (IsDebuggerPresent()) __debugbreak(); \ 320 else MessageBox( HWND_DESKTOP, \ 321 _T("MemFunc Breakpoint:\nNo debugger present.. You may want to attach one now."), \ 322 dbg_Name, MB_OK| MB_ICONSTOP ); \ 325 #define _IF_ERR( ... ) 331 return _PPhys(hMem)->pMem;
335 return _PPhys(hMem)->cbMem;
339 return _PPhys(hMem)->nrPages;
349 DWORD err = NO_ERROR;
351 SIZE_T cbSize = (SIZE_T) cbRequest;
353 if (
UINT64(cbSize) != cbRequest)
357 err = ERROR_INVALID_PARAMETER;
360 if (!cbRequest) err = ERROR_INVALID_PARAMETER;
363 HANDLE hProc = GetCurrentProcess(), hToken;
364 ULONG_PTR nPages = ULONG_PTR( cbRequest / _pageSize );
365 ULONG_PTR cPages = nPages;
367 SIZE_T cbPageIndex = nPages *
sizeof(ULONG_PTR);
368 PULONG_PTR memPageNrs = (PULONG_PTR)
mem_Alloc( cbPageIndex );
374 TOKEN_PRIVILEGES tp = { 1, { 0,0,0 }};
383 AllocateUserPhysicalPages( hProc, &cPages, memPageNrs ),
390 if (cPages != nPages) err = ERROR_NOT_ENOUGH_MEMORY;
391 cbSize = cPages * _pageSize;
393 PVOID pMem = VirtualAlloc(
394 NULL, cbSize, MEM_PHYSICAL | MEM_RESERVE, PAGE_READWRITE
400 MapUserPhysicalPages( pMem, cPages, memPageNrs ),
405 pm = (_PPhys)
mem_Alloc(
sizeof(_physicalMem) );
408 pm->pageNrs = memPageNrs;
409 pm->nrPages = cPages;
414 if (pMem) VirtualFree( pMem, 0, MEM_RELEASE );
415 FreeUserPhysicalPages( hProc, &cPages, memPageNrs );
422 if ( err ) SetLastError( err );
430 _PPhys pm = (_PPhys) hMem;
432 HANDLE hProc = GetCurrentProcess(), hToken;
433 TOKEN_PRIVILEGES tp = { 1, { 0,0,0 }};
437 BOOL ok = MapUserPhysicalPages( pm->pMem, pm->nrPages, NULL );
441 ok = VirtualFree( pm->pMem, 0, MEM_RELEASE );
444 ok = FreeUserPhysicalPages( hProc, &pm->nrPages, pm->pageNrs );
PVOID FreeMemoryPages(PVOID pBlk)
HPHYSICAL PhysicalFree(HPHYSICAL hMem)
DWORD GetMemoryPageSize()
void * mem_Realloc(void *pBlk, size_t Bytes)
void * mem_Alloc(size_t Bytes)
ULONG_PTR PhysicalPageCount(HPHYSICAL hMem)
void __cdecl DPrint(int Level, CSTR Fmt,...)
UINT GetPageRegionSize(PVOID pBlk)
PVOID GetPageBaseAddress(PVOID pBlk)
bool Flush(PVOID Addr, size_t Range)
BOOL(WINAPI *SysImgList::Shell_GetImageLists)(HIMAGELIST *pimlLarge
void * mem_Free(void *pBlk)
void GetWinSystemInfo(SYSTEM_INFO *si)
#define ERROR_INVALID_CRUNTIME_PARAMETER
CSTR SysErrorMsg(DWORD Err=0, TSTR Buf=NULL, UINT Length=0)
SECURITY_ATTRIBUTES DefSec
bool RestorePrivilege(HANDLE hToken, PTOKEN_PRIVILEGES pSaved)
See EnablePrivilege(()
bool __forceinline bool_cast(BOOL B52)
HPHYSICAL PhysicalAlloc(UINT64 nBytes)
Debug and error handling support.
int __cdecl MsgBox(HWND Owner, UINT Type, CSTR Cap, CSTR Fmt,...)
__inline bool __ChkOkGetErr(BOOL Ok, PDWORD pErr)
CSTR DataSizeStr(UINT64 Size)
#define ROUND_UP2(x, chunk)
bool __EnableProcPrivilege(IN CSTR Privilege, OUT LUID_AND_ATTRIBUTES *pPrv, OUT HANDLE *pToken)
PVOID AllocMemoryPages(WORD nPages, DWORD Access)
#define _IF_ERR(cond, fmt,...)
SharedMem(size_t Size, CSTR Name)
PVOID GetPageAllocationBase(PVOID pBlk)
PVOID PhysicalPtr(HPHYSICAL hMem)
UINT64 PhysicalSize(HPHYSICAL hMem)