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: Tim Shen <timshen at google dot com>
- To: Fan You <youfan dot noey at gmail dot com>
- Cc: Jonathan Wakely <jwakely at redhat dot com>, Tim Shen <timshen91 at gmail dot com>, "libstdc++" <libstdc++ at gcc dot gnu dot org>
- Date: Thu, 30 Apr 2015 23:31:37 -0700
- 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> <CALvpekGhEJfEWdHmtOyco-Ua=sPjk4yhpFFE7Fzffy9cTmnVbA at mail dot gmail dot com> <CAG4ZjN=a9nvQ3CgqRiSze-fbUvh0u69WQQdehS5+mFXsvtSP=A at mail dot gmail dot com> <20150429100344 dot GH3618 at redhat dot com> <CAG4ZjNnFTOtEoSPdzv9Dip8s7O6o4ToL2+N-j_oEuxvrD1spLA at mail dot gmail dot com> <20150430112352 dot GR3618 at redhat dot com> <CAG4ZjNmgtF-7jE17d=e8fK_iMhkDHT783Qv1sYpt6MPKsmLL=A at mail dot gmail dot com> <20150430174827 dot GU3618 at redhat dot com> <CALvpekGSvSvg=WcF4w2r3LQ377azPfsAKfLtA5pFm3OKz6DesA at mail dot gmail dot com>
On Thu, Apr 30, 2015 at 10:56 PM, Fan You <youfan.noey@gmail.com> wrote:
> To conclude:
>
> 1. What should I do is to create a new class _Sp_counted_array by merging
> _Sp_counted_ptr and _Sp_counted_deleter.
> So, some design like this?
> template<typename _Ptr, typename _Deleter, typename _Alloc>
> class _Sp_counted_array : public _Sp_counted_base {
> public:
> // implement here
> using _Tp = typename std::remove_pointer<_Ptr>::type;
> private:
> _Tp _array[]; // do init in ctor
What do you mean by this?
> size_t _size;
> _Alloc _alloc;
> _Deleter _del; // may use ebo to reduce size
> };
>
> Then, change __shared_count accordingly, others can remain unchanged.
>
> 2. Usage you mentioned
>>
>> shared_ptr<int[]> p(new int[3]);
>>
>> The shared_count only needs to allocate/deallocate a
>>
>> _Sp_counted_array<int> in both cases, and that has a known size.
>
I don't quite understand this.
> How can _Sp_counted_array<int> get the size when user didn't even pass one?
>
> 3. Actually it reminds me that user can use current std::shared_ptr with
> array as long as the right deleter are provided.
>>
>> #include <memory>
>>
>> int main()
>> {
>> int array[3];
>> std::shared_ptr<int[3]> p(&array, [](void*){});
>> }
>
>
> This will work because the _Tp = int[3] in original shared_ptr
> implementation. But the new implementation, which, _Tp = int; So, I am still
> not sure why size is not needed to be pass to _Sp_counted_array.
>>
>> shared_ptr<int[3]> p(new int[3]);
>
> Didn't specify the size but it do tell you by using int[3] as template
> parameter.
>
I believe there are various ways to implement this. Can you first
briefly describe your overall idea?
Two approaches I came up with, as I describe before:
1) Don't change __shared_ptr and anything in it; purely implement
shared_ptr<int[]> in terms of __shared_ptr<int> with special allocator
and/or deleter. This seems to be the same as your 3.
2) Let __shared_count support array directly, and propagate all
parameters from shared_ptr ctor to __shared_ptr -> __shared_count ->
_Sp_counted_array.
--
Regards,
Tim Shen