This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch]: new returns misaligned pointers
- To: gcc-patches at gcc dot gnu dot org
- Subject: [patch]: new returns misaligned pointers
- From: Diego Novillo <dnovillo at cygnus dot com>
- Date: Fri, 12 Nov 1999 11:31:29 -0700 (MST)
On some ports, malloc() returns pointers aligned to 16-byte
boundaries. When `new' is used to allocate an array of objects
for a class with explicit destructors, build_new_1() creates a
cookie header for the newly created pointer and moves the
original pointer to point immediately after this header.
The problem is that this cookie header is created with
double_type_node alignment which is 8, not 16. Therefore, the
actual pointer returned by `new' is misaligned.
The patch below addresses this problem. It aligns the cookie
header to BIGGEST_ALIGNMENT to avoid returning misaligned
pointers with `new'.
Thanks.
Tue Nov 9 17:43:28 MST 1999 Diego Novillo <dnovillo@cygnus.com>
* init.c (init_init_processing): Header information for
arrays allocated via `new' should have the same alignment used by
malloc.
Index: init.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/init.c,v
retrieving revision 1.149
diff -c -3 -p -r1.149 init.c
*** init.c 1999/11/12 17:45:10 1.149
--- init.c 1999/11/12 18:29:27
*************** void init_init_processing ()
*** 66,73 ****
BI_header_type = make_lang_type (RECORD_TYPE);
nelts_identifier = get_identifier ("nelts");
fields[0] = build_lang_decl (FIELD_DECL, nelts_identifier, sizetype);
finish_builtin_type (BI_header_type, "__new_cookie", fields,
! 0, double_type_node);
BI_header_size = size_in_bytes (BI_header_type);
ggc_add_tree_root (&BI_header_type, 1);
--- 66,77 ----
BI_header_type = make_lang_type (RECORD_TYPE);
nelts_identifier = get_identifier ("nelts");
fields[0] = build_lang_decl (FIELD_DECL, nelts_identifier, sizetype);
+
+ /* Use the biggest alignment supported by the target to prevent operator
+ new from returning misaligned pointers. */
+ TYPE_ALIGN (BI_header_type) = BIGGEST_ALIGNMENT;
finish_builtin_type (BI_header_type, "__new_cookie", fields,
! 0, BI_header_type);
BI_header_size = size_in_bytes (BI_header_type);
ggc_add_tree_root (&BI_header_type, 1);