static_vars.h 4.4 KB

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