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