private static unsafe void *Reallocation(void *pUserData, void *pOriginal, nuint size, nuint alignment, SystemAllocationScope allocationScope) { bool isAllocation = pOriginal is null; bool isFree = size == 0; var handle = GCHandle.FromIntPtr((IntPtr)pUserData); var @this = (GCAllocationCallbacks?)handle.Target; if (@this is null) { return(null); } if (isAllocation) { @this.Trace ("Reallocation as Allocation: {0} {1} {2}", size, alignment, allocationScope); return(Allocate(size, alignment)); } if (isFree) { @this.Trace("Reallocation as Free: {0} {1}", alignment, allocationScope); Free(pOriginal); return(null); } @this.Trace("Reallocation: {0} {1} {2}", size, alignment, allocationScope); var oldAlignment = *((UIntPtr *)pOriginal - 2); if (alignment != oldAlignment) { @this.Warning("Old alignment was unequal to new alignment, the spec forbids this. ({old}, {new})", oldAlignment, alignment); } var newMem = Allocate(size, alignment); var oldHandle = GCHandle.FromIntPtr(*((IntPtr *)pOriginal - 1)); var oldSize = ((byte[])oldHandle.Target !).Length; var oldSpan = new Span <byte>(pOriginal, (int)oldSize); var newSpan = new Span <byte>(newMem, (int)size); if (size < (ulong)oldSize) { oldSpan.Slice(0, (int)size).CopyTo(newSpan); } else { oldSpan.CopyTo(newSpan); } oldHandle.Free(); return(newMem); }
private static unsafe void *Allocation (void *pUserData, nuint size, nuint alignment, SystemAllocationScope allocationScope) { var handle = GCHandle.FromIntPtr((IntPtr)pUserData); var @this = (GCAllocationCallbacks?)handle.Target; if (@this is null) { return(null); } @this.Trace("Allocation: {0} {1} {2}", size, alignment, allocationScope); return(Allocate(size, alignment)); }
private static unsafe void InternalFreeNotification (void *pUserData, nuint size, InternalAllocationType allocationType, SystemAllocationScope allocationScope) { Console.WriteLine("Internal Free!"); var handle = GCHandle.FromIntPtr((IntPtr)pUserData); var @this = (GCAllocationCallbacks?)handle.Target; if (@this is null) { return; } @this.Trace("Internal Free: {0} {1}", allocationType, allocationScope); GC.RemoveMemoryPressure((long)size); }
public virtual IntPtr Reallocation(IntPtr userData, IntPtr original, IntPtr size, IntPtr alignment, SystemAllocationScope allocationScope) { throw new NotImplementedException(); }
public virtual IntPtr Allocation(IntPtr userData, IntPtr size, IntPtr alignment, SystemAllocationScope allocationScope) { var rawptr = Marshal.AllocHGlobal((int)(size + 8)); var longAlignment = (Int64)alignment; var aligned = new IntPtr(longAlignment * (((long)rawptr + (longAlignment - 1)) / longAlignment)); pointers.Add(aligned); pointerMemory.Add(aligned, (long)size); GC.AddMemoryPressure((long)size); TrackedBytes += (long)size; if (DebugLog) { Console.WriteLine($"[MALLOC] Allocated {size,4} bytes [SystemAllocationScope.{allocationScope}] ({Count})"); } return(aligned); }