proxygen
ThreadCachedArena.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 #pragma once
18 
19 #include <type_traits>
20 
21 #include <folly/Likely.h>
22 #include <folly/Synchronized.h>
23 #include <folly/ThreadLocal.h>
24 #include <folly/memory/Arena.h>
25 
26 namespace folly {
27 
40  public:
41  explicit ThreadCachedArena(
42  size_t minBlockSize = SysArena::kDefaultMinBlockSize,
43  size_t maxAlign = SysArena::kDefaultMaxAlign);
44 
45  void* allocate(size_t size) {
46  SysArena* arena = arena_.get();
47  if (UNLIKELY(!arena)) {
48  arena = allocateThreadLocalArena();
49  }
50 
51  return arena->allocate(size);
52  }
53 
54  void deallocate(void* /* p */, size_t = 0) {
55  // Deallocate? Never!
56  }
57 
58  // Gets the total memory used by the arena
59  size_t totalSize() const;
60 
61  private:
62  struct ThreadLocalPtrTag {};
63 
64  ThreadCachedArena(const ThreadCachedArena&) = delete;
68 
70 
71  // Zombify the blocks in arena, saving them for deallocation until
72  // the ThreadCachedArena is destroyed.
73  void zombify(SysArena&& arena);
74 
75  const size_t minBlockSize_;
76  const size_t maxAlign_;
77 
79 
80  // Allocations from threads that are now dead.
82 };
83 
84 template <>
86 
87 template <typename T>
89 
90 } // namespace folly
void * allocate(size_t size)
ThreadCachedArena(size_t minBlockSize=SysArena::kDefaultMinBlockSize, size_t maxAlign=SysArena::kDefaultMaxAlign)
void zombify(SysArena &&arena)
ThreadCachedArena & operator=(const ThreadCachedArena &)=delete
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
bool_constant< true > true_type
Definition: gtest-port.h:2210
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
void deallocate(void *, size_t=0)
static constexpr size_t kDefaultMinBlockSize
Definition: Arena.h:153
static constexpr size_t kDefaultMaxAlign
Definition: Arena.h:155
#define UNLIKELY(x)
Definition: Likely.h:48
void * allocate(size_t size)
Definition: Arena.h:82
ThreadLocalPtr< SysArena, ThreadLocalPtrTag > arena_
Synchronized< SysArena > zombies_