Exemple #1
0
        public void VerifyStringPtrSerialization()
        {
            var obj = new StringsPair
            {
                String1 = { Value = "Test123" },
                String2 = { Value = "Test234" },
            };

            int         size       = (int)CodegenTypeExtensions.GetSerializedSize(obj);
            Span <byte> byteBuffer = stackalloc byte[size + 1];

            byteBuffer[size] = (byte)'#';

            unsafe
            {
                fixed(byte *pinnedBuffer = &byteBuffer.GetPinnableReference())
                {
                    IntPtr buffer = new IntPtr(pinnedBuffer);

                    CodegenTypeExtensions.Serialize(obj, buffer);

                    MlosUnitTestProxy.StringsPair proxy = default;
                    proxy.Buffer = buffer;

                    Assert.Equal(obj.String1.Value, proxy.String1.Value);
                    Assert.Equal(obj.String2.Value, proxy.String2.Value);
                }
            }

            Assert.Equal((byte)'#', byteBuffer[size]);
        }
        /// <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);
        }
        /// <summary>
        /// Add a new shared config.
        /// </summary>
        /// <typeparam name="TProbingPolicy">HashTable lookup policy.</typeparam>
        /// <typeparam name="TType">Codegen config type.</typeparam>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigMemoryRegion"></param>
        /// <param name="componentConfig"></param>
        public static void Add <TProbingPolicy, TType, TProxy>(
            this SharedConfigMemoryRegion sharedConfigMemoryRegion,
            ComponentConfig <TType, TProxy> componentConfig)
            where TProbingPolicy : IProbingPolicy
            where TType : ICodegenType, new()
            where TProxy : ICodegenProxy <TType, TProxy>, new()
        {
            uint slotIndex = 0;

            SharedConfig <TProxy> sharedConfig = sharedConfigMemoryRegion.Get <TProbingPolicy, TProxy>(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 = sharedConfigMemoryRegion.Allocate <SharedConfig <TProxy> >();

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

            sharedConfigOffsets[(int)slotIndex] = (uint)sharedConfig.Buffer.Offset(sharedConfigMemoryRegion.Buffer);

            // 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);
        }