public void ArenaAllocatorTest()
        {
            ArenaAllocator allocator = new ArenaAllocator(1000);

            Assert.AreSame(allocator, Allocator.GetAllocatorByID(allocator.ID));
            Assert.IsTrue(Allocator.IsCached(allocator));

            int *p = allocator.Allocate <int>(4);

            for (int i = 0; i < 4; i++)
            {
                p[i] = i + 1;
            }

            Assert.AreEqual(1, p[0]);
            Assert.AreEqual(2, p[1]);
            Assert.AreEqual(3, p[2]);
            Assert.AreEqual(4, p[3]);

            p = allocator.Reallocate(p, 6);

            Assert.AreEqual(1, p[0]);
            Assert.AreEqual(2, p[1]);
            Assert.AreEqual(3, p[2]);
            Assert.AreEqual(4, p[3]);
            Assert.AreEqual(0, p[4]);
            Assert.AreEqual(0, p[5]);

            allocator.Free(p);
            allocator.Dispose();
            Assert.IsFalse(Allocator.IsCached(allocator));
        }
Пример #2
0
        /// <summary>
        /// Initialize GlobalMemoryRegion.
        /// </summary>
        /// <param name="globalMemoryRegion"></param>
        /// <returns></returns>
        public static GlobalMemoryRegion InitializeMemoryRegion(this GlobalMemoryRegion globalMemoryRegion)
        {
            // Initialize properties.
            //
            globalMemoryRegion.GlobalMemoryRegionIndex = 1;

            globalMemoryRegion.RegisteredSettingsAssemblyCount.Store(1);

            MemoryRegion   memoryHeader   = globalMemoryRegion.MemoryHeader;
            MemoryRegionId memoryRegionId = memoryHeader.MemoryRegionId;

            memoryRegionId.Type = MemoryRegionType.Global;

            SharedConfigDictionary sharedConfigDictionary = globalMemoryRegion.SharedConfigDictionary;
            ArenaAllocator         allocator = sharedConfigDictionary.Allocator;

            // Initialize memory allocator.
            //
            allocator.InitializeArenaAllocator(memoryHeader, (int)globalMemoryRegion.CodegenTypeSize());

            // Initialize shared config dictionary.
            //
            sharedConfigDictionary.InitializeSharedConfigDictionary();

            return(globalMemoryRegion);
        }
Пример #3
0
        private static ArenaAllocator GetAllocator(ref ArenaAllocator alloc, uint pageSize, uint pageCount)
        {
            if (alloc == null)
            {
                alloc = new ArenaAllocator(pageSize, pageCount);
            }

            return(alloc);
        }
Пример #4
0
        /// <summary>
        /// Allocates the memory for the given type in the shared memory region.
        /// </summary>
        /// <typeparam name="T">CodegenProxyType of the allocated object.</typeparam>
        /// <param name="allocator">Allocator instance.</param>
        /// <returns></returns>
        public static T Allocate <T>(this ArenaAllocator allocator)
            where T : ICodegenProxy, new()
        {
            AllocationEntry allocationEntry = allocator.Allocate(default(T).CodegenTypeSize());

            T codegenProxy = new T {
                Buffer = allocationEntry.Buffer + (int)default(AllocationEntry).CodegenTypeSize()
            };

            return(codegenProxy);
        }
Пример #5
0
        /// <summary>
        /// Initializes the arena allocator stored in the memory region.
        /// </summary>
        /// <param name="allocator"></param>
        /// <param name="memoryRegion"></param>
        /// <param name="memoryRegionHeaderSize"></param>
        public static void InitializeArenaAllocator(
            this ArenaAllocator allocator,
            MemoryRegion memoryRegion,
            int memoryRegionHeaderSize)
        {
            // Store offset to the allocator itself.
            //
            allocator.OffsetToAllocator = (int)allocator.Buffer.Offset(memoryRegion.Buffer);

            allocator.EndOffset = (uint)memoryRegion.MemoryRegionSize;

            allocator.FreeOffset      = Utils.Align((uint)memoryRegionHeaderSize, MlosInternal.ArenaAllocator.AllocationAlignment);
            allocator.AllocationCount = 0;
            allocator.LastOffset      = 0;
        }
Пример #6
0
        /// <summary>
        /// Initializes the arena allocator stored in the memory region.
        /// </summary>
        /// <param name="allocator"></param>
        /// <param name="memoryRegion"></param>
        /// <param name="firstAllocationOffset"></param>
        public static void InitializeArenaAllocator(
            this ArenaAllocator allocator,
            MemoryRegion memoryRegion,
            int firstAllocationOffset)
        {
            // Store offset to the allocator itself.
            //
            allocator.OffsetToAllocator = (int)allocator.Buffer.Offset(memoryRegion.Buffer);

            allocator.FirstAllocationOffset = Utils.Align((uint)firstAllocationOffset, 256);
            allocator.AllocationBlockSize   = (uint)memoryRegion.MemoryRegionSize - allocator.FirstAllocationOffset;

            allocator.FreeOffset          = allocator.FirstAllocationOffset;
            allocator.AllocationCount     = 0;
            allocator.LastAllocatedOffset = 0;
        }
Пример #7
0
        /// <summary>
        /// Allocates the memory in the shared memory region.
        /// </summary>
        /// <param name="allocator"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static AllocationEntry Allocate(this ArenaAllocator allocator, ulong size)
        {
            size += default(AllocationEntry).CodegenTypeSize();

            if (allocator.FreeOffset + size >= allocator.AllocationBlockSize + allocator.FirstAllocationOffset)
            {
                throw new OutOfMemoryException();
            }

            // Update the address.
            //
            uint offset = allocator.FreeOffset;

            // Update memory region properties.
            //
            allocator.FreeOffset += (uint)Utils.Align(size, 64);
            allocator.AllocationCount++;

            IntPtr memoryRegionPtr = allocator.Buffer - allocator.OffsetToAllocator;

            // Update last allocated entry.
            //
            if (allocator.LastAllocatedOffset != 0)
            {
                AllocationEntry lastAllocationEntry = new AllocationEntry
                {
                    Buffer = memoryRegionPtr + (int)allocator.LastAllocatedOffset,
                };

                lastAllocationEntry.NextEntryOffset = offset;
            }

            // Update current allocated entry.
            //
            AllocationEntry allocationEntry = new AllocationEntry
            {
                Buffer = memoryRegionPtr + (int)offset,
            };

            allocationEntry.PrevEntryoffset = allocator.LastAllocatedOffset;

            allocator.LastAllocatedOffset = offset;

            return(allocationEntry);
        }
Пример #8
0
        public void Setup()
        {
            int AllocatorSize = Bytes * 2;
            var defaultAlloc  = Allocator.Default;

            if (defaultAlloc == null)
            {
                throw new Exception("Null default allocator");
            }

            heapAllocator      = DefaultHeapAllocator.Instance;
            localAllocator     = DefaultLocalAllocator.Instance;
            cAllocator         = DefaultCppAllocator.Instance;
            arenaAllocator     = new ArenaAllocator(AllocatorSize);
            stackAllocator     = new StackAllocator(AllocatorSize);
            fixedPoolAllocator = new FixedMemoryPoolAllocator(10, AllocatorSize);
            poolAllocator      = new MemoryPoolAllocator(AllocatorSize);
        }
Пример #9
0
        /// <summary>
        /// Initialize SharedConfigMemoryRegion.
        /// </summary>
        /// <param name="sharedConfigMemoryRegion"></param>
        /// <returns></returns>
        public static SharedConfigMemoryRegion InitializeMemoryRegion(this SharedConfigMemoryRegion sharedConfigMemoryRegion)
        {
            SharedConfigDictionary sharedConfigDictionary = sharedConfigMemoryRegion.SharedConfigDictionary;
            ArenaAllocator         allocator = sharedConfigDictionary.Allocator;

            MemoryRegion   memoryHeader   = sharedConfigMemoryRegion.MemoryHeader;
            MemoryRegionId memoryRegionId = memoryHeader.MemoryRegionId;

            memoryRegionId.Type = MemoryRegionType.SharedConfig;

            // Initialize memory allocator.
            //
            allocator.InitializeArenaAllocator(sharedConfigMemoryRegion.MemoryHeader, (int)sharedConfigMemoryRegion.CodegenTypeSize());

            // Initialize shared config dictionary.
            //
            sharedConfigDictionary.InitializeSharedConfigDictionary();

            return(sharedConfigMemoryRegion);
        }
Пример #10
0
        public KMemoryManager(Process Process)
        {
            CpuMemory = Process.Memory;
            Allocator = Process.Device.Memory.Allocator;

            long CodeRegionSize;
            long MapRegionSize;
            long HeapRegionSize;
            long NewMapRegionSize;
            long TlsIoRegionSize;
            int  AddrSpaceWidth;

            AddressSpaceType AddrType = AddressSpaceType.Addr39Bits;

            if (Process.MetaData != null)
            {
                AddrType = (AddressSpaceType)Process.MetaData.AddressSpaceWidth;
            }

            switch (AddrType)
            {
            case AddressSpaceType.Addr32Bits:
                CodeRegionStart  = 0x200000;
                CodeRegionSize   = 0x3fe00000;
                MapRegionSize    = 0x40000000;
                HeapRegionSize   = 0x40000000;
                NewMapRegionSize = 0;
                TlsIoRegionSize  = 0;
                AddrSpaceWidth   = 32;
                break;

            case AddressSpaceType.Addr36Bits:
                CodeRegionStart  = 0x8000000;
                CodeRegionSize   = 0x78000000;
                MapRegionSize    = 0x180000000;
                HeapRegionSize   = 0x180000000;
                NewMapRegionSize = 0;
                TlsIoRegionSize  = 0;
                AddrSpaceWidth   = 36;
                break;

            case AddressSpaceType.Addr36BitsNoMap:
                CodeRegionStart  = 0x200000;
                CodeRegionSize   = 0x3fe00000;
                MapRegionSize    = 0;
                HeapRegionSize   = 0x80000000;
                NewMapRegionSize = 0;
                TlsIoRegionSize  = 0;
                AddrSpaceWidth   = 36;
                break;

            case AddressSpaceType.Addr39Bits:
                CodeRegionStart  = 0x8000000;
                CodeRegionSize   = 0x80000000;
                MapRegionSize    = 0x1000000000;
                HeapRegionSize   = 0x180000000;
                NewMapRegionSize = 0x80000000;
                TlsIoRegionSize  = 0x1000000000;
                AddrSpaceWidth   = 39;
                break;

            default: throw new InvalidOperationException();
            }

            AddrSpaceStart = 0;
            AddrSpaceEnd   = 1L << AddrSpaceWidth;

            CodeRegionEnd     = CodeRegionStart + CodeRegionSize;
            MapRegionStart    = CodeRegionEnd;
            MapRegionEnd      = CodeRegionEnd + MapRegionSize;
            HeapRegionStart   = MapRegionEnd;
            HeapRegionEnd     = MapRegionEnd + HeapRegionSize;
            NewMapRegionStart = HeapRegionEnd;
            NewMapRegionEnd   = HeapRegionEnd + NewMapRegionSize;
            TlsIoRegionStart  = NewMapRegionEnd;
            TlsIoRegionEnd    = NewMapRegionEnd + TlsIoRegionSize;

            CurrentHeapAddr = HeapRegionStart;

            if (NewMapRegionSize == 0)
            {
                NewMapRegionStart = AddrSpaceStart;
                NewMapRegionEnd   = AddrSpaceEnd;
            }

            Blocks = new LinkedList <KMemoryBlock>();

            long AddrSpacePagesCount = (AddrSpaceEnd - AddrSpaceStart) / PageSize;

            InsertBlock(AddrSpaceStart, AddrSpacePagesCount, MemoryState.Unmapped);
        }