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 <stdlib.h> // for abort() 00040 00041 //------------------------------------------------------------------- 00042 // Utility routines 00043 //------------------------------------------------------------------- 00044 00045 // Safe debugging routine: we write directly to the stderr file 00046 // descriptor and avoid FILE buffering because that may invoke 00047 // malloc() 00048 extern void TCMalloc_MESSAGE(const char* format, ...) 00049 #ifdef HAVE___ATTRIBUTE__ 00050 __attribute__ ((__format__ (__printf__, 1, 2))) 00051 #endif 00052 ; 00053 00054 // Short form for convenience 00055 #define MESSAGE TCMalloc_MESSAGE 00056 00057 // Like assert(), but executed even in NDEBUG mode 00058 #undef CHECK_CONDITION 00059 #define CHECK_CONDITION(cond) \ 00060 do { \ 00061 if (!(cond)) { \ 00062 MESSAGE("%s:%d: assertion failed: %s\n", __FILE__, __LINE__, #cond); \ 00063 abort(); \ 00064 } \ 00065 } while (0) 00066 00067 // Our own version of assert() so we can avoid hanging by trying to do 00068 // all kinds of goofy printing while holding the malloc lock. 00069 #ifndef NDEBUG 00070 #define ASSERT(cond) CHECK_CONDITION(cond) 00071 #else 00072 #define ASSERT(cond) ((void) 0) 00073 #endif 00074 00075 // Print into buffer 00076 class TCMalloc_Printer { 00077 private: 00078 char* buf_; // Where should we write next 00079 int left_; // Space left in buffer (including space for \0) 00080 00081 public: 00082 // REQUIRES: "length > 0" 00083 TCMalloc_Printer(char* buf, int length) : buf_(buf), left_(length) { 00084 buf[0] = '\0'; 00085 } 00086 00087 void printf(const char* format, ...) 00088 #ifdef HAVE___ATTRIBUTE__ 00089 __attribute__ ((__format__ (__printf__, 2, 3))) 00090 #endif 00091 ; 00092 }; 00093 00094 #endif // TCMALLOC_INTERNAL_LOGGING_H__