annotate src/generic.c @ 54:e109391424b5

some misc. cleanup and convert to mercurial source control
author carl@ns.five-ten-sg.com
date Thu, 31 Jan 2008 17:32:54 -0800
parents f6db1f060a95
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
1
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
2
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
3 #include <ctype.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
4 #include <errno.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
5 #include <malloc.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
6 #include <signal.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
7 #include <stdarg.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
8 #include <stdio.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
9 #include <stdlib.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
10 #include <string.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
11 #include "generic.h"
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
12
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
13 #ifdef WITH_DMALLOC
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
14 #include <dmalloc.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
15 #endif
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
16
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
17
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
18
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
19
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
20 void pDIE( char *fmt, ... )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
21 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
22 va_list ap;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
23 va_start( ap, fmt );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
24 //fprintf( stderr, "Fatal error (will segfault): ");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
25 vfprintf( stderr, fmt, ap );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
26 fprintf( stderr, "\n" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
27 va_end(ap);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
28 raise( SIGSEGV );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
29 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
30
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
31 void pWARN( char *fmt, ... )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
32 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
33 va_list ap;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
34 va_start( ap, fmt );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
35 fprintf( stderr, "WARNING: ");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
36 vfprintf( stderr, fmt, ap );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
37 fprintf( stderr, "\n" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
38 va_end(ap);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
39 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
40
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
41 void *F_MALLOC( size_t size )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
42 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
43 void *result;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
44
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
45 result = malloc( size );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
46 ASSERT( NULL != result, "malloc() failure." );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
47
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
48 return result;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
49 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
50
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
51 void *F_REALLOC( void *p, size_t size )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
52 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
53 void *result;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
54
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
55 //if( NULL != p ) hexdump((char*)p - 128, 0, 128, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
56 if(!p) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
57 ASSERT( NULL != ( result = malloc( size ) ), "malloc() failure." );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
58 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
59 else {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
60 ASSERT( NULL != ( result = realloc( p, size ) ), "realloc() failure." );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
61 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
62
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
63 //hexdump((char*)result - 128, 0, 128, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
64 fflush(stderr);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
65 return result;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
66 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
67
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
68
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
69
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
70 int DEBUG_LEVEL = DB_INFO;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
71
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
72 void db_default( char *file, int line, int level, char *fmt, ... )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
73 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
74 va_list ap;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
75 if( level <= DEBUG_LEVEL ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
76 switch( level ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
77 case DB_CRASH:
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
78 fprintf(stderr, "CRASH");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
79 break;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
80 case DB_ERR:
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
81 fprintf(stderr, "ERROR");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
82 break;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
83 case DB_WARN:
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
84 fprintf(stderr, "WARNING");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
85 break;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
86 case DB_INFO:
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
87 case DB_VERB:
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
88 break;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
89 default:
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
90 fprintf(stderr, "DEBUG(%d)", level );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
91 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
92
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
93 if( level <= DB_WARN )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
94 fprintf(stderr, " (%s:%d)", file, line );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
95
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
96 if( DB_INFO != level && DB_VERB != level )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
97 fprintf(stderr, ": ");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
98
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
99 va_start( ap, fmt );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
100 vfprintf(stderr, fmt, ap );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
101 fprintf(stderr, "\n" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
102 va_end( ap );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
103 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
104 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
105
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
106 void (*dbfunc)(char *file, int line, int level, char *fmt, ...) = &db_default;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
107
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
108
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
109
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
110