Mail Archives: djgpp-workers/2002/03/02/14:03:02
Richard Dawe wrote:
>
> Hello.
>
> Charles Sandmann wrote:
> > Two potential patches vs 2.03 (not yet committed to CVS):
> >
> > First version (probably faster but bigger, extra code only if new
> > sbrk is not aligned, the reactive patch):
> >
> > *** malloc.c_ Wed Dec 5 14:52:52 2001
> > --- malloc.c Mon Feb 11 22:22:32 2002
> > *************** malloc(size_t size)
> > *** 208,215 ****
> > --- 208,222 ----
> > rv = (BLOCK *)((char *)rv - 4);
> > }
> > else
> > {
> > + int align_bytes = (int)rv & (ALIGN-1);
> > + if(align_bytes)
> > + {
> > + align_bytes = ALIGN - align_bytes;
> > + if(sbrk(align_bytes) == (void *)((char *)rv + chunk_size) )
> > + rv = (BLOCK *)((char *)rv + align_bytes);
> > + }
> > expected_sbrk = (BLOCK *)((char *)rv + chunk_size);
> > #if DEBUG
> > printf(" disconnected sbrk\n");
> > #endif
> >
> > Second version (slower - extra sbrk and brk called for each sbrk but only
> > execute about 20 assembly lines, smaller code, the proactive patch):
> >
> > *** malloc.c_ Wed Dec 5 14:52:52 2001
> > --- malloc.c Tue Feb 12 10:52:20 2002
> > *************** malloc(size_t size)
> > *** 192,195 ****
> > --- 192,196 ----
> > }
> >
> > + brk((void *)( ((int)sbrk(0)+(ALIGN-1)) & ~(ALIGN-1) )); /* Align next sbrk */
> > chunk_size = size+16; /* two ends plus two placeholders */
> > rv = (BLOCK *)sbrk(chunk_size);
> >
> > I posted both; little feedback on which should be committed. But it's
> > important that the block overhead be a factor of the alignment or you
> > will almost never merge blocks.
>
> Personally I think we should go for the faster patch - the first one - since
> the code overhead doesn't look that great.
Take a look at the code in my nmalloc, published in this list a
while ago. sbrk is not called often, and when it is an unexpected
result is even rarer. So alignment code size is not important,
but accuracy is. The peculiar case is when two sbrk calls in a
row return unexpected values. For reference, here is the routine
as it now stands:
>
> /* 1------------------1 */
>
> /* Get the memory, see if it extends the present lastsbrk
> If not, put the old lastsbrk into the appropriate freelist
> and replace lastsbrk by the new, setting the headers up
> else update the size markers in lastsbrk. When done either
> lastsbrk can supply the memory szextra, or NULL is returned.
> */
> static memblockp extendsbrk(ulong szxtra)
> {
> memblockp m;
> byte *expected;
> static int alignerr = 0; /* remember last correction */
> int aligndelta = 0;
>
> DBGPRTM(", extending sbrk");
>
> /* we have to ensure that the new lastsbrk always has */
> /* room to both realign and to leave a header when split */
> szxtra += (ALIGN + sizeof(memblock));
> if (szxtra < MINSBRK) szxtra = MINSBRK;
> szxtra += ALIGN - alignerr;
>
> if (lastsbrk)
> expected = ((byte*)lastsbrk) + lastsbrk->sz;
> else expected = NULL;
>
> if ((aligndelta = (ulong)expected & ALIGNMASK)) {
> /* lastsbrk was misaligned */
> szxtra += ALIGN - aligndelta;
> aligndelta = 0;
> }
>
> m = fakesbrk(szxtra);
> if (-1 == (int)m) return NULL;
> else {
> if ((byte*)m == expected) { /* Extending size of lastsbrk */
> DBGPRTM(EOL " sbrk(%4u=0x%05x) got expected %p"
> " lastsbrk %p sz %lu",
> szxtra, szxtra, expected,
> lastsbrk, expected - (byte*)lastsbrk);
> lastsbrk->sz += szxtra;
> alignerr = 0;
> m = lastsbrk;
> }
> else {
> /* Here we have to check & fix alignment */
> DBGPRTM(EOL "=>sbrk(%4u=0x%05x) got UNEXPECTED %p/%p"
> " lastsbrk %p sz %lu",
> szxtra, szxtra, m, expected,
> lastsbrk, expected - (byte*)lastsbrk);
> if ((alignerr = (ALIGNMASK & (ulong)m))) {
> (byte*)m += (aligndelta = ALIGN - alignerr);
> DBGPRTM(", szerr %d/%d", aligndelta, alignerr);
> }
> m->sz = szxtra - aligndelta; /* discard alignerr bytes */
> m->prev = m->next = NULL;
> m->nextfree = m->prevfree = NULL;
> m->guardlo = 0xDEADBEEF;
> /* m->guardhi[0] = 0xF00DFEED; */
> /* m->guardhi[1] = 0xBEEFDEAD; */
> mv2freelist(lastsbrk);
> lastsbrk = m;
> }
> }
> return m;
> } /* extendsbrk */
--
Chuck F (cbfalconer AT yahoo DOT com) (cbfalconer AT XXXXworldnet DOT att DOT net)
Available for consulting/temporary embedded and systems.
(Remove "XXXX" from reply address. yahoo works unmodified)
mailto:uce AT ftc DOT gov (for spambots to harvest)
- Raw text -