Azinix

util.h

Go to the documentation of this file.
00001 /**
00002 
00003   \file util.h
00004 
00005   \brief Low-level utilities
00006 
00007 ******************************************************************************/
00008 
00009 #ifndef _UTIL
00010 #define _UTIL
00011 
00012 #ifdef __cplusplus
00013 extern "C" {
00014 #endif
00015 
00016 #include <stdio.h>
00017 #include <ctype.h>
00018 #include <math.h>
00019 
00020 #include <string.h>
00021 #include <stdlib.h>
00022 
00023 #include <assert.h>
00024 #include <unistd.h>
00025 #include <sys/time.h>
00026 #include <tcl.h>
00027 #include "array.h"
00028 #include "var_set.h"
00029 #include "timing.h"
00030 
00031 #ifdef NM_DEBUG
00032 #define util_assert( x ) if ( !x ) { fail("util_assert fails!\n"); }
00033 #else
00034 #define util_assert( x )
00035 #endif
00036 
00037 #define fail(why) {\
00038     (void) fprintf(stderr, "Fatal error: file %s, line %d\n%s\n",\
00039     __FILE__, __LINE__, why);\
00040     (void) fflush(stdout);\
00041     abort();\
00042 }
00043 
00044 /*
00045  * A neater way to define zero pointers
00046  *
00047  * Usage:
00048  *  int * fred;
00049  *  fred = NIL(int);
00050  */
00051 
00052 #define NIL(type)       ((type *) 0)
00053 
00054 
00055 /** \brief Allocates 'number' objects of type 'type'.  
00056 
00057 This macro should be
00058 used rather than calling <code>malloc()</code> directly because it casts the
00059 arguments appropriately, and <code>ALLOC()</code> will never return <code>NIL(char)</code>,
00060 choosing instead to terminate the program.
00061 */
00062 
00063 #  define ALLOC(type, num)  \
00064     ((type *) MMalloc((long) sizeof(type) * (long) (num)))
00065 
00066 
00067 /** \brief Re-allocate 'obj' to hold 'number' objects of type 'type'.
00068 
00069 This macro should be used rather than calling realloc()
00070 directly because it casts the arguments appropriately, and
00071 REALLOC() will never return NIL(char), instead choosing to
00072 terminate the program.  It also guarantees that REALLOC(type, 0, n)
00073 is the same as ALLOC(type, n).
00074 */
00075 
00076 #  define REALLOC(type, obj, num)   \
00077     ((type *) MMrealloc((char *) (obj), (long) sizeof(type) * (long) (num)))
00078 
00079 /**
00080   *     \brief  Macro to make an unused variable look like it's used.
00081   *
00082   *     Program checkers issue warnings about variables that are
00083   *     declared but never used.  Most of the time, such variables should be removed
00084   *     to simplify the code.  However, on occasion we want to keep such variables,
00085   *     but we don't want program checkers to complain about them.  Using this macro
00086   *     will make an unused variable look like it's being used.  This macro is
00087   *     straight out of the gcc documentation, Chapter 5 GCC 3.4.1.<p>
00088   *
00089   *     Usage:  put USE immediately after the variable gcc -Wall complains about<p>
00090   *     Example: int foo USE;
00091   *
00092   */
00093 
00094 
00095 #define USE  __attribute__((unused))
00096 
00097 
00098 // old decls abound
00099 
00100 #ifndef EXTERN
00101 #  define EXTERN extern
00102 #endif
00103 
00104 #ifndef ABS
00105 #  define ABS(a) ((a) < 0 ? -(a) : (a))
00106 #endif
00107 
00108 #ifndef MAX
00109 #  define MAX(a,b)              ((a) > (b) ? (a) : (b))
00110 #endif
00111 
00112 #ifndef MIN
00113 #  define MIN(a,b)              ((a) < (b) ? (a) : (b))
00114 #endif
00115 
00116 #ifndef HUGE_VAL
00117 #  ifndef HUGE
00118 #    define HUGE  8.9884656743115790e+307
00119 #  endif
00120 #  define HUGE_VAL HUGE
00121 #endif
00122 
00123 #ifndef MAXINT
00124 #  define MAXINT (1 << 30)
00125 #endif
00126 
00127 
00128 
00129 
00130 /**
00131   * \brief  Types of modes in which to open files.
00132   * 
00133   * Types of modes in which to open files.
00134   * 
00135   */
00136 
00137 
00138 typedef enum
00139 {
00140   Util_FileOverWrite_c,
00141   Util_FileAppend_c
00142 } Util_FileOpenMode_t;
00143 
00144 /**
00145   * \brief  Struct to represent int-arrays.
00146   * 
00147   * Struct to represent int-arrays. We allocate a single block
00148   * of sizeof(int) + sizeof(int*) + length * sizeof(int),
00149   * so entries is not malloced seperately.  This saves
00150   * one call to malloc, its associated wastage, also gives
00151   * us better locality of reference.
00152   * 
00153   */
00154 
00155 
00156 struct util_int_array_t
00157 {
00158   int length;
00159   int *entries;
00160 };
00161 
00162 typedef struct util_int_array_t util_int_array_t;
00163 
00164 /**
00165   * \brief  Struct to represent byte-array.
00166   * 
00167   * Struct to represent byte-array. Need length because cannot
00168   * depend on null termination, as 0 may be present in array. As
00169   * with int arrays, allocate as sizeof(int) + sizeof(char*) + 
00170   * length * sizeof(char). 
00171   * 
00172   */
00173 
00174 
00175 struct util_byte_array_t
00176 {
00177   int length;
00178   char *bytes;
00179 };
00180 
00181 typedef struct util_byte_array_t util_byte_array_t;
00182 
00183 
00184 /**
00185   *     \brief  Attribute-value pair used to read formula
00186   *
00187   */
00188 
00189 struct util_attrib_val_t
00190 {
00191   char *rawAttribute;
00192   char *rawValue;
00193 };
00194 
00195 typedef struct util_attrib_val_t util_attrib_val_t;
00196 
00197 
00198 /*---------------------------------------------------------------------------*/
00199 /* Function prototypes                                                       */
00200 /*---------------------------------------------------------------------------*/
00201 
00202 extern void MMout_of_memory (int);
00203 extern char *MMalloc (int);
00204 extern char *MMrealloc (char *, int);
00205 extern void MMfree (char *);
00206 
00207 
00208 extern void util_print_cpu_stats (FILE *);
00209 extern void util_getopt_reset (void);
00210 extern int util_getopt (int, char **, char *);
00211 extern char *util_optarg;
00212 
00213 extern char *util_path_search (char *);
00214 extern char *util_file_search (char *, char *, char *);
00215 extern int util_save_image (char *, char *);
00216 extern int strstrN (char *, char *, int);
00217 extern char *util_strsav (char *);
00218 extern char *util_strchr_N (char *, char, int);
00219 extern int util_strequal (char *, char *);
00220 extern char *util_strmax (char *, char *);
00221 extern char *util_strmin (char *, char *);
00222 extern int util_strequalN (char *, char *, int);
00223 extern char *util_strcat (char *, char *);
00224 extern char *util_strcat3 (char *, char *, char *);
00225 extern char *util_strcat5 (char *, char *, char *, char *, char *);
00226 extern char *util_sgets (array_t *, char *);
00227 extern char *util_file_to_string (FILE *);
00228 extern array_t *util_process_file (char *);
00229 extern array_t *util_get_raw_rules (char *);
00230 extern char *util_get_full_line (array_t *, char *);
00231 
00232 util_byte_array_t *util_byte_array_join (util_byte_array_t *,
00233                      util_byte_array_t *);
00234 int util_byte_array_free (util_byte_array_t *);
00235 int util_byte_array_print (util_byte_array_t *);
00236 int util_byte_array_hash (char *, int);
00237 u_int32_t util_fast_hash (const char *, int);
00238 int util_byte_array_cmp (char *, char *);
00239 util_byte_array_t *util_byte_array_create (char *, int);
00240 util_byte_array_t *util_byte_array_to_lower_case (util_byte_array_t *);
00241 
00242 int util_int_array_print (util_int_array_t *);
00243 int util_int_array_free (util_int_array_t *);
00244 util_int_array_t *util_int_array_create (int);
00245 util_int_array_t *util_arrayT_to_int_array (array_t *);
00246 util_int_array_t *var_set_to_int_array (var_set_t *);
00247 
00248 // random number generation code
00249 double util_unit_rand ();
00250 int util_rand_int ();
00251 
00252 // code for timing 
00253 void util_set_processor_frequency ();
00254 void util_time (char *);
00255 u_int32_t util_time_to_ticks (double);
00256 array_t *util_time_stamp_init (int);
00257 void util_time_stamp_set (array_t *, int);
00258 void util_time_stamp_print (array_t *);
00259 
00260 // code for performance checking
00261 
00262 extern int Perf_CheckHardware (ClientData cd, Tcl_Interp * interp, int objc,
00263                    Tcl_Obj * const objv[]);
00264 extern int Perf_TimePerf (ClientData cd, Tcl_Interp * interp, int objc,
00265               Tcl_Obj * const objv[]);
00266 extern int Perf_TimePerf2 (ClientData cd, Tcl_Interp * interp, int objc,
00267                Tcl_Obj * const objv[]);
00268 extern int Perf_SpacePerf (ClientData cd, Tcl_Interp * interp, int objc,
00269                Tcl_Obj * const objv[]);
00270 extern int Perf_CheckTimingCode (ClientData cd, Tcl_Interp * interp, int objc,
00271                  Tcl_Obj * const objv[]);
00272 
00273 #ifdef __cplusplus
00274 }
00275 #endif
00276 
00277 
00278 #endif /* _UTIL */