[net.unix-wizards] compaction using realloc, some questions.

schnable (11/29/82)

A fellow worker came across the following section of code:

char *ralloc(p,n)		/* compacting reallocation */
char *p;
{
	register char *q;
	char *realloc();
	 
	free(p);

	free(dummy);		/* dummy is externally declared */	
	dummy = malloc(1);

	q = realloc(p, (unsigned)n);
	if (q == NULL)
		noroom();	/*print error and do clean-up */
	return q;
}

He had some questions for me which I was unable to answer, so I
am forwarding them to the net.

Isn't that free of dummy and subsequent reallocation a little
dangerous? (couldn't malloc get the new space from inside of p's area?)

Why is dummy even operated on in such a manner?
(The original coder must have had something in mind.)
Does it have something to do with the forcing of compaction or
possibly to get better compaction?

In the UNIX(tm) 3.0 man page for malloc it says:
	
	realloc also works if ptr points to a block freed since
	the last call of malloc, realloc, or calloc; thus the
	sequence of free, malloc, and realloc can exploit the
	search strategy of malloc to do storage compaction.

Humm...do they mean the sequence malloc, free, and realloc?

What is this search strategy of realloc that we are supposedly
taking advantage of?

Either post to this net or respond to:

Andy Schnable (ihuxo!schnable) IH BTL (x2680) 


p.s.  It seems to me that a separate storage compaction routine
      would be much more desirable than the current state
      of affairs where one relies on the side-effects of
      remalloc to perform storage reclamation. Any other ideas?