1 // 2 // This tests that the alignment and size of a class are the same whether 3 // they have a `T*` or a shared pointer data member. 4 // 5 6 #include <libkern/c++/intrusive_shared_ptr.h> 7 #include "test_policy.h" 8 #include <cstddef> 9 #include <darwintest.h> 10 11 12 namespace ns1 { 13 struct FooShared { 14 test_shared_ptr<int> ptr; 15 }; 16 17 struct FooRaw { 18 int* ptr; 19 }; 20 21 static_assert(sizeof(FooShared) == sizeof(FooRaw)); 22 static_assert(alignof(FooShared) == alignof(FooRaw)); 23 static_assert(offsetof(FooShared, ptr) == offsetof(FooRaw, ptr)); 24 } 25 26 namespace ns2 { 27 struct FooShared { 28 int i; 29 test_shared_ptr<int> ptr; 30 }; 31 32 struct FooRaw { 33 int i; 34 int* ptr; 35 }; 36 37 static_assert(sizeof(FooShared) == sizeof(FooRaw)); 38 static_assert(alignof(FooShared) == alignof(FooRaw)); 39 static_assert(offsetof(FooShared, ptr) == offsetof(FooRaw, ptr)); 40 } 41 42 namespace ns3 { 43 struct FooShared { 44 char c; 45 test_shared_ptr<int> ptr; 46 int i; 47 }; 48 49 struct FooRaw { 50 char c; 51 int* ptr; 52 int i; 53 }; 54 55 static_assert(sizeof(FooShared) == sizeof(FooRaw)); 56 static_assert(alignof(FooShared) == alignof(FooRaw)); 57 static_assert(offsetof(FooShared, ptr) == offsetof(FooRaw, ptr)); 58 } 59 60 namespace ns4 { 61 struct FooShared { 62 char c; 63 unsigned int b : 5; 64 test_shared_ptr<int> ptr; 65 int i; 66 }; 67 68 struct FooRaw { 69 char c; 70 unsigned int b : 5; 71 int* ptr; 72 int i; 73 }; 74 75 static_assert(sizeof(FooShared) == sizeof(FooRaw)); 76 static_assert(alignof(FooShared) == alignof(FooRaw)); 77 static_assert(offsetof(FooShared, ptr) == offsetof(FooRaw, ptr)); 78 } 79 80 namespace ns5 { 81 struct __attribute__((packed)) FooShared { 82 char c; 83 unsigned int b : 5; 84 test_shared_ptr<int> ptr; 85 int i; 86 }; 87 88 struct __attribute__((packed)) FooRaw { 89 char c; 90 unsigned int b : 5; 91 int* ptr; 92 int i; 93 }; 94 95 static_assert(sizeof(FooShared) == sizeof(FooRaw)); 96 static_assert(alignof(FooShared) == alignof(FooRaw)); 97 static_assert(offsetof(FooShared, ptr) == offsetof(FooRaw, ptr)); 98 } 99 100 namespace ns6 { 101 struct FooShared { 102 char c; 103 unsigned int b : 5; 104 test_shared_ptr<int> ptr; 105 int i __attribute__((packed)); 106 }; 107 108 struct FooRaw { 109 char c; 110 unsigned int b : 5; 111 int* ptr; 112 int i __attribute__((packed)); 113 }; 114 115 static_assert(sizeof(FooShared) == sizeof(FooRaw)); 116 static_assert(alignof(FooShared) == alignof(FooRaw)); 117 static_assert(offsetof(FooShared, ptr) == offsetof(FooRaw, ptr)); 118 } 119 120 namespace ns7 { 121 struct FooShared { 122 char c; 123 unsigned int b : 5; 124 test_shared_ptr<int> ptr __attribute__((packed)); 125 int i; 126 }; 127 128 struct FooRaw { 129 char c; 130 unsigned int b : 5; 131 int* ptr __attribute__((packed)); 132 int i; 133 }; 134 135 static_assert(sizeof(FooShared) == sizeof(FooRaw)); 136 static_assert(alignof(FooShared) == alignof(FooRaw)); 137 static_assert(offsetof(FooShared, ptr) == offsetof(FooRaw, ptr)); 138 } 139 140 T_DECL(abi_size_alignment, "intrusive_shared_ptr.abi.size_alignment") { 141 T_PASS("intrusive_shared_ptr.abi.size_alignment compile-time tests passed"); 142 } 143