This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: pool allocator changes and delete[] void*
I'm not understanding something here. Why replace "new char[n]" with
"static_cast<char *>(::operator new(n))"? It seems to me this is less
readable, less obvious and therefore less maintainable. What's the
justification for such obfuscation?
I'm also not convinced that the memory deallocation will correctly
deallocate the memory allocated. It's not clear to me that operator
new(size_t) and operator delete(void *) are inverse functions.
Cheers,
Gawain
----- Original Message -----
From: "Paolo Carlini" <pcarlini@unitus.it>
To: "Gabriel Dos Reis" <gdr@integrable-solutions.net>
Cc: "Paolo Carlini" <pcarlini@suse.de>; "Gerald Pfeifer"
<gerald@pfeifer.com>; "Benjamin Kosnik" <bkoz@redhat.com>;
<libstdc++@gcc.gnu.org>
Sent: Friday, December 26, 2003 5:31 PM
Subject: Re: pool allocator changes and delete[] void*
> Gabriel Dos Reis wrote:
>
> >Please don't write that. Say what you mean: Call the deallocation
*function*
> >
> > operator delete[] (p);
> >
> Indeed you are right and I wanted to to that in the first place, but
> then noticed
> that for consistency we should then change a few more places and come up
> with
> something similar to what we already have in new_allocator.h
> (_S_start_free is a char*)
>
> What about the attached?
>
> Paolo.
>
> //////////
>
----------------------------------------------------------------------------
----
> --- pool_allocator.h.orig 2003-12-26 17:12:01.000000000 +0100
> +++ pool_allocator.h 2003-12-26 17:18:43.000000000 +0100
> @@ -200,7 +200,7 @@
> ((_Obj*)(void*)_S_start_free)->_M_free_list_link =
*__free_list;
> *__free_list = (_Obj*)(void*)_S_start_free;
> }
> - _S_start_free = new char[__bytes_to_get];
> + _S_start_free = static_cast<char*>(::operator
new(__bytes_to_get));
> if (_S_start_free == 0)
> {
> size_t __i;
> @@ -225,7 +225,7 @@
> }
> }
> _S_end_free = 0; // In case of exception.
> - _S_start_free = new char[__bytes_to_get];
> + _S_start_free = static_cast<char*>(::operator
new(__bytes_to_get));
> // This should either throw an exception or remedy the
situation.
> // Thus we assume it succeeded.
> }
> @@ -290,7 +290,7 @@
> }
>
> if ((__n > (size_t) _S_max_bytes) || (_S_force_new > 0))
> - __ret = new char[__n];
> + __ret = ::operator new(__n);
> else
> {
> _Obj* volatile* __free_list = _S_free_list + _S_freelist_index(__n);
> @@ -317,7 +317,7 @@
> __pool_alloc<__threads, __inst>::deallocate(void* __p, size_t __n)
> {
> if ((__n > (size_t) _S_max_bytes) || (_S_force_new > 0))
> - delete [] __p;
> + ::operator delete(__p);
> else
> {
> _Obj* volatile* __free_list = _S_free_list + _S_freelist_index(__n);
>