"I am a person who works hard and plays hard."

Yuan Wei
Second Year Graduate Student Department of Computer Science
University of Virginia Charlottesville, VA 22903
Email: yw3f@cs.virginia.edu


Source Code Analysis

Main Page   Compound List   File List   Compound Members   File Members  

misc.h

Go to the documentation of this file.
00001 /*
00002  * misc.h - miscellaneous interfaces
00003  *
00004  * This file is a part of the SimpleScalar tool suite written by
00005  * Todd M. Austin as a part of the Multiscalar Research Project.
00006  *  
00007  * The tool suite is currently maintained by Doug Burger and Todd M. Austin.
00008  * 
00009  * Copyright (C) 1994, 1995, 1996, 1997, 1998 by Todd M. Austin
00010  *
00011  * This source file is distributed "as is" in the hope that it will be
00012  * useful.  The tool set comes with no warranty, and no author or
00013  * distributor accepts any responsibility for the consequences of its
00014  * use. 
00015  * 
00016  * Everyone is granted permission to copy, modify and redistribute
00017  * this tool set under the following conditions:
00018  * 
00019  *    This source code is distributed for non-commercial use only. 
00020  *    Please contact the maintainer for restrictions applying to 
00021  *    commercial use.
00022  *
00023  *    Permission is granted to anyone to make or distribute copies
00024  *    of this source code, either as received or modified, in any
00025  *    medium, provided that all copyright notices, permission and
00026  *    nonwarranty notices are preserved, and that the distributor
00027  *    grants the recipient permission for further redistribution as
00028  *    permitted by this document.
00029  *
00030  *    Permission is granted to distribute this file in compiled
00031  *    or executable form under the same conditions that apply for
00032  *    source code, provided that either:
00033  *
00034  *    A. it is accompanied by the corresponding machine-readable
00035  *       source code,
00036  *    B. it is accompanied by a written offer, with no time limit,
00037  *       to give anyone a machine-readable copy of the corresponding
00038  *       source code in return for reimbursement of the cost of
00039  *       distribution.  This written offer must permit verbatim
00040  *       duplication by anyone, or
00041  *    C. it is distributed by someone who received only the
00042  *       executable form, and is accompanied by a copy of the
00043  *       written offer of source code that they received concurrently.
00044  *
00045  * In other words, you are welcome to use, share and improve this
00046 
00047  * source file.  You are forbidden to forbid anyone else to use, share
00048  * and improve what you give them.
00049  *
00050  * INTERNET: dburger@cs.wisc.edu
00051  * US Mail:  1210 W. Dayton Street, Madison, WI 53706
00052  *
00053  * $Id: misc.h,v 1.1.1.1 2000/05/26 15:18:58 taustin Exp $
00054  *
00055  * $Log: misc.h,v $
00056  * Revision 1.1.1.1  2000/05/26 15:18:58  taustin
00057  * SimpleScalar Tool Set
00058  *
00059  *
00060  * Revision 1.7  1999/12/31 18:46:11  taustin
00061  * quad_t naming conflicts removed
00062  *
00063  * Revision 1.6  1999/12/13 18:44:49  taustin
00064  * crc generation support added (useful for debugging)
00065  *
00066  * Revision 1.5  1998/08/27 15:45:24  taustin
00067  * implemented host interface description in host.h
00068  * added target interface support
00069  * implemented a more portable random() interface
00070  * disabled calls to sbrk() under malloc(), this breaks some
00071  *       malloc() implementation (e.g., newer Linux releases)
00072  * added myprintf() and myatoq() routines for printing and reading
00073  *       qword's, respectively
00074  * added gzopen() and gzclose() routines for reading and writing
00075  *       compressed files, updated sysprobe to search for GZIP, if found
00076  *       support is enabled
00077  * moved host-dependent definitions to host.h
00078  *
00079  * Revision 1.4  1997/03/11  01:18:24  taustin
00080  * updated copyright
00081  * supported added for non-GNU C compilers
00082  * ANSI C compiler check added
00083  * long/int tweaks made for ALPHA target support
00084  * hacks added to make SYMCAT() portable
00085  *
00086  * Revision 1.3  1997/01/06  16:02:01  taustin
00087  * comments updated
00088  * system prototypes deleted (-Wall flag no longer a clean compile)
00089  *
00090  * Revision 1.1  1996/12/05  18:50:23  taustin
00091  * Initial revision
00092  *
00093  *
00094  */
00095 
00096 #ifndef MISC_H
00097 #define MISC_H
00098 
00099 #include <stdio.h>
00100 #include <stdlib.h>
00101 #include <stdarg.h>
00102 #include <string.h>
00103 #include <sys/types.h>
00104 
00105 /* boolean value defs */
00106 #ifndef TRUE
00107 #define TRUE 1
00108 #endif
00109 #ifndef FALSE
00110 #define FALSE 0
00111 #endif
00112 
00113 /* various useful macros */
00114 #ifndef MAX
00115 #define MAX(a, b)    (((a) < (b)) ? (b) : (a))
00116 #endif
00117 #ifndef MIN
00118 #define MIN(a, b)    (((a) < (b)) ? (a) : (b))
00119 #endif
00120 
00121 /* for printing out "long long" vars */
00122 #define LLHIGH(L)               ((int)(((L)>>32) & 0xffffffff))
00123 #define LLLOW(L)                ((int)((L) & 0xffffffff))
00124 
00125 /* size of an array, in elements */
00126 #define N_ELT(ARR)   (sizeof(ARR)/sizeof((ARR)[0]))
00127 
00128 /* rounding macros, assumes ALIGN is a power of two */
00129 #define ROUND_UP(N,ALIGN)       (((N) + ((ALIGN)-1)) & ~((ALIGN)-1))
00130 #define ROUND_DOWN(N,ALIGN)     ((N) & ~((ALIGN)-1))
00131 
00132 /* verbose output flag */
00133 extern int verbose;
00134 
00135 #ifdef DEBUG
00136 /* active debug flag */
00137 extern int debugging;
00138 #endif /* DEBUG */
00139 
00140 /* register a function to be called when an error is detected */
00141 void
00142 fatal_hook(void (*hook_fn)(FILE *stream));      /* fatal hook function */
00143 
00144 #ifdef __GNUC__
00145 /* declare a fatal run-time error, calls fatal hook function */
00146 #define fatal(fmt, args...)     \
00147   _fatal(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
00148 
00149 void
00150 _fatal(char *file, char *func, int line, char *fmt, ...)
00151 __attribute__ ((noreturn));
00152 #else /* !__GNUC__ */
00153 void
00154 fatal(char *fmt, ...);
00155 #endif /* !__GNUC__ */
00156 
00157 #ifdef __GNUC__
00158 /* declare a panic situation, dumps core */
00159 #define panic(fmt, args...)     \
00160   _panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
00161 
00162 void
00163 _panic(char *file, char *func, int line, char *fmt, ...)
00164 __attribute__ ((noreturn));
00165 #else /* !__GNUC__ */
00166 void
00167 panic(char *fmt, ...);
00168 #endif /* !__GNUC__ */
00169 
00170 #ifdef __GNUC__
00171 /* declare a warning */
00172 #define warn(fmt, args...)      \
00173   _warn(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
00174 
00175 void
00176 _warn(char *file, char *func, int line, char *fmt, ...);
00177 #else /* !__GNUC__ */
00178 void
00179 warn(char *fmt, ...);
00180 #endif /* !__GNUC__ */
00181 
00182 #ifdef __GNUC__
00183 /* print general information */
00184 #define info(fmt, args...)      \
00185   _info(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
00186 
00187 void
00188 _info(char *file, char *func, int line, char *fmt, ...);
00189 #else /* !__GNUC__ */
00190 void
00191 info(char *fmt, ...);
00192 #endif /* !__GNUC__ */
00193 
00194 #ifdef DEBUG
00195 
00196 #ifdef __GNUC__
00197 /* print a debugging message */
00198 #define debug(fmt, args...)     \
00199     do {                        \
00200         if (debugging)          \
00201             _debug(__FILE__, __FUNCTION__, __LINE__, fmt, ## args); \
00202     } while(0)
00203 
00204 void
00205 _debug(char *file, char *func, int line, char *fmt, ...);
00206 #else /* !__GNUC__ */
00207 void
00208 debug(char *fmt, ...);
00209 #endif /* !__GNUC__ */
00210 
00211 #else /* !DEBUG */
00212 
00213 #ifdef __GNUC__
00214 #define debug(fmt, args...)
00215 #else /* !__GNUC__ */
00216 /* the optimizer should eliminate this call! */
00217 static void debug(char *fmt, ...) {}
00218 #endif /* !__GNUC__ */
00219 
00220 #endif /* !DEBUG */
00221 
00222 /* seed the random number generator */
00223 void
00224 mysrand(unsigned int seed);     /* random number generator seed */
00225 
00226 /* get a random number */
00227 int myrand(void);               /* returns random number */
00228 
00229 /* copy a string to a new storage allocation (NOTE: many machines are missing
00230    this trivial function, so I funcdup() it here...) */
00231 char *                          /* duplicated string */
00232 mystrdup(char *s);              /* string to duplicate to heap storage */
00233 
00234 /* find the last occurrence of a character in a string */
00235 char *
00236 mystrrchr(char *s, char c);
00237 
00238 /* case insensitive string compare (NOTE: many machines are missing this
00239    trivial function, so I funcdup() it here...) */
00240 int                             /* compare result, see strcmp() */
00241 mystricmp(char *s1, char *s2);  /* strings to compare, case insensitive */
00242 
00243 /* allocate some core, this memory has overhead no larger than a page
00244    in size and it cannot be released. the storage is returned cleared */
00245 void *getcore(int nbytes);
00246 
00247 /* return log of a number to the base 2 */
00248 int log_base2(int n);
00249 
00250 /* return string describing elapsed time, passed in SEC in seconds */
00251 char *elapsed_time(long sec);
00252 
00253 /* assume bit positions numbered 31 to 0 (31 high order bit), extract num bits
00254    from word starting at position pos (with pos as the high order bit of those
00255    to be extracted), result is right justified and zero filled to high order
00256    bit, for example, extractl(word, 6, 3) w/ 8 bit word = 01101011 returns
00257    00000110 */
00258 unsigned int
00259 extractl(int word,              /* the word from which to extract */
00260          int pos,               /* bit positions 31 to 0 */
00261          int num);              /* number of bits to extract */
00262 
00263 #if defined(sparc) && !defined(__svr4__)
00264 #define strtoul strtol
00265 #endif
00266 
00267 /* portable 64-bit I/O package */
00268 
00269 /* portable vsprintf with qword support, returns end pointer */
00270 char *myvsprintf(char *obuf, char *format, va_list v);
00271 
00272 /* portable sprintf with qword support, returns end pointer */
00273 char *mysprintf(char *obuf, char *format, ...);
00274 
00275 /* portable vfprintf with qword support, returns end pointer */
00276 void myvfprintf(FILE *stream, char *format, va_list v);
00277 
00278 /* portable fprintf with qword support, returns end pointer */
00279 void myfprintf(FILE *stream, char *format, ...);
00280 
00281 #ifdef HOST_HAS_QWORD
00282 
00283 /* convert a string to a signed result */
00284 sqword_t myatosq(char *nptr, char **endp, int base);
00285 
00286 /* convert a string to a unsigned result */
00287 qword_t myatoq(char *nptr, char **endp, int base);
00288 
00289 #endif /* HOST_HAS_QWORD */
00290 
00291 /* same semantics as fopen() except that filenames ending with a ".gz" or ".Z"
00292    will be automagically get compressed */
00293 FILE *gzopen(char *fname, char *type);
00294 
00295 /* close compressed stream */
00296 void gzclose(FILE *fd);
00297 
00298 /* update the CRC on the data block one byte at a time */
00299 word_t crc(word_t crc_accum, word_t data);
00300 
00301 #endif /* MISC_H */


UVa CS Department of Computer Science
School of Engineering, University of Virginia
151 Engineer's Way, P.O. Box 400740
Charlottesville, Virginia 22904-4740

(434) 982-2200  Fax: (434) 982-2214