Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2008, Google Inc. | 1 // Copyright (c) 2008, 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 15 matching lines...) Expand all Loading... | |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | 29 |
| 30 // --- | 30 // --- |
| 31 // Author: Ken Ashcraft <opensource@google.com> | 31 // Author: Ken Ashcraft <opensource@google.com> |
| 32 | 32 |
| 33 #include "static_vars.h" | 33 #include "static_vars.h" |
| 34 #include <stddef.h> // for NULL | 34 #include <stddef.h> // for NULL |
| 35 #include <new> // for operator new | 35 #include <new> // for operator new |
| 36 #ifdef HAVE_PTHREAD | |
| 37 #include <pthread.h> // for pthread_atfork | |
| 38 #endif | |
| 36 #include "internal_logging.h" // for CHECK_CONDITION | 39 #include "internal_logging.h" // for CHECK_CONDITION |
| 37 #include "common.h" | 40 #include "common.h" |
| 38 #include "sampler.h" // for Sampler | 41 #include "sampler.h" // for Sampler |
| 39 | 42 |
| 40 namespace tcmalloc { | 43 namespace tcmalloc { |
| 41 | 44 |
| 42 SpinLock Static::pageheap_lock_(SpinLock::LINKER_INITIALIZED); | 45 SpinLock Static::pageheap_lock_(SpinLock::LINKER_INITIALIZED); |
| 43 SizeMap Static::sizemap_; | 46 SizeMap Static::sizemap_; |
| 44 CentralFreeListPadded Static::central_cache_[kNumClasses]; | 47 CentralFreeListPadded Static::central_cache_[kNumClasses]; |
| 45 PageHeapAllocator<Span> Static::span_allocator_; | 48 PageHeapAllocator<Span> Static::span_allocator_; |
| 46 PageHeapAllocator<StackTrace> Static::stacktrace_allocator_; | 49 PageHeapAllocator<StackTrace> Static::stacktrace_allocator_; |
| 47 Span Static::sampled_objects_; | 50 Span Static::sampled_objects_; |
| 48 PageHeapAllocator<StackTraceTable::Bucket> Static::bucket_allocator_; | 51 PageHeapAllocator<StackTraceTable::Bucket> Static::bucket_allocator_; |
| 49 StackTrace* Static::growth_stacks_ = NULL; | 52 StackTrace* Static::growth_stacks_ = NULL; |
| 50 PageHeap* Static::pageheap_ = NULL; | 53 PageHeap* Static::pageheap_ = NULL; |
| 51 | 54 |
| 55 #ifdef HAVE_PTHREAD | |
| 56 // These following two functions are registered via pthread_atfork to make | |
| 57 // sure the central_cache locks remain in a consisten state in the forked | |
| 58 // version of the thread. | |
| 59 | |
| 60 void CentralCacheLockAll() | |
| 61 { | |
| 62 Static::pageheap_lock()->Lock(); | |
| 63 for (int i = 0; i < kNumClasses; ++i) | |
| 64 Static::central_cache()[i].Lock(); | |
| 65 } | |
| 66 | |
| 67 void CentralCacheUnlockAll() | |
| 68 { | |
| 69 for (int i = 0; i < kNumClasses; ++i) | |
| 70 Static::central_cache()[i].Unlock(); | |
| 71 Static::pageheap_lock()->Unlock(); | |
| 72 } | |
| 73 | |
| 74 #endif | |
| 75 | |
| 52 void Static::InitStaticVars() { | 76 void Static::InitStaticVars() { |
| 53 sizemap_.Init(); | 77 sizemap_.Init(); |
| 54 span_allocator_.Init(); | 78 span_allocator_.Init(); |
| 55 span_allocator_.New(); // Reduce cache conflicts | 79 span_allocator_.New(); // Reduce cache conflicts |
| 56 span_allocator_.New(); // Reduce cache conflicts | 80 span_allocator_.New(); // Reduce cache conflicts |
| 57 stacktrace_allocator_.Init(); | 81 stacktrace_allocator_.Init(); |
| 58 bucket_allocator_.Init(); | 82 bucket_allocator_.Init(); |
| 59 // Do a bit of sanitizing: make sure central_cache is aligned properly | 83 // Do a bit of sanitizing: make sure central_cache is aligned properly |
| 60 CHECK_CONDITION((sizeof(central_cache_[0]) % 64) == 0); | 84 CHECK_CONDITION((sizeof(central_cache_[0]) % 64) == 0); |
| 61 for (int i = 0; i < kNumClasses; ++i) { | 85 for (int i = 0; i < kNumClasses; ++i) { |
| 62 central_cache_[i].Init(i); | 86 central_cache_[i].Init(i); |
| 63 } | 87 } |
| 88 | |
| 89 #if defined(HAVE_PTHREAD) && !defined(__APPLE__) | |
| 90 pthread_atfork(CentralCacheLockAll, // parent calls before fork | |
|
rickyz (no longer on Chrome)
2015/12/16 12:13:39
Unlike the upstream commit, we keep the pthread_at
| |
| 91 CentralCacheUnlockAll, // parent calls after fork | |
| 92 CentralCacheUnlockAll); // child calls after fork | |
| 93 #endif | |
| 94 | |
| 64 // It's important to have PageHeap allocated, not in static storage, | 95 // It's important to have PageHeap allocated, not in static storage, |
| 65 // so that HeapLeakChecker does not consider all the byte patterns stored | 96 // so that HeapLeakChecker does not consider all the byte patterns stored |
| 66 // in is caches as pointers that are sources of heap object liveness, | 97 // in is caches as pointers that are sources of heap object liveness, |
| 67 // which leads to it missing some memory leaks. | 98 // which leads to it missing some memory leaks. |
| 68 pageheap_ = new (MetaDataAlloc(sizeof(PageHeap))) PageHeap; | 99 pageheap_ = new (MetaDataAlloc(sizeof(PageHeap))) PageHeap; |
| 69 DLL_Init(&sampled_objects_); | 100 DLL_Init(&sampled_objects_); |
| 70 Sampler::InitStatics(); | 101 Sampler::InitStatics(); |
| 71 } | 102 } |
| 72 | 103 |
| 73 } // namespace tcmalloc | 104 } // namespace tcmalloc |
| OLD | NEW |