/// <summary> /// Implements the hook for Sonic Heroes' malloc function, adding the address allocated to the set and /// calling any other necessary delegates. /// </summary> /// <param name="bytes">The amount of bytes to be allocated.</param> /// <returns>The memory address of allocation</returns> private void *MallocHookImpl(int bytes) { // Call our own delegate and get potential new return value. if (BeforeMallocDelegate != null) { void *potentialResult = BeforeMallocDelegate(bytes); if ((int)potentialResult > 0) { return(potentialResult); } } // Call original allocator. void *newMemoryLocation = _mallocHook.OriginalFunction(bytes); // Some extra prep. int newMemoryLocationCopy = (int)newMemoryLocation; var memoryAddressDetails = new MemoryAddressDetails(bytes); // Add to dictionary. AllocationList[newMemoryLocationCopy] = memoryAddressDetails; // Call subscribed delegates. MemoryTracer thisTracer = this; AfterMallocDelegate?.Invoke(ref thisTracer, ref newMemoryLocationCopy, ref memoryAddressDetails); // Return return(newMemoryLocation); }
/// <summary> /// Implements the hook for Sonic Heroes' free function, removing the address allocated from the set and /// calling any other necessary delegates. /// </summary> /// <param name="memoryAddress">The memory address to be freed from memory.</param> private void FreeHookImpl(void *memoryAddress) { // Call original deallocator. _freeHook.OriginalFunction(memoryAddress); // They can sometimes pass 0 in. if (AllocationList.ContainsKey((int)memoryAddress)) { // Call subscribed delegates. MemoryTracer thisTracer = this; int memoryAddressCopy = (int)memoryAddress; MemoryAddressDetails addressDetails = AllocationList[memoryAddressCopy]; // Remove from dictionary and call delegate. AllocationList.Remove((int)memoryAddress); AfterFreeDelegate?.Invoke(ref thisTracer, ref memoryAddressCopy, ref addressDetails); } }