File indexing completed on 2024-04-28 05:41:06

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 struct Empty {};
0019 
0020 struct OneByte { char m1; };
0021 
0022 struct PackedNumbers {
0023     int m1;
0024     short m2;
0025     char m3;
0026     unsigned char m4;
0027 };
0028 
0029 struct NonPackedNumbers {
0030     char m1;
0031     short m2;
0032     char m3;
0033     int m4;
0034 };
0035 
0036 struct BitFields {
0037     int m1:1;
0038     int m2:2;
0039     int m3:28;
0040     int m4:1;
0041 };
0042 
0043 struct NonPackedBitFields {
0044     int m1:1;
0045     int m2;
0046     int m3:1;
0047 };
0048 
0049 struct Arrays {
0050     int m1[3];
0051     char m2[4];
0052 };
0053 
0054 struct MultiDimArrays {
0055     double m1[2][3];
0056 };
0057 
0058 struct NonPackedArrays {
0059     char m1[3];
0060     int m2[3];
0061     char m3[1];
0062 };
0063 
0064 struct Unions {
0065     union {
0066         int u1;
0067         char u2;
0068     } m1;
0069     union {
0070         char u1;
0071         short u2;
0072     } m2;
0073 };
0074 
0075 struct ConstMembers {
0076     const int m1 = 42;
0077 };
0078 
0079 struct StaticMembers {
0080     static const int m1 = 42;
0081 };
0082 
0083 struct EmptyInheritance : PackedNumbers {
0084 };
0085 
0086 struct NonEmptyInheritance : PackedNumbers {
0087     short m1;
0088 };
0089 
0090 struct EmptyBaseClassOptimization : Empty {
0091     int m1;
0092     short m2;
0093 };
0094 
0095 struct UnpackedBools {
0096     bool m1;
0097     bool m2[2];
0098     bool m3[3][3];
0099 };
0100 
0101 struct Enums {
0102     enum SimpleEnum { e11, e12, e13, e14 }; // needs 2 bits
0103     enum SimpleFlag { e21 = 0x0, e22 = 0x1, e23 = 0x2, e24 = 0x4, e25 = 0x8 }; // needs 4 bits
0104     enum MixedEnum { e31 = 0x0, e32 = 0x1, e33 = 0x2, e34 = 0x3, e35 = 0x8 }; // needs 3 bits
0105     enum WeirdEnum { e41 = 0x0, e42 = 0xffffffff, e43 = 0xffff, e44 = 0xff }; // needs 3 bits
0106     SimpleEnum m1;
0107     SimpleFlag m2;
0108     MixedEnum m3;
0109     WeirdEnum m4;
0110 };
0111 
0112 int main (int, char**)
0113 {
0114     // make sure the structures aren't optimized away by the compiler
0115     unsigned long long dummy = 0;
0116     #define USED(StructType) { StructType s; dummy += (unsigned long long)&s; }
0117 
0118     USED(Empty)
0119     USED(OneByte)
0120     USED(PackedNumbers)
0121     USED(NonPackedNumbers)
0122     USED(BitFields)
0123     USED(NonPackedBitFields)
0124     USED(Arrays)
0125     USED(MultiDimArrays)
0126     USED(NonPackedArrays)
0127     USED(Unions)
0128     USED(ConstMembers)
0129     USED(StaticMembers)
0130     USED(EmptyInheritance)
0131     USED(NonEmptyInheritance)
0132     USED(EmptyBaseClassOptimization)
0133     USED(UnpackedBools)
0134     USED(Enums)
0135 
0136     return dummy;
0137 }