Fix the linking of various tests against GMock
[folly.git] / folly / ThreadCachedArena.h
1 /*
2  * Copyright 2017 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/Arena.h>
22 #include <folly/Likely.h>
23 #include <folly/Synchronized.h>
24 #include <folly/ThreadLocal.h>
25
26 namespace folly {
27
28 /**
29  * Thread-caching arena: allocate memory which gets freed when the arena gets
30  * destroyed.
31  *
32  * The arena itself allocates memory using malloc() in blocks of
33  * at least minBlockSize bytes.
34  *
35  * For speed, each thread gets its own Arena (see Arena.h); when threads
36  * exit, the Arena gets merged into a "zombie" Arena, which will be deallocated
37  * when the ThreadCachedArena object is destroyed.
38  */
39 class ThreadCachedArena {
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 */) {
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;
65   ThreadCachedArena(ThreadCachedArena&&) = delete;
66   ThreadCachedArena& operator=(const ThreadCachedArena&) = delete;
67   ThreadCachedArena& operator=(ThreadCachedArena&&) = delete;
68
69   SysArena* allocateThreadLocalArena();
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
78   ThreadLocalPtr<SysArena, ThreadLocalPtrTag> arena_;  // Per-thread arena.
79
80   // Allocations from threads that are now dead.
81   Synchronized<SysArena> zombies_;
82 };
83
84 template <>
85 struct IsArenaAllocator<ThreadCachedArena> : std::true_type { };
86
87 }  // namespace folly