1// Copyright (c) 2008, 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: Ken Ashcraft <opensource@google.com>
32//
33// Static variables shared by multiple classes.
34
35#ifndef TCMALLOC_STATIC_VARS_H_
36#define TCMALLOC_STATIC_VARS_H_
37
38#include <config.h>
39#include "base/spinlock.h"
40#include "central_freelist.h"
41#include "common.h"
42#include "page_heap.h"
43#include "page_heap_allocator.h"
44#include "span.h"
45#include "stack_trace_table.h"
46
47namespace tcmalloc {
48
49class Static {
50 public:
51  // Linker initialized, so this lock can be accessed at any time.
52  static SpinLock* pageheap_lock() { return &pageheap_lock_; }
53
54  // Must be called before calling any of the accessors below.
55  static void InitStaticVars();
56
57  // Central cache -- an array of free-lists, one per size-class.
58  // We have a separate lock per free-list to reduce contention.
59  static CentralFreeListPadded* central_cache() { return central_cache_; }
60
61  static SizeMap* sizemap() { return &sizemap_; }
62
63  //////////////////////////////////////////////////////////////////////
64  // In addition to the explicit initialization comment, the variables below
65  // must be protected by pageheap_lock.
66
67  // Page-level allocator.
68  static PageHeap* pageheap() { return pageheap_; }
69
70  static PageHeapAllocator<Span>* span_allocator() { return &span_allocator_; }
71
72  static PageHeapAllocator<StackTrace>* stacktrace_allocator() {
73    return &stacktrace_allocator_;
74  }
75
76  static StackTrace* growth_stacks() { return growth_stacks_; }
77  static void set_growth_stacks(StackTrace* s) { growth_stacks_ = s; }
78
79  // State kept for sampled allocations (/pprof/heap support)
80  static Span* sampled_objects() { return &sampled_objects_; }
81  static PageHeapAllocator<StackTraceTable::Bucket>* bucket_allocator() {
82    return &bucket_allocator_;
83  }
84
85 private:
86  static SpinLock pageheap_lock_;
87
88  // These static variables require explicit initialization.  We cannot
89  // count on their constructors to do any initialization because other
90  // static variables may try to allocate memory before these variables
91  // can run their constructors.
92
93  static SizeMap sizemap_;
94  static CentralFreeListPadded central_cache_[kNumClasses];
95  static PageHeapAllocator<Span> span_allocator_;
96  static PageHeapAllocator<StackTrace> stacktrace_allocator_;
97  static Span sampled_objects_;
98  static PageHeapAllocator<StackTraceTable::Bucket> bucket_allocator_;
99
100  // Linked list of stack traces recorded every time we allocated memory
101  // from the system.  Useful for finding allocation sites that cause
102  // increase in the footprint of the system.  The linked list pointer
103  // is stored in trace->stack[kMaxStackDepth-1].
104  static StackTrace* growth_stacks_;
105
106  static PageHeap* pageheap_;
107};
108
109}  // namespace tcmalloc
110
111#endif  // TCMALLOC_STATIC_VARS_H_
112