Exemplo n.º 1
0
        public unsafe MemoryFailPoint(int sizeInMegabytes)
        {
            if (sizeInMegabytes <= 0)
            {
                throw new ArgumentOutOfRangeException("sizeInMegabytes", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            ulong num = (ulong)((ulong)((long)sizeInMegabytes) << 20);

            this._reservedMemory = num;
            ulong num2 = (ulong)(Math.Ceiling(num / MemoryFailPoint.GCSegmentSize) * MemoryFailPoint.GCSegmentSize);

            if (num2 >= MemoryFailPoint.TopOfMemory)
            {
                throw new InsufficientMemoryException(Environment.GetResourceString("InsufficientMemory_MemFailPoint_TooBig"));
            }
            ulong num3 = (ulong)(Math.Ceiling((double)sizeInMegabytes / 16.0) * 16.0);

            num3 <<= 20;
            ulong num4 = 0UL;
            ulong num5 = 0UL;
            int   i    = 0;

            while (i < 3)
            {
                MemoryFailPoint.CheckForAvailableMemory(out num4, out num5);
                ulong memoryFailPointReservedMemory = SharedStatics.MemoryFailPointReservedMemory;
                ulong num6  = num2 + memoryFailPointReservedMemory;
                bool  flag  = num6 < num2 || num6 < memoryFailPointReservedMemory;
                bool  flag2 = num4 < num3 + memoryFailPointReservedMemory + 16777216UL || flag;
                bool  flag3 = num5 < num6 || flag;
                long  num7  = (long)Environment.TickCount;
                if (num7 > MemoryFailPoint.LastTimeCheckingAddressSpace + 10000L || num7 < MemoryFailPoint.LastTimeCheckingAddressSpace || MemoryFailPoint.LastKnownFreeAddressSpace < (long)num2)
                {
                    MemoryFailPoint.CheckForFreeAddressSpace(num2, false);
                }
                bool flag4 = MemoryFailPoint.LastKnownFreeAddressSpace < (long)num2;
                if (!flag2 && !flag3 && !flag4)
                {
                    break;
                }
                switch (i)
                {
                case 0:
                    GC.Collect();
                    break;

                case 1:
                    if (flag2)
                    {
                        RuntimeHelpers.PrepareConstrainedRegions();
                        try
                        {
                            break;
                        }
                        finally
                        {
                            UIntPtr numBytes = new UIntPtr(num2);
                            void *  ptr      = Win32Native.VirtualAlloc(null, numBytes, 4096, 4);
                            if (ptr != null && !Win32Native.VirtualFree(ptr, UIntPtr.Zero, 32768))
                            {
                                __Error.WinIOError();
                            }
                        }
                        goto IL_183;
                    }
                    break;

                case 2:
                    goto IL_183;
                }
IL_1B6:
                i++;
                continue;
IL_183:
                if (flag2 || flag3)
                {
                    InsufficientMemoryException ex = new InsufficientMemoryException(Environment.GetResourceString("InsufficientMemory_MemFailPoint"));
                    throw ex;
                }
                if (flag4)
                {
                    InsufficientMemoryException ex2 = new InsufficientMemoryException(Environment.GetResourceString("InsufficientMemory_MemFailPoint_VAFrag"));
                    throw ex2;
                }
                goto IL_1B6;
            }
            MemoryFailPoint.AddToLastKnownFreeAddressSpace((long)(-(long)num));
            if (MemoryFailPoint.LastKnownFreeAddressSpace < 0L)
            {
                MemoryFailPoint.CheckForFreeAddressSpace(num2, true);
            }
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
            }
            finally
            {
                SharedStatics.AddMemoryFailPointReservation((long)num);
                this._mustSubtractReservation = true;
            }
        }
Exemplo n.º 2
0
        public unsafe MemoryFailPoint(int sizeInMegabytes)
        {
            if (sizeInMegabytes <= 0)
            {
                throw new ArgumentOutOfRangeException("sizeInMegabytes", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            ulong num1 = (ulong)sizeInMegabytes << 20;

            this._reservedMemory = num1;
            ulong size = (ulong)(Math.Ceiling((double)num1 / (double)MemoryFailPoint.GCSegmentSize) * (double)MemoryFailPoint.GCSegmentSize);

            if (size >= MemoryFailPoint.TopOfMemory)
            {
                throw new InsufficientMemoryException(Environment.GetResourceString("InsufficientMemory_MemFailPoint_TooBig"));
            }
            ulong num2                  = (ulong)(Math.Ceiling((double)sizeInMegabytes / 16.0) * 16.0) << 20;
            ulong availPageFile         = 0;
            ulong totalAddressSpaceFree = 0;

            for (int index = 0; index < 3; ++index)
            {
                MemoryFailPoint.CheckForAvailableMemory(out availPageFile, out totalAddressSpaceFree);
                ulong pointReservedMemory = SharedStatics.MemoryFailPointReservedMemory;
                ulong num3  = size + pointReservedMemory;
                bool  flag1 = num3 < size || num3 < pointReservedMemory;
                bool  flag2 = availPageFile < num2 + pointReservedMemory + 16777216UL | flag1;
                bool  flag3 = totalAddressSpaceFree < num3 | flag1;
                long  num4  = (long)Environment.TickCount;
                if (num4 > MemoryFailPoint.LastTimeCheckingAddressSpace + 10000L || num4 < MemoryFailPoint.LastTimeCheckingAddressSpace || MemoryFailPoint.LastKnownFreeAddressSpace < (long)size)
                {
                    MemoryFailPoint.CheckForFreeAddressSpace(size, false);
                }
                bool flag4 = (ulong)MemoryFailPoint.LastKnownFreeAddressSpace < size;
                if (flag2 || flag3 || flag4)
                {
                    switch (index)
                    {
                    case 0:
                        GC.Collect();
                        break;

                    case 1:
                        if (flag2)
                        {
                            RuntimeHelpers.PrepareConstrainedRegions();
                            try
                            {
                            }
                            finally
                            {
                                void *address = Win32Native.VirtualAlloc((void *)null, new UIntPtr(size), 4096, 4);
                                if ((IntPtr)address != IntPtr.Zero && !Win32Native.VirtualFree(address, UIntPtr.Zero, 32768))
                                {
                                    __Error.WinIOError();
                                }
                            }
                            break;
                        }
                        break;

                    case 2:
                        if (flag2 | flag3)
                        {
                            throw new InsufficientMemoryException(Environment.GetResourceString("InsufficientMemory_MemFailPoint"));
                        }
                        if (flag4)
                        {
                            throw new InsufficientMemoryException(Environment.GetResourceString("InsufficientMemory_MemFailPoint_VAFrag"));
                        }
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            MemoryFailPoint.AddToLastKnownFreeAddressSpace(-(long)num1);
            if (MemoryFailPoint.LastKnownFreeAddressSpace < 0L)
            {
                MemoryFailPoint.CheckForFreeAddressSpace(size, true);
            }
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
            }
            finally
            {
                SharedStatics.AddMemoryFailPointReservation((long)num1);
                this._mustSubtractReservation = true;
            }
        }