Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
allocatable.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_ALLOCATABLE_H_
19 #define ION_BASE_ALLOCATABLE_H_
20 
21 #include "base/macros.h"
23 #include "ion/base/allocator.h"
24 #include "ion/base/logging.h"
25 #include "ion/external/gtest/gunit_prod.h" // For FRIEND_TEST().
26 
27 namespace ion {
28 namespace base {
29 
60 class ION_API Allocatable {
61  public:
63  virtual ~Allocatable();
64 
68  const AllocatorPtr& GetAllocator() const { return allocator_; }
69 
74  }
75 
79  AllocationLifetime lifetime) const {
80  return GetAllocator()->GetAllocatorForLifetime(lifetime);
81  }
82 
84  void* operator new(size_t size) { return New(size, GetDefaultAllocator()); }
85 
88  void* operator new(size_t size, AllocationLifetime lifetime) {
89  return New(size, GetDefaultAllocatorForLifetime(lifetime));
90  }
91 
95  void* operator new(size_t size, const AllocatorPtr& allocator) {
96  return New(size, allocator);
97  }
98 
100  void* operator new(size_t size, const AllocatorPtr& allocator, void* ptr) {
101  return PlacementNew(size, allocator, ptr);
102  }
103 
105  void* operator new(size_t size, void* ptr) { return ptr; }
106 
109  void operator delete(void* ptr) { Delete(ptr); }
110 
112  void operator delete(void* ptr, AllocationLifetime lifetime) { Delete(ptr); }
113  void operator delete(void* ptr, const AllocatorPtr& allocator) {
114  Delete(ptr);
115  }
116 
118  void operator delete(void* ptr, void* ptr2) {}
119 
120  protected:
124  Allocatable();
125 
129  Allocatable(const Allocatable& other);
130 
133  void operator=(const Allocatable& other) {}
134 
140  explicit Allocatable(const AllocatorPtr& allocator);
141 
142  private:
144  template <typename T> friend class StlAllocator;
145 
147  class Helper;
148 
152  void* operator new[](size_t size);
153  void* operator new[](size_t size, AllocationLifetime lifetime);
154  void* operator new[](size_t size, const AllocatorPtr& allocator);
155  void* operator new[](size_t size, void* ptr);
156  void operator delete[](void* ptr);
157  void operator delete[](void* ptr, AllocationLifetime lifetime);
158  void operator delete[](void* ptr, const AllocatorPtr& allocator);
159  void operator delete[](void* ptr, void* ptr2);
160 
162  void Construct();
163 
165  static const AllocatorPtr& GetDefaultAllocator() {
167  }
168  static const AllocatorPtr& GetDefaultAllocatorForLifetime(
169  AllocationLifetime lifetime) {
171  }
172 
174  static void* New(size_t size, const AllocatorPtr& allocator);
175  static void Delete(void* mem_ptr);
176  static void* PlacementNew(size_t size, const AllocatorPtr& allocator,
177  void* ptr);
178 
188  static void SetPlacementAllocator(Allocator* allocator);
189 
192  static Allocator* GetPlacementAllocator();
193 
196  static Helper* GetHelper();
197 
201 
205  const void* memory_ptr_;
206 
208  FRIEND_TEST(AllocatableTest, TrivialType);
209 };
210 
211 } // namespace base
212 } // namespace ion
213 
214 #endif // ION_BASE_ALLOCATABLE_H_
StlAllocator is derived std::allocator class that allows an Ion Allocator to be used for STL containe...
Definition: stlallocator.h:35
AllocationLifetime
All memory allocated within Ion uses an Allocator chosen based on the predicted lifetime of the targe...
Definition: allocator.h:33
base::AllocatorPtr allocator_
The Allocator for the FreeTypeManager and all its Fonts.
virtual const AllocatorPtr & GetAllocatorForLifetime(AllocationLifetime lifetime) const
Returns the correct Allocator to use to allocate memory with a specific lifetime. ...
Definition: allocator.cc:27
SharedPtr< Allocator > AllocatorPtr
Definition: allocator.h:51
const AllocatorPtr & GetAllocatorForLifetime(AllocationLifetime lifetime) const
Convenience function that returns the Allocator to use to allocate an object with a specific lifetime...
Definition: allocatable.h:78
static const AllocatorPtr & GetDefaultAllocator()
Convenience function that returns the default allocator to use when no lifetime is specified...
static const AllocatorPtr & GetNonNullAllocator(const AllocatorPtr &allocator)
This convenience function can be used where a non-NULL Allocator pointer is needed.
Allocatable is an abstract base class for classes whose memory is managed by an Allocator.
Definition: allocatable.h:60
Copyright 2016 Google Inc.
const AllocatorPtr & GetNonNullAllocator() const
Return our allocator, or the default allocator if the instance was declared on the stack...
Definition: allocatable.h:72
const AllocatorPtr & GetAllocator() const
Returns the Allocator that was used for the instance.
Definition: allocatable.h:68
void operator=(const Allocatable &other)
The assignment operator does nothing, since all members are intrinsically tied to a particular alloca...
Definition: allocatable.h:133
static const AllocatorPtr & GetDefaultAllocatorForLifetime(AllocationLifetime lifetime)