00001 // Copyright (c) 2005, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 // --- 00031 // Author: Sanjay Ghemawat <opensource@google.com> 00032 // 00033 // Internal logging and related utility routines. 00034 00035 #ifndef TCMALLOC_INTERNAL_LOGGING_H__ 00036 #define TCMALLOC_INTERNAL_LOGGING_H__ 00037 00038 #include "config.h" 00039 #include <stdarg.h> 00040 #include <stdlib.h> 00041 #include <unistd.h> 00042 00043 //------------------------------------------------------------------- 00044 // Utility routines 00045 //------------------------------------------------------------------- 00046 00047 struct TCMallocDebug { 00048 static int level; 00049 00050 enum { kNone, kInfo, kVerbose }; 00051 }; 00052 00053 // Safe debugging routine: we write directly to the stderr file 00054 // descriptor and avoid FILE buffering because that may invoke 00055 // malloc() 00056 extern void TCMalloc_MESSAGE(const char* format, ...) 00057 #ifdef HAVE___ATTRIBUTE__ 00058 __attribute__ ((__format__ (__printf__, 1, 2))) 00059 #endif 00060 ; 00061 00062 // Short form for convenience 00063 #define MESSAGE TCMalloc_MESSAGE 00064 00065 // Like assert(), but executed even in NDEBUG mode 00066 #undef CHECK_CONDITION 00067 #define CHECK_CONDITION(cond) \ 00068 do { \ 00069 if (!(cond)) { \ 00070 MESSAGE("%s:%d: assertion failed: %s\n", __FILE__, __LINE__, #cond); \ 00071 abort(); \ 00072 } \ 00073 } while (0) 00074 00075 // Our own version of assert() so we can avoid hanging by trying to do 00076 // all kinds of goofy printing while holding the malloc lock. 00077 #ifndef NDEBUG 00078 #define ASSERT(cond) CHECK_CONDITION(cond) 00079 #else 00080 #define ASSERT(cond) ((void) 0) 00081 #endif 00082 00083 // Print into buffer 00084 class TCMalloc_Printer { 00085 private: 00086 char* buf_; // Where should we write next 00087 int left_; // Space left in buffer (including space for \0) 00088 00089 public: 00090 // REQUIRES: "length > 0" 00091 TCMalloc_Printer(char* buf, int length) : buf_(buf), left_(length) { 00092 buf[0] = '\0'; 00093 } 00094 00095 void printf(const char* format, ...) 00096 #ifdef HAVE___ATTRIBUTE__ 00097 __attribute__ ((__format__ (__printf__, 2, 3))) 00098 #endif 00099 ; 00100 }; 00101 00102 #endif // TCMALLOC_INTERNAL_LOGGING_H__