page_heap_allocator.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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: Sanjay Ghemawat <opensource@google.com>
  32. #ifndef TCMALLOC_PAGE_HEAP_ALLOCATOR_H_
  33. #define TCMALLOC_PAGE_HEAP_ALLOCATOR_H_
  34. #include <stddef.h> // for NULL, size_t
  35. #include "common.h" // for MetaDataAlloc
  36. #include "internal_logging.h" // for ASSERT
  37. namespace tcmalloc {
  38. // Simple allocator for objects of a specified type. External locking
  39. // is required before accessing one of these objects.
  40. template <class T>
  41. class PageHeapAllocator {
  42. public:
  43. // We use an explicit Init function because these variables are statically
  44. // allocated and their constructors might not have run by the time some
  45. // other static variable tries to allocate memory.
  46. void Init() {
  47. ASSERT(sizeof(T) <= kAllocIncrement);
  48. inuse_ = 0;
  49. free_area_ = NULL;
  50. free_avail_ = 0;
  51. free_list_ = NULL;
  52. // Reserve some space at the beginning to avoid fragmentation.
  53. Delete(New());
  54. }
  55. T* New() {
  56. // Consult free list
  57. void* result;
  58. if (free_list_ != NULL) {
  59. result = free_list_;
  60. free_list_ = *(reinterpret_cast<void**>(result));
  61. } else {
  62. if (free_avail_ < sizeof(T)) {
  63. // Need more room. We assume that MetaDataAlloc returns
  64. // suitably aligned memory.
  65. free_area_ = reinterpret_cast<char*>(MetaDataAlloc(kAllocIncrement));
  66. if (free_area_ == NULL) {
  67. Log(kCrash, __FILE__, __LINE__,
  68. "FATAL ERROR: Out of memory trying to allocate internal "
  69. "tcmalloc data (bytes, object-size)",
  70. kAllocIncrement, sizeof(T));
  71. }
  72. free_avail_ = kAllocIncrement;
  73. }
  74. result = free_area_;
  75. free_area_ += sizeof(T);
  76. free_avail_ -= sizeof(T);
  77. }
  78. inuse_++;
  79. return reinterpret_cast<T*>(result);
  80. }
  81. void Delete(T* p) {
  82. *(reinterpret_cast<void**>(p)) = free_list_;
  83. free_list_ = p;
  84. inuse_--;
  85. }
  86. int inuse() const { return inuse_; }
  87. private:
  88. // How much to allocate from system at a time
  89. static const int kAllocIncrement = 128 << 10;
  90. // Free area from which to carve new objects
  91. char* free_area_;
  92. size_t free_avail_;
  93. // Free list of already carved objects
  94. void* free_list_;
  95. // Number of allocated but unfreed objects
  96. int inuse_;
  97. };
  98. } // namespace tcmalloc
  99. #endif // TCMALLOC_PAGE_HEAP_ALLOCATOR_H_