
/*
* Extended realloc; we want both the old and the new size, so we can  properly
* zero  out  the difference when enlarging (as is usually the case).  Note the
* Realloc(p,f,t) and ReallocM(p,f,t,,m) macros in  V.h,  for  enforcing  arg
* count  and  types.   Note  also that we accept p==0 or f==0 to mean that the
* block hasn't yet been allocated, and do it, so you  don't  have  to  make  a
* special first case.  In fact, you could just dispense with malloc() and call
* this routine for all your memory needs.
*/
#include "V_M_UC.h"
#include "sys_malloc.h"

void* d_realloc(p,f,t,m)
	char *p;	/* Old block of data to be enlarged */
	Sizt  f;	/* Old size ("from") */
	Sizt  t;	/* New size ("to") */
	char *m;	/* Message for errors */
{	void *v;
	int   e, n;
	if (!m) m = m_unnamed;
	if (t <= 0) {
		P1 m_nomem,pname,t,m,Errinfo D;
		return 0;
	}
	n = t + Mfudge;	/* For debugging */
	V8M "Realloc %08X from %d to %d bytes for %s.",p,f,t,m D;
	errno = 0;
	if (!p || !f) {	/* Did the caller actually give us an old chunk? */
		v = (void*)malloc(n);
	} else {		/* Yes, so really realloc it */
		v = (void*)realloc(p,n);
	}
	e = errno;
	if (!v) {		/* Didn't get the space */
		P1 m_nomem,pname,n,m?m:m_unnamed,Errinfo D;
		Fail;
	}
	if (f < n ) {	/* Zero out the new space */
		BZero((BP)v+f,(BP)n-f);
	}
	V5M "realloc(%08X,%d)=%08X %d bytes [%s]",p,t,v,n,m D;
fail:
	if (errno = e)
		V5M "realloc Err %d=%s=%s",Errinfo D;
	return v;
}
