OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // * allocation of a reasonably complicated struct | 86 // * allocation of a reasonably complicated struct |
87 // goes from about 1100 ns to about 300 ns. | 87 // goes from about 1100 ns to about 300 ns. |
88 | 88 |
89 #include "config.h" | 89 #include "config.h" |
90 #include <google/tcmalloc.h> | 90 #include <google/tcmalloc.h> |
91 | 91 |
92 #include <errno.h> // for ENOMEM, EINVAL, errno | 92 #include <errno.h> // for ENOMEM, EINVAL, errno |
93 #ifdef HAVE_SYS_CDEFS_H | 93 #ifdef HAVE_SYS_CDEFS_H |
94 #include <sys/cdefs.h> // for __THROW | 94 #include <sys/cdefs.h> // for __THROW |
95 #endif | 95 #endif |
96 #ifdef HAVE_FEATURES_H | |
97 #include <features.h> // for __GLIBC__ | |
98 #endif | |
99 #if defined HAVE_STDINT_H | 96 #if defined HAVE_STDINT_H |
100 #include <stdint.h> | 97 #include <stdint.h> |
101 #elif defined HAVE_INTTYPES_H | 98 #elif defined HAVE_INTTYPES_H |
102 #include <inttypes.h> | 99 #include <inttypes.h> |
103 #else | 100 #else |
104 #include <sys/types.h> | 101 #include <sys/types.h> |
105 #endif | 102 #endif |
106 #include <stddef.h> // for size_t, NULL | 103 #include <stddef.h> // for size_t, NULL |
107 #include <stdlib.h> // for getenv | 104 #include <stdlib.h> // for getenv |
108 #include <string.h> // for strcmp, memset, strlen, etc | 105 #include <string.h> // for strcmp, memset, strlen, etc |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 # include <sys/malloc.h> | 140 # include <sys/malloc.h> |
144 # elif defined(HAVE_MALLOC_MALLOC_H) | 141 # elif defined(HAVE_MALLOC_MALLOC_H) |
145 # include <malloc/malloc.h> | 142 # include <malloc/malloc.h> |
146 # endif | 143 # endif |
147 #endif | 144 #endif |
148 | 145 |
149 #if (defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)) && !defi
ned(WIN32_OVERRIDE_ALLOCATORS) | 146 #if (defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)) && !defi
ned(WIN32_OVERRIDE_ALLOCATORS) |
150 # define WIN32_DO_PATCHING 1 | 147 # define WIN32_DO_PATCHING 1 |
151 #endif | 148 #endif |
152 | 149 |
153 // GLibc 2.14+ requires the hook functions be declared volatile, based on the | |
154 // value of the define __MALLOC_HOOK_VOLATILE. For compatibility with | |
155 // older/non-GLibc implementations, provide an empty definition. | |
156 #if !defined(__MALLOC_HOOK_VOLATILE) | |
157 #define __MALLOC_HOOK_VOLATILE | |
158 #endif | |
159 | |
160 using STL_NAMESPACE::max; | 150 using STL_NAMESPACE::max; |
161 using STL_NAMESPACE::numeric_limits; | 151 using STL_NAMESPACE::numeric_limits; |
162 using STL_NAMESPACE::vector; | 152 using STL_NAMESPACE::vector; |
| 153 |
| 154 #include "libc_override.h" |
| 155 |
| 156 // __THROW is defined in glibc (via <sys/cdefs.h>). It means, |
| 157 // counter-intuitively, "This function will never throw an exception." |
| 158 // It's an optional optimization tool, but we may need to use it to |
| 159 // match glibc prototypes. |
| 160 #ifndef __THROW // I guess we're not on a glibc system |
| 161 # define __THROW // __THROW is just an optimization, so ok to make it "" |
| 162 #endif |
| 163 |
163 using tcmalloc::AlignmentForSize; | 164 using tcmalloc::AlignmentForSize; |
164 using tcmalloc::PageHeap; | 165 using tcmalloc::PageHeap; |
165 using tcmalloc::PageHeapAllocator; | 166 using tcmalloc::PageHeapAllocator; |
166 using tcmalloc::SizeMap; | 167 using tcmalloc::SizeMap; |
167 using tcmalloc::Span; | 168 using tcmalloc::Span; |
168 using tcmalloc::StackTrace; | 169 using tcmalloc::StackTrace; |
169 using tcmalloc::Static; | 170 using tcmalloc::Static; |
170 using tcmalloc::ThreadCache; | 171 using tcmalloc::ThreadCache; |
171 | 172 |
172 // __THROW is defined in glibc systems. It means, counter-intuitively, | |
173 // "This function will never throw an exception." It's an optional | |
174 // optimization tool, but we may need to use it to match glibc prototypes. | |
175 #ifndef __THROW // I guess we're not on a glibc system | |
176 # define __THROW // __THROW is just an optimization, so ok to make it "" | |
177 #endif | |
178 | |
179 // ---- Double free debug declarations | 173 // ---- Double free debug declarations |
180 static size_t ExcludeSpaceForMark(size_t size); | 174 static size_t ExcludeSpaceForMark(size_t size); |
181 static void AddRoomForMark(size_t* size); | 175 static void AddRoomForMark(size_t* size); |
182 static void ExcludeMarkFromSize(size_t* new_size); | 176 static void ExcludeMarkFromSize(size_t* new_size); |
183 static void MarkAllocatedRegion(void* ptr); | 177 static void MarkAllocatedRegion(void* ptr); |
184 static void ValidateAllocatedRegion(void* ptr, size_t cl); | 178 static void ValidateAllocatedRegion(void* ptr, size_t cl); |
185 // ---- End Double free debug declarations | 179 // ---- End Double free debug declarations |
186 | 180 |
187 DECLARE_int64(tcmalloc_sample_parameter); | 181 DECLARE_int64(tcmalloc_sample_parameter); |
188 DECLARE_double(tcmalloc_release_rate); | 182 DECLARE_double(tcmalloc_release_rate); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 // Some non-standard extensions that we support. | 267 // Some non-standard extensions that we support. |
274 | 268 |
275 // This is equivalent to | 269 // This is equivalent to |
276 // OS X: malloc_size() | 270 // OS X: malloc_size() |
277 // glibc: malloc_usable_size() | 271 // glibc: malloc_usable_size() |
278 // Windows: _msize() | 272 // Windows: _msize() |
279 size_t tc_malloc_size(void* p) __THROW | 273 size_t tc_malloc_size(void* p) __THROW |
280 ATTRIBUTE_SECTION(google_malloc); | 274 ATTRIBUTE_SECTION(google_malloc); |
281 } // extern "C" | 275 } // extern "C" |
282 | 276 |
283 // Override the libc functions to prefer our own instead. This comes | |
284 // first so code in tcmalloc.cc can use the overridden versions. One | |
285 // exception: in windows, by default, we patch our code into these | |
286 // functions (via src/windows/patch_function.cc) rather than override | |
287 // them. In that case, we don't want to do this overriding here. | |
288 #if !defined(WIN32_DO_PATCHING) | |
289 | |
290 // TODO(mbelshe): Turn off TCMalloc's symbols for libc. We do that | |
291 // elsewhere. | |
292 #ifndef _WIN32 | |
293 | |
294 #if defined(__GNUC__) && !defined(__MACH__) | |
295 // Potentially faster variants that use the gcc alias extension. | |
296 // FreeBSD does support aliases, but apparently not correctly. :-( | |
297 // NOTE: we make many of these symbols weak, but do so in the makefile | |
298 // (via objcopy -W) and not here. That ends up being more portable. | |
299 # define ALIAS(x) __attribute__ ((alias (x))) | |
300 void* operator new(size_t size) throw (std::bad_alloc) ALIAS("tc_new"); | |
301 void operator delete(void* p) __THROW ALIAS("tc_delete"); | |
302 void* operator new[](size_t size) throw (std::bad_alloc) ALIAS("tc_newarray"); | |
303 void operator delete[](void* p) __THROW ALIAS("tc_deletearray"); | |
304 void* operator new(size_t size, const std::nothrow_t&) __THROW | |
305 ALIAS("tc_new_nothrow"); | |
306 void* operator new[](size_t size, const std::nothrow_t&) __THROW | |
307 ALIAS("tc_newarray_nothrow"); | |
308 void operator delete(void* size, const std::nothrow_t&) __THROW | |
309 ALIAS("tc_delete_nothrow"); | |
310 void operator delete[](void* size, const std::nothrow_t&) __THROW | |
311 ALIAS("tc_deletearray_nothrow"); | |
312 extern "C" { | |
313 void* malloc(size_t size) __THROW ALIAS("tc_malloc"); | |
314 void free(void* ptr) __THROW ALIAS("tc_free"); | |
315 void* realloc(void* ptr, size_t size) __THROW ALIAS("tc_realloc"); | |
316 void* calloc(size_t n, size_t size) __THROW ALIAS("tc_calloc"); | |
317 void cfree(void* ptr) __THROW ALIAS("tc_cfree"); | |
318 void* memalign(size_t align, size_t s) __THROW ALIAS("tc_memalign"); | |
319 void* valloc(size_t size) __THROW ALIAS("tc_valloc"); | |
320 void* pvalloc(size_t size) __THROW ALIAS("tc_pvalloc"); | |
321 int posix_memalign(void** r, size_t a, size_t s) __THROW | |
322 ALIAS("tc_posix_memalign"); | |
323 void malloc_stats(void) __THROW ALIAS("tc_malloc_stats"); | |
324 int mallopt(int cmd, int value) __THROW ALIAS("tc_mallopt"); | |
325 #ifdef HAVE_STRUCT_MALLINFO | |
326 struct mallinfo mallinfo(void) __THROW ALIAS("tc_mallinfo"); | |
327 #endif | |
328 size_t malloc_size(void* p) __THROW ALIAS("tc_malloc_size"); | |
329 size_t malloc_usable_size(void* p) __THROW ALIAS("tc_malloc_size"); | |
330 } // extern "C" | |
331 #else // #if defined(__GNUC__) && !defined(__MACH__) | |
332 // Portable wrappers | |
333 void* operator new(size_t size) { return tc_new(size); } | |
334 void operator delete(void* p) __THROW { tc_delete(p); } | |
335 void* operator new[](size_t size) { return tc_newarray(size); } | |
336 void operator delete[](void* p) __THROW { tc_deletearray(p); } | |
337 void* operator new(size_t size, const std::nothrow_t& nt) __THROW { | |
338 return tc_new_nothrow(size, nt); | |
339 } | |
340 void* operator new[](size_t size, const std::nothrow_t& nt) __THROW { | |
341 return tc_newarray_nothrow(size, nt); | |
342 } | |
343 void operator delete(void* ptr, const std::nothrow_t& nt) __THROW { | |
344 return tc_delete_nothrow(ptr, nt); | |
345 } | |
346 void operator delete[](void* ptr, const std::nothrow_t& nt) __THROW { | |
347 return tc_deletearray_nothrow(ptr, nt); | |
348 } | |
349 extern "C" { | |
350 void* malloc(size_t s) __THROW { return tc_malloc(s); } | |
351 void free(void* p) __THROW { tc_free(p); } | |
352 void* realloc(void* p, size_t s) __THROW { return tc_realloc(p, s); } | |
353 void* calloc(size_t n, size_t s) __THROW { return tc_calloc(n, s); } | |
354 void cfree(void* p) __THROW { tc_cfree(p); } | |
355 void* memalign(size_t a, size_t s) __THROW { return tc_memalign(a, s); } | |
356 void* valloc(size_t s) __THROW { return tc_valloc(s); } | |
357 void* pvalloc(size_t s) __THROW { return tc_pvalloc(s); } | |
358 int posix_memalign(void** r, size_t a, size_t s) __THROW { | |
359 return tc_posix_memalign(r, a, s); | |
360 } | |
361 void malloc_stats(void) __THROW { tc_malloc_stats(); } | |
362 int mallopt(int cmd, int v) __THROW { return tc_mallopt(cmd, v); } | |
363 #ifdef HAVE_STRUCT_MALLINFO | |
364 struct mallinfo mallinfo(void) __THROW { return tc_mallinfo(); } | |
365 #endif | |
366 size_t malloc_size(void* p) __THROW { return tc_malloc_size(p); } | |
367 size_t malloc_usable_size(void* p) __THROW { return tc_malloc_size(p); } | |
368 } // extern "C" | |
369 #endif // #if defined(__GNUC__) | |
370 | |
371 // Some library routines on RedHat 9 allocate memory using malloc() | |
372 // and free it using __libc_free() (or vice-versa). Since we provide | |
373 // our own implementations of malloc/free, we need to make sure that | |
374 // the __libc_XXX variants (defined as part of glibc) also point to | |
375 // the same implementations. | |
376 #ifdef __GLIBC__ // only glibc defines __libc_* | |
377 extern "C" { | |
378 #ifdef ALIAS | |
379 void* __libc_malloc(size_t size) ALIAS("tc_malloc"); | |
380 void __libc_free(void* ptr) ALIAS("tc_free"); | |
381 void* __libc_realloc(void* ptr, size_t size) ALIAS("tc_realloc"); | |
382 void* __libc_calloc(size_t n, size_t size) ALIAS("tc_calloc"); | |
383 void __libc_cfree(void* ptr) ALIAS("tc_cfree"); | |
384 void* __libc_memalign(size_t align, size_t s) ALIAS("tc_memalign"); | |
385 void* __libc_valloc(size_t size) ALIAS("tc_valloc"); | |
386 void* __libc_pvalloc(size_t size) ALIAS("tc_pvalloc"); | |
387 int __posix_memalign(void** r, size_t a, size_t s) ALIAS("tc_posix_memalign"); | |
388 #else // #ifdef ALIAS | |
389 void* __libc_malloc(size_t size) { return malloc(size); } | |
390 void __libc_free(void* ptr) { free(ptr); } | |
391 void* __libc_realloc(void* ptr, size_t size) { return realloc(ptr, size); } | |
392 void* __libc_calloc(size_t n, size_t size) { return calloc(n, size); } | |
393 void __libc_cfree(void* ptr) { cfree(ptr); } | |
394 void* __libc_memalign(size_t align, size_t s) { return memalign(align, s); } | |
395 void* __libc_valloc(size_t size) { return valloc(size); } | |
396 void* __libc_pvalloc(size_t size) { return pvalloc(size); } | |
397 int __posix_memalign(void** r, size_t a, size_t s) { | |
398 return posix_memalign(r, a, s); | |
399 } | |
400 #endif // #ifdef ALIAS | |
401 } // extern "C" | |
402 #endif // ifdef __GLIBC__ | |
403 | |
404 #if defined(__GLIBC__) && defined(HAVE_MALLOC_H) | |
405 // If we're using glibc, then override glibc malloc hooks to make sure that even | |
406 // if calls fall through to ptmalloc (due to dlopen() with RTLD_DEEPBIND or what | |
407 // not), ptmalloc will use TCMalloc. | |
408 | |
409 static void* tc_ptmalloc_malloc_hook(size_t size, const void* caller) { | |
410 return tc_malloc(size); | |
411 } | |
412 | |
413 void* (*__MALLOC_HOOK_VOLATILE __malloc_hook)( | |
414 size_t size, const void* caller) = tc_ptmalloc_malloc_hook; | |
415 | |
416 static void* tc_ptmalloc_realloc_hook( | |
417 void* ptr, size_t size, const void* caller) { | |
418 return tc_realloc(ptr, size); | |
419 } | |
420 | |
421 void* (*__MALLOC_HOOK_VOLATILE __realloc_hook)( | |
422 void* ptr, size_t size, const void* caller) = tc_ptmalloc_realloc_hook; | |
423 | |
424 static void tc_ptmalloc_free_hook(void* ptr, const void* caller) { | |
425 tc_free(ptr); | |
426 } | |
427 | |
428 void (*__MALLOC_HOOK_VOLATILE __free_hook)(void* ptr, const void* caller) = tc_p
tmalloc_free_hook; | |
429 | |
430 #endif | |
431 | |
432 #endif // #ifndef _WIN32 | |
433 #undef ALIAS | |
434 | |
435 #endif // #ifndef(WIN32_DO_PATCHING) | |
436 | |
437 | |
438 // ----------------------- IMPLEMENTATION ------------------------------- | 277 // ----------------------- IMPLEMENTATION ------------------------------- |
439 | 278 |
440 static int tc_new_mode = 0; // See tc_set_new_mode(). | 279 static int tc_new_mode = 0; // See tc_set_new_mode(). |
441 | 280 |
442 // Routines such as free() and realloc() catch some erroneous pointers | 281 // Routines such as free() and realloc() catch some erroneous pointers |
443 // passed to them, and invoke the below when they do. (An erroneous pointer | 282 // passed to them, and invoke the below when they do. (An erroneous pointer |
444 // won't be caught if it's within a valid span or a stale span for which | 283 // won't be caught if it's within a valid span or a stale span for which |
445 // the pagemap cache has a non-zero sizeclass.) This is a cheap (source-editing | 284 // the pagemap cache has a non-zero sizeclass.) This is a cheap (source-editing |
446 // required) kind of exception handling for these routines. | 285 // required) kind of exception handling for these routines. |
447 namespace { | 286 namespace { |
(...skipping 21 matching lines...) Expand all Loading... |
469 PageHeap::Stats pageheap; // Stats from page heap | 308 PageHeap::Stats pageheap; // Stats from page heap |
470 }; | 309 }; |
471 | 310 |
472 // Get stats into "r". Also get per-size-class counts if class_count != NULL | 311 // Get stats into "r". Also get per-size-class counts if class_count != NULL |
473 static void ExtractStats(TCMallocStats* r, uint64_t* class_count) { | 312 static void ExtractStats(TCMallocStats* r, uint64_t* class_count) { |
474 r->central_bytes = 0; | 313 r->central_bytes = 0; |
475 r->transfer_bytes = 0; | 314 r->transfer_bytes = 0; |
476 for (int cl = 0; cl < kNumClasses; ++cl) { | 315 for (int cl = 0; cl < kNumClasses; ++cl) { |
477 const int length = Static::central_cache()[cl].length(); | 316 const int length = Static::central_cache()[cl].length(); |
478 const int tc_length = Static::central_cache()[cl].tc_length(); | 317 const int tc_length = Static::central_cache()[cl].tc_length(); |
| 318 const size_t cache_overhead = Static::central_cache()[cl].OverheadBytes(); |
479 const size_t size = static_cast<uint64_t>( | 319 const size_t size = static_cast<uint64_t>( |
480 Static::sizemap()->ByteSizeForClass(cl)); | 320 Static::sizemap()->ByteSizeForClass(cl)); |
481 r->central_bytes += (size * length); | 321 r->central_bytes += (size * length) + cache_overhead; |
482 r->transfer_bytes += (size * tc_length); | 322 r->transfer_bytes += (size * tc_length); |
483 if (class_count) class_count[cl] = length + tc_length; | 323 if (class_count) class_count[cl] = length + tc_length; |
484 } | 324 } |
485 | 325 |
486 // Add stats from per-thread heaps | 326 // Add stats from per-thread heaps |
487 r->thread_bytes = 0; | 327 r->thread_bytes = 0; |
488 { // scope | 328 { // scope |
489 SpinLockHolder h(Static::pageheap_lock()); | 329 SpinLockHolder h(Static::pageheap_lock()); |
490 ThreadCache::GetThreadStats(&r->thread_bytes, class_count); | 330 ThreadCache::GetThreadStats(&r->thread_bytes, class_count); |
491 r->metadata_bytes = tcmalloc::metadata_system_bytes(); | 331 r->metadata_bytes = tcmalloc::metadata_system_bytes(); |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
861 } else { | 701 } else { |
862 return tcmalloc::pages(size) << kPageShift; | 702 return tcmalloc::pages(size) << kPageShift; |
863 } | 703 } |
864 } | 704 } |
865 | 705 |
866 // This just calls GetSizeWithCallback, but because that's in an | 706 // This just calls GetSizeWithCallback, but because that's in an |
867 // unnamed namespace, we need to move the definition below it in the | 707 // unnamed namespace, we need to move the definition below it in the |
868 // file. | 708 // file. |
869 virtual size_t GetAllocatedSize(void* ptr); | 709 virtual size_t GetAllocatedSize(void* ptr); |
870 | 710 |
| 711 // This duplicates some of the logic in GetSizeWithCallback, but is |
| 712 // faster. This is important on OS X, where this function is called |
| 713 // on every allocation operation. |
| 714 virtual Ownership GetOwnership(const void* ptr) { |
| 715 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; |
| 716 // The rest of tcmalloc assumes that all allocated pointers use at |
| 717 // most kAddressBits bits. If ptr doesn't, then it definitely |
| 718 // wasn't alloacted by tcmalloc. |
| 719 if ((p >> (kAddressBits - kPageShift)) > 0) { |
| 720 return kNotOwned; |
| 721 } |
| 722 size_t cl = Static::pageheap()->GetSizeClassIfCached(p); |
| 723 if (cl != 0) { |
| 724 return kOwned; |
| 725 } |
| 726 const Span *span = Static::pageheap()->GetDescriptor(p); |
| 727 return span ? kOwned : kNotOwned; |
| 728 } |
| 729 |
871 virtual void GetFreeListSizes(vector<MallocExtension::FreeListInfo>* v) { | 730 virtual void GetFreeListSizes(vector<MallocExtension::FreeListInfo>* v) { |
872 static const char* kCentralCacheType = "tcmalloc.central"; | 731 static const char* kCentralCacheType = "tcmalloc.central"; |
873 static const char* kTransferCacheType = "tcmalloc.transfer"; | 732 static const char* kTransferCacheType = "tcmalloc.transfer"; |
874 static const char* kThreadCacheType = "tcmalloc.thread"; | 733 static const char* kThreadCacheType = "tcmalloc.thread"; |
875 static const char* kPageHeapType = "tcmalloc.page"; | 734 static const char* kPageHeapType = "tcmalloc.page"; |
876 static const char* kPageHeapUnmappedType = "tcmalloc.page_unmapped"; | 735 static const char* kPageHeapUnmappedType = "tcmalloc.page_unmapped"; |
877 static const char* kLargeSpanType = "tcmalloc.large"; | 736 static const char* kLargeSpanType = "tcmalloc.large"; |
878 static const char* kLargeUnmappedSpanType = "tcmalloc.large_unmapped"; | 737 static const char* kLargeUnmappedSpanType = "tcmalloc.large_unmapped"; |
879 | 738 |
880 v->clear(); | 739 v->clear(); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
974 // well for STL). | 833 // well for STL). |
975 // | 834 // |
976 // The destructor prints stats when the program exits. | 835 // The destructor prints stats when the program exits. |
977 static int tcmallocguard_refcount = 0; // no lock needed: runs before main() | 836 static int tcmallocguard_refcount = 0; // no lock needed: runs before main() |
978 TCMallocGuard::TCMallocGuard() { | 837 TCMallocGuard::TCMallocGuard() { |
979 if (tcmallocguard_refcount++ == 0) { | 838 if (tcmallocguard_refcount++ == 0) { |
980 #ifdef HAVE_TLS // this is true if the cc/ld/libc combo support TLS | 839 #ifdef HAVE_TLS // this is true if the cc/ld/libc combo support TLS |
981 // Check whether the kernel also supports TLS (needs to happen at runtime) | 840 // Check whether the kernel also supports TLS (needs to happen at runtime) |
982 tcmalloc::CheckIfKernelSupportsTLS(); | 841 tcmalloc::CheckIfKernelSupportsTLS(); |
983 #endif | 842 #endif |
984 #ifdef WIN32_DO_PATCHING | 843 ReplaceSystemAlloc(); // defined in libc_override_*.h |
985 // patch the windows VirtualAlloc, etc. | 844 #if defined(__APPLE__) |
986 PatchWindowsFunctions(); // defined in windows/patch_functions.cc | 845 // To break the recursive call of malloc, as malloc -> TCMALLOC_MESSAGE |
| 846 // -> snprintf -> localeconv_l -> malloc, on MacOS. |
| 847 char buf[32]; |
| 848 snprintf(buf, sizeof(buf), "%d", tcmallocguard_refcount); |
987 #endif | 849 #endif |
988 tc_free(tc_malloc(1)); | 850 tc_free(tc_malloc(1)); |
989 ThreadCache::InitTSD(); | 851 ThreadCache::InitTSD(); |
990 tc_free(tc_malloc(1)); | 852 tc_free(tc_malloc(1)); |
991 // Either we, or debugallocation.cc, or valgrind will control memory | 853 // Either we, or debugallocation.cc, or valgrind will control memory |
992 // management. We register our extension if we're the winner. | 854 // management. We register our extension if we're the winner. |
993 #ifdef TCMALLOC_USING_DEBUGALLOCATION | 855 #ifdef TCMALLOC_USING_DEBUGALLOCATION |
994 // Let debugallocation register its extension. | 856 // Let debugallocation register its extension. |
995 #else | 857 #else |
996 if (RunningOnValgrind()) { | 858 if (RunningOnValgrind()) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1074 (kPageSize > FLAGS_tcmalloc_large_alloc_report_threshold | 936 (kPageSize > FLAGS_tcmalloc_large_alloc_report_threshold |
1075 ? kPageSize : FLAGS_tcmalloc_large_alloc_report_threshold); | 937 ? kPageSize : FLAGS_tcmalloc_large_alloc_report_threshold); |
1076 | 938 |
1077 static void ReportLargeAlloc(Length num_pages, void* result) { | 939 static void ReportLargeAlloc(Length num_pages, void* result) { |
1078 StackTrace stack; | 940 StackTrace stack; |
1079 stack.depth = GetStackTrace(stack.stack, tcmalloc::kMaxStackDepth, 1); | 941 stack.depth = GetStackTrace(stack.stack, tcmalloc::kMaxStackDepth, 1); |
1080 | 942 |
1081 static const int N = 1000; | 943 static const int N = 1000; |
1082 char buffer[N]; | 944 char buffer[N]; |
1083 TCMalloc_Printer printer(buffer, N); | 945 TCMalloc_Printer printer(buffer, N); |
1084 printer.printf("tcmalloc: large alloc %llu bytes == %p @ ", | 946 printer.printf("tcmalloc: large alloc %"PRIu64" bytes == %p @ ", |
1085 static_cast<unsigned long long>(num_pages) << kPageShift, | 947 static_cast<uint64>(num_pages) << kPageShift, |
1086 result); | 948 result); |
1087 for (int i = 0; i < stack.depth; i++) { | 949 for (int i = 0; i < stack.depth; i++) { |
1088 printer.printf(" %p", stack.stack[i]); | 950 printer.printf(" %p", stack.stack[i]); |
1089 } | 951 } |
1090 printer.printf("\n"); | 952 printer.printf("\n"); |
1091 write(STDERR_FILENO, buffer, strlen(buffer)); | 953 write(STDERR_FILENO, buffer, strlen(buffer)); |
1092 } | 954 } |
1093 | 955 |
1094 inline void* cpp_alloc(size_t size, bool nothrow); | 956 inline void* cpp_alloc(size_t size, bool nothrow); |
1095 inline void* do_malloc(size_t size); | 957 inline void* do_malloc(size_t size); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1242 } | 1104 } |
1243 Static::pageheap()->Delete(span); | 1105 Static::pageheap()->Delete(span); |
1244 } | 1106 } |
1245 } | 1107 } |
1246 | 1108 |
1247 // The default "do_free" that uses the default callback. | 1109 // The default "do_free" that uses the default callback. |
1248 inline void do_free(void* ptr) { | 1110 inline void do_free(void* ptr) { |
1249 return do_free_with_callback(ptr, &InvalidFree); | 1111 return do_free_with_callback(ptr, &InvalidFree); |
1250 } | 1112 } |
1251 | 1113 |
| 1114 // NOTE: some logic here is duplicated in GetOwnership (above), for |
| 1115 // speed. If you change this function, look at that one too. |
1252 inline size_t GetSizeWithCallback(void* ptr, | 1116 inline size_t GetSizeWithCallback(void* ptr, |
1253 size_t (*invalid_getsize_fn)(void*)) { | 1117 size_t (*invalid_getsize_fn)(void*)) { |
1254 if (ptr == NULL) | 1118 if (ptr == NULL) |
1255 return 0; | 1119 return 0; |
1256 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; | 1120 const PageID p = reinterpret_cast<uintptr_t>(ptr) >> kPageShift; |
1257 size_t cl = Static::pageheap()->GetSizeClassIfCached(p); | 1121 size_t cl = Static::pageheap()->GetSizeClassIfCached(p); |
1258 if (cl != 0) { | 1122 if (cl != 0) { |
1259 return Static::sizemap()->ByteSizeForClass(cl); | 1123 return Static::sizemap()->ByteSizeForClass(cl); |
1260 } else { | 1124 } else { |
1261 Span *span = Static::pageheap()->GetDescriptor(p); | 1125 const Span *span = Static::pageheap()->GetDescriptor(p); |
1262 if (span == NULL) { // means we do not own this memory | 1126 if (span == NULL) { // means we do not own this memory |
1263 return (*invalid_getsize_fn)(ptr); | 1127 return (*invalid_getsize_fn)(ptr); |
1264 } else if (span->sizeclass != 0) { | 1128 } else if (span->sizeclass != 0) { |
1265 Static::pageheap()->CacheSizeClass(p, span->sizeclass); | 1129 Static::pageheap()->CacheSizeClass(p, span->sizeclass); |
1266 return Static::sizemap()->ByteSizeForClass(span->sizeclass); | 1130 return Static::sizemap()->ByteSizeForClass(span->sizeclass); |
1267 } else { | 1131 } else { |
1268 return span->length << kPageShift; | 1132 return span->length << kPageShift; |
1269 } | 1133 } |
1270 } | 1134 } |
1271 } | 1135 } |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1539 return p; | 1403 return p; |
1540 } | 1404 } |
1541 #endif // PREANSINEW | 1405 #endif // PREANSINEW |
1542 } | 1406 } |
1543 } | 1407 } |
1544 | 1408 |
1545 } // end unnamed namespace | 1409 } // end unnamed namespace |
1546 | 1410 |
1547 // As promised, the definition of this function, declared above. | 1411 // As promised, the definition of this function, declared above. |
1548 size_t TCMallocImplementation::GetAllocatedSize(void* ptr) { | 1412 size_t TCMallocImplementation::GetAllocatedSize(void* ptr) { |
| 1413 ASSERT(TCMallocImplementation::GetOwnership(ptr) |
| 1414 != TCMallocImplementation::kNotOwned); |
1549 return ExcludeSpaceForMark( | 1415 return ExcludeSpaceForMark( |
1550 GetSizeWithCallback(ptr, &InvalidGetAllocatedSize)); | 1416 GetSizeWithCallback(ptr, &InvalidGetAllocatedSize)); |
1551 } | 1417 } |
1552 | 1418 |
1553 void TCMallocImplementation::MarkThreadBusy() { | 1419 void TCMallocImplementation::MarkThreadBusy() { |
1554 // Allocate to force the creation of a thread cache, but avoid | 1420 // Allocate to force the creation of a thread cache, but avoid |
1555 // invoking any hooks. | 1421 // invoking any hooks. |
1556 do_free(do_malloc(0)); | 1422 do_free(do_malloc(0)); |
1557 } | 1423 } |
1558 | 1424 |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1737 return do_mallopt(cmd, value); | 1603 return do_mallopt(cmd, value); |
1738 } | 1604 } |
1739 | 1605 |
1740 #ifdef HAVE_STRUCT_MALLINFO | 1606 #ifdef HAVE_STRUCT_MALLINFO |
1741 extern "C" PERFTOOLS_DLL_DECL struct mallinfo tc_mallinfo(void) __THROW { | 1607 extern "C" PERFTOOLS_DLL_DECL struct mallinfo tc_mallinfo(void) __THROW { |
1742 return do_mallinfo(); | 1608 return do_mallinfo(); |
1743 } | 1609 } |
1744 #endif | 1610 #endif |
1745 | 1611 |
1746 extern "C" PERFTOOLS_DLL_DECL size_t tc_malloc_size(void* ptr) __THROW { | 1612 extern "C" PERFTOOLS_DLL_DECL size_t tc_malloc_size(void* ptr) __THROW { |
1747 return GetSizeWithCallback(ptr, &InvalidGetAllocatedSize); | 1613 return MallocExtension::instance()->GetAllocatedSize(ptr); |
1748 } | 1614 } |
1749 | 1615 |
1750 | |
1751 // Override __libc_memalign in libc on linux boxes specially. | |
1752 // They have a bug in libc that causes them to (very rarely) allocate | |
1753 // with __libc_memalign() yet deallocate with free() and the | |
1754 // definitions above don't catch it. | |
1755 // This function is an exception to the rule of calling MallocHook method | |
1756 // from the stack frame of the allocation function; | |
1757 // heap-checker handles this special case explicitly. | |
1758 static void *MemalignOverride(size_t align, size_t size, const void *caller) | |
1759 __THROW ATTRIBUTE_SECTION(google_malloc); | |
1760 | |
1761 static void *MemalignOverride(size_t align, size_t size, const void *caller) | |
1762 __THROW { | |
1763 void* result = do_memalign_or_cpp_memalign(align, size); | |
1764 MallocHook::InvokeNewHook(result, size); | |
1765 return result; | |
1766 } | |
1767 void *(*__MALLOC_HOOK_VOLATILE __memalign_hook)(size_t, size_t, const void *) =
MemalignOverride; | |
1768 #endif // TCMALLOC_USING_DEBUGALLOCATION | 1616 #endif // TCMALLOC_USING_DEBUGALLOCATION |
1769 | 1617 |
1770 // ---Double free() debugging implementation ----------------------------------- | 1618 // ---Double free() debugging implementation ----------------------------------- |
1771 // We will put a mark at the extreme end of each allocation block. We make | 1619 // We will put a mark at the extreme end of each allocation block. We make |
1772 // sure that we always allocate enough "extra memory" that we can fit in the | 1620 // sure that we always allocate enough "extra memory" that we can fit in the |
1773 // mark, and still provide the requested usable region. If ever that mark is | 1621 // mark, and still provide the requested usable region. If ever that mark is |
1774 // not as expected, then we know that the user is corrupting memory beyond their | 1622 // not as expected, then we know that the user is corrupting memory beyond their |
1775 // request size, or that they have called free a second time without having | 1623 // request size, or that they have called free a second time without having |
1776 // the memory allocated (again). This allows us to spot most double free()s, | 1624 // the memory allocated (again). This allows us to spot most double free()s, |
1777 // but some can "slip by" or confuse our logic if the caller reallocates memory | 1625 // but some can "slip by" or confuse our logic if the caller reallocates memory |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1926 *mark = ~allocated_mark; // Distinctively not allocated. | 1774 *mark = ~allocated_mark; // Distinctively not allocated. |
1927 } | 1775 } |
1928 | 1776 |
1929 static void MarkAllocatedRegion(void* ptr) { | 1777 static void MarkAllocatedRegion(void* ptr) { |
1930 if (ptr == NULL) return; | 1778 if (ptr == NULL) return; |
1931 MarkType* mark = GetMarkLocation(ptr); | 1779 MarkType* mark = GetMarkLocation(ptr); |
1932 *mark = GetMarkValue(ptr, mark); | 1780 *mark = GetMarkValue(ptr, mark); |
1933 } | 1781 } |
1934 | 1782 |
1935 #endif // TCMALLOC_VALIDATION | 1783 #endif // TCMALLOC_VALIDATION |
OLD | NEW |