delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2002/02/19/23:45:39

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-workers-bounces using -f
From: sandmann AT clio DOT rice DOT edu (Charles Sandmann)
Message-Id: <10202200445.AA15769@clio.rice.edu>
Subject: Re: Malloc/free DJGPP code
To: djgpp-workers AT delorie DOT com
Date: Tue, 19 Feb 2002 22:45:41 -0600 (CST)
Cc: cbfalconer AT yahoo DOT com
In-Reply-To: <3C72ED37.A7BD29CC@yahoo.com> from "CBFalconer" at Feb 19, 2002 07:26:31 PM
X-Mailer: ELM [version 2.5 PL2]
Mime-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> If sbrk alignment is controlled malloc alignment is controlled.
> Pick a size.

sbrk() alignment is NOT controlled - it is up to malloc to to do
the alignment.  Alignment should be on an 8 byte boundary to 
maximize performance with floating point arrays.  The overhead per
block must be a multiple of 8 bytes (at a minimum) to keep the 
blocks aligned.  Larger alignments might add value but not proven.

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.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019