Arena-inl.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2012-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FOLLY_ARENA_H_
  17. #error This file may only be included from Arena.h
  18. #endif
  19. // Implementation of Arena.h functions
  20. namespace folly {
  21. template <class Alloc>
  22. std::pair<typename Arena<Alloc>::Block*, size_t>
  23. Arena<Alloc>::Block::allocate(Alloc& alloc, size_t size, bool allowSlack) {
  24. size_t allocSize = sizeof(Block) + size;
  25. if (allowSlack) {
  26. allocSize = ArenaAllocatorTraits<Alloc>::goodSize(alloc, allocSize);
  27. }
  28. void* mem = std::allocator_traits<Alloc>::allocate(alloc, allocSize);
  29. return std::make_pair(new (mem) Block(), allocSize - sizeof(Block));
  30. }
  31. template <class Alloc>
  32. void Arena<Alloc>::Block::deallocate(Alloc& alloc) {
  33. this->~Block();
  34. std::allocator_traits<Alloc>::deallocate(alloc, this, 1);
  35. }
  36. template <class Alloc>
  37. void* Arena<Alloc>::allocateSlow(size_t size) {
  38. std::pair<Block*, size_t> p;
  39. char* start;
  40. size_t allocSize = std::max(size, minBlockSize()) + sizeof(Block);
  41. if (sizeLimit_ != kNoSizeLimit &&
  42. allocSize > sizeLimit_ - totalAllocatedSize_) {
  43. throw_exception(std::bad_alloc());
  44. }
  45. if (size > minBlockSize()) {
  46. // Allocate a large block for this chunk only, put it at the back of the
  47. // list so it doesn't get used for small allocations; don't change ptr_
  48. // and end_, let them point into a normal block (or none, if they're
  49. // null)
  50. p = Block::allocate(alloc(), size, false);
  51. start = p.first->start();
  52. blocks_.push_back(*p.first);
  53. } else {
  54. // Allocate a normal sized block and carve out size bytes from it
  55. p = Block::allocate(alloc(), minBlockSize(), true);
  56. start = p.first->start();
  57. blocks_.push_front(*p.first);
  58. ptr_ = start + size;
  59. end_ = start + p.second;
  60. }
  61. assert(p.second >= size);
  62. totalAllocatedSize_ += p.second + sizeof(Block);
  63. return start;
  64. }
  65. template <class Alloc>
  66. void Arena<Alloc>::merge(Arena<Alloc>&& other) {
  67. blocks_.splice_after(blocks_.before_begin(), other.blocks_);
  68. other.blocks_.clear();
  69. other.ptr_ = other.end_ = nullptr;
  70. totalAllocatedSize_ += other.totalAllocatedSize_;
  71. other.totalAllocatedSize_ = 0;
  72. }
  73. template <class Alloc>
  74. Arena<Alloc>::~Arena() {
  75. auto disposer = [this](Block* b) { b->deallocate(this->alloc()); };
  76. while (!blocks_.empty()) {
  77. blocks_.pop_front_and_dispose(disposer);
  78. }
  79. }
  80. } // namespace folly