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