"Fossies" - the Fresh Open Source Software Archive

Member "links-1.03/error.c" (22 Sep 2011, 3891 Bytes) of archive /linux/www/links-1.03.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "error.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.8_vs_1.03.

    1 #include "links.h"
    2 
    3 char dummy_val;
    4 volatile char *dummy_ptr = &dummy_val;
    5 
    6 void do_not_optimize_here(void *p)
    7 {
    8     *dummy_ptr = 0;
    9     /* break ANSI aliasing */
   10 }
   11 
   12 #ifdef LEAK_DEBUG
   13 long mem_amount = 0;
   14 long last_mem_amount = -1;
   15 #ifdef LEAK_DEBUG_LIST
   16 struct list_head memory_list = { &memory_list, &memory_list };
   17 #endif
   18 #endif
   19 
   20 static inline void force_dump()
   21 {
   22     fprintf(stderr, "\n\033[1m%s\033[0m\n", "Forcing core dump");
   23     fflush(stdout);
   24     fflush(stderr);
   25     fatal_tty_exit();
   26     raise(SIGSEGV);
   27 }
   28 
   29 void check_memory_leaks()
   30 {
   31 #ifdef LEAK_DEBUG
   32     if (mem_amount) {
   33         fprintf(stderr, "\n\033[1mMemory leak by %ld bytes\033[0m\n", mem_amount);
   34 #ifdef LEAK_DEBUG_LIST
   35         fprintf(stderr, "\nList of blocks: ");
   36         {
   37             int r = 0;
   38             struct alloc_header *ah;
   39             foreach (ah, memory_list) {
   40                 fprintf(stderr, "%s%p:%lu @ %s:%d", r ? ", ": "", (char *)ah + L_D_S, (unsigned long)ah->size, ah->file, ah->line), r = 1;
   41                 if (ah->comment) fprintf(stderr, ":\"%s\"", ah->comment);
   42             }
   43             fprintf(stderr, "\n");
   44         }
   45 #endif
   46         force_dump();
   47     }
   48 #endif
   49 }
   50 
   51 void er(int b, unsigned char *m, va_list l)
   52 {
   53     if (b) fprintf(stderr, "%c", (char)7);
   54 #ifdef HAVE_VPRINTF
   55     vfprintf(stderr, m, l);
   56 #else
   57     fprintf(stderr, "%s", m);
   58 #endif
   59     fprintf(stderr, "\n");
   60     sleep(1);
   61 }
   62 
   63 void error(unsigned char *m, ...)
   64 {
   65     va_list l;
   66     va_start(l, m);
   67     er(1, m, l);
   68     va_end(l);
   69 }
   70 
   71 int errline;
   72 unsigned char *errfile;
   73 
   74 unsigned char errbuf[4096];
   75 
   76 void int_error(unsigned char *m, ...)
   77 {
   78     va_list l;
   79     va_start(l, m);
   80     sprintf(errbuf, "\033[1mINTERNAL ERROR\033[0m at %s:%d: ", errfile, errline);
   81     strcat(errbuf, m);
   82     er(1, errbuf, l);
   83     force_dump();
   84     va_end(l);
   85 }
   86 
   87 void debug_msg(unsigned char *m, ...)
   88 {
   89     va_list l;
   90     va_start(l, m);
   91     sprintf(errbuf, "DEBUG MESSAGE at %s:%d: ", errfile, errline);
   92     strcat(errbuf, m);
   93     er(0, errbuf, l);
   94     va_end(l);
   95 }
   96 
   97 #ifdef LEAK_DEBUG
   98 
   99 void *debug_mem_alloc(unsigned char *file, int line, size_t size)
  100 {
  101     void *p;
  102 #ifdef LEAK_DEBUG
  103     struct alloc_header *ah;
  104 #endif
  105     if (!size) return DUMMY;
  106     if (size > MAXINT) overalloc();
  107 #ifdef LEAK_DEBUG
  108     mem_amount += size;
  109     size += L_D_S;
  110 #endif
  111     if (!(p = xmalloc(size))) {
  112         error("ERROR: out of memory (malloc returned NULL)");
  113         fatal_tty_exit();
  114         exit(RET_FATAL);
  115         return NULL;
  116     }
  117 #ifdef LEAK_DEBUG
  118     ah = p;
  119     p = (char *)p + L_D_S;
  120     ah->size = size - L_D_S;
  121 #ifdef LEAK_DEBUG_LIST
  122     ah->file = file;
  123     ah->line = line;
  124     ah->comment = NULL;
  125     add_to_list(memory_list, ah);
  126 #endif
  127 #endif
  128     return p;
  129 }
  130 
  131 void debug_mem_free(unsigned char *file, int line, void *p)
  132 {
  133 #ifdef LEAK_DEBUG
  134     struct alloc_header *ah;
  135 #endif
  136     if (p == DUMMY) return;
  137     if (!p) {
  138         errfile = file, errline = line, int_error("mem_free(NULL)");
  139         return;
  140     }
  141 #ifdef LEAK_DEBUG
  142     p = (char *)p - L_D_S;
  143     ah = p;
  144 #ifdef LEAK_DEBUG_LIST
  145     del_from_list(ah);
  146     if (ah->comment) free(ah->comment);
  147 #endif
  148     mem_amount -= ah->size;
  149 #endif
  150     xfree(p);
  151 }
  152 
  153 void *debug_mem_realloc(unsigned char *file, int line, void *p, size_t size)
  154 {
  155 #ifdef LEAK_DEBUG
  156     struct alloc_header *ah;
  157 #endif
  158     if (p == DUMMY) return debug_mem_alloc(file, line, size);
  159     if (!p) {
  160         errfile = file, errline = line, int_error("mem_realloc(NULL, %d)", size);
  161         return NULL;
  162     }
  163     if (!size) {
  164         debug_mem_free(file, line, p);
  165         return DUMMY;
  166     }
  167     if (size > MAXINT) overalloc();
  168     if (!(p = xrealloc((char *)p - L_D_S, size + L_D_S))) {
  169         error("ERROR: out of memory (realloc returned NULL)");
  170         fatal_tty_exit();
  171         exit(RET_FATAL);
  172         return NULL;
  173     }
  174 #ifdef LEAK_DEBUG
  175     ah = p;
  176     mem_amount += size - ah->size;
  177     ah->size = size;
  178 #ifdef LEAK_DEBUG_LIST
  179     ah->prev->next = ah;
  180     ah->next->prev = ah;
  181 #endif
  182 #endif
  183     return (char *)p + L_D_S;
  184 }
  185 
  186 void set_mem_comment(void *p, unsigned char *c, int l)
  187 {
  188 #ifdef LEAK_DEBUG_LIST
  189     struct alloc_header *ah = (struct alloc_header *)((char *)p - L_D_S);
  190     if (ah->comment) free(ah->comment);
  191     if ((ah->comment = malloc(l + 1))) memcpy(ah->comment, c, l), ah->comment[l] = 0;
  192 #endif
  193 }
  194 
  195 #endif
  196