Esempio n. 1
0
        public static void Free4KbAlignedMemory(byte *ptr, int size, ThreadStats stats)
        {
            Debug.Assert(ptr != null);

            var currentThreadValue = ThreadAllocations.Value;

            if (currentThreadValue == stats)
            {
                currentThreadValue.Allocations -= size;
                FixupReleasesFromOtherThreads(currentThreadValue);
            }
            else
            {
                Interlocked.Add(ref stats.ReleasesFromOtherThreads, 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();
            }
        }
Esempio n. 2
0
            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 void Free(byte *p)
        {
            var remaining         = (int)((long)p % 4096);
            var firstWritablePage = p - remaining;

            for (int i = 0; i < remaining; i++)
            {
                if (firstWritablePage[i] != 0xED)
                {
                    throw new InvalidOperationException("Invalid memory usage, you killed Ed!");
                }
            }
            Win32MemoryProtectMethods.MemoryProtection protect;
            var address = firstWritablePage - 4096;

            // this will access the memory, which will error if this was already freed
            if (Win32MemoryProtectMethods.VirtualProtect(address, (UIntPtr)4096, Win32MemoryProtectMethods.MemoryProtection.READWRITE, out protect) ==
                false)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to VirtualProtect (ElectricFence) at address=" + new IntPtr(address));
            }
            var dwSize = *(int *)address;

            // decommit, not release, they are not available for reuse again, any
            // future access will throw
            if (Win32MemoryProtectMethods.VirtualFree(address, (UIntPtr)dwSize, Win32MemoryProtectMethods.FreeType.MEM_DECOMMIT) == false)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to VirtualFree (ElectricFence) at address=" + new IntPtr(address));
            }
        }
Esempio n. 4
0
 static void FreeMemoryAtEndOfPager(byte *adjacentBlockAddress)
 {
     if (adjacentBlockAddress == null || adjacentBlockAddress == (byte *)0)
     {
         return;
     }
     if (StorageEnvironmentOptions.RunningOnPosix)
     {
         Syscall.munmap(new IntPtr(adjacentBlockAddress), (UIntPtr)16);
         return;
     }
     Win32MemoryProtectMethods.VirtualFree(adjacentBlockAddress, UIntPtr.Zero, Win32MemoryProtectMethods.FreeType.MEM_RELEASE);
 }
Esempio n. 5
0
            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();
                }
            }