00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "global.h"
00022 #include "htdebug.h"
00023
00024 #include <signal.h>
00025 #include <stdarg.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028
00029
00030 #define CPU_CLOCK 500000000
00031
00032 void ht_assert_failed(char *file, int line, char *assertion)
00033 {
00034 fprintf(stderr, "in file %s, line %d: assertion failed: %s\n", file, line, assertion);
00035 #ifndef WIN32
00036 #if 1
00037 fprintf(stderr, "sending SIGTRAP...");
00038 raise(SIGTRAP);
00039 #endif
00040 #endif
00041 exit(1);
00042 }
00043
00044 void ht_error(char *file, int line, char *format,...)
00045 {
00046 va_list arg;
00047 va_start(arg, format);
00048 fprintf(stderr, "in file %s, line %d: error: ", file, line);
00049 vfprintf(stderr, format, arg);
00050 fputc('\n', stderr);
00051 va_end(arg);
00052 exit(1);
00053 }
00054
00055 void ht_trace(char *file, int line, char *format,...)
00056 {
00057 FILE *ht_debug_file = stderr;
00058 va_list arg;
00059 va_start(arg, format);
00060 fprintf(ht_debug_file, "TRACE: %s.%d: ", file, line);
00061 vfprintf(ht_debug_file, format, arg);
00062 fputc('\n', ht_debug_file);
00063 va_end(arg);
00064 }
00065
00066 void ht_warn(char *file, int line, char *format,...)
00067 {
00068 va_list arg;
00069 va_start(arg, format);
00070 fprintf(stderr, "in file %s, line %d: warning: ", file, line);
00071 vfprintf(stderr, format, arg);
00072 fputc('\n', stderr);
00073 va_end(arg);
00074 }
00075
00076 #ifdef DJGPP
00077
00078 #define MAX_TIMERS 20
00079
00080 timepoint timer_start[MAX_TIMERS];
00081 timepoint timer_end[MAX_TIMERS];
00082 int handle[MAX_TIMERS];
00083 int handle_count=0;
00084
00085 timer_handle getfreetimerhandle()
00086 {
00087 timer_handle i;
00088 for (i=0; i<handle_count; i++) {
00089 if (!handle[i]) return i;
00090 }
00091 return -1;
00092 }
00093
00094 timer_handle new_timer()
00095 {
00096 int h=getfreetimerhandle();
00097 if (h==-1) {
00098 if (handle_count<MAX_TIMERS) {
00099 h=handle_count++;
00100 } else return -1;
00101 }
00102 handle[h]=1;
00103 timer_start[h] = to_qword(0);
00104 timer_end[h] = to_qword(0);
00105 return h;
00106 }
00107
00108 void start_timer(timer_handle h)
00109 {
00110 if ((h<0) || (h>handle_count)) return;
00111 dword s0, s1;
00112 asm("rdtsc" : "=a" (s0), "=d" (s1));
00113 timer_start[h].hi=s1;
00114 timer_start[h].lo=s0;
00115 }
00116
00117 void stop_timer(timer_handle h)
00118 {
00119 if ((h<0) || (h>handle_count)) return;
00120 dword e0, e1;
00121 asm("rdtsc" : "=a" (e0), "=d" (e1));
00122 timer_end[h].hi=e1;
00123 timer_end[h].lo=e0;
00124 }
00125
00126 void delete_timer(timer_handle h)
00127 {
00128 if ((h<0) || (h>handle_count)) return;
00129 handle[h]=0;
00130 }
00131
00132 void get_timer_tick_internal(timer_handle h, timepoint *p)
00133 {
00134 if ((h<0) || (h>handle_count)) {
00135 *p = to_qword(0);
00136 return;
00137 }
00138 *p = timer_end[h] - timer_start[h];
00139 }
00140
00141 dword get_timer_sec(timer_handle h)
00142 {
00143 if ((h<0) || (h>handle_count)) return 0;
00144 timepoint t;
00145 get_timer_tick_internal(h, &t);
00146 t = t / to_qword(CPU_CLOCK);
00147 if (t.hi) return 0xffffffff; else return t.lo;
00148 }
00149
00150 dword get_timer_msec(timer_handle h)
00151 {
00152 if ((h<0) || (h>handle_count)) return 0;
00153 timepoint t;
00154 get_timer_tick_internal(h, &t);
00155 t = t / to_qword(CPU_CLOCK/1000);
00156 if (t.hi) return 0xffffffff; else return t.lo;
00157 }
00158
00159 dword get_timer_tick(timer_handle h)
00160 {
00161 timepoint t;
00162 get_timer_tick_internal(h, &t);
00163 return t.lo;
00164 }
00165
00166 #else
00167
00168 timer_handle new_timer()
00169 {
00170 return -1;
00171 }
00172
00173 void start_timer(timer_handle handle)
00174 {
00175 }
00176
00177 void stop_timer(timer_handle handle)
00178 {
00179 }
00180
00181 void delete_timer(timer_handle handle)
00182 {
00183 }
00184
00185 dword get_timer_sec(timer_handle handle)
00186 {
00187 return 0;
00188 }
00189
00190 dword get_timer_msec(timer_handle handle)
00191 {
00192 return 0;
00193 }
00194
00195 dword get_timer_tick(timer_handle h)
00196 {
00197 return 0;
00198 }
00199
00200 #endif