"Fossies" - the Fresh Open Source Software Archive

Member "heaplayers-351/allocators/fragmentation/util.h" (30 Oct 2004, 1448 Bytes) of package /linux/misc/old/heaplayers_3_5_1.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 "util.h" see the Fossies "Dox" file reference documentation.

    1 #ifndef _UTIL_H_
    2 #define _UTIL_H_
    3 
    4 #include <assert.h>
    5 #include <stdlib.h>
    6 #include <stdio.h>
    7 #include <stdarg.h>
    8 
    9 #define BUF_SZ 1000
   10 
   11 //output debug messages without invoking malloc()
   12 void dbprintf(const char *fmt, ...) {
   13 #ifdef DEBUG
   14 
   15 #if DB_PRINT_TO_FILE
   16     static int debug_log = open("debug.log", O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
   17     if (debug_log < 0) {
   18         abort();
   19     }
   20 #endif
   21 
   22     char buf[BUF_SZ];
   23     va_list ap;
   24     va_start(ap, fmt);
   25 
   26     int n = vsnprintf(buf, BUF_SZ, fmt, ap);
   27     if (n < 0 || n >= BUF_SZ) {
   28         abort();
   29     }
   30 
   31 #if DB_PRINT_TO_FILE
   32     write(debug_log, buf, n);
   33 #else
   34     printf("<dbprintf>");
   35     printf(buf);
   36 #endif
   37 
   38 #endif
   39 }
   40 
   41 #ifdef MYASSERT
   42 #undef assert
   43 
   44 void __assert_fail (__const char *__assertion, __const char *__file,
   45                     unsigned int __line, __const char *__function) {
   46     dbprintf("ASSERTION FAILED!\n[%s:%d] in function %s:\n(%s) was false!\n", __file, __line, __function, __assertion);
   47     abort();
   48 }
   49 
   50 #define __ASSERT_FUNCTION __PRETTY_FUNCTION__
   51 
   52 #define assert(expr) \
   53   (__ASSERT_VOID_CAST ((expr) ? 0 :                       \
   54                (__assert_fail (__STRING(expr), __FILE__, __LINE__,    \
   55                        __ASSERT_FUNCTION), 0)))
   56 
   57 #else
   58 
   59 #include <assert.h>
   60 
   61 #endif
   62 
   63 
   64 inline void memClean(void * start, size_t bytes) {
   65     assert(reinterpret_cast<size_t>(start) % sizeof(size_t) == 0);
   66     assert(bytes % sizeof(size_t) == 0);
   67 
   68     for (int i = 0; i < bytes / sizeof(size_t); i++) {
   69         reinterpret_cast<size_t *>(start)[i] = 0;
   70     }
   71 }
   72 
   73 #endif