From: Olivier Hainque Date: Sat, 19 Jul 2008 17:49:18 +0000 (+0000) Subject: tm.texi (MALLOC_ABI_ALIGNMENT): New macro. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=953316147f5c41b3a0822ca63b39ea4ce2d942cd;p=gcc.git tm.texi (MALLOC_ABI_ALIGNMENT): New macro. * doc/tm.texi (MALLOC_ABI_ALIGNMENT): New macro. Alignment, in bits, a C conformant malloc implementation has to provide. * defaults.h (MALLOC_ABI_ALIGNMENT): Default to BITS_PER_WORD. ada/ * targtyps.c (get_target_default_allocator_alignment): Use it. testsuite/ * gcc.dg/mallign.c: New test. * gnat.dg/allocator_maxalign1.adb: New test. * gnat.dg/test_allocator_maxalign2.adb: Main caller for ... * gnat.dg/allocator_maxalign2.ad[bs]: New test. From-SVN: r137984 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4464f1190e7..392ba5876c9 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2008-07-19 Olivier Hainque + + * doc/tm.texi (MALLOC_ABI_ALIGNMENT): New macro. Alignment, in + bits, a C conformant malloc implementation has to provide. + * defaults.h (MALLOC_ABI_ALIGNMENT): Default to BITS_PER_WORD. + 2008-07-19 Joseph Myers PR target/36780 diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index 7b0873a86a4..f08dc9f72d7 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,8 @@ +2008-07-19 Olivier Hainque + + * targtyps.c (get_target_default_allocator_alignment): Use + MALLOC_ABI_ALIGNMENT. + 2008-07-17 Olivier Hainque * adaint.c (__MINGW32__ section): Include ctype.h and define diff --git a/gcc/ada/targtyps.c b/gcc/ada/targtyps.c index 79dafcaf2fe..c4e3299667d 100644 --- a/gcc/ada/targtyps.c +++ b/gcc/ada/targtyps.c @@ -164,17 +164,13 @@ get_target_maximum_default_alignment (void) Stricter alignment requests trigger gigi's aligning_type circuitry for objects allocated by the default allocator. */ -#ifndef MALLOC_ALIGNMENT -#define MALLOC_ALIGNMENT BIGGEST_ALIGNMENT -#endif - Pos get_target_default_allocator_alignment (void) { /* ??? Need a way to get info about __gnat_malloc from here (whether it is handy and what alignment it honors). */ - return MALLOC_ALIGNMENT / BITS_PER_UNIT; + return MALLOC_ABI_ALIGNMENT / BITS_PER_UNIT; } /* Standard'Maximum_Allowed_Alignment. Maximum alignment that we may diff --git a/gcc/defaults.h b/gcc/defaults.h index 3eecd8db81a..80294305850 100644 --- a/gcc/defaults.h +++ b/gcc/defaults.h @@ -551,6 +551,12 @@ along with GCC; see the file COPYING3. If not see #define PUSH_ARGS_REVERSED 0 #endif +/* Default value for the alignment (in bits) a C conformant malloc has to + provide. This default is intended to be safe and always correct. */ +#ifndef MALLOC_ABI_ALIGNMENT +#define MALLOC_ABI_ALIGNMENT BITS_PER_WORD +#endif + /* If PREFERRED_STACK_BOUNDARY is not defined, set it to STACK_BOUNDARY. STACK_BOUNDARY is required. */ #ifndef PREFERRED_STACK_BOUNDARY diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 58600186940..e238797ed77 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -1095,6 +1095,11 @@ bits. Note that this is not the biggest alignment that is supported, just the biggest alignment that, when violated, may cause a fault. @end defmac +@defmac MALLOC_ABI_ALIGNMENT +Alignment, in bits, a C conformant malloc implementation has to +provide. If not defined, the default value is @code{BITS_PER_WORD}. +@end defmac + @defmac MINIMUM_ATOMIC_ALIGNMENT If defined, the smallest alignment, in bits, that can be given to an object that can be referenced in one operation, without disturbing any diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 105edc4ccf4..a487a3e74c9 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2008-07-19 Olivier Hainque + + * gcc.dg/mallign.c: New test. + * gnat.dg/allocator_maxalign1.adb: New test. + * gnat.dg/test_allocator_maxalign2.adb: Main caller for ... + * gnat.dg/allocator_maxalign2.ad[bs]: New test. + 2008-07-19 Tobias Burnus * gfortran.dg/intrinsic_argument_conformance_2.f90: New. diff --git a/gcc/testsuite/gcc.dg/mallign.c b/gcc/testsuite/gcc.dg/mallign.c new file mode 100644 index 00000000000..4a64dbbcc3b --- /dev/null +++ b/gcc/testsuite/gcc.dg/mallign.c @@ -0,0 +1,15 @@ +/* Check that malloc's alignment honors what we trust it + minimally should. */ + +/* { dg-do run } */ +/* { dg-options "-fno-builtin-malloc" } */ + +#include +typedef int word __attribute__((mode(word))); + +int main() +{ + if ((long)malloc (1) & (sizeof(word)-1)) + abort (); + return 0; +} diff --git a/gcc/testsuite/gnat.dg/allocator_maxalign1.adb b/gcc/testsuite/gnat.dg/allocator_maxalign1.adb new file mode 100644 index 00000000000..062c39bbf87 --- /dev/null +++ b/gcc/testsuite/gnat.dg/allocator_maxalign1.adb @@ -0,0 +1,42 @@ +-- { dg-do run } + +with System.Storage_Elements; use System.Storage_Elements; +with Ada.Unchecked_Deallocation; + +procedure Allocator_Maxalign1 is + + Max_Alignment : constant := Standard'Maximum_Alignment; + + type Block is record + X : Integer; + end record; + for Block'Alignment use Standard'Maximum_Alignment; + + type Block_Access is access all Block; + procedure Free is new Ada.Unchecked_Deallocation (Block, Block_Access); + + N_Blocks : constant := 500; + Blocks : array (1 .. N_Blocks) of Block_Access; +begin + if Block'Alignment /= Max_Alignment then + raise Program_Error; + end if; + + for K in 1 .. 4 loop + + for I in Blocks'Range loop + Blocks (I) := new Block; + if Blocks (I).all'Address mod Block'Alignment /= 0 then + raise Program_Error; + end if; + Blocks(I).all.X := I; + end loop; + + for I in Blocks'Range loop + Free (Blocks (I)); + end loop; + + end loop; + +end; + diff --git a/gcc/testsuite/gnat.dg/allocator_maxalign2.adb b/gcc/testsuite/gnat.dg/allocator_maxalign2.adb new file mode 100644 index 00000000000..10644ea6b3c --- /dev/null +++ b/gcc/testsuite/gnat.dg/allocator_maxalign2.adb @@ -0,0 +1,33 @@ +with System, System.Storage_Elements; +use System.Storage_Elements; + +package body Allocator_Maxalign2 is + + Max_Align : constant Storage_Offset := Standard'Maximum_Alignment; + + procedure Validate is + use type System.Address; + begin + if Addr mod Max_Align /= 0 then + raise Program_Error; + end if; + end; + + procedure Check is + I : Integer; + B : Block; + type Block_Access is access all Block; + A : Block_Access; + begin + Addr := I'Address; + Addr := B'Address; + Validate; + for I in 1 .. 50 loop + A := new Block; + Addr := A.all'Address; + Validate; + end loop; + + end; + +end; diff --git a/gcc/testsuite/gnat.dg/allocator_maxalign2.ads b/gcc/testsuite/gnat.dg/allocator_maxalign2.ads new file mode 100644 index 00000000000..43c01081cb6 --- /dev/null +++ b/gcc/testsuite/gnat.dg/allocator_maxalign2.ads @@ -0,0 +1,12 @@ +with System; + +package Allocator_Maxalign2 is + type Block is record + X : Integer; + end record; + for Block'Alignment use Standard'Maximum_Alignment; + + Addr : System.Address; + + procedure Check; +end; diff --git a/gcc/testsuite/gnat.dg/test_allocator_maxalign2.adb b/gcc/testsuite/gnat.dg/test_allocator_maxalign2.adb new file mode 100644 index 00000000000..144914d2dc4 --- /dev/null +++ b/gcc/testsuite/gnat.dg/test_allocator_maxalign2.adb @@ -0,0 +1,8 @@ +-- { dg-do run } + +with Allocator_Maxalign2; + +procedure Test_Allocator_Maxalign2 is +begin + Allocator_Maxalign2.Check; +end;