CSPspEmu.Core.Memory.FastPspMemoryUnsafe.AllocMemoryOnce C# (CSharp) Method

AllocMemoryOnce() private method

private AllocMemoryOnce ( ) : void
return void
        private void AllocMemoryOnce()
        {
            if (!AlreadyInitialized)
            {
                AlreadyInitialized = true;

                //Logger.Info("FastPspMemory.AllocMemory");

                ulong[] TryBases;
                if (Platform.Is32Bit)
                {
                    if (Platform.OS == OS.Windows)
                    {
                        TryBases = new ulong[] { 0x31000000, 0x40000000, 0x50000000 };
                    }
                    else
                    {
                        //Logger.Error("Using mmap on linux x86 is known to cause problems");
                        TryBases = new ulong[] { 0xE1000000, 0x31008008, 0x40008000, 0x50008000, 0x31000000, 0x40000000, 0x50000000 };
                    }
                }
                else
                {
                    if (Platform.OS == OS.Windows)
                    {
                        TryBases = new ulong[] { 0xE7000000, 0xE1000000, 0x0012340080000000, 0x00123400A0000000 };
                    }
                    else
                    {
                        TryBases = new ulong[] { 0x2300000000, 0x31000000, 0x40000000, 0x50000000, 0xE1000000 };
                    }
                }

                uint ScratchPadAllocSize = ScratchPadSize * 0x10;
                uint FrameBufferAllocSize = FrameBufferSize;
                uint MainAllocSize = MainSize;

                foreach (var TryBase in TryBases)
                {
                    _Base = (byte*)TryBase;
                    Console.WriteLine("FastPspMemory.AllocMemoryOnce: Trying Base ... 0x{0:X}", TryBase);

                    StaticNullPtr = _Base;
                    Platform.AllocRangeGuard(_Base, _Base + ScratchPadOffset);
                    StaticScratchPadPtr = (byte*)Platform.AllocRange(_Base + ScratchPadOffset, ScratchPadAllocSize);
                    Platform.AllocRangeGuard(_Base + ScratchPadOffset + ScratchPadAllocSize, _Base + FrameBufferOffset);
                    StaticFrameBufferPtr = (byte*)Platform.AllocRange(_Base + FrameBufferOffset, FrameBufferAllocSize);
                    Platform.AllocRangeGuard(_Base + FrameBufferOffset + FrameBufferAllocSize, _Base + MainOffset);
                    StaticMainPtr = (byte*)Platform.AllocRange(_Base + MainOffset, MainAllocSize);

                    if (StaticScratchPadPtr != null && StaticFrameBufferPtr != null && StaticMainPtr != null)
                    {
                        Console.WriteLine("FastPspMemory.AllocMemoryOnce: Found Suitable Base ... 0x{0:X}", TryBase);
                        break;
                    }
                    else
                    {
                        if (StaticScratchPadPtr != null) Platform.Free(StaticScratchPadPtr, ScratchPadAllocSize);
                        if (StaticFrameBufferPtr != null) Platform.Free(StaticFrameBufferPtr, FrameBufferAllocSize);
                        if (StaticMainPtr != null) Platform.Free(StaticMainPtr, MainAllocSize);
                    }
                }

                if (_Base == null || StaticScratchPadPtr == null || StaticFrameBufferPtr == null || StaticMainPtr == null)
                {
                    //Logger.Fatal("Can't allocate virtual memory!");
                    Debug.Fail("Can't allocate virtual memory!");
                    throw (new InvalidOperationException("Can't allocate virtual memory!"));
                }
            }

            NullPtr = StaticNullPtr;
            ScratchPadPtr = StaticScratchPadPtr;
            FrameBufferPtr = StaticFrameBufferPtr;
            MainPtr = StaticMainPtr;
        }