Topic: realloc" without copying
Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Tue, 24 Oct 2006 16:22:32 GMT Raw View
It seems there is more than one application for a "realloc" function which
doesn't copy over the original data if the memory chunk should be relocated.
Any chance of adding such a function to the language?
--
Frederick Gotham
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Tue, 24 Oct 2006 16:39:46 GMT Raw View
Frederick Gotham posted:
>
>
> It seems there is more than one application for a "realloc" function
> which doesn't copy over the original data if the memory chunk should be
> relocated.
>
> Any chance of adding such a function to the language?
And perhaps even a "realloc_check" function which would check whether it is
necessary to reallocate the buffer?
--
Frederick Gotham
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: howard.hinnant@gmail.com (Howard Hinnant)
Date: Tue, 24 Oct 2006 18:32:07 GMT Raw View
In article <har%g.15176$j7.332517@news.indigo.ie>,
fgothamNO@SPAM.com (Frederick Gotham) wrote:
> Frederick Gotham posted:
>
> >
> >
> > It seems there is more than one application for a "realloc" function
> > which doesn't copy over the original data if the memory chunk should be
> > relocated.
> >
> > Any chance of adding such a function to the language?
>
>
> And perhaps even a "realloc_check" function which would check whether it is
> necessary to reallocate the buffer?
Your timing is impeccable. WG14 discussed this yesterday:
http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1085.htm
I'm not certain what their decision will be on it. But I believe some
support has been voiced for at least a subset of this proposal.
-Howard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Andrei Polushin" <polushin@gmail.com>
Date: Tue, 24 Oct 2006 13:36:54 CST Raw View
Frederick Gotham wrote:
> It seems there is more than one application for a "realloc" function
> which doesn't copy over the original data if the memory chunk should be
> relocated.
>
> Any chance of adding such a function to the language?
>
> And perhaps even a "realloc_check" function which would check whether it
> is necessary to reallocate the buffer?
Note that "reallocate" is not identical to "relocate". It may relocate
to prevent memory fragmentation or just for fun :)
"realloc_check" is simply harmful, because you may get "true" for the
first call, and "false" for the second call (due to multithreading).
Isn't it better to customize realloc with move handler?
typedef void(*move_handler)(void* to, const void* from, size_t n);
void* _expand_fit(void* p, size_t new_size); // find larger block
void* _better_fit(void* p, size_t new_size); // find better block
void* realloc(void* p, size_t old_size, size_t new_size,
move_handler move)
{
if (old_size < new_size) {
void* r = _expand_fit(p, new_size);
if (r != p) {
move(r, p, old_size);
free(p);
}
return r;
} else {
void* r = _better_fit(p, new_size);
if (r != p) {
move(r, p, new_size);
free(p);
}
return r;
}
}
size_t _mem_size(void* p); // get block size
// classic realloc:
void* realloc(void* p, size_t new_size)
{
return realloc(p, _mem_size(p), new_size,
(move_handler)memmove);
}
--
Andrei Polushin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]