Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: third_party/tcmalloc/chromium/src/libc_override_glibc.h

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

Powered by Google App Engine
This is Rietveld 408576698