This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [Fwd: basic_string<> - useless because of its poor performance]
Carlo Wood wrote:
>
> On Fri, Jul 06, 2001 at 08:38:34AM +0200, Ryszard Kabatek wrote:
> > size_type
> > get_new_capacity(size_type n,
> > size_type max_len)
> > {
> > if (n > max_len) // OK, we get an exception in _Rep::_S_create
> > return n;
> >
> > size_type result = n < 17 ? 32 : 2*n;
> > return result < max_len ? result : max_len; // Avoiding an exception in _Rep::_S_create
> > }
>
> Hmm. How about:
>
> size_type
> get_new_capacity(size_type n,
> size_type max_len)
> {
> // Not sure if this is still correct:
> if (n > max_len) // OK, we get an exception in _Rep::_S_create
> return n;
>
> const size_type l2 = 5; // 5 == 2log(32), 32 is the minimum size.
> size_type l = (n < __page_size ? n : __page_size) + __malloc_header_size - 1;
>
> if (l >= __page_size - 1)
> n = (((n - 1) >> __log_page_size) + 1) << __log_page_size; // __log_page_size == 2log(__page_size).
> else
> for (n = 1 << l2, l >>= l2; l > 0; n <<= 1)
> l >>= 1;
>
> size_type result = n - __malloc_header_size;
> return result < max_len ? result : max_len; // Avoiding an exception in _Rep::_S_create
> }
>
> I am not sure if I understand it correctly however: does this function
> return the new __capacity, or is this what is passed directly to malloc()?
> This implementation assumes that the result is passed directly to malloc().
> I have the feeling it should contain sizeof(CharT) and sizeof(_Rep) somehow though :/
> This function is not as trivial as it looks.
>
In my intention the parameter n of get_new_capacity is the storage amount we really need.
get_new_capacity should expand that value by a factor of 2 ( max. till string::max_size()).
Perhaps the name is not perfect :-\
The return value of get_new_capacity is passed to _Rep::_S_create.
Regards
--
Ryszard Kabatek