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 (at) 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 <gperftools/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 // from AvailabilityMacros.h 88 #if defined(MAC_OS_X_VERSION_10_6) && \ 89 MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 90 extern "C" { 91 // This function is only available on 10.6 (and later) but the 92 // LibSystem headers do not use AvailabilityMacros.h to handle weak 93 // importing automatically. This prototype is a copy of the one in 94 // <malloc/malloc.h> with the WEAK_IMPORT_ATTRBIUTE added. 95 extern malloc_zone_t *malloc_default_purgeable_zone(void) 96 WEAK_IMPORT_ATTRIBUTE; 97 } 98 #endif 99 100 // We need to provide wrappers around all the libc functions. 101 namespace { 102 size_t mz_size(malloc_zone_t* zone, const void* ptr) { 103 if (MallocExtension::instance()->GetOwnership(ptr) != MallocExtension::kOwned) 104 return 0; // malloc_zone semantics: return 0 if we don't own the memory 105 106 // TODO(csilvers): change this method to take a const void*, one day. 107 return MallocExtension::instance()->GetAllocatedSize(const_cast<void*>(ptr)); 108 } 109 110 void* mz_malloc(malloc_zone_t* zone, size_t size) { 111 return tc_malloc(size); 112 } 113 114 void* mz_calloc(malloc_zone_t* zone, size_t num_items, size_t size) { 115 return tc_calloc(num_items, size); 116 } 117 118 void* mz_valloc(malloc_zone_t* zone, size_t size) { 119 return tc_valloc(size); 120 } 121 122 void mz_free(malloc_zone_t* zone, void* ptr) { 123 return tc_free(ptr); 124 } 125 126 void* mz_realloc(malloc_zone_t* zone, void* ptr, size_t size) { 127 return tc_realloc(ptr, size); 128 } 129 130 void* mz_memalign(malloc_zone_t* zone, size_t align, size_t size) { 131 return tc_memalign(align, size); 132 } 133 134 void mz_destroy(malloc_zone_t* zone) { 135 // A no-op -- we will not be destroyed! 136 } 137 138 // malloc_introspection callbacks. I'm not clear on what all of these do. 139 kern_return_t mi_enumerator(task_t task, void *, 140 unsigned type_mask, vm_address_t zone_address, 141 memory_reader_t reader, 142 vm_range_recorder_t recorder) { 143 // Should enumerate all the pointers we have. Seems like a lot of work. 144 return KERN_FAILURE; 145 } 146 147 size_t mi_good_size(malloc_zone_t *zone, size_t size) { 148 // I think it's always safe to return size, but we maybe could do better. 149 return size; 150 } 151 152 boolean_t mi_check(malloc_zone_t *zone) { 153 return MallocExtension::instance()->VerifyAllMemory(); 154 } 155 156 void mi_print(malloc_zone_t *zone, boolean_t verbose) { 157 int bufsize = 8192; 158 if (verbose) 159 bufsize = 102400; // I picked this size arbitrarily 160 char* buffer = new char[bufsize]; 161 MallocExtension::instance()->GetStats(buffer, bufsize); 162 fprintf(stdout, "%s", buffer); 163 delete[] buffer; 164 } 165 166 void mi_log(malloc_zone_t *zone, void *address) { 167 // I don't think we support anything like this 168 } 169 170 void mi_force_lock(malloc_zone_t *zone) { 171 // Hopefully unneeded by us! 172 } 173 174 void mi_force_unlock(malloc_zone_t *zone) { 175 // Hopefully unneeded by us! 176 } 177 178 void mi_statistics(malloc_zone_t *zone, malloc_statistics_t *stats) { 179 // TODO(csilvers): figure out how to fill these out 180 stats->blocks_in_use = 0; 181 stats->size_in_use = 0; 182 stats->max_size_in_use = 0; 183 stats->size_allocated = 0; 184 } 185 186 boolean_t mi_zone_locked(malloc_zone_t *zone) { 187 return false; // Hopefully unneeded by us! 188 } 189 190 } // unnamed namespace 191 192 // OS X doesn't have pvalloc, cfree, malloc_statc, etc, so we can just 193 // define our own. :-) OS X supplies posix_memalign in some versions 194 // but not others, either strongly or weakly linked, in a way that's 195 // difficult enough to code to correctly, that I just don't try to 196 // support either memalign() or posix_memalign(). If you need them 197 // and are willing to code to tcmalloc, you can use tc_posix_memalign(). 198 extern "C" { 199 void cfree(void* p) { tc_cfree(p); } 200 void* pvalloc(size_t s) { return tc_pvalloc(s); } 201 void malloc_stats(void) { tc_malloc_stats(); } 202 int mallopt(int cmd, int v) { return tc_mallopt(cmd, v); } 203 // No struct mallinfo on OS X, so don't define mallinfo(). 204 // An alias for malloc_size(), which OS X defines. 205 size_t malloc_usable_size(void* p) { return tc_malloc_size(p); } 206 } // extern "C" 207 208 static void ReplaceSystemAlloc() { 209 static malloc_introspection_t tcmalloc_introspection; 210 memset(&tcmalloc_introspection, 0, sizeof(tcmalloc_introspection)); 211 212 tcmalloc_introspection.enumerator = &mi_enumerator; 213 tcmalloc_introspection.good_size = &mi_good_size; 214 tcmalloc_introspection.check = &mi_check; 215 tcmalloc_introspection.print = &mi_print; 216 tcmalloc_introspection.log = &mi_log; 217 tcmalloc_introspection.force_lock = &mi_force_lock; 218 tcmalloc_introspection.force_unlock = &mi_force_unlock; 219 220 static malloc_zone_t tcmalloc_zone; 221 memset(&tcmalloc_zone, 0, sizeof(malloc_zone_t)); 222 223 // Start with a version 4 zone which is used for OS X 10.4 and 10.5. 224 tcmalloc_zone.version = 4; 225 tcmalloc_zone.zone_name = "tcmalloc"; 226 tcmalloc_zone.size = &mz_size; 227 tcmalloc_zone.malloc = &mz_malloc; 228 tcmalloc_zone.calloc = &mz_calloc; 229 tcmalloc_zone.valloc = &mz_valloc; 230 tcmalloc_zone.free = &mz_free; 231 tcmalloc_zone.realloc = &mz_realloc; 232 tcmalloc_zone.destroy = &mz_destroy; 233 tcmalloc_zone.batch_malloc = NULL; 234 tcmalloc_zone.batch_free = NULL; 235 tcmalloc_zone.introspect = &tcmalloc_introspection; 236 237 // from AvailabilityMacros.h 238 #if defined(MAC_OS_X_VERSION_10_6) && \ 239 MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 240 // Switch to version 6 on OSX 10.6 to support memalign. 241 tcmalloc_zone.version = 6; 242 tcmalloc_zone.free_definite_size = NULL; 243 tcmalloc_zone.memalign = &mz_memalign; 244 tcmalloc_introspection.zone_locked = &mi_zone_locked; 245 246 // Request the default purgable zone to force its creation. The 247 // current default zone is registered with the purgable zone for 248 // doing tiny and small allocs. Sadly, it assumes that the default 249 // zone is the szone implementation from OS X and will crash if it 250 // isn't. By creating the zone now, this will be true and changing 251 // the default zone won't cause a problem. This only needs to 252 // happen when actually running on OS X 10.6 and higher (note the 253 // ifdef above only checks if we were *compiled* with 10.6 or 254 // higher; at runtime we have to check if this symbol is defined.) 255 if (malloc_default_purgeable_zone) { 256 malloc_default_purgeable_zone(); 257 } 258 #endif 259 260 // Register the tcmalloc zone. At this point, it will not be the 261 // default zone. 262 malloc_zone_register(&tcmalloc_zone); 263 264 // Unregister and reregister the default zone. Unregistering swaps 265 // the specified zone with the last one registered which for the 266 // default zone makes the more recently registered zone the default 267 // zone. The default zone is then re-registered to ensure that 268 // allocations made from it earlier will be handled correctly. 269 // Things are not guaranteed to work that way, but it's how they work now. 270 malloc_zone_t *default_zone = malloc_default_zone(); 271 malloc_zone_unregister(default_zone); 272 malloc_zone_register(default_zone); 273 } 274 275 #endif // TCMALLOC_LIBC_OVERRIDE_OSX_INL_H_ 276