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 // See the LICENSE file in the project root for more information.
4 
5 //
6 // Low-level types describing GC object layouts.
7 //
8 
9 // Bits stolen from the sync block index that the GC/HandleTable knows about (currently these are at the same
10 // positions as the mainline runtime but we can change this below when it becomes apparent how Redhawk will
11 // handle sync blocks).
12 #define BIT_SBLK_GC_RESERVE                 0x20000000
13 #define BIT_SBLK_FINALIZER_RUN              0x40000000
14 
15 // The sync block index header (small structure that immediately precedes every object in the GC heap). Only
16 // the GC uses this so far, and only to store a couple of bits of information.
17 class ObjHeader
18 {
19 private:
20 #if defined(BIT64)
21     UInt32   m_uAlignpad;
22 #endif // BIT64
23     UInt32   m_uSyncBlockValue;
24 
25 public:
GetBits()26     UInt32 GetBits() { return m_uSyncBlockValue; }
27     void SetBit(UInt32 uBit);
28     void ClrBit(UInt32 uBit);
SetGCBit()29     void SetGCBit() { m_uSyncBlockValue |= BIT_SBLK_GC_RESERVE; }
ClrGCBit()30     void ClrGCBit() { m_uSyncBlockValue &= ~BIT_SBLK_GC_RESERVE; }
31 };
32 
33 //-------------------------------------------------------------------------------------------------
34 static UIntNative const SYNC_BLOCK_SKEW  = sizeof(void *);
35 
36 class EEType;
37 typedef DPTR(class EEType) PTR_EEType;
38 class MethodTable;
39 
40 //-------------------------------------------------------------------------------------------------
41 class Object
42 {
43     friend class AsmOffsets;
44 
45     PTR_EEType  m_pEEType;
46 public:
get_EEType()47     EEType * get_EEType() const
48         { return m_pEEType; }
get_SafeEEType()49     EEType * get_SafeEEType() const
50         { return dac_cast<PTR_EEType>((dac_cast<TADDR>(m_pEEType)) & ~((UIntNative)3)); }
GetHeader()51     ObjHeader * GetHeader() { return dac_cast<DPTR(ObjHeader)>(dac_cast<TADDR>(this) - SYNC_BLOCK_SKEW); }
52 #ifndef DACCESS_COMPILE
set_EEType(EEType * pEEType)53     void set_EEType(EEType * pEEType)
54         { m_pEEType = pEEType; }
55     void InitEEType(EEType * pEEType);
56 
57     size_t GetSize();
58 #endif
59 
60     //
61     // Adapter methods for GC code so that GC and runtime code can use the same type.
62     // These methods are deprecated -- only use from existing GC code.
63     //
RawGetMethodTable()64     MethodTable * RawGetMethodTable() const
65     {
66         return (MethodTable*)get_EEType();
67     }
GetGCSafeMethodTable()68     MethodTable * GetGCSafeMethodTable() const
69     {
70         return (MethodTable *)get_SafeEEType();
71     }
RawSetMethodTable(MethodTable * pMT)72     void RawSetMethodTable(MethodTable * pMT)
73     {
74         m_pEEType = PTR_EEType((EEType *)pMT);
75     }
76     ////// End adaptor methods
77 };
78 typedef DPTR(Object) PTR_Object;
79 typedef DPTR(PTR_Object) PTR_PTR_Object;
80 
81 //-------------------------------------------------------------------------------------------------
82 static UIntNative const MIN_OBJECT_SIZE  = (2 * sizeof(void*)) + sizeof(ObjHeader);
83 
84 //-------------------------------------------------------------------------------------------------
85 static UIntNative const REFERENCE_SIZE   = sizeof(Object *);
86 
87 //-------------------------------------------------------------------------------------------------
88 class Array : public Object
89 {
90     friend class ArrayBase;
91     friend class AsmOffsets;
92 
93     UInt32       m_Length;
94 #if defined(BIT64)
95     UInt32       m_uAlignpad;
96 #endif // BIT64
97 public:
98     UInt32 GetArrayLength();
99     void InitArrayLength(UInt32 length);
100     void* GetArrayData();
101 };
102 typedef DPTR(Array) PTR_Array;
103 
104 //-------------------------------------------------------------------------------------------------
105 class String : public Object
106 {
107     friend class AsmOffsets;
108     friend class StringConstants;
109 
110     UInt32       m_Length;
111     UInt16       m_FirstChar;
112 };
113 typedef DPTR(String) PTR_String;
114 
115 //-------------------------------------------------------------------------------------------------
116 class StringConstants
117 {
118 public:
119     static UIntNative const ComponentSize = sizeof(((String*)0)->m_FirstChar);
120     static UIntNative const BaseSize = sizeof(ObjHeader) + offsetof(String, m_FirstChar) + ComponentSize;
121 };
122 
123 //-------------------------------------------------------------------------------------------------
124 static UIntNative const STRING_COMPONENT_SIZE = StringConstants::ComponentSize;
125 
126 //-------------------------------------------------------------------------------------------------
127 static UIntNative const STRING_BASE_SIZE = StringConstants::BaseSize;
128