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