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> |
38 | void Arena<Alloc>::Block::deallocate(Alloc& alloc) { |
39 | this->~Block(); |
40 | std::allocator_traits<Alloc>::deallocate(alloc, this, 1); |
41 | } |
42 | |
43 | template <class Alloc> |
44 | void* Arena<Alloc>::allocateSlow(size_t size) { |
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> |
77 | void Arena<Alloc>::merge(Arena<Alloc>&& other) { |
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> |
86 | Arena<Alloc>::~Arena() { |
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 |
94 | |