Kenneth Brody wrote:
>
.... snip ...
>
I have seen implementations take a shrinking realloc(), and merge
the released space with an adjoining free chunk, creating a single,
larger chunk. (I would suspect that this is relatively common.)
Here is an extraction from nmalloc.c, with most code removed,
showing the cases handled to avoid any unnecessary memory copying.
/* if decreasing simply reduce size and move excess to free */
else if (szneed ((ulong)(INT_MAX - 65536))) {
/* reject excessive size request */
p = NULL; goto exeunt;
}
else if (ISFREE(m->next) &&
(szneed <= (m->sz + m->next->sz)) ) {
/* the 'next' block is free and adequate so use it */
/* else m is the oversized return block */
}
else if ((lastsbrk == m->next) &&
((szneed + MINSAVE) <= (m->sz + lastsbrk->sz)) ) {
/* lastsbrk is adequate and adjacent so use it */
}
else if (ISFREE(m->prev) &&
(szneed <= (m->sz + m->prev->sz)) ) {
/* the 'prev' block is free and adequate so use it */
}
else if ((b = searchfree(szneed))) {
/* An adequate free block exists, copy over, free old */
}
else if (lastsbrk &&
((szneed + MINSAVE) <= lastsbrk->sz) ) {
DBGPRTR(EOL " Realloc is copying into lastsbrk");
}
/* else malloc new size, copy data, and free old */
else if ((m1 = extendsbrk(szneed))) {
if (lastsbrk == m->next) {
DBGPRTR(EOL " Realloc is now using lastsbrk extended");
}
else {
/* At this point lastsbrk is adequate size */
/* split off, copy over, and free old */
}
}
else m = NULL; /* failure */
You can find the complete code on my site (URL in sig).
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from
http://www.teranews.com