| 124 | } |
| 125 | |
| 126 | void* VMAllocateAlign(void* pAddress, size_t nSize, size_t nAlign, |
| 127 | VM_ALLOCATION_TYPE nType, VM_PROTECT_FLAG nProtect) |
| 128 | { |
| 129 | |
| 130 | #ifdef GPCS4_DEBUG |
| 131 | // This make it easier to find a function a Ida Pro. |
| 132 | // When aligned with debugAlign, the loaded image address |
| 133 | // is exactly the same as it in Ida Pro. |
| 134 | // For example, without this debug align, |
| 135 | // a function address may be 0x00000002b0240e21, |
| 136 | // but with this align, you can search for sub_240e21 |
| 137 | // in Ida directly. |
| 138 | const uint32_t debugAlign = 0x10000000; |
| 139 | nAlign = debugAlign; |
| 140 | #endif |
| 141 | |
| 142 | void* pAlignedAddr = nullptr; |
| 143 | do |
| 144 | { |
| 145 | DWORD dwProtect = GetProtectFlag(nProtect); |
| 146 | void* pAddr = VirtualAlloc(pAddress, nSize, MEM_RESERVE, dwProtect); |
| 147 | uintptr_t pRefAddr = util::align((uintptr_t)pAddr, nAlign); |
| 148 | |
| 149 | if (pAddr) |
| 150 | { |
| 151 | VirtualFree(pAddr, 0, MEM_RELEASE); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | do |
| 159 | { |
| 160 | pAlignedAddr = VirtualAlloc((void*)pRefAddr, nSize, MEM_RESERVE | MEM_COMMIT, dwProtect); |
| 161 | pRefAddr += nAlign; |
| 162 | } while (pAlignedAddr == nullptr); |
| 163 | |
| 164 | } while (false); |
| 165 | |
| 166 | return pAlignedAddr; |
| 167 | |
| 168 | } |
| 169 | |
| 170 | void VMFree(void* pAddress) |
| 171 | { |
no test coverage detected