This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: aligned attribute and the new operator (pr/15795)


trevor_smigiel@playstation.sony.com wrote:

> I've attached a preliminary patch for the sake of discussion and to
> see if this approach is ok.
> 
> Briefly, the idea is to introduce four new special functions,
>   void* __align_new   (std::size_t size, std::size_t align);
>   void* __align_new_a (std::size_t size, std::size_t align);
>   void __align_delete   (void* ptr, std::size_t align);
>   void __align_delete_a (void* ptr, std::size_t align);
> 
> When a type being operated on with new or delete has alignment greater
> than some target defined value the front end searches for one of the
> special functions and replaces the call. 

I don't understand the motivation for this patch.

First, from C99 7.20.3, malloc is required to return storage that is
aligned sufficiently for any type that fits in the size requested:

The pointer returned if the allocation succeeds is suitably aligned so
that it may be assigned to a pointer to any type of object and then used
to access such an object or an array of such objects in the space
allocated (until the space is explicitly deallocated).

So, you're apparently stuck with a non-conforming implementation of
malloc?  Why can't it just be fixed?

Second, you could replace the default "operator new" and "operator
delete" to call a more-aligned malloc, e.g., posix_memalign.

The only thing I can think of is that you have small type with very
strong alignment requirements (e.g., an 8-byte type with a 256-byte
alignment requirements) and so you want to be able to allocate most 8
byte types without this alignment requirement?  Is that the situation?

Since we're in a C++ context, I would think the right solution is to
create a class wrapper for the type with its own class-specific new and
delete operators?

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]