ClassFieldLayoutCalculator.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include <vector>
  3. #include "MetadataUtil.h"
  4. namespace hybridclr
  5. {
  6. namespace metadata
  7. {
  8. struct FieldLayout
  9. {
  10. const Il2CppType* type;
  11. int32_t offset;
  12. int32_t size;
  13. bool isNormalStatic;
  14. bool isThreadStatic;
  15. };
  16. struct ClassLayoutInfo
  17. {
  18. const Il2CppType* type;
  19. std::vector<FieldLayout> fields;
  20. int32_t instanceSize;
  21. int32_t actualSize;
  22. int32_t nativeSize;
  23. uint32_t staticFieldsSize;
  24. uint32_t threadStaticFieldsSize;
  25. uint8_t alignment;
  26. #if !HYBRIDCLR_UNITY_2022_OR_NEW
  27. uint8_t naturalAlignment;
  28. #endif
  29. };
  30. struct SizeAndAlignment
  31. {
  32. int32_t size;
  33. int32_t nativeSize;
  34. uint8_t alignment;
  35. #if !HYBRIDCLR_UNITY_2022_OR_NEW
  36. uint8_t naturalAlignment;
  37. #endif
  38. };
  39. struct FieldLayoutData
  40. {
  41. std::vector<size_t> FieldOffsets;
  42. int32_t classSize;
  43. int32_t actualClassSize;
  44. int32_t nativeSize;
  45. uint8_t minimumAlignment;
  46. #if !HYBRIDCLR_UNITY_2022_OR_NEW
  47. uint8_t naturalAlignment;
  48. #endif
  49. };
  50. class InterpreterImage;
  51. typedef Il2CppHashMap<const Il2CppType*, ClassLayoutInfo*, Il2CppTypeHash, Il2CppTypeEqualTo> Il2CppType2ClassLayoutInfoMap;
  52. class ClassFieldLayoutCalculator
  53. {
  54. private:
  55. InterpreterImage* _image;
  56. Il2CppType2ClassLayoutInfoMap _classMap;
  57. public:
  58. ClassFieldLayoutCalculator(InterpreterImage* image) : _image(image)
  59. {
  60. }
  61. ~ClassFieldLayoutCalculator()
  62. {
  63. for (auto it : _classMap)
  64. {
  65. ClassLayoutInfo* info = it.second;
  66. info->~ClassLayoutInfo();
  67. HYBRIDCLR_FREE(info);
  68. }
  69. }
  70. ClassLayoutInfo* GetClassLayoutInfo(const Il2CppType* type)
  71. {
  72. auto it = _classMap.find(type);
  73. return it != _classMap.end() ? it->second : nullptr;
  74. }
  75. void CalcClassNotStaticFields(const Il2CppType* type);
  76. void CalcClassStaticFields(const Il2CppType* type);
  77. void LayoutFields(int32_t actualParentSize, int32_t parentAlignment, uint8_t packing, std::vector<FieldLayout*>& fields, FieldLayoutData& data);
  78. SizeAndAlignment GetTypeSizeAndAlignment(const Il2CppType* type);
  79. };
  80. }
  81. }