[GSoC] __enable_shared_from_this_helper

Fan You youfan.noey@gmail.com
Wed Apr 29 02:21:00 GMT 2015


To make sure I understand the question.

Shared_count actually allocate the memory which is exact the same size
as single _Sp_cp_type object by doing this:

> _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, 1);


However, assume we have some usage like this:
>
> auto sp1 = std::experimental::make_shared<int>(5);
>
> auto sp2 = std::experimental::make_shared<int[5]>(5);


Both of them use std::allocator<int> as an allocator. They will have
same behavior when allocate memory (which only allocate single
_Sp_cp_type object.) And it's clearly not right to do something like:

> _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, size);


So, what's the best method to allocate the right size for array type?

2015-04-28 17:53 GMT-04:00 Jonathan Wakely <jwakely@redhat.com>:
> On 28/04/15 22:50 +0100, Jonathan Wakely wrote:
>>
>> On 28/04/15 16:48 -0400, Fan You wrote:
>>>
>>> 2. Also, I am little bit unsure about what _Sp_counted_ptr_inplace does.
>>>
>>> It seems to me that when I call allocate_shared<>(); it construct a
>>> single _Sp_counted_ptr_inplace object.
>
>
> Right.
>
>>> Then, _Sp_counted_ptr_inplace is taking charge of allocating the
>>> needed memory for shared_ptr by using the given allocator.
>
>
> No, all the memory is already allocated by the time the
> _Sp_counted_ptr_inplace's lifetime begins.
>
> _Sp_counted_ptr_inplace contains the shared_count but also a buffer
> where the object will be created.
>
> When you use _Sp_counted_ptr the object is allocated separately, but
> when you use _Sp_counted_ptr_inplace the object lives inside the
> _Sp_counted_ptr_inplace so there is no separate allocation.
>



More information about the Libstdc++ mailing list