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