Example #1
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);
        }
Example #2
0
        /// <summary>
        /// Lookup shared config by codegen key in given shared config dictionary.
        /// </summary>
        /// <typeparam name="TType">Codegen type.</typeparam>
        /// <typeparam name="TKey">Codegen key type.</typeparam>
        /// <typeparam name="TProxy">Codegen proxy.</typeparam>
        /// <param name="sharedConfigDictionary"></param>
        /// <param name="codegenKey"></param>
        /// <returns></returns>
        public static SharedConfig <TProxy> Lookup <TType, TKey, TProxy>(MlosProxyInternal.SharedConfigDictionary sharedConfigDictionary, ICodegenKey <TType, TKey, TProxy> codegenKey)
            where TType : ICodegenType, new()
            where TKey : ICodegenKey, new()
            where TProxy : ICodegenProxy <TType, TProxy>, new()
        {
            uint slotIndex = 0;

            return(MlosProxyInternal.SharedConfigDictionaryLookup <ProbingPolicy> .Get <TProxy>(sharedConfigDictionary, codegenKey, ref slotIndex));
        }
        /// <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;
        }
        /// <summary>
        /// Internal lookup. TProxy type is deduced by the caller.
        /// </summary>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigDictionary"></param>
        /// <param name="codegenKey"></param>
        /// <param name="slotIndex"></param>
        /// <returns></returns>
        internal static SharedConfig <TProxy> Get <TProxy>(
            SharedConfigDictionary sharedConfigDictionary,
            ICodegenKey codegenKey,
            ref uint slotIndex)
            where TProxy : ICodegenProxy, new()
        {
            TProbingPolicy probingPolicy = default;

            uint probingCount = 0;

            slotIndex = 0;

            UIntArray configsArray = sharedConfigDictionary.ConfigsOffsetArray;

            uint elementCount = configsArray.Count;
            ProxyArray <uint> sharedConfigsOffsets = configsArray.Elements;

            SharedConfig <TProxy> sharedConfig = default;

            while (true)
            {
                slotIndex = probingPolicy.CalculateIndex(codegenKey, ref probingCount, elementCount);

                uint offsetToSharedConfig = sharedConfigsOffsets[(int)slotIndex];

                if (offsetToSharedConfig == 0)
                {
                    // Slot entry is empty.
                    //
                    sharedConfig.Buffer = IntPtr.Zero;
                    break;
                }

                // Compare the object keys.
                // Create a proxy to the shared config.
                //
                sharedConfig.Buffer = sharedConfigDictionary.Buffer - sharedConfigDictionary.Allocator.OffsetToAllocator + (int)offsetToSharedConfig;

                // Compare key with the proxy.
                //
                bool foundEntry = codegenKey.CodegenTypeIndex() == sharedConfig.Header.CodegenTypeIndex &&
                                  codegenKey.CompareKey(sharedConfig.Config);
                if (foundEntry)
                {
                    break;
                }

                ++probingCount;
            }

            return(sharedConfig);
        }
        /// <summary>
        /// Add a new shared config.
        /// </summary>
        /// <typeparam name="TType">Codegen config type.</typeparam>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigDictionary"></param>
        /// <param name="componentConfig"></param>
        internal static void Add <TType, TProxy>(
            SharedConfigDictionary sharedConfigDictionary,
            ComponentConfig <TType, TProxy> componentConfig)
            where TType : ICodegenType, new()
            where TProxy : ICodegenProxy <TType, TProxy>, new()
        {
            uint slotIndex = 0;

            SharedConfig <TProxy> sharedConfig = Get <TProxy>(sharedConfigDictionary, componentConfig.Config, ref slotIndex);

            if (sharedConfig.Buffer != IntPtr.Zero)
            {
                throw new ArgumentException("Config already present", nameof(componentConfig));
            }

            TType config = componentConfig.Config;

            // Calculate size to allocate.
            //
            sharedConfig = sharedConfigDictionary.Allocator.Allocate <SharedConfig <TProxy> >();

            // Update hash map
            //
            ProxyArray <uint> sharedConfigOffsets = sharedConfigDictionary.ConfigsOffsetArray.Elements;

            // #TODO verify.
            sharedConfigOffsets[(int)slotIndex] = (uint)sharedConfig.Buffer.Offset(sharedConfigDictionary.Buffer - sharedConfigDictionary.Allocator.OffsetToAllocator);

            // Copy header, copy config.
            //
            SharedConfigHeader sharedHeader = sharedConfig.Header;
            TProxy             configProxy  = sharedConfig.Config;

            // Initialize header.
            //
            sharedHeader.ConfigId.Store(1);
            sharedHeader.CodegenTypeIndex = config.CodegenTypeIndex();

            // Copy the config to proxy.
            //
            CodegenTypeExtensions.Serialize(componentConfig.Config, sharedConfig.Config.Buffer);
        }
Example #6
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);
        }