private static void *PxAllocatorCallback_Allocate(PxAllocatorCallback * @this, nuint size, byte *typeName, byte *filePath, int lineNumber) { AllocationCount++; // Allocate memory // Over-provision it by sizeof(AllocationInformation) so we can store our extra info and 16 bytes for alignment const ulong alignment = 16ul; ulong bytesToAllocate = size + (ulong)sizeof(AllocationInformation) + alignment; byte * actualAllocation = (byte *)Marshal.AllocHGlobal((IntPtr)bytesToAllocate); // Skip the allocation information byte *physXMemory = actualAllocation + sizeof(AllocationInformation); // Align physXMemory += alignment - (((ulong)physXMemory) % alignment); Debug.Assert(((ulong)physXMemory) % alignment == 0, "The memory must be appropriately aligned!"); Debug.Assert((((ulong)physXMemory) & 15) == 0, "The memory must be appropriately aligned!"); // This is the same assert PhysX uses internally. // Record the pointer to the actual allocation AllocationInformation *info = (AllocationInformation *)(physXMemory - sizeof(AllocationInformation)); info->ActualAllocation = actualAllocation; // Return the buffer return(physXMemory); }
private static void PxAllocatorCallback_Deallocate(PxAllocatorCallback * @this, void *ptr) { AllocationInformation *info = (AllocationInformation *)((byte *)ptr - sizeof(AllocationInformation)); Console.Write("Deallocating "); Console.Out.WriteAnsi(info->TypeName); Console.Write($" ({info->AllocationSize} bytes) for "); Console.Out.WriteAnsi(info->FilePath); Console.WriteLine($":{info->LineNumber}"); Marshal.FreeHGlobal((IntPtr)info->ActualAllocation); }
private static void PxAllocatorCallback_Deallocate(PxAllocatorCallback * @this, void *ptr) { AllocationInformation *info = (AllocationInformation *)((byte *)ptr - sizeof(AllocationInformation)); Marshal.FreeHGlobal((IntPtr)info->ActualAllocation); }