Example #1
0
        /// <summary>
        /// Initialize SharedConfigMemoryRegion.
        /// </summary>
        /// <param name="sharedConfigMemoryRegion"></param>
        /// <returns></returns>
        public static SharedConfigMemoryRegion InitializeMemoryRegion(this SharedConfigMemoryRegion sharedConfigMemoryRegion)
        {
            // Initialize memory allocator.
            //
            var allocator = sharedConfigMemoryRegion.Allocator;

            allocator.AllocationBlockOffset = Utils.Align((uint)sharedConfigMemoryRegion.CodegenTypeSize(), 256);
            allocator.AllocationBlockSize   = (uint)sharedConfigMemoryRegion.MemoryHeader.MemoryRegionSize - allocator.AllocationBlockOffset;

            allocator.FreeOffset          = allocator.AllocationBlockOffset;
            allocator.AllocationCount     = 0;
            allocator.LastAllocatedOffset = 0;

            // Allocate array for shared config offsets.
            //
            uint            elementCount    = 2048;
            AllocationEntry allocationEntry = sharedConfigMemoryRegion.Allocate(default(UIntArray).CodegenTypeSize() + (sizeof(uint) * elementCount));

            sharedConfigMemoryRegion.ConfigsArrayOffset = (uint)allocationEntry.Buffer.Offset(sharedConfigMemoryRegion.Buffer) + (uint)default(AllocationEntry).CodegenTypeSize();

            UIntArray configsOffsetArray = sharedConfigMemoryRegion.ConfigsOffsetArray;

            configsOffsetArray.Count = elementCount;

            return(sharedConfigMemoryRegion);
        }
        /// <summary>
        /// Initializes the shared config dictionary stored in the memory region.
        /// </summary>
        /// <param name="sharedConfigDictionary"></param>
        public static void InitializeSharedConfigDictionary(this SharedConfigDictionary sharedConfigDictionary)
        {
            uint            elementCount    = 2048;
            AllocationEntry allocationEntry = sharedConfigDictionary.Allocator.Allocate(default(UIntArray).CodegenTypeSize() + (sizeof(uint) * elementCount));

            sharedConfigDictionary.OffsetToConfigsArray = (uint)allocationEntry.Buffer.Offset(sharedConfigDictionary.Buffer) + (uint)default(AllocationEntry).CodegenTypeSize();

            UIntArray configsOffsetArray = sharedConfigDictionary.ConfigsOffsetArray;

            configsOffsetArray.Count = elementCount;
        }
Example #3
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);
        }
        public static T Allocate <T>(this SharedConfigMemoryRegion sharedConfigMemoryRegion)
            where T : ICodegenProxy, new()
        {
            AllocationEntry allocationEntry = sharedConfigMemoryRegion.Allocate(default(T).CodegenTypeSize());

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

            return(codegenProxy);
        }
        public static AllocationEntry Allocate(this SharedConfigMemoryRegion sharedConfigMemoryRegion, ulong size)
        {
            size += default(AllocationEntry).CodegenTypeSize();

            var allocator = sharedConfigMemoryRegion.Allocator;

            if (allocator.FreeOffset + size >= sharedConfigMemoryRegion.MemoryHeader.MemoryRegionSize)
            {
                throw new OutOfMemoryException();
            }

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

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

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

                lastAllocationEntry.NextEntryOffset = offset;
            }

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

            allocationEntry.PrevEntryoffset = allocator.LastAllocatedOffset;

            allocator.LastAllocatedOffset = offset;

            return(allocationEntry);
        }
Example #6
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);
        }