Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2008, Google Inc. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // --- |
| 32 | // Author: Ken Ashcraft <opensource@google.com> |
| 33 | // |
| 34 | // Static variables shared by multiple classes. |
| 35 | |
| 36 | #ifndef TCMALLOC_STATIC_VARS_H_ |
| 37 | #define TCMALLOC_STATIC_VARS_H_ |
| 38 | |
| 39 | #include <config.h> |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 40 | #include "base/basictypes.h" |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 41 | #include "base/spinlock.h" |
| 42 | #include "central_freelist.h" |
| 43 | #include "common.h" |
| 44 | #include "page_heap.h" |
| 45 | #include "page_heap_allocator.h" |
| 46 | #include "span.h" |
| 47 | #include "stack_trace_table.h" |
| 48 | |
| 49 | namespace tcmalloc { |
| 50 | |
| 51 | class Static { |
| 52 | public: |
| 53 | // Linker initialized, so this lock can be accessed at any time. |
| 54 | static SpinLock* pageheap_lock() { return &pageheap_lock_; } |
| 55 | |
| 56 | // Must be called before calling any of the accessors below. |
| 57 | static void InitStaticVars(); |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 58 | static void InitLateMaybeRecursive(); |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 59 | |
| 60 | // Central cache -- an array of free-lists, one per size-class. |
| 61 | // We have a separate lock per free-list to reduce contention. |
| 62 | static CentralFreeListPadded* central_cache() { return central_cache_; } |
| 63 | |
| 64 | static SizeMap* sizemap() { return &sizemap_; } |
| 65 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 66 | static unsigned num_size_classes() { return sizemap_.num_size_classes; } |
| 67 | |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 68 | ////////////////////////////////////////////////////////////////////// |
| 69 | // In addition to the explicit initialization comment, the variables below |
| 70 | // must be protected by pageheap_lock. |
| 71 | |
| 72 | // Page-level allocator. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 73 | static PageHeap* pageheap() { return reinterpret_cast<PageHeap *>(&pageheap_.memory); } |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 74 | |
| 75 | static PageHeapAllocator<Span>* span_allocator() { return &span_allocator_; } |
| 76 | |
| 77 | static PageHeapAllocator<StackTrace>* stacktrace_allocator() { |
| 78 | return &stacktrace_allocator_; |
| 79 | } |
| 80 | |
| 81 | static StackTrace* growth_stacks() { return growth_stacks_; } |
| 82 | static void set_growth_stacks(StackTrace* s) { growth_stacks_ = s; } |
| 83 | |
| 84 | // State kept for sampled allocations (/pprof/heap support) |
| 85 | static Span* sampled_objects() { return &sampled_objects_; } |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 86 | |
| 87 | // Check if InitStaticVars() has been run. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 88 | static bool IsInited() { return inited_; } |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 89 | |
| 90 | private: |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 91 | // some unit tests depend on this and link to static vars |
| 92 | // imperfectly. Thus we keep those unhidden for now. Thankfully |
| 93 | // they're not performance-critical. |
| 94 | /* ATTRIBUTE_HIDDEN */ static bool inited_; |
| 95 | /* ATTRIBUTE_HIDDEN */ static SpinLock pageheap_lock_; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 96 | |
| 97 | // These static variables require explicit initialization. We cannot |
| 98 | // count on their constructors to do any initialization because other |
| 99 | // static variables may try to allocate memory before these variables |
| 100 | // can run their constructors. |
| 101 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 102 | ATTRIBUTE_HIDDEN static SizeMap sizemap_; |
| 103 | ATTRIBUTE_HIDDEN static CentralFreeListPadded central_cache_[kClassSizesMax]; |
| 104 | ATTRIBUTE_HIDDEN static PageHeapAllocator<Span> span_allocator_; |
| 105 | ATTRIBUTE_HIDDEN static PageHeapAllocator<StackTrace> stacktrace_allocator_; |
| 106 | ATTRIBUTE_HIDDEN static Span sampled_objects_; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 107 | |
| 108 | // Linked list of stack traces recorded every time we allocated memory |
| 109 | // from the system. Useful for finding allocation sites that cause |
| 110 | // increase in the footprint of the system. The linked list pointer |
| 111 | // is stored in trace->stack[kMaxStackDepth-1]. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 112 | ATTRIBUTE_HIDDEN static StackTrace* growth_stacks_; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 113 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 114 | // PageHeap uses a constructor for initialization. Like the members above, |
| 115 | // we can't depend on initialization order, so pageheap is new'd |
| 116 | // into this buffer. |
| 117 | union PageHeapStorage { |
| 118 | char memory[sizeof(PageHeap)]; |
| 119 | uintptr_t extra; // To force alignment |
| 120 | }; |
| 121 | ATTRIBUTE_HIDDEN static PageHeapStorage pageheap_; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace tcmalloc |
| 125 | |
| 126 | #endif // TCMALLOC_STATIC_VARS_H_ |