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