OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2005, 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: Sanjay Ghemawat <opensource@google.com> |
| 32 // |
| 33 // A malloc that uses a per-thread cache to satisfy small malloc requests. |
| 34 // (The time for malloc/free of a small object drops from 300 ns to 50 ns.) |
| 35 // |
| 36 // See doc/tcmalloc.html for a high-level |
| 37 // description of how this malloc works. |
| 38 // |
| 39 // SYNCHRONIZATION |
| 40 // 1. The thread-specific lists are accessed without acquiring any locks. |
| 41 // This is safe because each such list is only accessed by one thread. |
| 42 // 2. We have a lock per central free-list, and hold it while manipulating |
| 43 // the central free list for a particular size. |
| 44 // 3. The central page allocator is protected by "pageheap_lock". |
| 45 // 4. The pagemap (which maps from page-number to descriptor), |
| 46 // can be read without holding any locks, and written while holding |
| 47 // the "pageheap_lock". |
| 48 // 5. To improve performance, a subset of the information one can get |
| 49 // from the pagemap is cached in a data structure, pagemap_cache_, |
| 50 // that atomically reads and writes its entries. This cache can be |
| 51 // read and written without locking. |
| 52 // |
| 53 // This multi-threaded access to the pagemap is safe for fairly |
| 54 // subtle reasons. We basically assume that when an object X is |
| 55 // allocated by thread A and deallocated by thread B, there must |
| 56 // have been appropriate synchronization in the handoff of object |
| 57 // X from thread A to thread B. The same logic applies to pagemap_cache_. |
| 58 // |
| 59 // THE PAGEID-TO-SIZECLASS CACHE |
| 60 // Hot PageID-to-sizeclass mappings are held by pagemap_cache_. If this cache |
| 61 // returns 0 for a particular PageID then that means "no information," not that |
| 62 // the sizeclass is 0. The cache may have stale information for pages that do |
| 63 // not hold the beginning of any free()'able object. Staleness is eliminated |
| 64 // in Populate() for pages with sizeclass > 0 objects, and in do_malloc() and |
| 65 // do_memalign() for all other relevant pages. |
| 66 // |
| 67 // PAGEMAP |
| 68 // ------- |
| 69 // Page map contains a mapping from page id to Span. |
| 70 // |
| 71 // If Span s occupies pages [p..q], |
| 72 // pagemap[p] == s |
| 73 // pagemap[q] == s |
| 74 // pagemap[p+1..q-1] are undefined |
| 75 // pagemap[p-1] and pagemap[q+1] are defined: |
| 76 // NULL if the corresponding page is not yet in the address space. |
| 77 // Otherwise it points to a Span. This span may be free |
| 78 // or allocated. If free, it is in one of pageheap's freelist. |
| 79 // |
| 80 // TODO: Bias reclamation to larger addresses |
| 81 // TODO: implement mallinfo/mallopt |
| 82 // TODO: Better testing |
| 83 // |
| 84 // 9/28/2003 (new page-level allocator replaces ptmalloc2): |
| 85 // * malloc/free of small objects goes from ~300 ns to ~50 ns. |
| 86 // * allocation of a reasonably complicated struct |
| 87 // goes from about 1100 ns to about 300 ns. |
| 88 |
| 89 #include <config.h> |
| 90 #include <new> |
| 91 #include <stdio.h> |
| 92 #include <stddef.h> |
| 93 #if defined HAVE_STDINT_H |
| 94 #include <stdint.h> |
| 95 #elif defined HAVE_INTTYPES_H |
| 96 #include <inttypes.h> |
| 97 #else |
| 98 #include <sys/types.h> |
| 99 #endif |
| 100 #if defined(HAVE_MALLOC_H) && defined(HAVE_STRUCT_MALLINFO) |
| 101 #include <malloc.h> // for struct mallinfo |
| 102 #endif |
| 103 #include <string.h> |
| 104 #ifdef HAVE_PTHREAD |
| 105 #include <pthread.h> |
| 106 #endif |
| 107 #ifdef HAVE_UNISTD_H |
| 108 #include <unistd.h> |
| 109 #endif |
| 110 #include <errno.h> |
| 111 #include <stdarg.h> |
| 112 #include <algorithm> |
| 113 #include <google/tcmalloc.h> |
| 114 #include "base/commandlineflags.h" |
| 115 #include "base/basictypes.h" // gets us PRIu64 |
| 116 #include "base/sysinfo.h" |
| 117 #include "base/spinlock.h" |
| 118 #include "common.h" |
| 119 #include "malloc_hook-inl.h" |
| 120 #include <google/malloc_hook.h> |
| 121 #include <google/malloc_extension.h> |
| 122 #include "central_freelist.h" |
| 123 #include "internal_logging.h" |
| 124 #include "linked_list.h" |
| 125 #include "maybe_threads.h" |
| 126 #include "page_heap.h" |
| 127 #include "page_heap_allocator.h" |
| 128 #include "pagemap.h" |
| 129 #include "span.h" |
| 130 #include "static_vars.h" |
| 131 #include "system-alloc.h" |
| 132 #include "tcmalloc_guard.h" |
| 133 #include "thread_cache.h" |
| 134 |
| 135 #if (defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)) && !defi
ned(WIN32_OVERRIDE_ALLOCATORS) |
| 136 # define WIN32_DO_PATCHING 1 |
| 137 #endif |
| 138 |
| 139 using tcmalloc::PageHeap; |
| 140 using tcmalloc::PageHeapAllocator; |
| 141 using tcmalloc::SizeMap; |
| 142 using tcmalloc::Span; |
| 143 using tcmalloc::StackTrace; |
| 144 using tcmalloc::Static; |
| 145 using tcmalloc::ThreadCache; |
| 146 |
| 147 // __THROW is defined in glibc systems. It means, counter-intuitively, |
| 148 // "This function will never throw an exception." It's an optional |
| 149 // optimization tool, but we may need to use it to match glibc prototypes. |
| 150 #ifndef __THROW // I guess we're not on a glibc system |
| 151 # define __THROW // __THROW is just an optimization, so ok to make it "" |
| 152 #endif |
| 153 |
| 154 DECLARE_int64(tcmalloc_sample_parameter); |
| 155 DECLARE_double(tcmalloc_release_rate); |
| 156 |
| 157 // For windows, the printf we use to report large allocs is |
| 158 // potentially dangerous: it could cause a malloc that would cause an |
| 159 // infinite loop. So by default we set the threshold to a huge number |
| 160 // on windows, so this bad situation will never trigger. You can |
| 161 // always set TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD manually if you |
| 162 // want this functionality. |
| 163 #ifdef _WIN32 |
| 164 const int64 kDefaultLargeAllocReportThreshold = static_cast<int64>(1) << 62; |
| 165 #else |
| 166 const int64 kDefaultLargeAllocReportThreshold = static_cast<int64>(1) << 30; |
| 167 #endif |
| 168 DEFINE_int64(tcmalloc_large_alloc_report_threshold, |
| 169 EnvToInt64("TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD", |
| 170 kDefaultLargeAllocReportThreshold), |
| 171 "Allocations larger than this value cause a stack " |
| 172 "trace to be dumped to stderr. The threshold for " |
| 173 "dumping stack traces is increased by a factor of 1.125 " |
| 174 "every time we print a message so that the threshold " |
| 175 "automatically goes up by a factor of ~1000 every 60 " |
| 176 "messages. This bounds the amount of extra logging " |
| 177 "generated by this flag. Default value of this flag " |
| 178 "is very large and therefore you should see no extra " |
| 179 "logging unless the flag is overridden. Set to 0 to " |
| 180 "disable reporting entirely."); |
| 181 |
| 182 |
| 183 // We already declared these functions in tcmalloc.h, but we have to |
| 184 // declare them again to give them an ATTRIBUTE_SECTION: we want to |
| 185 // put all callers of MallocHook::Invoke* in this module into |
| 186 // ATTRIBUTE_SECTION(google_malloc) section, so that |
| 187 // MallocHook::GetCallerStackTrace can function accurately. |
| 188 #ifndef _WIN32 // windows doesn't have attribute_section, so don't bother |
| 189 extern "C" { |
| 190 void* tc_malloc(size_t size) __THROW |
| 191 ATTRIBUTE_SECTION(google_malloc); |
| 192 void tc_free(void* ptr) __THROW |
| 193 ATTRIBUTE_SECTION(google_malloc); |
| 194 void* tc_realloc(void* ptr, size_t size) __THROW |
| 195 ATTRIBUTE_SECTION(google_malloc); |
| 196 void* tc_calloc(size_t nmemb, size_t size) __THROW |
| 197 ATTRIBUTE_SECTION(google_malloc); |
| 198 void tc_cfree(void* ptr) __THROW |
| 199 ATTRIBUTE_SECTION(google_malloc); |
| 200 |
| 201 void* tc_memalign(size_t __alignment, size_t __size) __THROW |
| 202 ATTRIBUTE_SECTION(google_malloc); |
| 203 int tc_posix_memalign(void** ptr, size_t align, size_t size) __THROW |
| 204 ATTRIBUTE_SECTION(google_malloc); |
| 205 void* tc_valloc(size_t __size) __THROW |
| 206 ATTRIBUTE_SECTION(google_malloc); |
| 207 void* tc_pvalloc(size_t __size) __THROW |
| 208 ATTRIBUTE_SECTION(google_malloc); |
| 209 |
| 210 void tc_malloc_stats(void) __THROW |
| 211 ATTRIBUTE_SECTION(google_malloc); |
| 212 int tc_mallopt(int cmd, int value) __THROW |
| 213 ATTRIBUTE_SECTION(google_malloc); |
| 214 #ifdef HAVE_STRUCT_MALLINFO // struct mallinfo isn't defined on freebsd |
| 215 struct mallinfo tc_mallinfo(void) __THROW |
| 216 ATTRIBUTE_SECTION(google_malloc); |
| 217 #endif |
| 218 |
| 219 void* tc_new(size_t size) |
| 220 ATTRIBUTE_SECTION(google_malloc); |
| 221 void tc_delete(void* p) __THROW |
| 222 ATTRIBUTE_SECTION(google_malloc); |
| 223 void* tc_newarray(size_t size) |
| 224 ATTRIBUTE_SECTION(google_malloc); |
| 225 void tc_deletearray(void* p) __THROW |
| 226 ATTRIBUTE_SECTION(google_malloc); |
| 227 |
| 228 // And the nothrow variants of these: |
| 229 void* tc_new_nothrow(size_t size, const std::nothrow_t&) __THROW |
| 230 ATTRIBUTE_SECTION(google_malloc); |
| 231 void* tc_newarray_nothrow(size_t size, const std::nothrow_t&) __THROW |
| 232 ATTRIBUTE_SECTION(google_malloc); |
| 233 // Surprisingly, compilers use a nothrow-delete internally. See, eg: |
| 234 // http://www.dinkumware.com/manuals/?manual=compleat&page=new.html |
| 235 void tc_delete_nothrow(void* ptr, const std::nothrow_t&) __THROW |
| 236 ATTRIBUTE_SECTION(google_malloc); |
| 237 void tc_deletearray_nothrow(void* ptr, const std::nothrow_t&) __THROW |
| 238 ATTRIBUTE_SECTION(google_malloc); |
| 239 } // extern "C" |
| 240 #endif // #ifndef _WIN32 |
| 241 |
| 242 // Override the libc functions to prefer our own instead. This comes |
| 243 // first so code in tcmalloc.cc can use the overridden versions. One |
| 244 // exception: in windows, by default, we patch our code into these |
| 245 // functions (via src/windows/patch_function.cc) rather than override |
| 246 // them. In that case, we don't want to do this overriding here. |
| 247 #if !defined(WIN32_DO_PATCHING) && !defined(TCMALLOC_FOR_DEBUGALLOCATION) |
| 248 |
| 249 #if defined(__GNUC__) && !defined(__MACH__) |
| 250 // Potentially faster variants that use the gcc alias extension. |
| 251 // FreeBSD does support aliases, but apparently not correctly. :-( |
| 252 // NOTE: we make many of these symbols weak, but do so in the makefile |
| 253 // (via objcopy -W) and not here. That ends up being more portable. |
| 254 # define ALIAS(x) __attribute__ ((alias (x))) |
| 255 void* operator new(size_t size) ALIAS("tc_new"); |
| 256 void operator delete(void* p) __THROW ALIAS("tc_delete"); |
| 257 void* operator new[](size_t size) ALIAS("tc_newarray"); |
| 258 void operator delete[](void* p) __THROW ALIAS("tc_deletearray"); |
| 259 void* operator new(size_t size, const std::nothrow_t&) __THROW |
| 260 ALIAS("tc_new_nothrow"); |
| 261 void* operator new[](size_t size, const std::nothrow_t&) __THROW |
| 262 ALIAS("tc_newarray_nothrow"); |
| 263 void operator delete(void* size, const std::nothrow_t&) __THROW |
| 264 ALIAS("tc_delete_nothrow"); |
| 265 void operator delete[](void* size, const std::nothrow_t&) __THROW |
| 266 ALIAS("tc_deletearray_nothrow")
; |
| 267 extern "C" { |
| 268 void* malloc(size_t size) __THROW ALIAS("tc_malloc"); |
| 269 void free(void* ptr) __THROW ALIAS("tc_free"); |
| 270 void* realloc(void* ptr, size_t size) __THROW ALIAS("tc_realloc"); |
| 271 void* calloc(size_t n, size_t size) __THROW ALIAS("tc_calloc"); |
| 272 void cfree(void* ptr) __THROW ALIAS("tc_cfree"); |
| 273 void* memalign(size_t align, size_t s) __THROW ALIAS("tc_memalign"); |
| 274 void* valloc(size_t size) __THROW ALIAS("tc_valloc"); |
| 275 void* pvalloc(size_t size) __THROW ALIAS("tc_pvalloc"); |
| 276 int posix_memalign(void** r, size_t a, size_t s) __THROW |
| 277 ALIAS("tc_posix_memalign"); |
| 278 void malloc_stats(void) __THROW ALIAS("tc_malloc_stats"); |
| 279 int mallopt(int cmd, int value) __THROW ALIAS("tc_mallopt"); |
| 280 #ifdef HAVE_STRUCT_MALLINFO |
| 281 struct mallinfo mallinfo(void) __THROW ALIAS("tc_mallinfo"); |
| 282 #endif |
| 283 } // extern "C" |
| 284 #else // #if defined(__GNUC__) && !defined(__MACH__) |
| 285 // Portable wrappers |
| 286 void* operator new(size_t size) { return tc_new(size); } |
| 287 void operator delete(void* p) __THROW { tc_delete(p); } |
| 288 void* operator new[](size_t size) { return tc_newarray(size); } |
| 289 void operator delete[](void* p) __THROW { tc_deletearray(p); } |
| 290 void* operator new(size_t size, const std::nothrow_t& nt) __THROW { |
| 291 return tc_new_nothrow(size, nt); |
| 292 } |
| 293 void* operator new[](size_t size, const std::nothrow_t& nt) __THROW { |
| 294 return tc_newarray_nothrow(size, nt); |
| 295 } |
| 296 void operator delete(void* ptr, const std::nothrow_t& nt) __THROW { |
| 297 return tc_delete_nothrow(ptr, nt); |
| 298 } |
| 299 void operator delete[](void* ptr, const std::nothrow_t& nt) __THROW { |
| 300 return tc_deletearray_nothrow(ptr, nt); |
| 301 } |
| 302 extern "C" { |
| 303 void* malloc(size_t s) __THROW { return tc_malloc(s); } |
| 304 void free(void* p) __THROW { tc_free(p); } |
| 305 void* realloc(void* p, size_t s) __THROW { return tc_realloc(p, s); } |
| 306 void* calloc(size_t n, size_t s) __THROW { return tc_calloc(n, s); } |
| 307 void cfree(void* p) __THROW { tc_cfree(p); } |
| 308 void* memalign(size_t a, size_t s) __THROW { return tc_memalign(a, s); } |
| 309 void* valloc(size_t s) __THROW { return tc_valloc(s); } |
| 310 void* pvalloc(size_t s) __THROW { return tc_pvalloc(s); } |
| 311 int posix_memalign(void** r, size_t a, size_t s) __THROW { |
| 312 return tc_posix_memalign(r, a, s); |
| 313 } |
| 314 void malloc_stats(void) __THROW { tc_malloc_stats(); } |
| 315 int mallopt(int cmd, int v) __THROW { return tc_mallopt(cmd, v); } |
| 316 #ifdef HAVE_STRUCT_MALLINFO |
| 317 struct mallinfo mallinfo(void) __THROW { return tc_mallinfo(); } |
| 318 #endif |
| 319 } // extern "C" |
| 320 #endif // #if defined(__GNUC__) |
| 321 |
| 322 // Some library routines on RedHat 9 allocate memory using malloc() |
| 323 // and free it using __libc_free() (or vice-versa). Since we provide |
| 324 // our own implementations of malloc/free, we need to make sure that |
| 325 // the __libc_XXX variants (defined as part of glibc) also point to |
| 326 // the same implementations. |
| 327 #ifdef __GLIBC__ // only glibc defines __libc_* |
| 328 extern "C" { |
| 329 #ifdef ALIAS |
| 330 void* __libc_malloc(size_t size) ALIAS("tc_malloc"); |
| 331 void __libc_free(void* ptr) ALIAS("tc_free"); |
| 332 void* __libc_realloc(void* ptr, size_t size) ALIAS("tc_realloc"); |
| 333 void* __libc_calloc(size_t n, size_t size) ALIAS("tc_calloc"); |
| 334 void __libc_cfree(void* ptr) ALIAS("tc_cfree"); |
| 335 void* __libc_memalign(size_t align, size_t s) ALIAS("tc_memalign"); |
| 336 void* __libc_valloc(size_t size) ALIAS("tc_valloc"); |
| 337 void* __libc_pvalloc(size_t size) ALIAS("tc_pvalloc"); |
| 338 int __posix_memalign(void** r, size_t a, size_t s) ALIAS("tc_posix_memalign"); |
| 339 #else // #ifdef ALIAS |
| 340 void* __libc_malloc(size_t size) { return malloc(size); } |
| 341 void __libc_free(void* ptr) { free(ptr); } |
| 342 void* __libc_realloc(void* ptr, size_t size) { return realloc(ptr, size); } |
| 343 void* __libc_calloc(size_t n, size_t size) { return calloc(n, size); } |
| 344 void __libc_cfree(void* ptr) { cfree(ptr); } |
| 345 void* __libc_memalign(size_t align, size_t s) { return memalign(align, s); } |
| 346 void* __libc_valloc(size_t size) { return valloc(size); } |
| 347 void* __libc_pvalloc(size_t size) { return pvalloc(size); } |
| 348 int __posix_memalign(void** r, size_t a, size_t s) { |
| 349 return posix_memalign(r, a, s); |
| 350 } |
| 351 #endif // #ifdef ALIAS |
| 352 } // extern "C" |
| 353 #endif // ifdef __GLIBC__ |
| 354 |
| 355 #undef ALIAS |
| 356 |
| 357 #endif // #ifndef(WIN32_DO_PATCHING) && ndef(TCMALLOC_FOR_DEBUGALLOCATION) |
| 358 |
| 359 |
| 360 // ----------------------- IMPLEMENTATION ------------------------------- |
| 361 |
| 362 static int tc_new_mode = 0; // See tc_set_new_mode(). |
| 363 |
| 364 // Routines such as free() and realloc() catch some erroneous pointers |
| 365 // passed to them, and invoke the below when they do. (An erroneous pointer |
| 366 // won't be caught if it's within a valid span or a stale span for which |
| 367 // the pagemap cache has a non-zero sizeclass.) This is a cheap (source-editing |
| 368 // required) kind of exception handling for these routines. |
| 369 namespace { |
| 370 void InvalidFree(void* ptr) { |
| 371 CRASH("Attempt to free invalid pointer: %p\n", ptr); |
| 372 } |
| 373 |
| 374 size_t InvalidGetSizeForRealloc(void* old_ptr) { |
| 375 CRASH("Attempt to realloc invalid pointer: %p\n", old_ptr); |
| 376 return 0; |
| 377 } |
| 378 |
| 379 size_t InvalidGetAllocatedSize(void* ptr) { |
| 380 CRASH("Attempt to get the size of an invalid pointer: %p\n", ptr); |
| 381 return 0; |
| 382 } |
| 383 } // unnamed namespace |
| 384 |
| 385 // Extract interesting stats |
| 386 struct TCMallocStats { |
| 387 uint64_t system_bytes; // Bytes alloced from system |
| 388 uint64_t thread_bytes; // Bytes in thread caches |
| 389 uint64_t central_bytes; // Bytes in central cache |
| 390 uint64_t transfer_bytes; // Bytes in central transfer cache |
| 391 uint64_t pageheap_bytes; // Bytes in page heap |
| 392 uint64_t metadata_bytes; // Bytes alloced for metadata |
| 393 }; |
| 394 |
| 395 // Get stats into "r". Also get per-size-class counts if class_count != NULL |
| 396 static void ExtractStats(TCMallocStats* r, uint64_t* class_count) { |
| 397 r->central_bytes = 0; |
| 398 r->transfer_bytes = 0; |
| 399 for (int cl = 0; cl < kNumClasses; ++cl) { |
| 400 const int length = Static::central_cache()[cl].length(); |
| 401 const int tc_length = Static::central_cache()[cl].tc_length(); |
| 402 const size_t size = static_cast<uint64_t>( |
| 403 Static::sizemap()->ByteSizeForClass(cl)); |
| 404 r->central_bytes += (size * length); |
| 405 r->transfer_bytes += (size * tc_length); |
| 406 if (class_count) class_count[cl] = length + tc_length; |
| 407 } |
| 408 |
| 409 // Add stats from per-thread heaps |
| 410 r->thread_bytes = 0; |
| 411 { // scope |
| 412 SpinLockHolder h(Static::pageheap_lock()); |
| 413 ThreadCache::GetThreadStats(&r->thread_bytes, class_count); |
| 414 } |
| 415 |
| 416 { //scope |
| 417 SpinLockHolder h(Static::pageheap_lock()); |
| 418 r->system_bytes = Static::pageheap()->SystemBytes(); |
| 419 r->metadata_bytes = tcmalloc::metadata_system_bytes(); |
| 420 r->pageheap_bytes = Static::pageheap()->FreeBytes(); |
| 421 } |
| 422 } |
| 423 |
| 424 // WRITE stats to "out" |
| 425 static void DumpStats(TCMalloc_Printer* out, int level) { |
| 426 TCMallocStats stats; |
| 427 uint64_t class_count[kNumClasses]; |
| 428 ExtractStats(&stats, (level >= 2 ? class_count : NULL)); |
| 429 |
| 430 static const double MB = 1048576.0; |
| 431 |
| 432 if (level >= 2) { |
| 433 out->printf("------------------------------------------------\n"); |
| 434 out->printf("Size class breakdown\n"); |
| 435 out->printf("------------------------------------------------\n"); |
| 436 uint64_t cumulative = 0; |
| 437 for (int cl = 0; cl < kNumClasses; ++cl) { |
| 438 if (class_count[cl] > 0) { |
| 439 uint64_t class_bytes = |
| 440 class_count[cl] * Static::sizemap()->ByteSizeForClass(cl); |
| 441 cumulative += class_bytes; |
| 442 out->printf("class %3d [ %8" PRIuS " bytes ] : " |
| 443 "%8" PRIu64 " objs; %5.1f MB; %5.1f cum MB\n", |
| 444 cl, Static::sizemap()->ByteSizeForClass(cl), |
| 445 class_count[cl], |
| 446 class_bytes / MB, |
| 447 cumulative / MB); |
| 448 } |
| 449 } |
| 450 |
| 451 SpinLockHolder h(Static::pageheap_lock()); |
| 452 Static::pageheap()->Dump(out); |
| 453 |
| 454 out->printf("------------------------------------------------\n"); |
| 455 DumpSystemAllocatorStats(out); |
| 456 } |
| 457 |
| 458 const uint64_t bytes_in_use = stats.system_bytes |
| 459 - stats.pageheap_bytes |
| 460 - stats.central_bytes |
| 461 - stats.transfer_bytes |
| 462 - stats.thread_bytes; |
| 463 |
| 464 out->printf("------------------------------------------------\n" |
| 465 "MALLOC: %12" PRIu64 " (%7.1f MB) Heap size\n" |
| 466 "MALLOC: %12" PRIu64 " (%7.1f MB) Bytes in use by application\n" |
| 467 "MALLOC: %12" PRIu64 " (%7.1f MB) Bytes free in page heap\n" |
| 468 "MALLOC: %12" PRIu64 " (%7.1f MB) Bytes free in central cache\n" |
| 469 "MALLOC: %12" PRIu64 " (%7.1f MB) Bytes free in transfer cache\n" |
| 470 "MALLOC: %12" PRIu64 " (%7.1f MB) Bytes free in thread caches\n" |
| 471 "MALLOC: %12" PRIu64 " Spans in use\n" |
| 472 "MALLOC: %12" PRIu64 " Thread heaps in use\n" |
| 473 "MALLOC: %12" PRIu64 " (%7.1f MB) Metadata allocated\n" |
| 474 "------------------------------------------------\n", |
| 475 stats.system_bytes, stats.system_bytes / MB, |
| 476 bytes_in_use, bytes_in_use / MB, |
| 477 stats.pageheap_bytes, stats.pageheap_bytes / MB, |
| 478 stats.central_bytes, stats.central_bytes / MB, |
| 479 stats.transfer_bytes, stats.transfer_bytes / MB, |
| 480 stats.thread_bytes, stats.thread_bytes / MB, |
| 481 uint64_t(Static::span_allocator()->inuse()), |
| 482 uint64_t(ThreadCache::HeapsInUse()), |
| 483 stats.metadata_bytes, stats.metadata_bytes / MB); |
| 484 } |
| 485 |
| 486 static void PrintStats(int level) { |
| 487 const int kBufferSize = 16 << 10; |
| 488 char* buffer = new char[kBufferSize]; |
| 489 TCMalloc_Printer printer(buffer, kBufferSize); |
| 490 DumpStats(&printer, level); |
| 491 write(STDERR_FILENO, buffer, strlen(buffer)); |
| 492 delete[] buffer; |
| 493 } |
| 494 |
| 495 static void** DumpHeapGrowthStackTraces() { |
| 496 // Count how much space we need |
| 497 int needed_slots = 0; |
| 498 { |
| 499 SpinLockHolder h(Static::pageheap_lock()); |
| 500 for (StackTrace* t = Static::growth_stacks(); |
| 501 t != NULL; |
| 502 t = reinterpret_cast<StackTrace*>( |
| 503 t->stack[tcmalloc::kMaxStackDepth-1])) { |
| 504 needed_slots += 3 + t->depth; |
| 505 } |
| 506 needed_slots += 100; // Slop in case list grows |
| 507 needed_slots += needed_slots/8; // An extra 12.5% slop |
| 508 } |
| 509 |
| 510 void** result = new void*[needed_slots]; |
| 511 if (result == NULL) { |
| 512 MESSAGE("tcmalloc: allocation failed for stack trace slots", |
| 513 needed_slots * sizeof(*result)); |
| 514 return NULL; |
| 515 } |
| 516 |
| 517 SpinLockHolder h(Static::pageheap_lock()); |
| 518 int used_slots = 0; |
| 519 for (StackTrace* t = Static::growth_stacks(); |
| 520 t != NULL; |
| 521 t = reinterpret_cast<StackTrace*>( |
| 522 t->stack[tcmalloc::kMaxStackDepth-1])) { |
| 523 ASSERT(used_slots < needed_slots); // Need to leave room for terminator |
| 524 if (used_slots + 3 + t->depth >= needed_slots) { |
| 525 // No more room |
| 526 break; |
| 527 } |
| 528 |
| 529 result[used_slots+0] = reinterpret_cast<void*>(static_cast<uintptr_t>(1)); |
| 530 result[used_slots+1] = reinterpret_cast<void*>(t->size); |
| 531 result[used_slots+2] = reinterpret_cast<void*>(t->depth); |
| 532 for (int d = 0; d < t->depth; d++) { |
| 533 result[used_slots+3+d] = t->stack[d]; |
| 534 } |
| 535 used_slots += 3 + t->depth; |
| 536 } |
| 537 result[used_slots] = reinterpret_cast<void*>(static_cast<uintptr_t>(0)); |
| 538 return result; |
| 539 } |
| 540 |
| 541 // TCMalloc's support for extra malloc interfaces |
| 542 class TCMallocImplementation : public MallocExtension { |
| 543 public: |
| 544 virtual void GetStats(char* buffer, int buffer_length) { |
| 545 ASSERT(buffer_length > 0); |
| 546 TCMalloc_Printer printer(buffer, buffer_length); |
| 547 |
| 548 // Print level one stats unless lots of space is available |
| 549 if (buffer_length < 10000) { |
| 550 DumpStats(&printer, 1); |
| 551 } else { |
| 552 DumpStats(&printer, 2); |
| 553 } |
| 554 } |
| 555 |
| 556 virtual void** ReadStackTraces(int* sample_period) { |
| 557 tcmalloc::StackTraceTable table; |
| 558 { |
| 559 SpinLockHolder h(Static::pageheap_lock()); |
| 560 Span* sampled = Static::sampled_objects(); |
| 561 for (Span* s = sampled->next; s != sampled; s = s->next) { |
| 562 table.AddTrace(*reinterpret_cast<StackTrace*>(s->objects)); |
| 563 } |
| 564 } |
| 565 *sample_period = ThreadCache::GetCache()->GetSamplePeriod(); |
| 566 return table.ReadStackTracesAndClear(); // grabs and releases pageheap_lock |
| 567 } |
| 568 |
| 569 virtual void** ReadHeapGrowthStackTraces() { |
| 570 return DumpHeapGrowthStackTraces(); |
| 571 } |
| 572 |
| 573 virtual bool GetNumericProperty(const char* name, size_t* value) { |
| 574 ASSERT(name != NULL); |
| 575 |
| 576 if (strcmp(name, "generic.current_allocated_bytes") == 0) { |
| 577 TCMallocStats stats; |
| 578 ExtractStats(&stats, NULL); |
| 579 *value = stats.system_bytes |
| 580 - stats.thread_bytes |
| 581 - stats.central_bytes |
| 582 - stats.transfer_bytes |
| 583 - stats.pageheap_bytes; |
| 584 return true; |
| 585 } |
| 586 |
| 587 if (strcmp(name, "generic.heap_size") == 0) { |
| 588 TCMallocStats stats; |
| 589 ExtractStats(&stats, NULL); |
| 590 *value = stats.system_bytes; |
| 591 return true; |
| 592 } |
| 593 |
| 594 if (strcmp(name, "tcmalloc.slack_bytes") == 0) { |
| 595 // We assume that bytes in the page heap are not fragmented too |
| 596 // badly, and are therefore available for allocation. |
| 597 SpinLockHolder l(Static::pageheap_lock()); |
| 598 *value = Static::pageheap()->FreeBytes(); |
| 599 return true; |
| 600 } |
| 601 |
| 602 if (strcmp(name, "tcmalloc.max_total_thread_cache_bytes") == 0) { |
| 603 SpinLockHolder l(Static::pageheap_lock()); |
| 604 *value = ThreadCache::overall_thread_cache_size(); |
| 605 return true; |
| 606 } |
| 607 |
| 608 if (strcmp(name, "tcmalloc.current_total_thread_cache_bytes") == 0) { |
| 609 TCMallocStats stats; |
| 610 ExtractStats(&stats, NULL); |
| 611 *value = stats.thread_bytes; |
| 612 return true; |
| 613 } |
| 614 |
| 615 return false; |
| 616 } |
| 617 |
| 618 virtual bool SetNumericProperty(const char* name, size_t value) { |
| 619 ASSERT(name != NULL); |
| 620 |
| 621 if (strcmp(name, "tcmalloc.max_total_thread_cache_bytes") == 0) { |
| 622 SpinLockHolder l(Static::pageheap_lock()); |
| 623 ThreadCache::set_overall_thread_cache_size(value); |
| 624 return true; |
| 625 } |
| 626 |
| 627 return false; |
| 628 } |
| 629 |
| 630 virtual void MarkThreadIdle() { |
| 631 ThreadCache::BecomeIdle(); |
| 632 } |
| 633 |
| 634 virtual void MarkThreadBusy(); // Implemented below |
| 635 |
| 636 virtual void ReleaseFreeMemory() { |
| 637 SpinLockHolder h(Static::pageheap_lock()); |
| 638 Static::pageheap()->ReleaseFreePages(); |
| 639 } |
| 640 |
| 641 virtual void SetMemoryReleaseRate(double rate) { |
| 642 FLAGS_tcmalloc_release_rate = rate; |
| 643 } |
| 644 |
| 645 virtual double GetMemoryReleaseRate() { |
| 646 return FLAGS_tcmalloc_release_rate; |
| 647 } |
| 648 virtual size_t GetEstimatedAllocatedSize(size_t size) { |
| 649 if (size <= kMaxSize) { |
| 650 const size_t cl = Static::sizemap()->SizeClass(size); |
| 651 const size_t alloc_size = Static::sizemap()->ByteSizeForClass(cl); |
| 652 return alloc_size; |
| 653 } else { |
| 654 return tcmalloc::pages(size) << kPageShift; |
| 655 } |
| 656 } |
| 657 |
| 658 // This just calls GetSizeWithCallback, but because that's in an |
| 659 // unnamed namespace, we need to move the definition below it in the |
| 660 // file. |
| 661 virtual size_t GetAllocatedSize(void* ptr); |
| 662 }; |
| 663 |
| 664 // The constructor allocates an object to ensure that initialization |
| 665 // runs before main(), and therefore we do not have a chance to become |
| 666 // multi-threaded before initialization. We also create the TSD key |
| 667 // here. Presumably by the time this constructor runs, glibc is in |
| 668 // good enough shape to handle pthread_key_create(). |
| 669 // |
| 670 // The constructor also takes the opportunity to tell STL to use |
| 671 // tcmalloc. We want to do this early, before construct time, so |
| 672 // all user STL allocations go through tcmalloc (which works really |
| 673 // well for STL). |
| 674 // |
| 675 // The destructor prints stats when the program exits. |
| 676 static int tcmallocguard_refcount = 0; // no lock needed: runs before main() |
| 677 TCMallocGuard::TCMallocGuard() { |
| 678 if (tcmallocguard_refcount++ == 0) { |
| 679 #ifdef HAVE_TLS // this is true if the cc/ld/libc combo support TLS |
| 680 // Check whether the kernel also supports TLS (needs to happen at runtime) |
| 681 tcmalloc::CheckIfKernelSupportsTLS(); |
| 682 #endif |
| 683 #ifdef WIN32_DO_PATCHING |
| 684 // patch the windows VirtualAlloc, etc. |
| 685 PatchWindowsFunctions(); // defined in windows/patch_functions.cc |
| 686 #endif |
| 687 free(malloc(1)); |
| 688 ThreadCache::InitTSD(); |
| 689 free(malloc(1)); |
| 690 MallocExtension::Register(new TCMallocImplementation); |
| 691 } |
| 692 } |
| 693 |
| 694 TCMallocGuard::~TCMallocGuard() { |
| 695 if (--tcmallocguard_refcount == 0) { |
| 696 const char* env = getenv("MALLOCSTATS"); |
| 697 if (env != NULL) { |
| 698 int level = atoi(env); |
| 699 if (level < 1) level = 1; |
| 700 PrintStats(level); |
| 701 } |
| 702 } |
| 703 } |
| 704 #ifndef WIN32_OVERRIDE_ALLOCATORS |
| 705 static TCMallocGuard module_enter_exit_hook; |
| 706 #endif |
| 707 |
| 708 //------------------------------------------------------------------- |
| 709 // Helpers for the exported routines below |
| 710 //------------------------------------------------------------------- |
| 711 |
| 712 static Span* DoSampledAllocation(size_t size) { |
| 713 // Grab the stack trace outside the heap lock |
| 714 StackTrace tmp; |
| 715 tmp.depth = GetStackTrace(tmp.stack, tcmalloc::kMaxStackDepth, 1); |
| 716 tmp.size = size; |
| 717 |
| 718 SpinLockHolder h(Static::pageheap_lock()); |
| 719 // Allocate span |
| 720 Span *span = Static::pageheap()->New(tcmalloc::pages(size == 0 ? 1 : size)); |
| 721 if (span == NULL) { |
| 722 return NULL; |
| 723 } |
| 724 |
| 725 // Allocate stack trace |
| 726 StackTrace *stack = Static::stacktrace_allocator()->New(); |
| 727 if (stack == NULL) { |
| 728 // Sampling failed because of lack of memory |
| 729 return span; |
| 730 } |
| 731 |
| 732 *stack = tmp; |
| 733 span->sample = 1; |
| 734 span->objects = stack; |
| 735 tcmalloc::DLL_Prepend(Static::sampled_objects(), span); |
| 736 |
| 737 return span; |
| 738 } |
| 739 |
| 740 static inline bool CheckCachedSizeClass(void *ptr) { |
| 741 PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; |
| 742 size_t cached_value = Static::pageheap()->GetSizeClassIfCached(p); |
| 743 return cached_value == 0 || |
| 744 cached_value == Static::pageheap()->GetDescriptor(p)->sizeclass; |
| 745 } |
| 746 |
| 747 static inline void* CheckedMallocResult(void *result) |
| 748 { |
| 749 ASSERT(result == 0 || CheckCachedSizeClass(result)); |
| 750 return result; |
| 751 } |
| 752 |
| 753 static inline void* SpanToMallocResult(Span *span) { |
| 754 Static::pageheap()->CacheSizeClass(span->start, 0); |
| 755 return |
| 756 CheckedMallocResult(reinterpret_cast<void*>(span->start << kPageShift)); |
| 757 } |
| 758 |
| 759 // Copy of FLAGS_tcmalloc_large_alloc_report_threshold with |
| 760 // automatic increases factored in. |
| 761 static int64_t large_alloc_threshold = |
| 762 (kPageSize > FLAGS_tcmalloc_large_alloc_report_threshold |
| 763 ? kPageSize : FLAGS_tcmalloc_large_alloc_report_threshold); |
| 764 |
| 765 static void ReportLargeAlloc(Length num_pages, void* result) { |
| 766 StackTrace stack; |
| 767 stack.depth = GetStackTrace(stack.stack, tcmalloc::kMaxStackDepth, 1); |
| 768 |
| 769 static const int N = 1000; |
| 770 char buffer[N]; |
| 771 TCMalloc_Printer printer(buffer, N); |
| 772 printer.printf("tcmalloc: large alloc %llu bytes == %p @ ", |
| 773 static_cast<unsigned long long>(num_pages) << kPageShift, |
| 774 result); |
| 775 for (int i = 0; i < stack.depth; i++) { |
| 776 printer.printf(" %p", stack.stack[i]); |
| 777 } |
| 778 printer.printf("\n"); |
| 779 write(STDERR_FILENO, buffer, strlen(buffer)); |
| 780 } |
| 781 |
| 782 namespace { |
| 783 |
| 784 inline void* cpp_alloc(size_t size, bool nothrow); |
| 785 inline void* do_malloc(size_t size); |
| 786 |
| 787 inline void* cpp_or_malloc(size_t size, bool nothrow) { |
| 788 return tc_new_mode ? cpp_alloc(size, nothrow) : do_malloc(size); |
| 789 } |
| 790 |
| 791 inline void* cpp_memalign(size_t align, size_t size, bool nothrow); |
| 792 inline void* do_memalign(size_t align, size_t size); |
| 793 |
| 794 inline void* cpp_or_memalign(size_t align, size_t size, bool nothrow) { |
| 795 return tc_new_mode ? cpp_memalign(align, size, nothrow) : |
| 796 do_memalign(align, size); |
| 797 } |
| 798 |
| 799 // Helper for do_malloc(). |
| 800 inline void* do_malloc_pages(Length num_pages) { |
| 801 Span *span; |
| 802 bool report_large = false; |
| 803 { |
| 804 SpinLockHolder h(Static::pageheap_lock()); |
| 805 span = Static::pageheap()->New(num_pages); |
| 806 const int64 threshold = large_alloc_threshold; |
| 807 if (threshold > 0 && num_pages >= (threshold >> kPageShift)) { |
| 808 // Increase the threshold by 1/8 every time we generate a report. |
| 809 // We cap the threshold at 8GB to avoid overflow problems. |
| 810 large_alloc_threshold = (threshold + threshold/8 < 8ll<<30 |
| 811 ? threshold + threshold/8 : 8ll<<30); |
| 812 report_large = true; |
| 813 } |
| 814 } |
| 815 |
| 816 void* result = (span == NULL ? NULL : SpanToMallocResult(span)); |
| 817 if (report_large) { |
| 818 ReportLargeAlloc(num_pages, result); |
| 819 } |
| 820 return result; |
| 821 } |
| 822 |
| 823 inline void* do_malloc(size_t size) { |
| 824 void* ret = NULL; |
| 825 |
| 826 // The following call forces module initialization |
| 827 ThreadCache* heap = ThreadCache::GetCache(); |
| 828 if ((FLAGS_tcmalloc_sample_parameter > 0) && heap->SampleAllocation(size)) { |
| 829 Span* span = DoSampledAllocation(size); |
| 830 if (span != NULL) { |
| 831 ret = SpanToMallocResult(span); |
| 832 } |
| 833 } else if (size <= kMaxSize) { |
| 834 // The common case, and also the simplest. This just pops the |
| 835 // size-appropriate freelist, after replenishing it if it's empty. |
| 836 ret = CheckedMallocResult(heap->Allocate(size)); |
| 837 } else { |
| 838 ret = do_malloc_pages(tcmalloc::pages(size)); |
| 839 } |
| 840 if (ret == NULL) errno = ENOMEM; |
| 841 return ret; |
| 842 } |
| 843 |
| 844 inline void* do_calloc(size_t n, size_t elem_size) { |
| 845 // Overflow check |
| 846 const size_t size = n * elem_size; |
| 847 if (elem_size != 0 && size / elem_size != n) return NULL; |
| 848 |
| 849 void* result = cpp_or_malloc(size, false); |
| 850 if (result != NULL) { |
| 851 memset(result, 0, size); |
| 852 } |
| 853 return result; |
| 854 } |
| 855 |
| 856 static inline ThreadCache* GetCacheIfPresent() { |
| 857 void* const p = ThreadCache::GetCacheIfPresent(); |
| 858 return reinterpret_cast<ThreadCache*>(p); |
| 859 } |
| 860 |
| 861 // This lets you call back to a given function pointer if ptr is invalid. |
| 862 // It is used primarily by windows code which wants a specialized callback. |
| 863 inline void do_free_with_callback(void* ptr, void (*invalid_free_fn)(void*)) { |
| 864 if (ptr == NULL) return; |
| 865 ASSERT(Static::pageheap() != NULL); // Should not call free() before malloc() |
| 866 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; |
| 867 Span* span = NULL; |
| 868 size_t cl = Static::pageheap()->GetSizeClassIfCached(p); |
| 869 |
| 870 if (cl == 0) { |
| 871 span = Static::pageheap()->GetDescriptor(p); |
| 872 if (!span) { |
| 873 // span can be NULL because the pointer passed in is invalid |
| 874 // (not something returned by malloc or friends), or because the |
| 875 // pointer was allocated with some other allocator besides |
| 876 // tcmalloc. The latter can happen if tcmalloc is linked in via |
| 877 // a dynamic library, but is not listed last on the link line. |
| 878 // In that case, libraries after it on the link line will |
| 879 // allocate with libc malloc, but free with tcmalloc's free. |
| 880 (*invalid_free_fn)(ptr); // Decide how to handle the bad free request |
| 881 return; |
| 882 } |
| 883 cl = span->sizeclass; |
| 884 Static::pageheap()->CacheSizeClass(p, cl); |
| 885 } |
| 886 if (cl != 0) { |
| 887 ASSERT(!Static::pageheap()->GetDescriptor(p)->sample); |
| 888 ThreadCache* heap = GetCacheIfPresent(); |
| 889 if (heap != NULL) { |
| 890 heap->Deallocate(ptr, cl); |
| 891 } else { |
| 892 // Delete directly into central cache |
| 893 tcmalloc::SLL_SetNext(ptr, NULL); |
| 894 Static::central_cache()[cl].InsertRange(ptr, ptr, 1); |
| 895 } |
| 896 } else { |
| 897 SpinLockHolder h(Static::pageheap_lock()); |
| 898 ASSERT(reinterpret_cast<uintptr_t>(ptr) % kPageSize == 0); |
| 899 ASSERT(span != NULL && span->start == p); |
| 900 if (span->sample) { |
| 901 tcmalloc::DLL_Remove(span); |
| 902 Static::stacktrace_allocator()->Delete( |
| 903 reinterpret_cast<StackTrace*>(span->objects)); |
| 904 span->objects = NULL; |
| 905 } |
| 906 Static::pageheap()->Delete(span); |
| 907 } |
| 908 } |
| 909 |
| 910 // The default "do_free" that uses the default callback. |
| 911 inline void do_free(void* ptr) { |
| 912 return do_free_with_callback(ptr, &InvalidFree); |
| 913 } |
| 914 |
| 915 inline size_t GetSizeWithCallback(void* ptr, |
| 916 size_t (*invalid_getsize_fn)(void*)) { |
| 917 if (ptr == NULL) |
| 918 return 0; |
| 919 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; |
| 920 size_t cl = Static::pageheap()->GetSizeClassIfCached(p); |
| 921 if (cl != 0) { |
| 922 return Static::sizemap()->ByteSizeForClass(cl); |
| 923 } else { |
| 924 Span *span = Static::pageheap()->GetDescriptor(p); |
| 925 if (span == NULL) { // means we do not own this memory |
| 926 return (*invalid_getsize_fn)(ptr); |
| 927 } else if (span->sizeclass != 0) { |
| 928 Static::pageheap()->CacheSizeClass(p, span->sizeclass); |
| 929 return Static::sizemap()->ByteSizeForClass(span->sizeclass); |
| 930 } else { |
| 931 return span->length << kPageShift; |
| 932 } |
| 933 } |
| 934 } |
| 935 |
| 936 // This lets you call back to a given function pointer if ptr is invalid. |
| 937 // It is used primarily by windows code which wants a specialized callback. |
| 938 inline void* do_realloc_with_callback( |
| 939 void* old_ptr, size_t new_size, |
| 940 void (*invalid_free_fn)(void*), |
| 941 size_t (*invalid_get_size_fn)(void*)) { |
| 942 // Get the size of the old entry |
| 943 const size_t old_size = GetSizeWithCallback(old_ptr, invalid_get_size_fn); |
| 944 |
| 945 // Reallocate if the new size is larger than the old size, |
| 946 // or if the new size is significantly smaller than the old size. |
| 947 // We do hysteresis to avoid resizing ping-pongs: |
| 948 // . If we need to grow, grow to max(new_size, old_size * 1.X) |
| 949 // . Don't shrink unless new_size < old_size * 0.Y |
| 950 // X and Y trade-off time for wasted space. For now we do 1.25 and 0.5. |
| 951 const int lower_bound_to_grow = old_size + old_size / 4; |
| 952 const int upper_bound_to_shrink = old_size / 2; |
| 953 if ((new_size > old_size) || (new_size < upper_bound_to_shrink)) { |
| 954 // Need to reallocate. |
| 955 void* new_ptr = NULL; |
| 956 |
| 957 if (new_size > old_size && new_size < lower_bound_to_grow) { |
| 958 new_ptr = cpp_or_malloc(lower_bound_to_grow, false); |
| 959 } |
| 960 if (new_ptr == NULL) { |
| 961 // Either new_size is not a tiny increment, or last do_malloc failed. |
| 962 new_ptr = cpp_or_malloc(new_size, false); |
| 963 } |
| 964 if (new_ptr == NULL) { |
| 965 return NULL; |
| 966 } |
| 967 MallocHook::InvokeNewHook(new_ptr, new_size); |
| 968 memcpy(new_ptr, old_ptr, ((old_size < new_size) ? old_size : new_size)); |
| 969 MallocHook::InvokeDeleteHook(old_ptr); |
| 970 // We could use a variant of do_free() that leverages the fact |
| 971 // that we already know the sizeclass of old_ptr. The benefit |
| 972 // would be small, so don't bother. |
| 973 do_free_with_callback(old_ptr, invalid_free_fn); |
| 974 return new_ptr; |
| 975 } else { |
| 976 // We still need to call hooks to report the updated size: |
| 977 MallocHook::InvokeDeleteHook(old_ptr); |
| 978 MallocHook::InvokeNewHook(old_ptr, new_size); |
| 979 return old_ptr; |
| 980 } |
| 981 } |
| 982 |
| 983 inline void* do_realloc(void* old_ptr, size_t new_size) { |
| 984 return do_realloc_with_callback(old_ptr, new_size, |
| 985 &InvalidFree, &InvalidGetSizeForRealloc); |
| 986 } |
| 987 |
| 988 // For use by exported routines below that want specific alignments |
| 989 // |
| 990 // Note: this code can be slow, and can significantly fragment memory. |
| 991 // The expectation is that memalign/posix_memalign/valloc/pvalloc will |
| 992 // not be invoked very often. This requirement simplifies our |
| 993 // implementation and allows us to tune for expected allocation |
| 994 // patterns. |
| 995 void* do_memalign(size_t align, size_t size) { |
| 996 ASSERT((align & (align - 1)) == 0); |
| 997 ASSERT(align > 0); |
| 998 if (size + align < size) return NULL; // Overflow |
| 999 |
| 1000 if (Static::pageheap() == NULL) ThreadCache::InitModule(); |
| 1001 |
| 1002 // Allocate at least one byte to avoid boundary conditions below |
| 1003 if (size == 0) size = 1; |
| 1004 |
| 1005 if (size <= kMaxSize && align < kPageSize) { |
| 1006 // Search through acceptable size classes looking for one with |
| 1007 // enough alignment. This depends on the fact that |
| 1008 // InitSizeClasses() currently produces several size classes that |
| 1009 // are aligned at powers of two. We will waste time and space if |
| 1010 // we miss in the size class array, but that is deemed acceptable |
| 1011 // since memalign() should be used rarely. |
| 1012 int cl = Static::sizemap()->SizeClass(size); |
| 1013 while (cl < kNumClasses && |
| 1014 ((Static::sizemap()->class_to_size(cl) & (align - 1)) != 0)) { |
| 1015 cl++; |
| 1016 } |
| 1017 if (cl < kNumClasses) { |
| 1018 ThreadCache* heap = ThreadCache::GetCache(); |
| 1019 return CheckedMallocResult(heap->Allocate( |
| 1020 Static::sizemap()->class_to_size(cl))); |
| 1021 } |
| 1022 } |
| 1023 |
| 1024 // We will allocate directly from the page heap |
| 1025 SpinLockHolder h(Static::pageheap_lock()); |
| 1026 |
| 1027 if (align <= kPageSize) { |
| 1028 // Any page-level allocation will be fine |
| 1029 // TODO: We could put the rest of this page in the appropriate |
| 1030 // TODO: cache but it does not seem worth it. |
| 1031 Span* span = Static::pageheap()->New(tcmalloc::pages(size)); |
| 1032 return span == NULL ? NULL : SpanToMallocResult(span); |
| 1033 } |
| 1034 |
| 1035 // Allocate extra pages and carve off an aligned portion |
| 1036 const Length alloc = tcmalloc::pages(size + align); |
| 1037 Span* span = Static::pageheap()->New(alloc); |
| 1038 if (span == NULL) return NULL; |
| 1039 |
| 1040 // Skip starting portion so that we end up aligned |
| 1041 Length skip = 0; |
| 1042 while ((((span->start+skip) << kPageShift) & (align - 1)) != 0) { |
| 1043 skip++; |
| 1044 } |
| 1045 ASSERT(skip < alloc); |
| 1046 if (skip > 0) { |
| 1047 Span* rest = Static::pageheap()->Split(span, skip); |
| 1048 Static::pageheap()->Delete(span); |
| 1049 span = rest; |
| 1050 } |
| 1051 |
| 1052 // Skip trailing portion that we do not need to return |
| 1053 const Length needed = tcmalloc::pages(size); |
| 1054 ASSERT(span->length >= needed); |
| 1055 if (span->length > needed) { |
| 1056 Span* trailer = Static::pageheap()->Split(span, needed); |
| 1057 Static::pageheap()->Delete(trailer); |
| 1058 } |
| 1059 return SpanToMallocResult(span); |
| 1060 } |
| 1061 |
| 1062 // Helpers for use by exported routines below: |
| 1063 |
| 1064 inline void do_malloc_stats() { |
| 1065 PrintStats(1); |
| 1066 } |
| 1067 |
| 1068 inline int do_mallopt(int cmd, int value) { |
| 1069 return 1; // Indicates error |
| 1070 } |
| 1071 |
| 1072 #ifdef HAVE_STRUCT_MALLINFO // mallinfo isn't defined on freebsd, for instance |
| 1073 inline struct mallinfo do_mallinfo() { |
| 1074 TCMallocStats stats; |
| 1075 ExtractStats(&stats, NULL); |
| 1076 |
| 1077 // Just some of the fields are filled in. |
| 1078 struct mallinfo info; |
| 1079 memset(&info, 0, sizeof(info)); |
| 1080 |
| 1081 // Unfortunately, the struct contains "int" field, so some of the |
| 1082 // size values will be truncated. |
| 1083 info.arena = static_cast<int>(stats.system_bytes); |
| 1084 info.fsmblks = static_cast<int>(stats.thread_bytes |
| 1085 + stats.central_bytes |
| 1086 + stats.transfer_bytes); |
| 1087 info.fordblks = static_cast<int>(stats.pageheap_bytes); |
| 1088 info.uordblks = static_cast<int>(stats.system_bytes |
| 1089 - stats.thread_bytes |
| 1090 - stats.central_bytes |
| 1091 - stats.transfer_bytes |
| 1092 - stats.pageheap_bytes); |
| 1093 |
| 1094 return info; |
| 1095 } |
| 1096 #endif // #ifndef HAVE_STRUCT_MALLINFO |
| 1097 |
| 1098 static SpinLock set_new_handler_lock(SpinLock::LINKER_INITIALIZED); |
| 1099 |
| 1100 inline void* cpp_alloc(size_t size, bool nothrow) { |
| 1101 for (;;) { |
| 1102 void* p = do_malloc(size); |
| 1103 #ifdef PREANSINEW |
| 1104 return p; |
| 1105 #else |
| 1106 if (p == NULL) { // allocation failed |
| 1107 // Get the current new handler. NB: this function is not |
| 1108 // thread-safe. We make a feeble stab at making it so here, but |
| 1109 // this lock only protects against tcmalloc interfering with |
| 1110 // itself, not with other libraries calling set_new_handler. |
| 1111 std::new_handler nh; |
| 1112 { |
| 1113 SpinLockHolder h(&set_new_handler_lock); |
| 1114 nh = std::set_new_handler(0); |
| 1115 (void) std::set_new_handler(nh); |
| 1116 } |
| 1117 #if (defined(__GNUC__) && !defined(__EXCEPTIONS)) || (defined(_HAS_EXCEPTIONS) &
& !_HAS_EXCEPTIONS) |
| 1118 if (nh) { |
| 1119 // Since exceptions are disabled, we don't really know if new_handler |
| 1120 // failed. Assume it will abort if it fails. |
| 1121 (*nh)(); |
| 1122 continue; |
| 1123 } |
| 1124 return 0; |
| 1125 #else |
| 1126 // If no new_handler is established, the allocation failed. |
| 1127 if (!nh) { |
| 1128 if (nothrow) return 0; |
| 1129 throw std::bad_alloc(); |
| 1130 } |
| 1131 // Otherwise, try the new_handler. If it returns, retry the |
| 1132 // allocation. If it throws std::bad_alloc, fail the allocation. |
| 1133 // if it throws something else, don't interfere. |
| 1134 try { |
| 1135 (*nh)(); |
| 1136 } catch (const std::bad_alloc&) { |
| 1137 if (!nothrow) throw; |
| 1138 return p; |
| 1139 } |
| 1140 #endif // (defined(__GNUC__) && !defined(__EXCEPTIONS)) || (defined(_HAS_EXCEPT
IONS) && !_HAS_EXCEPTIONS) |
| 1141 } else { // allocation success |
| 1142 return p; |
| 1143 } |
| 1144 #endif // PREANSINEW |
| 1145 } |
| 1146 } |
| 1147 |
| 1148 inline void* cpp_memalign(size_t align, size_t size, bool nothrow) { |
| 1149 for (;;) { |
| 1150 void* p = do_memalign(align, size); |
| 1151 #ifdef PREANSINEW |
| 1152 return p; |
| 1153 #else |
| 1154 if (p == NULL) { // allocation failed |
| 1155 // Get the current new handler. NB: this function is not |
| 1156 // thread-safe. We make a feeble stab at making it so here, but |
| 1157 // this lock only protects against tcmalloc interfering with |
| 1158 // itself, not with other libraries calling set_new_handler. |
| 1159 std::new_handler nh; |
| 1160 { |
| 1161 SpinLockHolder h(&set_new_handler_lock); |
| 1162 nh = std::set_new_handler(0); |
| 1163 (void) std::set_new_handler(nh); |
| 1164 } |
| 1165 #if (defined(__GNUC__) && !defined(__EXCEPTIONS)) || (defined(_HAS_EXCEPTIONS) &
& !_HAS_EXCEPTIONS) |
| 1166 if (nh) { |
| 1167 // Since exceptions are disabled, we don't really know if new_handler |
| 1168 // failed. Assume it will abort if it fails. |
| 1169 (*nh)(); |
| 1170 continue; |
| 1171 } |
| 1172 return 0; |
| 1173 #else |
| 1174 // If no new_handler is established, the allocation failed. |
| 1175 if (!nh) { |
| 1176 if (nothrow) return 0; |
| 1177 throw std::bad_alloc(); |
| 1178 } |
| 1179 // Otherwise, try the new_handler. If it returns, retry the |
| 1180 // allocation. If it throws std::bad_alloc, fail the allocation. |
| 1181 // if it throws something else, don't interfere. |
| 1182 try { |
| 1183 (*nh)(); |
| 1184 } catch (const std::bad_alloc&) { |
| 1185 if (!nothrow) throw; |
| 1186 return p; |
| 1187 } |
| 1188 #endif // (defined(__GNUC__) && !defined(__EXCEPTIONS)) || (defined(_HAS_EXCEPT
IONS) && !_HAS_EXCEPTIONS) |
| 1189 } else { // allocation success |
| 1190 return p; |
| 1191 } |
| 1192 #endif // PREANSINEW |
| 1193 } |
| 1194 } |
| 1195 |
| 1196 } // end unnamed namespace |
| 1197 |
| 1198 // As promised, the definition of this function, declared above. |
| 1199 size_t TCMallocImplementation::GetAllocatedSize(void* ptr) { |
| 1200 return GetSizeWithCallback(ptr, &InvalidGetAllocatedSize); |
| 1201 } |
| 1202 |
| 1203 void TCMallocImplementation::MarkThreadBusy() { |
| 1204 // Allocate to force the creation of a thread cache, but avoid |
| 1205 // invoking any hooks. |
| 1206 do_free(do_malloc(0)); |
| 1207 } |
| 1208 |
| 1209 //------------------------------------------------------------------- |
| 1210 // Exported routines |
| 1211 //------------------------------------------------------------------- |
| 1212 |
| 1213 extern "C" PERFTOOLS_DLL_DECL const char* tc_version( |
| 1214 int* major, int* minor, const char** patch) __THROW { |
| 1215 if (major) *major = TC_VERSION_MAJOR; |
| 1216 if (minor) *minor = TC_VERSION_MINOR; |
| 1217 if (patch) *patch = TC_VERSION_PATCH; |
| 1218 return TC_VERSION_STRING; |
| 1219 } |
| 1220 |
| 1221 // CAVEAT: The code structure below ensures that MallocHook methods are always |
| 1222 // called from the stack frame of the invoked allocation function. |
| 1223 // heap-checker.cc depends on this to start a stack trace from |
| 1224 // the call to the (de)allocation function. |
| 1225 |
| 1226 extern "C" PERFTOOLS_DLL_DECL void* tc_malloc(size_t size) __THROW { |
| 1227 void* result = cpp_or_malloc(size, false); |
| 1228 MallocHook::InvokeNewHook(result, size); |
| 1229 return result; |
| 1230 } |
| 1231 |
| 1232 extern "C" PERFTOOLS_DLL_DECL void tc_free(void* ptr) __THROW { |
| 1233 MallocHook::InvokeDeleteHook(ptr); |
| 1234 do_free(ptr); |
| 1235 } |
| 1236 |
| 1237 extern "C" PERFTOOLS_DLL_DECL void* tc_calloc(size_t n, |
| 1238 size_t elem_size) __THROW { |
| 1239 void* result = do_calloc(n, elem_size); |
| 1240 MallocHook::InvokeNewHook(result, n * elem_size); |
| 1241 return result; |
| 1242 } |
| 1243 |
| 1244 extern "C" PERFTOOLS_DLL_DECL void tc_cfree(void* ptr) __THROW { |
| 1245 MallocHook::InvokeDeleteHook(ptr); |
| 1246 do_free(ptr); |
| 1247 } |
| 1248 |
| 1249 extern "C" PERFTOOLS_DLL_DECL void* tc_realloc(void* old_ptr, |
| 1250 size_t new_size) __THROW { |
| 1251 if (old_ptr == NULL) { |
| 1252 void* result = cpp_or_malloc(new_size, false); |
| 1253 MallocHook::InvokeNewHook(result, new_size); |
| 1254 return result; |
| 1255 } |
| 1256 if (new_size == 0) { |
| 1257 MallocHook::InvokeDeleteHook(old_ptr); |
| 1258 do_free(old_ptr); |
| 1259 return NULL; |
| 1260 } |
| 1261 return do_realloc(old_ptr, new_size); |
| 1262 } |
| 1263 |
| 1264 extern "C" PERFTOOLS_DLL_DECL void* tc_new(size_t size) { |
| 1265 void* p = cpp_alloc(size, false); |
| 1266 // We keep this next instruction out of cpp_alloc for a reason: when |
| 1267 // it's in, and new just calls cpp_alloc, the optimizer may fold the |
| 1268 // new call into cpp_alloc, which messes up our whole section-based |
| 1269 // stacktracing (see ATTRIBUTE_SECTION, above). This ensures cpp_alloc |
| 1270 // isn't the last thing this fn calls, and prevents the folding. |
| 1271 MallocHook::InvokeNewHook(p, size); |
| 1272 return p; |
| 1273 } |
| 1274 |
| 1275 extern "C" PERFTOOLS_DLL_DECL void* tc_new_nothrow( |
| 1276 size_t size, const std::nothrow_t&) __THROW { |
| 1277 void* p = cpp_alloc(size, true); |
| 1278 MallocHook::InvokeNewHook(p, size); |
| 1279 return p; |
| 1280 } |
| 1281 |
| 1282 extern "C" PERFTOOLS_DLL_DECL void tc_delete(void* p) __THROW { |
| 1283 MallocHook::InvokeDeleteHook(p); |
| 1284 do_free(p); |
| 1285 } |
| 1286 |
| 1287 // Compilers define and use this (via ::operator delete(ptr, nothrow)). |
| 1288 // But it's really the same as normal delete, so we just do the same thing. |
| 1289 extern "C" PERFTOOLS_DLL_DECL void tc_delete_nothrow( |
| 1290 void* p, const std::nothrow_t&) __THROW { |
| 1291 MallocHook::InvokeDeleteHook(p); |
| 1292 do_free(p); |
| 1293 } |
| 1294 |
| 1295 extern "C" PERFTOOLS_DLL_DECL void* tc_newarray(size_t size) { |
| 1296 void* p = cpp_alloc(size, false); |
| 1297 // We keep this next instruction out of cpp_alloc for a reason: when |
| 1298 // it's in, and new just calls cpp_alloc, the optimizer may fold the |
| 1299 // new call into cpp_alloc, which messes up our whole section-based |
| 1300 // stacktracing (see ATTRIBUTE_SECTION, above). This ensures cpp_alloc |
| 1301 // isn't the last thing this fn calls, and prevents the folding. |
| 1302 MallocHook::InvokeNewHook(p, size); |
| 1303 return p; |
| 1304 } |
| 1305 |
| 1306 extern "C" PERFTOOLS_DLL_DECL void* tc_newarray_nothrow( |
| 1307 size_t size, const std::nothrow_t&) __THROW { |
| 1308 void* p = cpp_alloc(size, true); |
| 1309 MallocHook::InvokeNewHook(p, size); |
| 1310 return p; |
| 1311 } |
| 1312 |
| 1313 extern "C" PERFTOOLS_DLL_DECL void tc_deletearray(void* p) __THROW { |
| 1314 MallocHook::InvokeDeleteHook(p); |
| 1315 do_free(p); |
| 1316 } |
| 1317 |
| 1318 extern "C" PERFTOOLS_DLL_DECL void tc_deletearray_nothrow( |
| 1319 void* p, const std::nothrow_t&) __THROW { |
| 1320 MallocHook::InvokeDeleteHook(p); |
| 1321 do_free(p); |
| 1322 } |
| 1323 |
| 1324 extern "C" PERFTOOLS_DLL_DECL void* tc_memalign(size_t align, |
| 1325 size_t size) __THROW { |
| 1326 void* result = cpp_or_memalign(align, size, false); |
| 1327 MallocHook::InvokeNewHook(result, size); |
| 1328 return result; |
| 1329 } |
| 1330 |
| 1331 extern "C" PERFTOOLS_DLL_DECL int tc_posix_memalign( |
| 1332 void** result_ptr, size_t align, size_t size) __THROW { |
| 1333 if (((align % sizeof(void*)) != 0) || |
| 1334 ((align & (align - 1)) != 0) || |
| 1335 (align == 0)) { |
| 1336 return EINVAL; |
| 1337 } |
| 1338 |
| 1339 void* result = cpp_or_memalign(align, size, false); |
| 1340 MallocHook::InvokeNewHook(result, size); |
| 1341 if (result == NULL) { |
| 1342 return ENOMEM; |
| 1343 } else { |
| 1344 *result_ptr = result; |
| 1345 return 0; |
| 1346 } |
| 1347 } |
| 1348 |
| 1349 static size_t pagesize = 0; |
| 1350 |
| 1351 extern "C" PERFTOOLS_DLL_DECL void* tc_valloc(size_t size) __THROW { |
| 1352 // Allocate page-aligned object of length >= size bytes |
| 1353 if (pagesize == 0) pagesize = getpagesize(); |
| 1354 void* result = cpp_or_memalign(pagesize, size, false); |
| 1355 MallocHook::InvokeNewHook(result, size); |
| 1356 return result; |
| 1357 } |
| 1358 |
| 1359 extern "C" PERFTOOLS_DLL_DECL void* tc_pvalloc(size_t size) __THROW { |
| 1360 // Round up size to a multiple of pagesize |
| 1361 if (pagesize == 0) pagesize = getpagesize(); |
| 1362 if (size == 0) { // pvalloc(0) should allocate one page, according to |
| 1363 size = pagesize; // http://man.free4web.biz/man3/libmpatrol.3.html |
| 1364 } |
| 1365 size = (size + pagesize - 1) & ~(pagesize - 1); |
| 1366 void* result = cpp_or_memalign(pagesize, size, false); |
| 1367 MallocHook::InvokeNewHook(result, size); |
| 1368 return result; |
| 1369 } |
| 1370 |
| 1371 extern "C" PERFTOOLS_DLL_DECL void tc_malloc_stats(void) __THROW { |
| 1372 do_malloc_stats(); |
| 1373 } |
| 1374 |
| 1375 extern "C" PERFTOOLS_DLL_DECL int tc_mallopt(int cmd, int value) __THROW { |
| 1376 return do_mallopt(cmd, value); |
| 1377 } |
| 1378 |
| 1379 #ifdef HAVE_STRUCT_MALLINFO |
| 1380 extern "C" PERFTOOLS_DLL_DECL struct mallinfo tc_mallinfo(void) __THROW { |
| 1381 return do_mallinfo(); |
| 1382 } |
| 1383 #endif |
| 1384 |
| 1385 // This function behaves similarly to MSVC's _set_new_mode. |
| 1386 // If flag is 0 (default), calls to malloc will behave normally. |
| 1387 // If flag is 1, calls to malloc will behave like calls to new, |
| 1388 // and the std_new_handler will be invoked on failure. |
| 1389 // Returns the previous mode. |
| 1390 extern "C" PERFTOOLS_DLL_DECL int tc_set_new_mode(int flag) __THROW { |
| 1391 int old_mode = tc_new_mode; |
| 1392 tc_new_mode = flag; |
| 1393 return old_mode; |
| 1394 } |
| 1395 |
| 1396 |
| 1397 // Override __libc_memalign in libc on linux boxes specially. |
| 1398 // They have a bug in libc that causes them to (very rarely) allocate |
| 1399 // with __libc_memalign() yet deallocate with free() and the |
| 1400 // definitions above don't catch it. |
| 1401 // This function is an exception to the rule of calling MallocHook method |
| 1402 // from the stack frame of the allocation function; |
| 1403 // heap-checker handles this special case explicitly. |
| 1404 #ifndef TCMALLOC_FOR_DEBUGALLOCATION |
| 1405 static void *MemalignOverride(size_t align, size_t size, const void *caller) |
| 1406 __THROW ATTRIBUTE_SECTION(google_malloc); |
| 1407 |
| 1408 static void *MemalignOverride(size_t align, size_t size, const void *caller) |
| 1409 __THROW { |
| 1410 void* result = do_memalign(align, size); |
| 1411 MallocHook::InvokeNewHook(result, size); |
| 1412 return result; |
| 1413 } |
| 1414 void *(*__memalign_hook)(size_t, size_t, const void *) = MemalignOverride; |
| 1415 #endif // #ifndef TCMALLOC_FOR_DEBUGALLOCATION |
OLD | NEW |