C# Class ErrorProne.NET.Utils.StructSizeCalculator

ファイルを表示 Open project: SergeyTeplyakov/ErrorProne.NET

Public Methods

Method Description
ComputeStructSize ( this type, Microsoft.CodeAnalysis.SemanticModel semanticModel ) : int

Computes size of the struct.

This computation is not perfect, but could be good for the first time. Current algorythm was reversed engineered for sequential layout by set of test cases and here it is: CLR tries to pack members based on their size. If next item has larger size, then previous items are aligned to it (with empty space) and new item is aligned in memory: [1][1] // byte, byte => 2 [1 ][ 2 ] // byte, short => 4 [1 ][ 2 ][ 4 ] // byte, short, int => 8 [1 ][8 ] // byte, long => 16 [ 4 ][1][1][ 2 ] // int, byte, byte, short => 8 One caveat for composite fields: For nested composite field the same rule applied recursively:

Private Methods

Method Description
GetSize ( Microsoft.CodeAnalysis.SemanticModel semanticModel, ITypeSymbol type, int &capacity, int &largestFieldSize, int &actualSize ) : void
TryGetPrimitiveSize ( ITypeSymbol type, int &size, int &largestFieldSize ) : bool

Method Details

ComputeStructSize() public static method

Computes size of the struct.
This computation is not perfect, but could be good for the first time. Current algorythm was reversed engineered for sequential layout by set of test cases and here it is: CLR tries to pack members based on their size. If next item has larger size, then previous items are aligned to it (with empty space) and new item is aligned in memory: [1][1] // byte, byte => 2 [1 ][ 2 ] // byte, short => 4 [1 ][ 2 ][ 4 ] // byte, short, int => 8 [1 ][8 ] // byte, long => 16 [ 4 ][1][1][ 2 ] // int, byte, byte, short => 8 One caveat for composite fields: For nested composite field the same rule applied recursively:
public static ComputeStructSize ( this type, Microsoft.CodeAnalysis.SemanticModel semanticModel ) : int
type this
semanticModel Microsoft.CodeAnalysis.SemanticModel
return int