/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_MEMreceive_data[] = "@(#)MEMreceive.c	1.15 10/19/92 13:25:32  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	6/27/90

	ROUTINE:	receive_data
	Version:	__ROr_MEMreceive_data

PURPOSE:
	This routine receives data "from" memory.


IN:
	instance - send state

OUT:
	instance - Send state and Exception state are updated.


CALLED ROUTINES:
	SDataRequest

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>

#define MEMORY	(instance->subsystem->specific.memory.in)

int __ROr_MEMreceive_data(instance)

   __RO_Instance_Record *	instance;


{
   int done_avail;

#ifndef __RO_PRODUCTION
   if (instance->subsystem->platform != __ROm_platform_MEM)
      __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_PLATFORMNOTMEM);
#endif

   done_avail = instance->receive_state.done_available;
   instance->receive_state.done_available = 0;

   if (MEMORY.count == 0)
   {
      if (instance->receive_state.buffers)
         free(instance->receive_state.buffers);
      if (done_avail)
      {
         __RO_RETURN_DONE(instance);
      }
      else
      {
         __RO_RETURN_RUNTIME(instance,__ROx_GET_UNEXPECTDONE)
      }
   }
   else
   {
      instance->receive_state.buffers = (__RO_Receivebuffers)
		malloc(sizeof(struct __RO_Receivebuffer_struct));
      if (!instance->receive_state.buffers)
         __RO_RETURN_RUNTIME(instance,__ROx_GENERAL_MALLOCFAILED);
      instance->receive_state.buffers->forward = NULL;
      instance->receive_state.buffers->backward = NULL;
      instance->receive_state.buffers->data_ptr = MEMORY.pointer;
      instance->receive_state.buffers->data_length = MEMORY.count;
      MEMORY.count = 0;
   }

   if (__ROr_log6receive(instance))
      return(EXCEPTION);

   return(SUCCESS);
}

