public static void Free4KbAlignedMemory(byte *ptr, long size, Sparrow.Utils.NativeMemory.ThreadStats stats) { Debug.Assert(ptr != null); if (stats != null) { Sparrow.Utils.NativeMemory.UpdateMemoryStatsForThread(stats, size); } Interlocked.Add(ref Sparrow.Utils.NativeMemory._totalAllocatedMemory, -size); if (PlatformDetails.RunningOnPosix) { var result = Syscall.munmap((IntPtr)ptr, (UIntPtr)(uint)size); if (result == -1) { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, "Failed to munmap "); } return; } if (Win32MemoryProtectMethods.VirtualFree(ptr, UIntPtr.Zero, Win32MemoryProtectMethods.FreeType.MEM_RELEASE) == false) { ThrowFailedToFree(); } }
public static byte *Allocate4KbAlignedMemory(long size, out Sparrow.Utils.NativeMemory.ThreadStats thread) { Debug.Assert(size >= 0); thread = Sparrow.Utils.NativeMemory.ThreadAllocations.Value; thread.Allocations += size; Interlocked.Add(ref Sparrow.Utils.NativeMemory._totalAllocatedMemory, size); if (PlatformDetails.RunningOnPosix) { byte *ptr; var rc = Syscall.posix_memalign(&ptr, (IntPtr)4096, (IntPtr)size); if (rc != 0) { Syscall.ThrowLastError(rc, "Could not allocate memory"); } return(ptr); } var allocate4KbAllignedMemory = Win32MemoryProtectMethods.VirtualAlloc(null, (UIntPtr)size, Win32MemoryProtectMethods.AllocationType.COMMIT, Win32MemoryProtectMethods.MemoryProtection.READWRITE); if (allocate4KbAllignedMemory == null) { ThrowFailedToAllocate(); } return(allocate4KbAllignedMemory); }
public static void Free4KbAlignedMemory(byte *ptr, int size, Sparrow.Utils.NativeMemory.ThreadStats stats) { Debug.Assert(ptr != null); var currentThreadValue = Sparrow.Utils.NativeMemory.ThreadAllocations.Value; if (currentThreadValue == stats) { currentThreadValue.Allocations -= size; Sparrow.Utils.NativeMemory.FixupReleasesFromOtherThreads(currentThreadValue); } else { Interlocked.Add(ref stats.ReleasesFromOtherThreads, size); } Interlocked.Add(ref Sparrow.Utils.NativeMemory._totalAllocatedMemory, -size); var p = new IntPtr(ptr); if (PlatformDetails.RunningOnPosix) { Syscall.free(p); return; } if (Win32MemoryProtectMethods.VirtualFree(ptr, UIntPtr.Zero, Win32MemoryProtectMethods.FreeType.MEM_RELEASE) == false) { ThrowFailedToFree(); } }
public static byte *Allocate4KbAlignedMemory(long size, out Sparrow.Utils.NativeMemory.ThreadStats thread) { Debug.Assert(size >= 0); thread = Sparrow.Utils.NativeMemory.ThreadAllocations.Value; thread.Allocations += size; Interlocked.Add(ref Sparrow.Utils.NativeMemory._totalAllocatedMemory, size); if (PlatformDetails.RunningOnPosix) { // we pass NULL (IntPtr.Zero) as the first parameter (address / start) so the kernel chooses the(page-aligned) address at which to create the mapping var pageAlignedMemory = Syscall.mmap64(IntPtr.Zero, (UIntPtr)size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_PRIVATE | MmapFlags.MAP_ANONYMOUS, -1, 0L); if (pageAlignedMemory.ToInt64() == -1) { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, $"Could not allocate memory (allocation size: {size / Constants.Size.Kilobyte:#,#0} kb)"); } return((byte *)pageAlignedMemory); } var allocate4KbAlignedMemory = Win32MemoryProtectMethods.VirtualAlloc(null, (UIntPtr)size, Win32MemoryProtectMethods.AllocationType.COMMIT, Win32MemoryProtectMethods.MemoryProtection.READWRITE); if (allocate4KbAlignedMemory == null) { ThrowFailedToAllocate(); } return(allocate4KbAlignedMemory); }
public static void Free4KbAlignedMemory(byte *ptr, long size, Sparrow.Utils.NativeMemory.ThreadStats stats) { Debug.Assert(ptr != null); if (stats != null) { Sparrow.Utils.NativeMemory.UpdateMemoryStatsForThread(stats, size); } Interlocked.Add(ref Sparrow.Utils.NativeMemory._totalAllocatedMemory, -size); var p = new IntPtr(ptr); if (PlatformDetails.RunningOnPosix) { Syscall.free(p); return; } if (Win32MemoryProtectMethods.VirtualFree(ptr, UIntPtr.Zero, Win32MemoryProtectMethods.FreeType.MEM_RELEASE) == false) { ThrowFailedToFree(); } }