00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00047 #ifndef BASE_COMMON_H
00048 #define BASE_COMMON_H
00049
00050 #ifndef _REENTRANT
00051 #define _REENTRANT
00052 #endif
00053
00054 #include "fastlib/base/basic_types.h"
00055 #include "fastlib/base/compiler.h"
00056 #include "fastlib/base/ansi_colors.h"
00057
00058
00059
00060
00061 #include <stdlib.h>
00062 #include <stdio.h>
00063 #include <string.h>
00064 #include <ctype.h>
00065 #include <math.h>
00066 #include <limits.h>
00067 #include <float.h>
00068
00069 EXTERN_C_BEGIN
00070
00072 #define NOP ((void)0)
00073
00075 #define COMMA ,
00076
00077
00078
00079
00080
00081
00082 #if defined(SCALE_MASSIVE)
00083 #if defined(SCALE_LARGE) || defined(SCALE_NORMAL)
00084 #error Only one of SCALE_MASSIVE, SCALE_LARGE, or SCALE_NORMAL may be defined.
00085 #endif
00086 #elif defined(SCALE_LARGE)
00087 #if defined(SCALE_NORMAL)
00088 #error Only one of SCALE_MASSIVE, SCALE_LARGE, or SCALE_NORMAL may be defined.
00089 #endif
00090 #elif !defined(SCALE_NORMAL)
00091 #define SCALE_NORMAL
00092 #endif
00093
00112 #if defined(SCALE_MASSIVE)
00113 typedef int64 index_t;
00114 #elif defined(SCALE_LARGE)
00115 typedef ssize_t index_t;
00116 #elif defined(SCALE_NORMAL)
00117 typedef int index_t;
00118 #endif
00119
00129 #if defined(SCALE_MASSIVE)
00130 #define LI L64
00131 #elif defined(SCALE_LARGE)
00132 #define LI "l"
00133 #elif defined(SCALE_NORMAL)
00134 #define LI ""
00135 #endif
00136
00138 #define KILOBYTE (((size_t)1) << 10)
00139
00140 #define MEGABYTE (((size_t)1) << 20)
00141
00142 #define GIGABYTE (((size_t)1) << 30)
00143
00144
00145
00146
00147
00148
00150 extern int segfault_on_abort;
00152 extern int abort_on_nonfatal;
00154 extern int pause_on_nonfatal;
00156 extern int print_notify_locs;
00157
00159 typedef enum {
00161 FL_MSG_FATAL = 0,
00163 FL_MSG_NONFATAL = 1,
00165 FL_MSG_NOTIFY_STAR = 2,
00167 FL_MSG_NOTIFY = 3
00168 } fl_msg_t;
00169
00171 extern char fl_msg_marker[];
00173 extern const char *fl_msg_color[];
00174
00181 COMPILER_NO_RETURN
00182 void fl_abort(void);
00183
00189 void fl_pause(void);
00190
00192 void fl_print_msg_header(char marker, const char *color);
00193
00195 void fl_print_msg_loc(const char *file, const char *func, int line);
00196
00198 COMPILER_NO_RETURN
00199 COMPILER_PRINTF(4, 5)
00200 void fl_print_fatal_msg(const char *file, const char *func, int line,
00201 const char* format, ...);
00202
00204 COMPILER_PRINTF(5, 6)
00205 void fl_print_msg(const char *file, const char *func, int line,
00206 fl_msg_t msg_type, const char* format, ...);
00207
00213 #define FATAL(msg_params...) (fl_print_fatal_msg( \
00214 __FILE__, __FUNCTION__, __LINE__, msg_params))
00215
00222 #define NONFATAL(msg_params...) (fl_print_msg( \
00223 __FILE__, __FUNCTION__, __LINE__, FL_MSG_NONFATAL, msg_params))
00224
00231 #define NOTIFY_STAR(msg_params...) (fl_print_msg( \
00232 __FILE__, __FUNCTION__, __LINE__, FL_MSG_NOTIFY_STAR, msg_params))
00233
00240 #define NOTIFY(msg_params...) (fl_print_msg( \
00241 __FILE__, __FUNCTION__, __LINE__, FL_MSG_NOTIFY, msg_params))
00242
00255 void fl_print_progress(const char *name, int perc);
00256
00257
00258
00268 void hex_to_stream(FILE *stream, const char *src, const char *ok_char);
00269
00284 char *hex_to_string(char *dest, const char *src, const char *ok_char);
00285
00297 char *unhex_in_place(char *str);
00298
00299
00300
00301
00302
00322 typedef enum {
00324 SUCCESS_FAIL = 31,
00326 SUCCESS_WARN = 48,
00328 SUCCESS_PASS = 96
00329 } success_t;
00330
00336 #define PASSED(x) (likely((x) >= SUCCESS_PASS))
00337
00343 #define FAILED(x) (unlikely((x) <= SUCCESS_FAIL))
00344
00351 #define MUST_PASS_MSG(x, msg_params...) \
00352 (likely(x >= SUCCESS_PASS) ? NOP : FATAL(msg_params))
00353
00360 #define MUST_PASS(x) \
00361 MUST_PASS_MSG(x, "MUST_PASS failed: %s", #x)
00362
00369 #define MUST_NOT_FAIL_MSG(x, msg_params...) \
00370 (likely(x > SUCCESS_FAIL) ? NOP : FATAL(msg_params))
00371
00378 #define MUST_NOT_FAIL(x) \
00379 MUST_NOT_FAIL_MSG(x, "MUST_NOT_FAIL failed: %s", #x)
00380
00382 #define SUCCESS_FROM_C(x) (unlikely((x) < 0) ? SUCCESS_FAIL : SUCCESS_PASS)
00383
00384
00385
00386 EXTERN_C_END
00387
00388 #endif