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