/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/*=~=~=~=~ McGill-Computer-Vision-and-Robotics-Laboratory--C-Module ~=~=~=~=
 *		
 * MODULE NAME:	talloc
 *
 * DESCRIPTION:	The routine talloc() performs a malloc(), but also keeps all
 *		the items allocated chained together in a linked list.
 *		This allows us to free everything at once using one call.
 *		The utility of this is that it is much easier to clean
 *		up temporary storage (a lot of which is used in the
 *		evaluation process) since everthing can be freed in one
 *		swoop. This is particularly using in freeing storage after
 *		an error condition has occured.
 * 
 *		The calculator calls talloc_reset() once at every prompt.
 *
 * AUTHOR/DATE:	John Lloyd, Summer 1987
 *
 *~=~=~=~=~=~= McGill-Computer-Vision-and-Robotics-Laboratory ~=~=~=~=~=~=*/

#include "cdefs.h"

typedef struct _link {
	struct _link *next;
} LINK;

static LINK *head = NULL;

void *talloc (n)
int n;
{
	LINK *p;

	if ((p = (LINK *)malloc (sizeof(LINK)+n)) == NULL)
	 { return (NULL);
	 }
	else
	 { p->next = head;
	   head = p;
	   return ((void*)(p+1));
	 }
}

talloc_reset ()
{
	LINK *p;

	while (head != NULL)
	 { p = head->next;
	   free (head);
	   head = p;
	 }
}
