add verbose debug; won't work with loader project

This commit is contained in:
RadWolfie 2020-03-23 18:25:00 -05:00
parent fb7398d7c9
commit fbe4987569
1 changed files with 40 additions and 0 deletions

View File

@ -26,6 +26,12 @@
// *
// ******************************************************************
// NOTE: Cannot be use in loader project due to force exclude std libraries.
//#define DEBUG // Uncomment whenever need to verify memory leaks or bad configure.
#ifdef DEBUG
#include <cstdio> // For printf
#endif
#include <cstdint> // For uint32_t
#include "AddressRanges.h"
@ -43,6 +49,10 @@ bool ReserveMemoryRange(int index, uint32_t blocks_reserved[384])
const DWORD Protect = XboxAddressRanges[index].InitialMemoryProtection;
bool NeedsReservationTracking = false;
unsigned int arr_index = BLOCK_REGION_DEVKIT_INDEX_BEGIN;
#ifdef DEBUG
std::printf("DEBUG: ReserveMemoryRange call begin\n");
std::printf(" : Comment = %s\n", XboxAddressRanges[index].Comment);
#endif
switch (Start) {
case 0x80000000:
case 0x84000000:
@ -71,6 +81,9 @@ bool ReserveMemoryRange(int index, uint32_t blocks_reserved[384])
0,
Size,
(LPVOID)Start);
#ifdef DEBUG
std::printf(" : MapViewOfFile; Start = 0x%08X; Result = %p\n", Start, Result);
#endif
if (Result == nullptr) {
HadAnyFailure = true;
}
@ -97,18 +110,28 @@ bool ReserveMemoryRange(int index, uint32_t blocks_reserved[384])
if (Result == nullptr) {
HadAnyFailure = true;
}
#ifdef DEBUG
std::printf(" : Start = %08X; Result = %p;\n", Start, Result);
#endif
// Handle the next block
Start += BLOCK_SIZE;
Size -= BLOCK_SIZE;
if (NeedsReservationTracking) {
if (Result != nullptr) {
blocks_reserved[arr_index / 32] |= (1 << (arr_index % 32));
#ifdef DEBUG
std::printf(" : arr_index = 0x%08X; set bit = 0x%08X;\n", arr_index, (1 << (arr_index % 32)));
std::printf(" : blocks_reserved[%08X] = 0x%08X\n", arr_index/32, blocks_reserved[arr_index/32]);
#endif
}
arr_index++;
}
}
}
}
#ifdef DEBUG
std::printf(" : ReserveMemoryRange call end: HadAnyFailure = %d\n\n", HadAnyFailure);
#endif
// Only a complete success when the entire request was reserved in a single range
// (Otherwise, we have either a complete failure, or reserved it partially over multiple ranges)
@ -122,11 +145,18 @@ void FreeMemoryRange(int index, uint32_t blocks_reserved[384])
int Size = XboxAddressRanges[index].Size;
bool NeedsReservationTracking = false;
unsigned int arr_index = BLOCK_REGION_DEVKIT_INDEX_BEGIN;
#ifdef DEBUG
std::printf("DEBUG: FreeMemoryRange call begin\n");
std::printf(" : Comment = %s\n", XboxAddressRanges[index].Comment);
#endif
switch (Start) {
case 0x80000000:
case 0x84000000:
case 0xF0000000: {
(void)UnmapViewOfFile((LPVOID)Start);
#ifdef DEBUG
std::printf(" : UnmapViewOfFile; Start = 0x%08X\n", Start);
#endif
}
break;
@ -147,18 +177,28 @@ void FreeMemoryRange(int index, uint32_t blocks_reserved[384])
while (Size > 0) {
_Start = Start; // Require to silence C6001's warning complaint
BOOL Result = VirtualFree((LPVOID)_Start, 0, MEM_RELEASE);
#ifdef DEBUG
std::printf(" : Start = %08X; Result = %d;\n", Start, Result);
#endif
// Handle the next block
Start += BLOCK_SIZE;
Size -= BLOCK_SIZE;
if (NeedsReservationTracking) {
if (Result != 0) {
blocks_reserved[arr_index / 32] &= ~(1 << (arr_index % 32));
#ifdef DEBUG
std::printf(" : arr_index = 0x%08X; clear bit = 0x%08X;\n", arr_index, (1 << (arr_index % 32)));
std::printf(" : blocks_reserved[%08X] = 0x%08X\n", arr_index/32, blocks_reserved[arr_index/32]);
#endif
}
arr_index++;
}
}
}
}
#ifdef DEBUG
std::printf(" : FreeMemoryRange call end\n\n");
#endif
}
bool ReserveAddressRanges(const unsigned int system, uint32_t blocks_reserved[384]) {