/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1991 by John E. Lloyd.				    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*---------------------------------------------------------------------------
 *		
 * MODULE NAME:	locateMemory
 *
 * DESCRIPTION:	Contains a routine which allows us to lookup the
 *		the local address of a piece of shared memory given
 *		the address of that piece of shared memory as seen
 *		by the ARBITER CPU.
 * 
 * AUTHOR/DATE:	John Lloyd, Fall 1989
 *
 * MODIFICATION HISTORY:
 *
 *--------------------------------------------------------------------------*/

#include "../h/cdefs.h"
#include "../rci/sharedMemory.h"
#include "../h/rciConfig.h"

extern SECTION_DESC sharedMemTable[][MAX_CPUS];
extern int rciCpu;

void *locateSharedMem (addr)
register void *addr;
{
	/* Get the address of a piece of shared memory for the
	   local CPU, given the equivalent address on the ARBITER CPU.
	 */

	register SECTION_DESC *arbAddrs;
	register int k;
	int offset;
	char *base;

	arbAddrs = sharedMemTable[ARBITER_CPU];
	for (k=0; k<MAX_CPUS; k++)
	 { if (arbAddrs[k].low)
	    { if (arbAddrs[k].low <= addr &&
		  arbAddrs[k].high > addr)
	       { break;
	       }
	    }
	 }
	if (k==MAX_CPUS)
	 { return (NULL);
	 }
	offset = (char*)addr - (char*)arbAddrs[k].low;
	if (base = (char*)sharedMemTable[rciCpu][k].low)
	 { return ((void*)(base + offset));
	 }
	else
	 { return (NULL);
	 }
}
