|
From: <sv...@va...> - 2012-02-26 17:04:36
|
Author: florian
Date: 2012-02-26 17:00:03 +0000 (Sun, 26 Feb 2012)
New Revision: 2263
Log:
Do not assume that a pointer is the worst-aligned data type.
Fixes #283671
Modified:
trunk/pub/libvex.h
Modified: trunk/pub/libvex.h
===================================================================
--- trunk/pub/libvex.h 2012-02-24 12:16:11 UTC (rev 2262)
+++ trunk/pub/libvex.h 2012-02-26 17:00:03 UTC (rev 2263)
@@ -345,6 +345,24 @@
static inline void* LibVEX_Alloc ( Int nbytes )
{
+ struct {
+ char c;
+ union {
+ char c;
+ short s;
+ int i;
+ long l;
+ long long ll;
+ float f;
+ double d;
+ /* long double is currently not used and would increase alignment
+ unnecessarily. */
+ /* long double ld; */
+ void *pto;
+ void (*ptf)(void);
+ } x;
+ } s;
+
#if 0
/* Nasty debugging hack, do not use. */
return malloc(nbytes);
@@ -352,7 +370,7 @@
HChar* curr;
HChar* next;
Int ALIGN;
- ALIGN = sizeof(void*)-1;
+ ALIGN = ((Int) ((UChar *)&s.x - (UChar *)&s)) - 1;
nbytes = (nbytes + ALIGN) & ~ALIGN;
curr = private_LibVEX_alloc_curr;
next = curr + nbytes;
|
|
From: Bart V. A. <bva...@ac...> - 2012-02-26 17:54:26
|
On Sun, Feb 26, 2012 at 5:00 PM, <sv...@va...> wrote: > @@ -352,7 +370,7 @@ > HChar* curr; > HChar* next; > Int ALIGN; > - ALIGN = sizeof(void*)-1; > + ALIGN = ((Int) ((UChar *)&s.x - (UChar *)&s)) - 1; > nbytes = (nbytes + ALIGN) & ~ALIGN; > curr = private_LibVEX_alloc_curr; > next = curr + nbytes; > Maybe it's a good idea to introduce a VEX_OFFSETOF() macro in order to improve readability of the above statement ? An additional benefit is that doing so would allow to remove the stack variable "s" again - at least if a name would be assigned to the structure definition. Bart. |
|
From: Florian K. <br...@ac...> - 2012-02-26 20:39:43
|
On 02/26/2012 12:54 PM, Bart Van Assche wrote: > On Sun, Feb 26, 2012 at 5:00 PM, <sv...@va...> wrote: > >> @@ -352,7 +370,7 @@ >> HChar* curr; >> HChar* next; >> Int ALIGN; >> - ALIGN = sizeof(void*)-1; >> + ALIGN = ((Int) ((UChar *)&s.x - (UChar *)&s)) - 1; >> nbytes = (nbytes + ALIGN) & ~ALIGN; >> curr = private_LibVEX_alloc_curr; >> next = curr + nbytes; >> > > Maybe it's a good idea to introduce a VEX_OFFSETOF() macro in order to > improve readability of the above statement ? Would there be other uses for VEX_OFFSETOF elsewhere? Then it might be worth doing. The whole thing is a bit ugly.... > An additional benefit is that > doing so would allow to remove the stack variable "s" again - at least if a > name would be assigned to the structure definition. > The compiler will throw out the variable as its value is not used. Florian |
|
From: Philippe W. <phi...@sk...> - 2012-02-26 22:35:37
|
On Sun, 2012-02-26 at 15:39 -0500, Florian Krohm wrote: > On 02/26/2012 12:54 PM, Bart Van Assche wrote: > > Maybe it's a good idea to introduce a VEX_OFFSETOF() macro in order to > > improve readability of the above statement ? > > Would there be other uses for VEX_OFFSETOF elsewhere? Then it might be > worth doing. The whole thing is a bit ugly.... There are already so many uses of offset of that it is already defined :). See e.g. coregrind/m_translate.c various usage of 'offsetof'. Philippe |
|
From: Florian K. <br...@ac...> - 2012-02-27 02:12:09
|
On 02/26/2012 05:35 PM, Philippe Waroquiers wrote: > On Sun, 2012-02-26 at 15:39 -0500, Florian Krohm wrote: >> On 02/26/2012 12:54 PM, Bart Van Assche wrote: >>> Maybe it's a good idea to introduce a VEX_OFFSETOF() macro in order to >>> improve readability of the above statement ? >> >> Would there be other uses for VEX_OFFSETOF elsewhere? Then it might be >> worth doing. The whole thing is a bit ugly.... > > There are already so many uses of offset of that it is already > defined :). > Yeah, I had seen all those uses of offsetof but did not realise it is actually defined in libvex_basictypes.h. I'll fix it. Florian |