simutil.h 4.27 KB
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */

/*****************************************************************
 * simutil.h
 *
 * Error handling headers for SimOS.
 *
 * Author: $Author: blythe $
 * Date:   $Date: 2002/05/29 01:09:09 $
 ****************************************************************/

#ifndef _UTILITIES_H
#define _UTILITIES_H
#include <stdio.h>
#include <stdlib.h>
#include "simtypes.h"

/*
 * Include prototypes for the "standard" bcopy/bzero we use. 
 */
#ifdef __svr4__
# include <string.h>
# define bzero(dst,len)      memset((char *)dst, 0, len)
# define bcopy(src,dst,len)  memcpy((char *)dst, (char *)src, len)
# define bcmp(src,dst,len)   memcmp((char *)dst, (char *)src, len)
int getpagesize(void);
#endif /* def __svr4__ */
#if defined(sun) && !defined(__svr4__)
# include <strings.h>
#endif /* def sgi */
#ifdef sgi
# include <bstring.h>
#endif /* def sgi */

extern int GetLineFromFile(FILE *f, char *buf, int bufLen);

extern int GetLog2(int);
extern SimTime NanoSecsToCycles(SimTime);
extern SimTime CyclesToNanoSecs(SimTime);


/* *****************************************************
 * Wrappers to deal with %lld vs %ld problems
 * *****************************************************/

extern void SubstituteLLx(char *out, const char *text, int maxlen);
/* each of these returns the number of characters copied to the string */
extern int PrintLLD(char *string,  int64 val);
extern int PrintLLU(char *string, uint64 val);
extern int PrintLLX(char *string,  int64 val);


/* **************************************************************
 * TimeDelay: uniform way of handling averages and std deviations
 * **************************************************************/

typedef  struct TimeDelay {
   uint count,zerocount;
   SimTime total,min,max;
   uint64 total2;
} TimeDelay;

extern void SaveTimeDelay(TimeDelay *t,SimTime delay);
extern void PrintTimeDelay(FILE *f, char *s, TimeDelay *t);

/*
 * Simulator memory allocation routines:
 * MALLOC to be used for conventionnal malloc/free data stuctures 
 * ZMALLOC bzeros the allocated region on top of that
 * 
 */

#define MALLOC(_size, _name)  malloc((_size))
#define ZMALLOC(_size,_name)  calloc(1,(_size))

#if defined(SIMOS_PURE)
#define ZALLOC_PERM(_size,_name)  ZMALLOC((_size),_name)
#else
#define ZALLOC_PERM(_size,_name) AllocZeroMem(_size,_name)
#endif


/* ********************************************************************
 * MallocShared can only be called before the processes are forked. It is 
 * called by the original process to allocated shared backdoor space.
 * **********************************************************************/

void *MallocShared(unsigned nbytes, char *name);


/* ********************************************************************
 * SaveString.  Save the string parameter into the private space.
 * **********************************************************************/

char *SaveString(char *string);

/* ********************************************************************
 * MemAlign. Allocate memory that is aligned
 * ********************************************************************/

void *MemAlign(int alignment,int size);


/* ********************************************************************
 * AllocZeroMem.  Allocates initialized memory that is permanently 
 * needed, ie, that will never be freed. 
 * **********************************************************************/


void *AllocZeroMem(int size, char *name);
void InitZeroMem(char * startAddr,char * endAddr,int redzone);



/* ********************************************************************
 * StripPath.  Return the name of the file.
 * **********************************************************************/

char *StripPath(char *string);

/*****************************************************************
 * Dump the real-life time for simulation timing purposes
 ****************************************************************/
void PrintRealTime(char *string);

void toLowerStr(char *dest, char *src);
void toUpperStr(char *dest, char *src);


/*
 * MakeFile
 */

extern int MakeFile(char *directoryname, 
		    char *filename, int length, int unlinkit);


#endif