00001
00002
00003
00004
00005
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
00046
00047
00048
00049
00050
00051
00052 #define NIL(type) ((type *) 0)
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 # define ALLOC(type, num) \
00064 ((type *) MMalloc((long) sizeof(type) * (long) (num)))
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 # define REALLOC(type, obj, num) \
00077 ((type *) MMrealloc((char *) (obj), (long) sizeof(type) * (long) (num)))
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 #define USE __attribute__((unused))
00096
00097
00098
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
00132
00133
00134
00135
00136
00137
00138 typedef enum
00139 {
00140 Util_FileOverWrite_c,
00141 Util_FileAppend_c
00142 } Util_FileOpenMode_t;
00143
00144
00145
00146
00147
00148
00149
00150
00151
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
00166
00167
00168
00169
00170
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
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
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
00249 double util_unit_rand ();
00250 int util_rand_int ();
00251
00252
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
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