[net.bugs.usg] SVR2.0 -lmalloc bug fixes

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (06/28/85)

BUG FIXES for UNIX System V Release 2.0 libmalloc.a (-lmalloc),
lines added or changed flagged with "DAG", lines omitted "...":


/*	@(#)malloc.c	1.5	*/

...

char *
malloc(nbytes)
unsigned nbytes;
{
	register struct header *blk;
	register unsigned nb;      /* size of entire block we need */
	char *sbrk();

	/*      on first call, initialize       */
	if (freeptr[0].nextfree == GROUND) {	/* DAG -- clarify */

...

			/* temporarily alter maxfast, to avoid
			   infinite recursion */
			maxfast = 0;
			holdhead = (struct holdblk **)
				   malloc(sizeof(struct holdblk *)*
				   (fastct + 1));
			if (holdhead == 0)		/* DAG -- bug fix */
				return malloc(nbytes);	/* DAG leave maxfast==0 */
			for(i=1; i<=fastct; i++)  {	/* DAG -- cosmetic */

...

		/*   check that a very large request won't cause an inf. loop */
		/* DAG NOTE -- following does not work on Gould due to cc bug */
		/* DAG -- bug fix next line (casts were omitted): */
		if ((char *)freeptr[1].nextblk - (char *)&freeptr[1] < nb)
			return NULL;
		{

...

				/* get memory */
				if ((int)sbrk(nget) == -1)  {	/* DAG */
					return NULL;
				}
				/* add to arena */

...

#endif
				if ((int)sbrk(nget) == -1)  {	/* DAG */
					return NULL;
				}
				/* block must be word aligned */
...


				assert(((int)newblk%ALIGNSZ) == 0);
				if ((int)sbrk(nget) == -1)  {	/* DAG */
					return NULL;
				}
				/* combine with last block, put in arena */

...

	CHECKQ;
	/* DAG -- following statement block added as bug fix: */
	{	register struct lblk	*lblk =
			(struct lblk *)((char *)blk + minhead - MINHEAD);

		lblk->header.nextfree = CLRSMAL( lblk->header.nextfree );
	}
	return (char *)blk + minhead;
}

...

void
free(ptr)
char *ptr;
{
	register struct holdblk *holdblk;       /* block holding blk */
	register struct holdblk *oldhead;       /* former head of the hold 
						   block queue containing
						   blk's holder */

	if ( ptr == NULL )		/* DAG -- added safety net */
		return;			/* DAG */

	/* DAG -- bug fix: */
	if (TESTSMAL(((struct lblk *)(ptr - MINHEAD))->header.holder))  {

...

struct mallinfo
mallinfo()
{

...

	/* 	examine space in holding blks	*/
    if (holdhead != 0)			/* DAG -- bug fix */
	for (i=fastct; i>0; i--)  {  /* loop thru ea. chain */
	    hblk = holdhead[i];
	    if (hblk != HGROUND)  {  /* do only if chain not empty */
		/* DAG -- following statement moved, bug fix */
		size = hblk->blksz + sizeof(struct lblk) - sizeof(int);
		do  {		     /* loop thru 1 hold blk chain */

...

rstalloc()
{
/*	struct header *temp;	/* DAG -- unused */

/*	temp = arena;		/* DAG -- unused */

...

	if(freeptr[0].nextfree == GROUND) return;	/* DAG -- clarify */
	brk(CLRBUSY(arena[1].nextblk));
	freeptr[0].nextfree = GROUND;			/* DAG -- clarify */
}
#endif /*RSTALLOC*/