proxygen
Arena-inl.h
Go to the documentation of this file.
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 
17 #ifndef FOLLY_ARENA_H_
18 #error This file may only be included from Arena.h
19 #endif
20 
21 // Implementation of Arena.h functions
22 
23 namespace folly {
24 
25 template <class Alloc>
26 std::pair<typename Arena<Alloc>::Block*, size_t>
27 Arena<Alloc>::Block::allocate(Alloc& alloc, size_t size, bool allowSlack) {
28  size_t allocSize = sizeof(Block) + size;
29  if (allowSlack) {
30  allocSize = ArenaAllocatorTraits<Alloc>::goodSize(alloc, allocSize);
31  }
32 
33  void* mem = std::allocator_traits<Alloc>::allocate(alloc, allocSize);
34  return std::make_pair(new (mem) Block(), allocSize - sizeof(Block));
35 }
36 
37 template <class Alloc>
39  this->~Block();
40  std::allocator_traits<Alloc>::deallocate(alloc, this, 1);
41 }
42 
43 template <class Alloc>
45  std::pair<Block*, size_t> p;
46  char* start;
47 
48  size_t allocSize = std::max(size, minBlockSize()) + sizeof(Block);
49  if (sizeLimit_ != kNoSizeLimit &&
50  allocSize > sizeLimit_ - totalAllocatedSize_) {
51  throw_exception(std::bad_alloc());
52  }
53 
54  if (size > minBlockSize()) {
55  // Allocate a large block for this chunk only, put it at the back of the
56  // list so it doesn't get used for small allocations; don't change ptr_
57  // and end_, let them point into a normal block (or none, if they're
58  // null)
59  p = Block::allocate(alloc(), size, false);
60  start = p.first->start();
61  blocks_.push_back(*p.first);
62  } else {
63  // Allocate a normal sized block and carve out size bytes from it
64  p = Block::allocate(alloc(), minBlockSize(), true);
65  start = p.first->start();
66  blocks_.push_front(*p.first);
67  ptr_ = start + size;
68  end_ = start + p.second;
69  }
70 
71  assert(p.second >= size);
72  totalAllocatedSize_ += p.second + sizeof(Block);
73  return start;
74 }
75 
76 template <class Alloc>
78  blocks_.splice_after(blocks_.before_begin(), other.blocks_);
79  other.blocks_.clear();
80  other.ptr_ = other.end_ = nullptr;
81  totalAllocatedSize_ += other.totalAllocatedSize_;
82  other.totalAllocatedSize_ = 0;
83 }
84 
85 template <class Alloc>
87  auto disposer = [this](Block* b) { b->deallocate(this->alloc()); };
88  while (!blocks_.empty()) {
89  blocks_.pop_front_and_dispose(disposer);
90  }
91 }
92 
93 } // namespace folly
const size_t sizeLimit_
Definition: Arena.h:207
static constexpr size_t kNoSizeLimit
Definition: Arena.h:154
char b
LogLevel max
Definition: LogLevel.cpp:31
static std::pair< Block *, size_t > allocate(Alloc &alloc, size_t size, bool allowSlack)
Definition: Arena-inl.h:27
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
char * ptr_
Definition: Arena.h:203
static size_t goodSize(const Alloc &, size_t size)
Definition: Arena.h:219
void deallocate(Alloc &alloc)
Definition: Arena-inl.h:38
void merge(Arena &&other)
Definition: Arena-inl.h:77
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
size_t totalAllocatedSize_
Definition: Arena.h:205
void * allocateSlow(size_t size)
Definition: Arena-inl.h:44
char * start()
Definition: Arena.h:143
char * end_
Definition: Arena.h:204
BlockList blocks_
Definition: Arena.h:202
size_t minBlockSize() const
Definition: Arena.h:191
FOLLY_NOINLINE FOLLY_COLD void throw_exception(Ex &&ex)
Definition: Exception.h:32
Alloc & alloc()
Definition: Arena.h:194