 /*
 |  Basic memory allocation functions
 |  This is currently staggeringly ineffecient
*/

#include "alloc.h"

static char *firstblk;


 /*
 |  Allocate a piece of memory
*/
char *wmalloc( size )
int	size;
{
	char	*oldbrk;
	int	bsize;

	/* Round size to longword boundry */
	bsize = ((size-1)&~3)+4;

	/* This is the real memory usage */
	oldbrk = (char*)sbrk( bsize + sizeof(mblk) );

	/* If the break couldn't be moved, fail */
	if( (int)oldbrk == -1 )
		return( 0 );

	/* Set up the block descriptor */
	((mblk*)oldbrk)->size = bsize+sizeof(mblk);
	((mblk*)oldbrk)->use  = USED;

	/* If this is the first block, keep its address */
	if( !firstblk )
		firstblk = oldbrk;

	return( oldbrk );
}


 /*
 |  Free a piece of memory
*/
void wfree( addr )
char *addr;
{
	mblk *blk = (mblk*)(addr-sizeof(mblk));

	if( blk->use != USED )
	{
		/* Init cannot print and current has no log
		fprintf( stderr, "wfree(): block corrupt" ); */
		return;
	}

	blk->use = FREE;
}


