1 // Copyright (c) 2011, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 // --- 31 // Author: Craig Silverstein <opensource (at) google.com> 32 // 33 // Used on systems that don't have their own definition of 34 // malloc/new/etc. (Typically this will be a windows msvcrt.dll that 35 // has been edited to remove the definitions.) We can just define our 36 // own as normal functions. 37 // 38 // This should also work on systems were all the malloc routines are 39 // defined as weak symbols, and there's no support for aliasing. 40 41 #ifndef TCMALLOC_LIBC_OVERRIDE_REDEFINE_H_ 42 #define TCMALLOC_LIBC_OVERRIDE_REDEFINE_H_ 43 44 #ifdef HAVE_SYS_CDEFS_H 45 #include <sys/cdefs.h> // for __THROW 46 #endif 47 48 #ifndef __THROW // I guess we're not on a glibc-like system 49 # define __THROW // __THROW is just an optimization, so ok to make it "" 50 #endif 51 52 void* operator new(size_t size) { return tc_new(size); } 53 void operator delete(void* p) __THROW { tc_delete(p); } 54 void* operator new[](size_t size) { return tc_newarray(size); } 55 void operator delete[](void* p) __THROW { tc_deletearray(p); } 56 void* operator new(size_t size, const std::nothrow_t& nt) __THROW { 57 return tc_new_nothrow(size, nt); 58 } 59 void* operator new[](size_t size, const std::nothrow_t& nt) __THROW { 60 return tc_newarray_nothrow(size, nt); 61 } 62 void operator delete(void* ptr, const std::nothrow_t& nt) __THROW { 63 return tc_delete_nothrow(ptr, nt); 64 } 65 void operator delete[](void* ptr, const std::nothrow_t& nt) __THROW { 66 return tc_deletearray_nothrow(ptr, nt); 67 } 68 extern "C" { 69 void* malloc(size_t s) __THROW { return tc_malloc(s); } 70 void free(void* p) __THROW { tc_free(p); } 71 void* realloc(void* p, size_t s) __THROW { return tc_realloc(p, s); } 72 void* calloc(size_t n, size_t s) __THROW { return tc_calloc(n, s); } 73 void cfree(void* p) __THROW { tc_cfree(p); } 74 void* memalign(size_t a, size_t s) __THROW { return tc_memalign(a, s); } 75 void* valloc(size_t s) __THROW { return tc_valloc(s); } 76 void* pvalloc(size_t s) __THROW { return tc_pvalloc(s); } 77 int posix_memalign(void** r, size_t a, size_t s) __THROW { 78 return tc_posix_memalign(r, a, s); 79 } 80 void malloc_stats(void) __THROW { tc_malloc_stats(); } 81 int mallopt(int cmd, int v) __THROW { return tc_mallopt(cmd, v); } 82 #ifdef HAVE_STRUCT_MALLINFO 83 struct mallinfo mallinfo(void) __THROW { return tc_mallinfo(); } 84 #endif 85 size_t malloc_size(void* p) __THROW { return tc_malloc_size(p); } 86 size_t malloc_usable_size(void* p) __THROW { return tc_malloc_size(p); } 87 } // extern "C" 88 89 // No need to do anything at tcmalloc-registration time: we do it all 90 // via overriding weak symbols (at link time). 91 static void ReplaceSystemAlloc() { } 92 93 #endif // TCMALLOC_LIBC_OVERRIDE_REDEFINE_H_ 94