OLD | NEW |
| (Empty) |
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@google.com> | |
32 // | |
33 // Used to override malloc routines on systems that are using glibc. | |
34 | |
35 #ifndef TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ | |
36 #define TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ | |
37 | |
38 // MALLOC_HOOK_MAYBE_VOLATILE is defined at config.h in the original gperftools. | |
39 // Chromium does this check with the macro __MALLOC_HOOK_VOLATILE. | |
40 // GLibc 2.14+ requires the hook functions be declared volatile, based on the | |
41 // value of the define __MALLOC_HOOK_VOLATILE. For compatibility with | |
42 // older/non-GLibc implementations, provide an empty definition. | |
43 #if !defined(__MALLOC_HOOK_VOLATILE) | |
44 #define MALLOC_HOOK_MAYBE_VOLATILE /**/ | |
45 #else | |
46 #define MALLOC_HOOK_MAYBE_VOLATILE __MALLOC_HOOK_VOLATILE | |
47 #endif | |
48 | |
49 #include <config.h> | |
50 #include <features.h> // for __GLIBC__ | |
51 #ifdef HAVE_SYS_CDEFS_H | |
52 #include <sys/cdefs.h> // for __THROW | |
53 #endif | |
54 #include <gperftools/tcmalloc.h> | |
55 | |
56 #ifndef __GLIBC__ | |
57 # error libc_override_glibc.h is for glibc distributions only. | |
58 #endif | |
59 | |
60 // In glibc, the memory-allocation methods are weak symbols, so we can | |
61 // just override them with our own. If we're using gcc, we can use | |
62 // __attribute__((alias)) to do the overriding easily (exception: | |
63 // Mach-O, which doesn't support aliases). Otherwise we have to use a | |
64 // function call. | |
65 #if !defined(__GNUC__) || defined(__MACH__) | |
66 | |
67 // This also defines ReplaceSystemAlloc(). | |
68 # include "libc_override_redefine.h" // defines functions malloc()/etc | |
69 | |
70 #else // #if !defined(__GNUC__) || defined(__MACH__) | |
71 | |
72 // If we get here, we're a gcc system, so do all the overriding we do | |
73 // with gcc. This does the overriding of all the 'normal' memory | |
74 // allocation. This also defines ReplaceSystemAlloc(). | |
75 # include "libc_override_gcc_and_weak.h" | |
76 | |
77 // We also have to do some glibc-specific overriding. Some library | |
78 // routines on RedHat 9 allocate memory using malloc() and free it | |
79 // using __libc_free() (or vice-versa). Since we provide our own | |
80 // implementations of malloc/free, we need to make sure that the | |
81 // __libc_XXX variants (defined as part of glibc) also point to the | |
82 // same implementations. Since it only matters for redhat, we | |
83 // do it inside the gcc #ifdef, since redhat uses gcc. | |
84 // TODO(csilvers): only do this if we detect we're an old enough glibc? | |
85 | |
86 #define ALIAS(tc_fn) __attribute__ ((alias (#tc_fn))) | |
87 extern "C" { | |
88 void* __libc_malloc(size_t size) ALIAS(tc_malloc); | |
89 void __libc_free(void* ptr) ALIAS(tc_free); | |
90 void* __libc_realloc(void* ptr, size_t size) ALIAS(tc_realloc); | |
91 void* __libc_calloc(size_t n, size_t size) ALIAS(tc_calloc); | |
92 void __libc_cfree(void* ptr) ALIAS(tc_cfree); | |
93 void* __libc_memalign(size_t align, size_t s) ALIAS(tc_memalign); | |
94 void* __libc_valloc(size_t size) ALIAS(tc_valloc); | |
95 void* __libc_pvalloc(size_t size) ALIAS(tc_pvalloc); | |
96 int __posix_memalign(void** r, size_t a, size_t s) ALIAS(tc_posix_memalign); | |
97 } // extern "C" | |
98 #undef ALIAS | |
99 | |
100 #endif // #if defined(__GNUC__) && !defined(__MACH__) | |
101 | |
102 | |
103 // We also have to hook libc malloc. While our work with weak symbols | |
104 // should make sure libc malloc is never called in most situations, it | |
105 // can be worked around by shared libraries with the DEEPBIND | |
106 // environment variable set. The below hooks libc to call our malloc | |
107 // routines even in that situation. In other situations, this hook | |
108 // should never be called. | |
109 extern "C" { | |
110 static void* glibc_override_malloc(size_t size, const void *caller) { | |
111 return tc_malloc(size); | |
112 } | |
113 static void* glibc_override_realloc(void *ptr, size_t size, | |
114 const void *caller) { | |
115 return tc_realloc(ptr, size); | |
116 } | |
117 static void glibc_override_free(void *ptr, const void *caller) { | |
118 tc_free(ptr); | |
119 } | |
120 static void* glibc_override_memalign(size_t align, size_t size, | |
121 const void *caller) { | |
122 return tc_memalign(align, size); | |
123 } | |
124 | |
125 // We should be using __malloc_initialize_hook here, like the #if 0 | |
126 // code below. (See http://swoolley.org/man.cgi/3/malloc_hook.) | |
127 // However, this causes weird linker errors with programs that link | |
128 // with -static, so instead we just assign the vars directly at | |
129 // static-constructor time. That should serve the same effect of | |
130 // making sure the hooks are set before the first malloc call the | |
131 // program makes. | |
132 #if 0 | |
133 #include <malloc.h> // for __malloc_hook, etc. | |
134 void glibc_override_malloc_init_hook(void) { | |
135 __malloc_hook = glibc_override_malloc; | |
136 __realloc_hook = glibc_override_realloc; | |
137 __free_hook = glibc_override_free; | |
138 __memalign_hook = glibc_override_memalign; | |
139 } | |
140 | |
141 void (* MALLOC_HOOK_MAYBE_VOLATILE __malloc_initialize_hook)(void) | |
142 = &glibc_override_malloc_init_hook; | |
143 #endif | |
144 | |
145 void* (* MALLOC_HOOK_MAYBE_VOLATILE __malloc_hook)(size_t, const void*) | |
146 = &glibc_override_malloc; | |
147 void* (* MALLOC_HOOK_MAYBE_VOLATILE __realloc_hook)(void*, size_t, const void*) | |
148 = &glibc_override_realloc; | |
149 void (* MALLOC_HOOK_MAYBE_VOLATILE __free_hook)(void*, const void*) | |
150 = &glibc_override_free; | |
151 void* (* MALLOC_HOOK_MAYBE_VOLATILE __memalign_hook)(size_t,size_t, const void*) | |
152 = &glibc_override_memalign; | |
153 | |
154 } // extern "C" | |
155 | |
156 // No need to write ReplaceSystemAlloc(); one of the #includes above | |
157 // did it for us. | |
158 | |
159 #endif // TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ | |
OLD | NEW |