Exemple #1
0
        /// <summary>
        /// Gets the <see cref="HeapIndex"/> of the heap corresponding to the specified <see cref="HandleKind"/>.
        /// </summary>
        /// <param name="type">Handle type.</param>
        /// <param name="index">Heap index.</param>
        /// <returns>True if the handle type corresponds to an Ecma335 heap, false otherwise.</returns>
        public static bool TryGetHeapIndex(HandleKind type, out HeapIndex index)
        {
            switch (type)
            {
            case HandleKind.UserString:
                index = HeapIndex.UserString;
                return(true);

            case HandleKind.String:
            case HandleKind.NamespaceDefinition:
                index = HeapIndex.String;
                return(true);

            case HandleKind.Blob:
                index = HeapIndex.Blob;
                return(true);

            case HandleKind.Guid:
                index = HeapIndex.Guid;
                return(true);

            default:
                index = 0;
                return(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// Sets the capacity of the specified table.
        /// </summary>
        /// <param name="heap">Heap index.</param>
        /// <param name="byteCount">Number of bytes.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heap"/> is not a valid heap index.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="byteCount"/> is negative.</exception>
        /// <remarks>
        /// Use to reduce allocations if the approximate number of bytes is known ahead of time.
        /// </remarks>
        public void SetCapacity(HeapIndex heap, int byteCount)
        {
            if (byteCount < 0)
            {
                Throw.ArgumentOutOfRange(nameof(byteCount));
            }

            switch (heap)
            {
            case HeapIndex.Blob:
                // Not useful to set capacity.
                // By the time the blob heap is serialized we know the exact size we need.
                break;

            case HeapIndex.Guid:
                _guidBuilder.SetCapacity(byteCount);
                break;

            case HeapIndex.String:
                _stringBuilder.SetCapacity(byteCount);
                break;

            case HeapIndex.UserString:
                _userStringBuilder.SetCapacity(byteCount);
                break;

            default:
                Throw.ArgumentOutOfRange(nameof(heap));
                break;
            }
        }
        /// <summary>
        /// Returns the offset from the start of metadata to the specified heap.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heapIndex"/> is not a valid heap index.</exception>
        public static unsafe int GetHeapMetadataOffset(this MetadataReader reader, HeapIndex heapIndex)
        {
            if (reader == null)
            {
                Throw.ArgumentNull(nameof(reader));
            }

            return((int)(reader.GetMetadataBlock(heapIndex).Pointer - reader.Block.Pointer));
        }
Exemple #4
0
        /// <summary>
        /// Returns the offset from the start of metadata to the specified heap.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heapIndex"/> is not a valid heap index.</exception>
        public static unsafe int GetHeapMetadataOffset(this MetadataReader reader, HeapIndex heapIndex)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            return((int)(reader.GetMetadataBlock(heapIndex).Pointer - reader.Block.Pointer));
        }
        /// <summary>
        /// Returns the size of the specified heap.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heapIndex"/> is not a valid heap index.</exception>
        public static int GetHeapSize(this MetadataReader reader, HeapIndex heapIndex)
        {
            if (reader == null)
            {
                Throw.ArgumentNull(nameof(reader));
            }

            return(reader.GetMetadataBlock(heapIndex).Length);
        }
Exemple #6
0
        /// <summary>
        /// Returns the size of the specified heap.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heapIndex"/> is not a valid heap index.</exception>
        public static int GetHeapSize(this MetadataReader reader, HeapIndex heapIndex)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            return(reader.GetMetadataBlock(heapIndex).Length);
        }
Exemple #7
0
        /// <summary>
        /// Returns aligned size of the specified heap.
        /// </summary>
        public int GetAlignedHeapSize(HeapIndex index)
        {
            int i = (int)index;

            if (i < 0 || i > HeapSizes.Length)
            {
                Throw.ArgumentOutOfRange(nameof(index));
            }

            return(BitArithmetic.Align(HeapSizes[i], StreamAlignment));
        }
Exemple #8
0
        /// <summary>
        /// Returns the size of the specified heap.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heapIndex"/> is not a valid heap index.</exception>
        private static MemoryBlock GetMetadataBlock(this MetadataReader reader, HeapIndex heapIndex)
        {
            Debug.Assert(reader != null);

            switch (heapIndex)
            {
            case HeapIndex.UserString:
                return(reader.UserStringStream.Block);

            case HeapIndex.String:
                return(reader.StringStream.Block);

            case HeapIndex.Blob:
                return(reader.BlobStream.Block);

            case HeapIndex.Guid:
                return(reader.GuidStream.Block);

            default:
                throw new ArgumentOutOfRangeException("heapIndex");
            }
        }
Exemple #9
0
 internal static void HeapSizeLimitExceeded(HeapIndex heap)
 {
     throw new ImageFormatLimitationException(StarkPlatform.Reflection.Resources.SR.Format(SR.HeapSizeLimitExceeded, heap));
 }
Exemple #10
0
 internal static void ThrowHeapSizeLimitExceeded(HeapIndex heapIndex)
 {
     // TODO: localize
     throw new ImageFormatLimitationException($"The limit on the size of #{heapIndex} heap has been exceeded.");
 }
Exemple #11
0
        /// <summary>
        /// Gets the <see cref="HeapIndex"/> of the heap corresponding to the specified <see cref="HandleKind"/>.
        /// </summary>
        /// <param name="type">Handle type.</param>
        /// <param name="index">Heap index.</param>
        /// <returns>True if the handle type corresponds to an Ecma335 heap, false otherwise.</returns>
        public static bool TryGetHeapIndex(HandleKind type, out HeapIndex index)
        {
            switch (type)
            {
                case HandleKind.UserString:
                    index = HeapIndex.UserString;
                    return true;

                case HandleKind.String:
                case HandleKind.NamespaceDefinition:
                    index = HeapIndex.String;
                    return true;

                case HandleKind.Blob:
                    index = HeapIndex.Blob;
                    return true;

                case HandleKind.Guid:
                    index = HeapIndex.Guid;
                    return true;

                default:
                    index = 0;
                    return false;
            }
        }
Exemple #12
0
 public int GetAlignedHeapSize(HeapIndex index)
 {
     return(BitArithmeticUtilities.Align(HeapSizes[(int)index], StreamAlignment));
 }
        /// <summary>
        /// Returns the size of the specified heap.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heapIndex"/> is not a valid heap index.</exception>
        private static MemoryBlock GetMetadataBlock(this MetadataReader reader, HeapIndex heapIndex)
        {
            Debug.Assert(reader != null);

            switch (heapIndex)
            {
                case HeapIndex.UserString:
                    return reader.UserStringHeap.Block;

                case HeapIndex.String:
                    return reader.StringHeap.Block;

                case HeapIndex.Blob:
                    return reader.BlobHeap.Block;

                case HeapIndex.Guid:
                    return reader.GuidHeap.Block;

                default:
                    throw new ArgumentOutOfRangeException(nameof(heapIndex));
            }
        }
Exemple #14
0
 internal static void HeapSizeLimitExceeded(HeapIndex heap)
 {
     throw new ImageFormatLimitationException(SR.Format(SR.HeapSizeLimitExceeded, heap));
 }
        /// <summary>
        /// Returns the size of the specified heap.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heapIndex"/> is not a valid heap index.</exception>
        public static int GetHeapSize(this MetadataReader reader, HeapIndex heapIndex)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            return reader.GetMetadataBlock(heapIndex).Length;
        }
Exemple #16
0
 internal static ImageFormatLimitationException HeapSizeLimitExceeded(HeapIndex heapIndex)
 {
     return(new ImageFormatLimitationException(SR.Format(SR.HeapSizeLimitExceeded, heapIndex)));
 }
Exemple #17
0
        /// <summary>
        /// Returns aligned size of the specified heap.
        /// </summary>
        public int GetAlignedHeapSize(HeapIndex index)
        {
            int i = (int)index;
            if (i < 0 || i > HeapSizes.Length)
            {
                Throw.ArgumentOutOfRange(nameof(index));
            }

            return BitArithmetic.Align(HeapSizes[i], StreamAlignment);
        }
 internal static ImageFormatLimitationException HeapSizeLimitExceeded(HeapIndex heapIndex)
 {
     return new ImageFormatLimitationException(SR.Format(SR.HeapSizeLimitExceeded, heapIndex));
 }
        /// <summary>
        /// Returns the offset from the start of metadata to the specified heap.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heapIndex"/> is not a valid heap index.</exception>
        public static unsafe int GetHeapMetadataOffset(this MetadataReader reader, HeapIndex heapIndex)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            return (int)(reader.GetMetadataBlock(heapIndex).Pointer - reader.Block.Pointer);
        }
 public int GetAlignedHeapSize(HeapIndex index)
 {
     return Align(HeapSizes[(int)index], StreamAlignment);
 }
Exemple #21
0
 internal static void HeapSizeLimitExceeded(HeapIndex heap)
 {
     throw new ImageFormatLimitationException(SR.Format(SR.HeapSizeLimitExceeded, heap));
 }
        /// <summary>
        /// Returns the size of the specified heap.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heapIndex"/> is not a valid heap index.</exception>
        public static int GetHeapSize(this MetadataReader reader, HeapIndex heapIndex)
        {
            if (reader == null)
            {
                Throw.ArgumentNull(nameof(reader));
            }

            return reader.GetMetadataBlock(heapIndex).Length;
        }
        /// <summary>
        /// Returns the offset from the start of metadata to the specified heap.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="heapIndex"/> is not a valid heap index.</exception>
        public static unsafe int GetHeapMetadataOffset(this MetadataReader reader, HeapIndex heapIndex)
        {
            if (reader == null)
            {
                Throw.ArgumentNull(nameof(reader));
            }

            return (int)(reader.GetMetadataBlock(heapIndex).Pointer - reader.Block.Pointer);
        }
 public int GetAlignedHeapSize(HeapIndex index)
 {
     return BitArithmeticUtilities.Align(HeapSizes[(int)index], StreamAlignment);
 }
Exemple #25
0
 public int GetAlignedHeapSize(HeapIndex index)
 {
     return(Align(HeapSizes[(int)index], StreamAlignment));
 }