/*****************************************************************/
/**                                                             **/
/**             (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_Bind_mem[] = "@(#)STDbind_mem.c	1.24 11/13/92 10:50:34  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	6/28/90

	ROUTINE:	Bind_mem
	Version:	__ROr_STDBind_mem

PURPOSE:


IN:
	instance - valid pointer filled by STDinit_instance.
	rules = ASN_1
	memstate - pointer to __RO_Memory object
	openstring - either "r" or "w"

OUT:
	instance - All fields are initialized and ready for
		communication


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:
	91/12/17 - changed for Any compatability (ls)


*/

#include <elros/instance.h>
#include <elros/binds.h>
#include <malloc.h>

#include <elros/specifics.h>

#define MEMORY subsystem->specific.memory

int __ROr_STDBind_mem(instance,rules,memstate,openstring)

	__RO_Instance_Record *instance;
	__RO_Presentation_rules rules;
	__RO_Memory *memstate;
	char *openstring;

{

        /* log "addresses" a level 6 */
   if (__ROr_Logging6on(instance))
   {
      char buffer[512];

      sprintf(buffer,"BIND: platform=MEM, direction=%s",
		*openstring=='w' ? "SENDING" :
		*openstring=='r' ? "RECEIVING" : "***UNKNOWN***");
      if (__ROr_log6comment(instance,buffer))
         return(EXCEPTION);
   }

   if (!memstate)
     __RO_RETURN_RUNTIME(instance,__ROx_BIND_BADMEMARG);

   if (*openstring == 'w')
   {
      /* Check memstate, create data area for writing */
      instance->MEMORY.out = memstate;
      memstate->pointer = 
        (unsigned char*) malloc(instance->receive_state.buffer_maximum);
      if (!memstate->pointer)
        __RO_RETURN_RUNTIME(instance,__ROx_SEND_MEM_MALLOCFAILED);
      memstate->size = instance->receive_state.buffer_maximum;
      memstate->count = 0;
   }

   else if (*openstring == 'r')
   {
      /* Check memstate, data area must already exist */
      if (!memstate->pointer) 
        __RO_RETURN_RUNTIME(instance,__ROx_BIND_BADRECEIVEARG);
      instance->MEMORY.in = *memstate;
   }

   else
      __RO_RETURN_RUNTIME(instance,__ROx_BIND_BADOPENSTRING);

   instance->subsystem->platform = __ROm_platform_MEM;
   instance->ISO_Contexts.specific_pcid = 0;

   if (__ROr_STDinit_bind(instance,rules,__ROr_MEMsend_data,__ROr_MEMreceive_data))
      return(EXCEPTION);

   return(SUCCESS);

}


