1*1fd5a2e1SPrashanth Swaminathan /* Determine alignment of types. 2*1fd5a2e1SPrashanth Swaminathan Copyright (C) 2003-2004, 2006, 2009-2017 Free Software Foundation, Inc. 3*1fd5a2e1SPrashanth Swaminathan 4*1fd5a2e1SPrashanth Swaminathan This program is free software; you can redistribute it and/or modify 5*1fd5a2e1SPrashanth Swaminathan it under the terms of the GNU General Public License as published by 6*1fd5a2e1SPrashanth Swaminathan the Free Software Foundation; either version 2, or (at your option) 7*1fd5a2e1SPrashanth Swaminathan any later version. 8*1fd5a2e1SPrashanth Swaminathan 9*1fd5a2e1SPrashanth Swaminathan This program is distributed in the hope that it will be useful, 10*1fd5a2e1SPrashanth Swaminathan but WITHOUT ANY WARRANTY; without even the implied warranty of 11*1fd5a2e1SPrashanth Swaminathan MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*1fd5a2e1SPrashanth Swaminathan GNU General Public License for more details. 13*1fd5a2e1SPrashanth Swaminathan 14*1fd5a2e1SPrashanth Swaminathan You should have received a copy of the GNU General Public License 15*1fd5a2e1SPrashanth Swaminathan along with this program; if not, see <https://www.gnu.org/licenses/>. */ 16*1fd5a2e1SPrashanth Swaminathan 17*1fd5a2e1SPrashanth Swaminathan #ifndef _ALIGNOF_H 18*1fd5a2e1SPrashanth Swaminathan #define _ALIGNOF_H 19*1fd5a2e1SPrashanth Swaminathan 20*1fd5a2e1SPrashanth Swaminathan #include <stddef.h> 21*1fd5a2e1SPrashanth Swaminathan 22*1fd5a2e1SPrashanth Swaminathan /* alignof_slot (TYPE) 23*1fd5a2e1SPrashanth Swaminathan Determine the alignment of a structure slot (field) of a given type, 24*1fd5a2e1SPrashanth Swaminathan at compile time. Note that the result depends on the ABI. 25*1fd5a2e1SPrashanth Swaminathan This is the same as alignof (TYPE) and _Alignof (TYPE), defined in 26*1fd5a2e1SPrashanth Swaminathan <stdalign.h> if __alignof_is_defined is 1. 27*1fd5a2e1SPrashanth Swaminathan Note: The result cannot be used as a value for an 'enum' constant, 28*1fd5a2e1SPrashanth Swaminathan due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc. */ 29*1fd5a2e1SPrashanth Swaminathan #if defined __cplusplus 30*1fd5a2e1SPrashanth Swaminathan template <class type> struct alignof_helper { char __slot1; type __slot2; }; 31*1fd5a2e1SPrashanth Swaminathan # define alignof_slot(type) offsetof (alignof_helper<type>, __slot2) 32*1fd5a2e1SPrashanth Swaminathan #else 33*1fd5a2e1SPrashanth Swaminathan # define alignof_slot(type) offsetof (struct { char __slot1; type __slot2; }, __slot2) 34*1fd5a2e1SPrashanth Swaminathan #endif 35*1fd5a2e1SPrashanth Swaminathan 36*1fd5a2e1SPrashanth Swaminathan /* alignof_type (TYPE) 37*1fd5a2e1SPrashanth Swaminathan Determine the good alignment of an object of the given type at compile time. 38*1fd5a2e1SPrashanth Swaminathan Note that this is not necessarily the same as alignof_slot(type). 39*1fd5a2e1SPrashanth Swaminathan For example, with GNU C on x86 platforms: alignof_type(double) = 8, but 40*1fd5a2e1SPrashanth Swaminathan - when -malign-double is not specified: alignof_slot(double) = 4, 41*1fd5a2e1SPrashanth Swaminathan - when -malign-double is specified: alignof_slot(double) = 8. 42*1fd5a2e1SPrashanth Swaminathan Note: The result cannot be used as a value for an 'enum' constant, 43*1fd5a2e1SPrashanth Swaminathan due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc. */ 44*1fd5a2e1SPrashanth Swaminathan #if defined __GNUC__ || defined __IBM__ALIGNOF__ 45*1fd5a2e1SPrashanth Swaminathan # define alignof_type __alignof__ 46*1fd5a2e1SPrashanth Swaminathan #else 47*1fd5a2e1SPrashanth Swaminathan # define alignof_type alignof_slot 48*1fd5a2e1SPrashanth Swaminathan #endif 49*1fd5a2e1SPrashanth Swaminathan 50*1fd5a2e1SPrashanth Swaminathan #endif /* _ALIGNOF_H */ 51