
#include "exec/types.h"
#include "exec/memory.h"
#include "exec/ports.h"

char *program;
struct MsgPort *FindPort();

ULONG StrToInt(s)
    char *s;
{
    ULONG   num = 0;
    char    ch;

    while ((ch = *s++) != 0) {
	switch (ch) {
	    case '0': case '1': case '2': case '3': case '4':
	    case '5': case '6': case '7': case '8': case '9': 
		num = num * 10 + (ch - '0');
		break;
	    default: 
		printf ("usage: %s <decimal-number>\n", program);
		exit (100);
	}
    }

    return num;
}


main(argc,argv)
    int argc;
    char *argv[];
{
    struct MsgPort *port;
    long    threshold,
            temp,
           *past,
            curr;
    char   *name;
    char   *s,
           *d;

    program = *argv;

    name = "drip.port";		/* no larger than 11 chars */

    AllocMem (16000000,0);	/* cause an expunge sweep */

    if (argc < 2)
	threshold = 0;
    else
	threshold = StrToInt (argv[1]);

    port = FindPort (name);
    if (port == 0) {
	port = (struct MsgPort *) AllocMem (sizeof (*port), MEMF_CLEAR);
	if (port == 0) {
	    printf ("%s: not enough memory\n");
	    exit (100);
	}
	else {
	    AddPort (port);
	    s = &name[0];
	    port -> mp_Node.ln_Name = d = (char *) (&port -> mp_MsgList);
	    while (*s != 0) {
		*d++ = *s++;
	    }
	    *d = 0;
	    port -> mp_Node.ln_Type = NT_MSGPORT;
	    port -> mp_SigTask = (struct Task  *) AvailMem (0);
	}
    }
    past = (long *) (&port -> mp_SigTask);

    curr = AvailMem (0);
    temp = *past - curr;
    *past = curr;

    if (temp > threshold) {
	printf ("==========>> %s: lost %ld bytes\n", program, temp);
	exit (5);
    }

    if (temp < -threshold) {
	printf ("==========>> %s: gained %ld bytes\n", program, -temp);
	exit (5);
    }
}
    
