delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/29/06:18:52

From: "A.Appleyard" <A DOT APPLEYARD AT fs2 DOT mt DOT umist DOT ac DOT uk>
Organization: Materials Science Centre
To: eliz AT is DOT elta DOT co DOT il, DJGPP AT DELORIE DOT COM
Date: Wed, 29 Jan 1997 11:00:20 GMT
Subject: Re: A misfeature re malloc() and new
Message-ID: <C00E0C14FA@fs2.mt.umist.ac.uk>

Please send me (a DOT appleyard AT fs2 DOT mt DOT umist DOT ac DOT uk) a copy of any reply, as I had
to unsubscribe from djgpp email group because of severe email intray overload.

  I (= A.Appleyard) wrote:-
>  Would it be safe to free the 1 megabyte block by breaking it up into smaller
> more useful blocks like this, instead of calling free() on it?:-
> /* free the block B and break it up into several blocks of size n */
>   void myfree(void*B,int n){int i,j,k,l; void*p,*q; ...

  I have looked through this function, and I have corrected it to:-

/* Free the block B and break it up into several blocks of size n */
/* This assumes that D:\DJGPP\SRC\LIBC\ANSI\STDLIB\MALLOC.C was compiled
with RCHECK not defined */
void free_and_split(void*B,int n){int i,oldsize,newsize,j; void*p,*q;
B-=4; /* now points to the block's header info */
if(*(unsigned char*)B!=0xef) return; /* *B etseq was not malloc()'ed */
oldsize=1<<(((unsigned char*)B)[1]+3); /* actual size of big block B */
p=malloc(n)-4; /* p now points into the chain for the desired small blocks */
newsize=1<<(((unsigned char*)p)[1]+3); /* actual size of a desired small block*/
free(p+4);
j=oldsize-newsize;
if(j<=0) return; /* error: oldsize/newsize must be power of 2 */
for(i=0;i<j;i+=newsize) *(void**)(B+i)=B+i+newsize;
  /* chain each new small block to the next */
*(void**)(B+i)=0; /* end of chain */
while((q=*(void**)p)) p=q; /*find end of existing chain of blocks of that size*/
*(void**)p=B; /* link on the new blocks */}

- Raw text -


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