#include <exec/memory.h>

struct _clean {
	int (*function)();
	char *name;
	struct _clean *next;
} *cleanlist = NULL;

add_cleanup(function, name)
int (*function)();
{
	struct _clean *ptr;

	ptr = AllocMem(sizeof(struct _clean), MEMF_PUBLIC);
	if(!ptr)
		return 0;
	ptr->function = function;
	ptr->name = name;
	ptr->next = cleanlist;
	cleanlist = ptr;
}

cleanup(debug)
int debug;
{
	struct _clean *ptr;
	int (*f)();

	while(cleanlist) {
		ptr = cleanlist;
		cleanlist = cleanlist->next;
		f = ptr->function;
		if(debug == 1)
			printf("Cleanup: removing '%s'\n", ptr->name);
		FreeMem(ptr, sizeof(struct _clean));
		(*f)();
	}
}
