]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Add a vg_alignof macro
authorPhilippe Waroquiers <philippe.waroquiers@skynet.be>
Fri, 18 Jan 2013 06:15:44 +0000 (06:15 +0000)
committerPhilippe Waroquiers <philippe.waroquiers@skynet.be>
Fri, 18 Jan 2013 06:15:44 +0000 (06:15 +0000)
Note that this macro behaves slightly differently for some types
from the gcc __alignof__ and from the equivalent (new) standardised
alignof.

vg_alignof macro is needed for the "perm_malloc" callers (next commit)
to determine the alignment of small blocks, but might be useful
for other purposes => placed in libvex_basictypes.h, close to offsetof.

git-svn-id: svn://svn.valgrind.org/vex/trunk@2643

VEX/pub/libvex_basictypes.h

index 9f60f19eff3548127740205a98fd4475bcededfb..110ca231e42178e22bc57daf6fa066e3a00b23c8 100644 (file)
@@ -127,6 +127,30 @@ typedef  unsigned long HWord;
 #if !defined(offsetof)
 #   define offsetof(type,memb) ((Int)(HWord)&((type*)0)->memb)
 #endif
+/* Our definition of offsetof is giving the same result as
+   the standard/predefined offsetof. So, we use the same name.
+   We use a prefix vg_ for vg_alignof as its behaviour slightly
+   differs from the standard alignof/gcc defined __alignof__
+*/
+
+#define vg_alignof(_type) (sizeof(struct {char c;_type _t;})-sizeof(_type))
+/* vg_alignof returns a "safe" alignement.
+   "safe" is defined as the alignment chosen by the compiler in
+   a struct made of a char followed by this type.
+
+      Note that this is not necessarily the "preferred" alignment
+      for a platform. This preferred alignment is returned by the gcc
+       __alignof__ and by the standard (in recent standard) alignof.
+      Compared to __alignof__, vg_alignof gives on some platforms (e.g.
+      amd64, ppc32, ppc64) a bigger alignment for long double (16 bytes
+      instead of 8).
+      On some platforms (e.g. x86), vg_alignof gives a smaller alignment
+      than __alignof__ for long long and double (4 bytes instead of 8). 
+      If we want to have the "preferred" alignment for the basic types,
+      then either we need to depend on gcc __alignof__, or on a (too)
+      recent standard and compiler (implementing <stdalign.h>).
+*/
+
 
 
 /* We need to know the host word size in order to write Ptr_to_ULong