Example #1
0
        private void WriteAlignedBlobHeap(BlobBuilder writer)
        {
            int heapStart = writer.Position;

            // ensure enough space in the buffer:
            writer.SetPosition(writer.Position + _blobHeapSize);

            // Perf consideration: With large heap the following loop may cause a lot of cache misses
            // since the order of entries in _blobs dictionary depends on the hash of the array values,
            // which is not correlated to the heap index. If we observe such issue we should order
            // the entries by heap position before running this loop.
            foreach (var entry in _blobs)
            {
                int heapOffset = entry.Value.HeapPosition;
                var blob       = entry.Key;

                writer.SetPosition(heapStart + heapOffset);
                writer.WriteCompressedInteger((uint)blob.Length);
                writer.WriteBytes(blob);
            }

            Debug.Assert(writer.Length - heapStart == _blobHeapSize);

            // add padding:
            writer.SetPosition(writer.Length);
            writer.WriteBytes(0, BitArithmeticUtilities.Align(_blobHeapSize, 4) - _blobHeapSize);
        }
Example #2
0
        private static BlobBuilder SerializeRecord(byte kind, Action <BlobBuilder> data)
        {
            var cmw = new BlobBuilder();

            cmw.WriteByte(CDI.CdiVersion);
            cmw.WriteByte(kind);
            cmw.WriteByte(0);

            // alignment size (will be patched)
            int alignmentSizeAndLengthPosition = cmw.Position;

            cmw.WriteByte(0);

            // length (will be patched)
            cmw.WriteUInt32(0);

            data(cmw);

            int  length        = cmw.Position;
            int  alignedLength = 4 * ((length + 3) / 4);
            byte alignmentSize = (byte)(alignedLength - length);

            for (int i = 0; i < alignmentSize; i++)
            {
                cmw.WriteByte(0);
            }

            cmw.SetPosition(alignmentSizeAndLengthPosition);
            cmw.WriteByte(alignmentSize);
            cmw.WriteUInt32((uint)alignedLength);

            cmw.SetPosition(length);
            return(cmw);
        }
        private static BlobBuilder SerializeRecord(byte kind, Action<BlobBuilder> data)
        {
            var cmw = new BlobBuilder();
            cmw.WriteByte(CDI.CdiVersion);
            cmw.WriteByte(kind);
            cmw.WriteByte(0);

            // alignment size (will be patched)
            int alignmentSizeAndLengthPosition = cmw.Position;
            cmw.WriteByte(0);

            // length (will be patched)
            cmw.WriteUInt32(0);

            data(cmw);

            int length = cmw.Position;
            int alignedLength = 4 * ((length + 3) / 4);
            byte alignmentSize = (byte)(alignedLength - length);

            for (int i = 0; i < alignmentSize; i++)
            {
                cmw.WriteByte(0);
            }

            cmw.SetPosition(alignmentSizeAndLengthPosition);
            cmw.WriteByte(alignmentSize);
            cmw.WriteUInt32((uint)alignedLength);

            cmw.SetPosition(length);
            return cmw;
        }