/*****************************************************************/
/**                                                             **/
/**             (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_MEMsend_data[] = "@(#)MEMsend.c	1.13 10/19/92 13:25:36  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	6/27/90

	ROUTINE:	send_data
	Version:	__ROr_MEMsend_data

PURPOSE:
	This routine sends data "to" 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.out)

int __ROr_MEMsend_data(instance)

   __RO_Instance_Record *	instance;


{

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

   if (__ROr_log6send(instance))
      return(EXCEPTION);
   if (instance->send_state.index > 0)
   {  unsigned long newsize;

      newsize = MEMORY->size;
      while (newsize < MEMORY->count + instance->send_state.index)
        newsize *= 2;
      if (newsize > MEMORY->size) 
      {  unsigned char *tbuffer;

         if (!(tbuffer = (unsigned char *) malloc(newsize)))
            __RO_RETURN_RUNTIME(instance,__ROx_SEND_MEM_MALLOCFAILED);
         memcpy(tbuffer, MEMORY->pointer, MEMORY->count);
         free(MEMORY->pointer);
         MEMORY->pointer = tbuffer;
      }
      memcpy(MEMORY->pointer + MEMORY->count,
             instance->send_state.buffer,
             instance->send_state.index);
      MEMORY->count += instance->send_state.index;
      instance->send_state.index = 0;
   }
   instance->send_state.boundary_flag = 0;
   return(SUCCESS);
}

