This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [GSoC] __enable_shared_from_this_helper
- From: Fan You <youfan dot noey at gmail dot com>
- To: Jonathan Wakely <jwakely at redhat dot com>
- Cc: Tim Shen <timshen91 at gmail dot com>, "libstdc++" <libstdc++ at gcc dot gnu dot org>
- Date: Tue, 28 Apr 2015 22:21:06 -0400
- Subject: Re: [GSoC] __enable_shared_from_this_helper
- Authentication-results: sourceware.org; auth=none
- References: <CALvpekGVZEMtGN5MKVXrArG5cKJhqDQjsfgjdFZYk0=H=7+V_Q at mail dot gmail dot com> <20150428215024 dot GF3618 at redhat dot com> <20150428215349 dot GG3618 at redhat dot com>
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.
>