Esempio n. 1
0
        /// <summary>
        /// Copy to given SpanByteAndMemory (header and payload copied to actual span/memory)
        /// </summary>
        /// <param name="dst"></param>
        /// <param name="memoryPool"></param>
        public void CopyWithHeaderTo(ref SpanByteAndMemory dst, MemoryPool <byte> memoryPool)
        {
            if (dst.IsSpanByte)
            {
                if (dst.Length >= TotalSize)
                {
                    dst.Length = TotalSize;
                    var span = dst.SpanByte.AsSpan();

                    fixed(byte *ptr = span)
                    * (int *)ptr = Length;

                    AsReadOnlySpan().CopyTo(span.Slice(sizeof(int)));
                    return;
                }
                dst.ConvertToHeap();
            }

            dst.Memory = memoryPool.Rent(TotalSize);
            dst.Length = TotalSize;

            fixed(byte *ptr = dst.Memory.Memory.Span)
            * (int *)ptr = Length;

            AsReadOnlySpan().CopyTo(dst.Memory.Memory.Span.Slice(sizeof(int)));
        }
Esempio n. 2
0
        /// <summary>
        /// Copy to given SpanByteAndMemory (only payload copied to actual span/memory)
        /// </summary>
        /// <param name="dst"></param>
        /// <param name="memoryPool"></param>
        public void CopyTo(ref SpanByteAndMemory dst, MemoryPool <byte> memoryPool)
        {
            if (dst.IsSpanByte)
            {
                if (dst.Length >= Length)
                {
                    dst.Length = Length;
                    AsReadOnlySpan().CopyTo(dst.SpanByte.AsSpan());
                    return;
                }
                dst.ConvertToHeap();
            }

            dst.Memory = memoryPool.Rent(Length);
            dst.Length = Length;
            AsReadOnlySpan().CopyTo(dst.Memory.Memory.Span);
        }