New type-based pool allocator code miscompiled due to aliasing issue?

Marc Glisse marc.glisse@inria.fr
Tue Jun 16 19:26:00 GMT 2015


On Tue, 16 Jun 2015, Martin Liška wrote:

> On 06/15/2015 07:31 PM, Marc Glisse wrote:
>> On Mon, 15 Jun 2015, Martin Liška wrote:
>>
>>> Ah, I overlooked that it's not a placement new, but just static casting.
>>> Anyway, if I added:
>>>
>>> cselib_val () {}
>>>
>>> to struct cselib_val and changed the cast to placement new:
>>>  char *ptr = (char *) header;
>>>  return new (ptr) T ();
>>>
>>> I got following compilation error:
>>>
>>> In file included from ../../gcc/alias.c:46:0:
>>> ../../gcc/alloc-pool.h: In instantiation of ‘T* pool_allocator<T>::allocate() [with T = cselib_val]’:
>>> ../../gcc/cselib.h:51:27:   required from here
>>> ../../gcc/alloc-pool.h:416:23: error: no matching function for call to ‘cselib_val::operator new(sizetype, char*&)’
>>>   return new (ptr) T ();
>>>                       ^
>>> In file included from ../../gcc/alias.c:47:0:
>>> ../../gcc/cselib.h:49:16: note: candidate: static void* cselib_val::operator new(size_t)
>>>   inline void *operator new (size_t)
>>>                ^
>>> ../../gcc/cselib.h:49:16: note:   candidate expects 1 argument, 2 provided
>>
>> #include <new>
>
> Hi.
>
> <new> header file is not missing (explicit addition of the file does not help).
> Feel free to play with following patch which should fix cselib.h compilation error.

-  return (T *)(header);
+  return ::new (header) T ();

compiles just fine for me (without touching cselib.h).
(the discussion has moved on so this might not be relevant anymore)

-- 
Marc Glisse



More information about the Gcc-patches mailing list