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

Side by Side Diff: third_party/tcmalloc/vendor/src/libc_override_osx.h

Issue 7430002: Update the tcmalloc vendor branch to r111 (perftools version 1.8) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 5 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
Property Changes:
Added: svn:eol-style
+ LF
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 OS X systems. We use the
34 // malloc-zone functionality built into OS X to register our malloc
35 // routine.
36 //
37 // 1) We used to use the normal 'override weak libc malloc/etc'
38 // technique for OS X. This is not optimal because mach does not
39 // support the 'alias' attribute, so we had to have forwarding
40 // functions. It also does not work very well with OS X shared
41 // libraries (dylibs) -- in general, the shared libs don't use
42 // tcmalloc unless run with the DYLD_FORCE_FLAT_NAMESPACE envvar.
43 //
44 // 2) Another approach would be to use an interposition array:
45 // static const interpose_t interposers[] __attribute__((section("__DATA, _ _interpose"))) = {
46 // { (void *)tc_malloc, (void *)malloc },
47 // { (void *)tc_free, (void *)free },
48 // };
49 // This requires the user to set the DYLD_INSERT_LIBRARIES envvar, so
50 // is not much better.
51 //
52 // 3) Registering a new malloc zone avoids all these issues:
53 // http://www.opensource.apple.com/source/Libc/Libc-583/include/malloc/malloc.h
54 // http://www.opensource.apple.com/source/Libc/Libc-583/gen/malloc.c
55 // If we make tcmalloc the default malloc zone (undocumented but
56 // possible) then all new allocs use it, even those in shared
57 // libraries. Allocs done before tcmalloc was installed, or in libs
58 // that aren't using tcmalloc for some reason, will correctly go
59 // through the malloc-zone interface when free-ing, and will pick up
60 // the libc free rather than tcmalloc free. So it should "never"
61 // cause a crash (famous last words).
62 //
63 // 4) The routines one must define for one's own malloc have changed
64 // between OS X versions. This requires some hoops on our part, but
65 // is only really annoying when it comes to posix_memalign. The right
66 // behavior there depends on what OS version tcmalloc was compiled on,
67 // but also what OS version the program is running on. For now, we
68 // punt and don't implement our own posix_memalign. Apps that really
69 // care can use tc_posix_memalign directly.
70
71 #ifndef TCMALLOC_LIBC_OVERRIDE_OSX_INL_H_
72 #define TCMALLOC_LIBC_OVERRIDE_OSX_INL_H_
73
74 #include <config.h>
75 #ifdef HAVE_FEATURES_H
76 #include <features.h>
77 #endif
78 #include <google/tcmalloc.h>
79
80 #if !defined(__APPLE__)
81 # error libc_override_glibc-osx.h is for OS X distributions only.
82 #endif
83
84 #include <AvailabilityMacros.h>
85 #include <malloc/malloc.h>
86
87 // We need to provide wrappers around all the libc functions.
88 namespace {
89 size_t mz_size(malloc_zone_t* zone, const void* ptr) {
90 if (MallocExtension::instance()->GetOwnership(ptr) != MallocExtension::kOwned)
91 return 0; // malloc_zone semantics: return 0 if we don't own the memory
92
93 // TODO(csilvers): change this method to take a const void*, one day.
94 return MallocExtension::instance()->GetAllocatedSize(const_cast<void*>(ptr));
95 }
96
97 void* mz_malloc(malloc_zone_t* zone, size_t size) {
98 return tc_malloc(size);
99 }
100
101 void* mz_calloc(malloc_zone_t* zone, size_t num_items, size_t size) {
102 return tc_calloc(num_items, size);
103 }
104
105 void* mz_valloc(malloc_zone_t* zone, size_t size) {
106 return tc_valloc(size);
107 }
108
109 void mz_free(malloc_zone_t* zone, void* ptr) {
110 return tc_free(ptr);
111 }
112
113 void* mz_realloc(malloc_zone_t* zone, void* ptr, size_t size) {
114 return tc_realloc(ptr, size);
115 }
116
117 void* mz_memalign(malloc_zone_t* zone, size_t align, size_t size) {
118 return tc_memalign(align, size);
119 }
120
121 void mz_destroy(malloc_zone_t* zone) {
122 // A no-op -- we will not be destroyed!
123 }
124
125 // malloc_introspection callbacks. I'm not clear on what all of these do.
126 kern_return_t mi_enumerator(task_t task, void *,
127 unsigned type_mask, vm_address_t zone_address,
128 memory_reader_t reader,
129 vm_range_recorder_t recorder) {
130 // Should enumerate all the pointers we have. Seems like a lot of work.
131 return KERN_FAILURE;
132 }
133
134 size_t mi_good_size(malloc_zone_t *zone, size_t size) {
135 // I think it's always safe to return size, but we maybe could do better.
136 return size;
137 }
138
139 boolean_t mi_check(malloc_zone_t *zone) {
140 return MallocExtension::instance()->VerifyAllMemory();
141 }
142
143 void mi_print(malloc_zone_t *zone, boolean_t verbose) {
144 int bufsize = 8192;
145 if (verbose)
146 bufsize = 102400; // I picked this size arbitrarily
147 char* buffer = new char[bufsize];
148 MallocExtension::instance()->GetStats(buffer, bufsize);
149 fprintf(stdout, "%s", buffer);
150 delete[] buffer;
151 }
152
153 void mi_log(malloc_zone_t *zone, void *address) {
154 // I don't think we support anything like this
155 }
156
157 void mi_force_lock(malloc_zone_t *zone) {
158 // Hopefully unneeded by us!
159 }
160
161 void mi_force_unlock(malloc_zone_t *zone) {
162 // Hopefully unneeded by us!
163 }
164
165 void mi_statistics(malloc_zone_t *zone, malloc_statistics_t *stats) {
166 // TODO(csilvers): figure out how to fill these out
167 stats->blocks_in_use = 0;
168 stats->size_in_use = 0;
169 stats->max_size_in_use = 0;
170 stats->size_allocated = 0;
171 }
172
173 boolean_t mi_zone_locked(malloc_zone_t *zone) {
174 return false; // Hopefully unneeded by us!
175 }
176
177 } // unnamed namespace
178
179 // OS X doesn't have pvalloc, cfree, malloc_statc, etc, so we can just
180 // define our own. :-) OS X supplies posix_memalign in some versions
181 // but not others, either strongly or weakly linked, in a way that's
182 // difficult enough to code to correctly, that I just don't try to
183 // support either memalign() or posix_memalign(). If you need them
184 // and are willing to code to tcmalloc, you can use tc_posix_memalign().
185 extern "C" {
186 void cfree(void* p) { tc_cfree(p); }
187 void* pvalloc(size_t s) { return tc_pvalloc(s); }
188 void malloc_stats(void) { tc_malloc_stats(); }
189 int mallopt(int cmd, int v) { return tc_mallopt(cmd, v); }
190 // No struct mallinfo on OS X, so don't define mallinfo().
191 // An alias for malloc_size(), which OS X defines.
192 size_t malloc_usable_size(void* p) { return tc_malloc_size(p); }
193 } // extern "C"
194
195 static void ReplaceSystemAlloc() {
196 static malloc_introspection_t tcmalloc_introspection;
197 memset(&tcmalloc_introspection, 0, sizeof(tcmalloc_introspection));
198
199 tcmalloc_introspection.enumerator = &mi_enumerator;
200 tcmalloc_introspection.good_size = &mi_good_size;
201 tcmalloc_introspection.check = &mi_check;
202 tcmalloc_introspection.print = &mi_print;
203 tcmalloc_introspection.log = &mi_log;
204 tcmalloc_introspection.force_lock = &mi_force_lock;
205 tcmalloc_introspection.force_unlock = &mi_force_unlock;
206
207 static malloc_zone_t tcmalloc_zone;
208 memset(&tcmalloc_zone, 0, sizeof(malloc_zone_t));
209
210 // Start with a version 4 zone which is used for OS X 10.4 and 10.5.
211 tcmalloc_zone.version = 4;
212 tcmalloc_zone.zone_name = "tcmalloc";
213 tcmalloc_zone.size = &mz_size;
214 tcmalloc_zone.malloc = &mz_malloc;
215 tcmalloc_zone.calloc = &mz_calloc;
216 tcmalloc_zone.valloc = &mz_valloc;
217 tcmalloc_zone.free = &mz_free;
218 tcmalloc_zone.realloc = &mz_realloc;
219 tcmalloc_zone.destroy = &mz_destroy;
220 tcmalloc_zone.batch_malloc = NULL;
221 tcmalloc_zone.batch_free = NULL;
222 tcmalloc_zone.introspect = &tcmalloc_introspection;
223
224 // from AvailabilityMacros.h
225 #if defined(MAC_OS_X_VERSION_10_6) && \
226 MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
227 // Switch to version 6 on OSX 10.6 to support memalign.
228 tcmalloc_zone.version = 6;
229 tcmalloc_zone.free_definite_size = NULL;
230 tcmalloc_zone.memalign = &mz_memalign;
231 tcmalloc_introspection.zone_locked = &mi_zone_locked;
232
233 // Request the default purgable zone to force its creation. The
234 // current default zone is registered with the purgable zone for
235 // doing tiny and small allocs. Sadly, it assumes that the default
236 // zone is the szone implementation from OS X and will crash if it
237 // isn't. By creating the zone now, this will be true and changing
238 // the default zone won't cause a problem. (OS X 10.6 and higher.)
239 malloc_default_purgeable_zone();
240 #endif
241
242 // Register the tcmalloc zone. At this point, it will not be the
243 // default zone.
244 malloc_zone_register(&tcmalloc_zone);
245
246 // Unregister and reregister the default zone. Unregistering swaps
247 // the specified zone with the last one registered which for the
248 // default zone makes the more recently registered zone the default
249 // zone. The default zone is then re-registered to ensure that
250 // allocations made from it earlier will be handled correctly.
251 // Things are not guaranteed to work that way, but it's how they work now.
252 malloc_zone_t *default_zone = malloc_default_zone();
253 malloc_zone_unregister(default_zone);
254 malloc_zone_register(default_zone);
255 }
256
257 #endif // TCMALLOC_LIBC_OVERRIDE_OSX_INL_H_
OLDNEW
« no previous file with comments | « third_party/tcmalloc/vendor/src/libc_override_glibc.h ('k') | third_party/tcmalloc/vendor/src/libc_override_redefine.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698