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