1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
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 FRUIT_ARENA_ALLOCATOR_H
18 #define FRUIT_ARENA_ALLOCATOR_H
19 
20 #include <fruit/impl/data_structures/memory_pool.h>
21 
22 namespace fruit {
23 namespace impl {
24 
25 /**
26  * An allocator that allocates memory in pages and only de-allocates memory on destruction.
27  * This is useful in code that needs many short-lived allocations.
28  * Each ArenaAllocator object should only be accessed by a single thread.
29  */
30 template <typename T>
31 class ArenaAllocator {
32 private:
33   MemoryPool* pool;
34 
35   template <class U>
36   friend class ArenaAllocator;
37 
38   template <class U, class V>
39   friend bool operator==(const ArenaAllocator<U>& x, const ArenaAllocator<V>& y);
40 
41   template <class U, class V>
42   friend bool operator!=(const ArenaAllocator<U>& x, const ArenaAllocator<V>& y);
43 
44 public:
45   using value_type = T;
46 
47   template <typename U>
48   struct rebind {
49     using other = ArenaAllocator<U>;
50   };
51 
52   /**
53    * Constructs an arena allocator using the specified memory pool.
54    * The MemoryPool object must outlive all allocators constructed with it and all allocated objects.
55    */
56   explicit ArenaAllocator(MemoryPool& memory_pool);
57 
58   template <typename U>
59   ArenaAllocator(const ArenaAllocator<U>&); // NOLINT(google-explicit-constructor)
60 
61   T* allocate(std::size_t n);
62   void deallocate(T* p, std::size_t);
63 };
64 
65 template <class T, class U>
66 bool operator==(const ArenaAllocator<T>&, const ArenaAllocator<U>&);
67 
68 template <class T, class U>
69 bool operator!=(const ArenaAllocator<T>&, const ArenaAllocator<U>&);
70 
71 } // namespace impl
72 } // namespace fruit
73 
74 #include <fruit/impl/data_structures/arena_allocator.defn.h>
75 
76 #endif // FRUIT_ARENA_ALLOCATOR_H
77