
/* cift10-c.c -- CIF test 10, C part */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>

#include "cifccsp.h"

/*
 * note: the current organisation of channel-types has
 * the reference-count in the first word, followed by the
 * declared channels in order.  also relies on gcc allocating
 * the structure in the order given..
 */

struct chantype_LINK;

typedef struct chantype_LINK {
	int refcount;
#ifdef CHANTYPEDESC
	unsigned int *typedesc;
	void *ihook;
#endif
	Channel in;
	Channel out;
} ct_LINK;


void my_process (Process *me, Channel *link_out)
{
	int i, v;
	ct_LINK *cli, *svr;

	cli = svr = (ct_LINK *)DMemAlloc (sizeof (ct_LINK));
	cli->refcount = 2;
	ChanInit (&(cli->in));
	ChanInit (&(cli->out));

	/* communicate client end back to occam */
	ChanMOutN (link_out, &cli, 1);

	/* now serve it */
	for (i=0;;) {
		ChanInInt (&(svr->in), &v);
		i += v;
		ChanOutInt (&(svr->out), i);
	}

	return;
}


/* allocates process and returns address to occam world */
void real_cift10_init (int *raddr, Channel *link_out)
{
	Process *p = ProcAlloc (my_process, 65536, 1, link_out);

	*raddr = (int)p;
	return;
}

/* frees memory used by a process -- lets hope it's finished :) */
void real_cift10_free (int addr)
{
	Process *p = (Process *)addr;

	ProcAllocClean (p);
	return;
}

void _cift10_init (int *ws) { real_cift10_init ((int *)(ws[0]), (Channel *)(ws[1])); }
void _cift10_free (int *ws) { real_cift10_free ((int)(ws[0])); }

