#ifdef VXWORKS_CFG
#include <signal.h>
#include <vxWorks.h>
#include <sys/time.h>

#ifdef VXWORKS_V5
#define SIG_RETURN void
#else
#define SIG_RETURN int
#endif

#include "../h/assignMem.h"

getpagesize()
{
	return (1024);
}

static struct sigvec origvecs[NUM_SIGNALS];
static int sigWasSet[NUM_SIGNALS];

doSignalCleanup()
{
	int s;
	int mask;
	struct sigvec tmp;
	
	mask = 0;
	for (s=0; s<NUM_SIGNALS; s++)
	 { if (sigWasSet[s])
	    { sigvec(s, &origvecs[s], 0);
	      mask |= SIGMASK(s);
	    }
	 }
/*	printf ("cleared signals 0x%x\n", mask); */
}

FUNCPTR signal(s, fxn)
int s;
SIG_RETURN (*fxn)();
{
	struct sigvec osv, sv;
	static int mask[NUM_SIGNALS];
	static int flags[NUM_SIGNALS];
	static initDone = 0;

	if (!initDone)
	 { int k;
	   for (k=0; k<NUM_SIGNALS; k++)
	    { mask[k] = 0;
	      flags[k] = 0;
	      origvecs[k].sv_handler = 0;
	      sigWasSet[k] = 0;
	    }
	   initDone = 1;
	   requestFlagInit(&initDone);
	 }

	sv.sv_handler = fxn;
	sv.sv_mask = mask[s];
	sv.sv_flags = flags[s];
	if (sigvec(s, &sv, &osv) < 0)
	 { return ((FUNCPTR)-1);
	 }
	if (sv.sv_mask != osv.sv_mask || sv.sv_flags != osv.sv_flags)
	 { mask[s] = sv.sv_mask = osv.sv_mask;
	   flags[s] = sv.sv_flags = osv.sv_flags;
	   if (sigvec(s, &sv, 0) < 0)
	    { return ((FUNCPTR)-1);
	    }
	 }
	if (!sigWasSet[s])
	 { origvecs[s] = osv;
	   sigWasSet[s] = 1;
	 }
	return ((FUNCPTR)osv.sv_handler);
}

#define MEM_DEBUG 1

#if MEM_DEBUG
#define BUF_SIZE 1024
typedef struct sbrkLink {
	struct sbrkLink *next;
	int buf1[BUF_SIZE];
	int *buf2;
} SBRK_LINK;
#else
typedef struct sbrkLink {
	struct sbrkLink *next;
} SBRK_LINK;
#endif

SBRK_LINK *sbrkList = NULL;
static int sbrkInitDone = 0;

#if MEM_DEBUG

assignBuf(buf)
int *buf;
{
	register int i;
	for (i=0; i<BUF_SIZE; i++)
	 { buf[i] = 0xdeadbeef;
	 }
}

checkBuf(buf)
int *buf;
{
	register int i;
	for (i=0; i<BUF_SIZE; i++)
	 { if (buf[i] != 0xdeadbeef)
	    { return (0);
	    }
	 }
	return(1);
}

checkAllBufs()
{
	SBRK_LINK *link;

	for (link=sbrkList; link; link=link->next)
	 { if (!checkBuf(link->buf1) || !checkBuf(link->buf2))
	    { return (0);
	    }
	   else
	    { printf ("0x%x OK\n", link);
	    }
	 }
	return (1);
}

#endif

char *sbrk(size)
{
	SBRK_LINK *link;
	char *mem;
	
	if (!sbrkInitDone)
	 { SBRK_LINK *link_next;
	   sbrkInitDone = 1;
	   for (link=sbrkList; link; link=link_next)
	    { link_next = link->next;
	      free((char*)link);
	    }
	   sbrkList = NULL;
	   requestFlagInit(&sbrkInitDone);
	 }
#if MEM_DEBUG
	if ((mem = (char*)malloc(ALIGN_SIZE(sizeof(SBRK_LINK))
				+ALIGN_SIZE(size)+
				 BUF_SIZE*sizeof(int))) == NULL)
	 { return ((char*)-1);
	 }
	else
	 { ASSIGN_MEM(link, SBRK_LINK*, sizeof(SBRK_LINK), mem);
	   link->buf2 = (int*)(mem + ALIGN_SIZE(size));
	   assignBuf(link->buf1);
	   assignBuf(link->buf2);
	   link->next = sbrkList;
	   sbrkList = link;
	   return (mem);
	 }
#else
	if ((mem = (char*)malloc(ALIGN_SIZE(sizeof(SBRK_LINK))
				+ALIGN_SIZE(size))) == NULL)
	 { return ((char*)-1);
	 }
	else
	 { ASSIGN_MEM(link, SBRK_LINK*, sizeof(SBRK_LINK), mem);
	   link->next = sbrkList;
	   sbrkList = link;
	   return (mem);
	 }
#endif
}

atoi(p)
register char *p;
{
	register int n;
	register int f;

	n = 0;
	f = 0;
	for(;;p++) {
		switch(*p) {
		case ' ':
		case '\t':
			continue;
		case '-':
			f++;
		case '+':
			p++;
		}
		break;
	}
	while(*p >= '0' && *p <= '9')
		n = n*10 + *p++ - '0';
	return(f? -n: n);
}

getpid()
{
	return(taskIdSelf());
}
#endif
