
ENV

xx_msgbuf (msg_pt,leng)
	struct xx_msg **msg_pt;
	int leng;
	{
    int i,n;
    struct xx_buffer *b1,*b2;
	
	LOCK(xx_cmem->avl_lock)

/* allocate buffers for message */

		if ( leng)
		{
		    n = (leng + (BUFF_SIZE - 1)) / BUFF_SIZE;
		    b1 = xx_cmem->avail_buffs;
		    for (b2=b1, i=n; 
			 (b2) && (--i); 
			 b2=b2->link);
		    if (!b2)
		    {
				printf("*** out of buffers for send/receive process %d msg length %d  buffers needed %d***\n",ntohl(xx_my_id.proc_table_indx),leng,n);
				exit(3);
		    }
		    xx_cmem->avail_buffs = b2->link;
		    b2->link = 0;
		}
		else
		    b1=0;

	*msg_pt = xx_cmem->avail_msgs;

    if (!*msg_pt)
	    {
		printf("*** out of message headers for send/receive ***\n");
		exit(3);
	    };
	xx_cmem->avail_msgs = (*msg_pt)->link;
	UNLOCK(xx_cmem->avl_lock)
	(*msg_pt)->first_buff = b1;

	}


xx_msgcpy(msg,xx_len,to_proc,msg_pt)
    struct xx_process **to_proc;
	struct xx_msg *msg_pt;
	int xx_len;
	char *msg;
{
    int xx_i,xx_n,xx_rem;
    char *xx_c1,*xx_c2;
    struct xx_buffer *xx_b2;
	if ( xx_len ) 
		{
			for (xx_b2=msg_pt->first_buff,xx_rem=xx_len,xx_c1=msg;
				 xx_rem;
				 xx_b2 = xx_b2->link)
			{
				if (xx_rem > BUFF_SIZE)
				xx_i = BUFF_SIZE;
				else
				xx_i = xx_rem;

				xx_rem = xx_rem - xx_i;
				xx_b2->ln = xx_i;

				for (xx_c2=xx_b2->buff; xx_i--;)
				*(xx_c2++) = *(xx_c1++);
			}
		}

	MENTER((*to_proc)->msg_q_monitor)
	if ((*to_proc)->first_msg)
	{
	    (*to_proc)->last_msg->link = msg_pt;
	    (*to_proc)->last_msg = msg_pt;
	}
	else
	{
	    (*to_proc)->first_msg = (*to_proc)->last_msg = msg_pt;
	}
	CONTINUE((*to_proc)->msg_q_monitor,0)
	MEXIT((*to_proc)->msg_q_monitor)

}

xx_send (proc_id,msg_type,msgtype_len,msg,length,ack)
    PROC_ID *proc_id;
	int msg_type,msgtype_len;
	char *msg;
	int length,ack;
	
	{
    struct xx_msg *xx_m1;
    struct xx_process *proc_pt;
    int xx_ln,xx_ln2;
	
	/****
	  xx_ln2 = 0;
	  xx_ln = msgtype_len;
	  if (length) xx_ln = length;
	  if ( msg ) xx_ln2 = xx_ln;
	   get message buffers
	****/
    xx_ln = length ? length : msgtype_len;
	xx_ln2 = msg ? xx_ln : 0;

	xx_msgbuf(&xx_m1,xx_ln2);
	WHO_AM_I(&(xx_m1->sender))
	xx_m1->link = 0;
	xx_m1->type = msg_type;
	xx_m1->ack = ack; 
	proc_pt = xx_cmem->active_processes[ntohl(proc_id->proc_table_indx)];
	/* copy message into queue */
	xx_msgcpy(msg,xx_ln2,&proc_pt,xx_m1);
    }
