| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.Immutable; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 9 | | { |
| | | 10 | | public sealed class MetadataAggregator |
| | | 11 | | { |
| | | 12 | | // For each heap handle and each delta contains aggregate heap lengths. |
| | | 13 | | // For GUIDs: heapSizes[heap kind][reader index] == reader[reader index].GuidHeap.Block.Length |
| | | 14 | | // For all other heaps: heapSizes[heap kind][reader index] == Sum { 0..reader index | reader[reader index].XxxHe |
| | | 15 | | private readonly ImmutableArray<ImmutableArray<int>> _heapSizes; |
| | | 16 | | |
| | | 17 | | private readonly ImmutableArray<ImmutableArray<RowCounts>> _rowCounts; |
| | | 18 | | |
| | | 19 | | // internal for testing |
| | | 20 | | internal struct RowCounts : IComparable<RowCounts> |
| | | 21 | | { |
| | | 22 | | public int AggregateInserts; |
| | | 23 | | public int Updates; |
| | | 24 | | |
| | | 25 | | public int CompareTo(RowCounts other) |
| | 0 | 26 | | { |
| | 0 | 27 | | return AggregateInserts - other.AggregateInserts; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public override string ToString() |
| | 0 | 31 | | { |
| | 0 | 32 | | return $"+0x{AggregateInserts:x} ~0x{Updates:x}"; |
| | 0 | 33 | | } |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public MetadataAggregator(MetadataReader baseReader, IReadOnlyList<MetadataReader> deltaReaders) |
| | 0 | 37 | | : this(baseReader, null, null, deltaReaders) |
| | 0 | 38 | | { |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | public MetadataAggregator( |
| | | 42 | | IReadOnlyList<int>? baseTableRowCounts, |
| | | 43 | | IReadOnlyList<int>? baseHeapSizes, |
| | | 44 | | IReadOnlyList<MetadataReader>? deltaReaders) |
| | 0 | 45 | | : this(null, baseTableRowCounts, baseHeapSizes, deltaReaders) |
| | 0 | 46 | | { |
| | 0 | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | private MetadataAggregator( |
| | 0 | 50 | | MetadataReader? baseReader, |
| | 0 | 51 | | IReadOnlyList<int>? baseTableRowCounts, |
| | 0 | 52 | | IReadOnlyList<int>? baseHeapSizes, |
| | 0 | 53 | | IReadOnlyList<MetadataReader>? deltaReaders) |
| | 0 | 54 | | { |
| | 0 | 55 | | if (baseTableRowCounts == null) |
| | 0 | 56 | | { |
| | 0 | 57 | | if (baseReader == null) |
| | 0 | 58 | | { |
| | 0 | 59 | | Throw.ArgumentNull(nameof(baseReader)); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | if (baseReader.GetTableRowCount(TableIndex.EncMap) != 0) |
| | 0 | 63 | | { |
| | 0 | 64 | | throw new ArgumentException(SR.BaseReaderMustBeFullMetadataReader, nameof(baseReader)); |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | CalculateBaseCounts(baseReader, out baseTableRowCounts, out baseHeapSizes); |
| | 0 | 68 | | Debug.Assert(baseTableRowCounts != null); |
| | 0 | 69 | | } |
| | | 70 | | else |
| | 0 | 71 | | { |
| | 0 | 72 | | if (baseTableRowCounts.Count != MetadataTokens.TableCount) |
| | 0 | 73 | | { |
| | 0 | 74 | | throw new ArgumentException(SR.Format(SR.ExpectedListOfSize, MetadataTokens.TableCount), nameof(base |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | if (baseHeapSizes == null) |
| | 0 | 78 | | { |
| | 0 | 79 | | Throw.ArgumentNull(nameof(baseHeapSizes)); |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | if (baseHeapSizes.Count != MetadataTokens.HeapCount) |
| | 0 | 83 | | { |
| | 0 | 84 | | throw new ArgumentException(SR.Format(SR.ExpectedListOfSize, MetadataTokens.HeapCount), nameof(baseT |
| | | 85 | | } |
| | 0 | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | if (deltaReaders == null || deltaReaders.Count == 0) |
| | 0 | 89 | | { |
| | 0 | 90 | | throw new ArgumentException(SR.ExpectedNonEmptyList, nameof(deltaReaders)); |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | for (int i = 0; i < deltaReaders.Count; i++) |
| | 0 | 94 | | { |
| | 0 | 95 | | if (deltaReaders[i].GetTableRowCount(TableIndex.EncMap) == 0 || !deltaReaders[i].IsMinimalDelta) |
| | 0 | 96 | | { |
| | 0 | 97 | | throw new ArgumentException(SR.ReadersMustBeDeltaReaders, nameof(deltaReaders)); |
| | | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | _heapSizes = CalculateHeapSizes(baseHeapSizes, deltaReaders); |
| | 0 | 102 | | _rowCounts = CalculateRowCounts(baseTableRowCounts, deltaReaders); |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | // for testing only |
| | 0 | 106 | | internal MetadataAggregator(RowCounts[][] rowCounts, int[][] heapSizes) |
| | 0 | 107 | | { |
| | 0 | 108 | | _rowCounts = ToImmutable(rowCounts); |
| | 0 | 109 | | _heapSizes = ToImmutable(heapSizes); |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | private static void CalculateBaseCounts( |
| | | 113 | | MetadataReader baseReader, |
| | | 114 | | out IReadOnlyList<int> baseTableRowCounts, |
| | | 115 | | out IReadOnlyList<int> baseHeapSizes) |
| | 0 | 116 | | { |
| | 0 | 117 | | int[] rowCounts = new int[MetadataTokens.TableCount]; |
| | 0 | 118 | | int[] heapSizes = new int[MetadataTokens.HeapCount]; |
| | | 119 | | |
| | 0 | 120 | | for (int i = 0; i < rowCounts.Length; i++) |
| | 0 | 121 | | { |
| | 0 | 122 | | rowCounts[i] = baseReader.GetTableRowCount((TableIndex)i); |
| | 0 | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | for (int i = 0; i < heapSizes.Length; i++) |
| | 0 | 126 | | { |
| | 0 | 127 | | heapSizes[i] = baseReader.GetHeapSize((HeapIndex)i); |
| | 0 | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | baseTableRowCounts = rowCounts; |
| | 0 | 131 | | baseHeapSizes = heapSizes; |
| | 0 | 132 | | } |
| | | 133 | | |
| | | 134 | | private static ImmutableArray<ImmutableArray<int>> CalculateHeapSizes( |
| | | 135 | | IReadOnlyList<int> baseSizes, |
| | | 136 | | IReadOnlyList<MetadataReader> deltaReaders) |
| | 0 | 137 | | { |
| | | 138 | | // GUID heap index is multiple of sizeof(Guid) == 16 |
| | | 139 | | const int guidSize = 16; |
| | 0 | 140 | | int generationCount = 1 + deltaReaders.Count; |
| | | 141 | | |
| | 0 | 142 | | var userStringSizes = new int[generationCount]; |
| | 0 | 143 | | var stringSizes = new int[generationCount]; |
| | 0 | 144 | | var blobSizes = new int[generationCount]; |
| | 0 | 145 | | var guidSizes = new int[generationCount]; |
| | | 146 | | |
| | 0 | 147 | | userStringSizes[0] = baseSizes[(int)HeapIndex.UserString]; |
| | 0 | 148 | | stringSizes[0] = baseSizes[(int)HeapIndex.String]; |
| | 0 | 149 | | blobSizes[0] = baseSizes[(int)HeapIndex.Blob]; |
| | 0 | 150 | | guidSizes[0] = baseSizes[(int)HeapIndex.Guid] / guidSize; |
| | | 151 | | |
| | 0 | 152 | | for (int r = 0; r < deltaReaders.Count; r++) |
| | 0 | 153 | | { |
| | 0 | 154 | | userStringSizes[r + 1] = userStringSizes[r] + deltaReaders[r].GetHeapSize(HeapIndex.UserString); |
| | 0 | 155 | | stringSizes[r + 1] = stringSizes[r] + deltaReaders[r].GetHeapSize(HeapIndex.String); |
| | 0 | 156 | | blobSizes[r + 1] = blobSizes[r] + deltaReaders[r].GetHeapSize(HeapIndex.Blob); |
| | 0 | 157 | | guidSizes[r + 1] = deltaReaders[r].GetHeapSize(HeapIndex.Guid) / guidSize; |
| | 0 | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | return ImmutableArray.Create( |
| | 0 | 161 | | userStringSizes.ToImmutableArray(), |
| | 0 | 162 | | stringSizes.ToImmutableArray(), |
| | 0 | 163 | | blobSizes.ToImmutableArray(), |
| | 0 | 164 | | guidSizes.ToImmutableArray()); |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | private static ImmutableArray<ImmutableArray<RowCounts>> CalculateRowCounts( |
| | | 168 | | IReadOnlyList<int> baseRowCounts, |
| | | 169 | | IReadOnlyList<MetadataReader> deltaReaders) |
| | 0 | 170 | | { |
| | | 171 | | // TODO: optimize - we don't need to allocate all these arrays |
| | 0 | 172 | | var rowCounts = GetBaseRowCounts(baseRowCounts, generations: 1 + deltaReaders.Count); |
| | | 173 | | |
| | 0 | 174 | | for (int generation = 1; generation <= deltaReaders.Count; generation++) |
| | 0 | 175 | | { |
| | 0 | 176 | | CalculateDeltaRowCountsForGeneration(rowCounts, generation, ref deltaReaders[generation - 1].EncMapTable |
| | 0 | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | return ToImmutable(rowCounts); |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | private static ImmutableArray<ImmutableArray<T>> ToImmutable<T>(T[][] array) |
| | 0 | 183 | | { |
| | 0 | 184 | | var immutable = new ImmutableArray<T>[array.Length]; |
| | 0 | 185 | | for (int i = 0; i < array.Length; i++) |
| | 0 | 186 | | { |
| | 0 | 187 | | immutable[i] = array[i].ToImmutableArray(); |
| | 0 | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | return immutable.ToImmutableArray(); |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | // internal for testing |
| | | 194 | | internal static RowCounts[][] GetBaseRowCounts(IReadOnlyList<int> baseRowCounts, int generations) |
| | 0 | 195 | | { |
| | 0 | 196 | | var rowCounts = new RowCounts[MetadataTokens.TableCount][]; |
| | | 197 | | |
| | 0 | 198 | | for (int t = 0; t < rowCounts.Length; t++) |
| | 0 | 199 | | { |
| | 0 | 200 | | rowCounts[t] = new RowCounts[generations]; |
| | 0 | 201 | | rowCounts[t][0].AggregateInserts = baseRowCounts[t]; |
| | 0 | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | return rowCounts; |
| | 0 | 205 | | } |
| | | 206 | | |
| | | 207 | | // internal for testing |
| | | 208 | | internal static void CalculateDeltaRowCountsForGeneration(RowCounts[][] rowCounts, int generation, ref EnCMapTab |
| | 0 | 209 | | { |
| | 0 | 210 | | foreach (var tableRowCounts in rowCounts) |
| | 0 | 211 | | { |
| | 0 | 212 | | tableRowCounts[generation].AggregateInserts = tableRowCounts[generation - 1].AggregateInserts; |
| | 0 | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | int mapRowCount = encMapTable.NumberOfRows; |
| | 0 | 216 | | for (int mapRid = 1; mapRid <= mapRowCount; mapRid++) |
| | 0 | 217 | | { |
| | 0 | 218 | | uint token = encMapTable.GetToken(mapRid); |
| | 0 | 219 | | int rid = (int)(token & TokenTypeIds.RIDMask); |
| | | 220 | | |
| | 0 | 221 | | var tableRowCounts = rowCounts[token >> TokenTypeIds.RowIdBitCount]; |
| | | 222 | | |
| | 0 | 223 | | if (rid > tableRowCounts[generation].AggregateInserts) |
| | 0 | 224 | | { |
| | 0 | 225 | | if (rid != tableRowCounts[generation].AggregateInserts + 1) |
| | 0 | 226 | | { |
| | 0 | 227 | | throw new BadImageFormatException(SR.EnCMapNotSorted); |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | // insert: |
| | 0 | 231 | | tableRowCounts[generation].AggregateInserts = rid; |
| | 0 | 232 | | } |
| | | 233 | | else |
| | 0 | 234 | | { |
| | | 235 | | // update: |
| | 0 | 236 | | tableRowCounts[generation].Updates++; |
| | 0 | 237 | | } |
| | 0 | 238 | | } |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Given a handle of an entity in an aggregate metadata calculates |
| | | 243 | | /// a handle of the entity within the metadata generation it is defined in. |
| | | 244 | | /// </summary> |
| | | 245 | | /// <param name="handle">Handle of an entity in an aggregate metadata.</param> |
| | | 246 | | /// <param name="generation">The generation the entity is defined in.</param> |
| | | 247 | | /// <returns>Handle of the entity within the metadata generation <paramref name="generation"/>.</returns> |
| | | 248 | | public Handle GetGenerationHandle(Handle handle, out int generation) |
| | 0 | 249 | | { |
| | 0 | 250 | | if (handle.IsVirtual) |
| | 0 | 251 | | { |
| | | 252 | | // TODO: if a virtual handle is connected to real handle then translate the rid, |
| | | 253 | | // otherwise return vhandle and base. |
| | 0 | 254 | | throw new NotSupportedException(); |
| | | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | if (handle.IsHeapHandle) |
| | 0 | 258 | | { |
| | 0 | 259 | | int heapOffset = handle.Offset; |
| | | 260 | | |
| | | 261 | | HeapIndex heapIndex; |
| | 0 | 262 | | MetadataTokens.TryGetHeapIndex(handle.Kind, out heapIndex); |
| | | 263 | | |
| | 0 | 264 | | var sizes = _heapSizes[(int)heapIndex]; |
| | | 265 | | |
| | | 266 | | // #Guid heap offset is 1-based, other heaps have 0-based offset: |
| | 0 | 267 | | var size = (handle.Type == HandleType.Guid) ? heapOffset - 1 : heapOffset; |
| | | 268 | | |
| | 0 | 269 | | generation = sizes.BinarySearch(size); |
| | 0 | 270 | | if (generation >= 0) |
| | 0 | 271 | | { |
| | 0 | 272 | | Debug.Assert(sizes[generation] == size); |
| | | 273 | | |
| | | 274 | | // the index points to the start of the next generation that added data to the heap: |
| | | 275 | | do |
| | 0 | 276 | | { |
| | 0 | 277 | | generation++; |
| | 0 | 278 | | } |
| | 0 | 279 | | while (generation < sizes.Length && sizes[generation] == size); |
| | 0 | 280 | | } |
| | | 281 | | else |
| | 0 | 282 | | { |
| | 0 | 283 | | generation = ~generation; |
| | 0 | 284 | | } |
| | | 285 | | |
| | 0 | 286 | | if (generation >= sizes.Length) |
| | 0 | 287 | | { |
| | 0 | 288 | | throw new ArgumentException(SR.HandleBelongsToFutureGeneration, nameof(handle)); |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | // GUID heap accumulates - previous heap is copied to the next generation |
| | 0 | 292 | | int relativeHeapOffset = (handle.Type == HandleType.Guid || generation == 0) ? heapOffset : heapOffset - |
| | | 293 | | |
| | 0 | 294 | | return new Handle((byte)handle.Type, relativeHeapOffset); |
| | | 295 | | } |
| | | 296 | | else |
| | 0 | 297 | | { |
| | 0 | 298 | | int rowId = handle.RowId; |
| | | 299 | | |
| | 0 | 300 | | var sizes = _rowCounts[(int)handle.Type]; |
| | | 301 | | |
| | 0 | 302 | | generation = sizes.BinarySearch(new RowCounts { AggregateInserts = rowId }); |
| | 0 | 303 | | if (generation >= 0) |
| | 0 | 304 | | { |
| | 0 | 305 | | Debug.Assert(sizes[generation].AggregateInserts == rowId); |
| | | 306 | | |
| | | 307 | | // the row is in a generation that inserted exactly one row -- the one that we are looking for; |
| | | 308 | | // or it's in a preceding generation if the current one didn't insert any rows of the kind: |
| | 0 | 309 | | while (generation > 0 && sizes[generation - 1].AggregateInserts == rowId) |
| | 0 | 310 | | { |
| | 0 | 311 | | generation--; |
| | 0 | 312 | | } |
| | 0 | 313 | | } |
| | | 314 | | else |
| | 0 | 315 | | { |
| | | 316 | | // the row is in a generation that inserted multiple new rows: |
| | 0 | 317 | | generation = ~generation; |
| | | 318 | | |
| | 0 | 319 | | if (generation >= sizes.Length) |
| | 0 | 320 | | { |
| | 0 | 321 | | throw new ArgumentException(SR.HandleBelongsToFutureGeneration, nameof(handle)); |
| | | 322 | | } |
| | 0 | 323 | | } |
| | | 324 | | |
| | | 325 | | // In each delta table updates always precede inserts. |
| | 0 | 326 | | int relativeRowId = (generation == 0) ? rowId : |
| | 0 | 327 | | rowId - |
| | 0 | 328 | | sizes[generation - 1].AggregateInserts + |
| | 0 | 329 | | sizes[generation].Updates; |
| | | 330 | | |
| | 0 | 331 | | return new Handle((byte)handle.Type, relativeRowId); |
| | | 332 | | } |
| | 0 | 333 | | } |
| | | 334 | | } |
| | | 335 | | } |
| | | 336 | | |