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