Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
scopedallocation.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_SCOPEDALLOCATION_H_
19 #define ION_BASE_SCOPEDALLOCATION_H_
20 
21 #include <functional>
22 
23 #include "base/macros.h"
25 #include "ion/base/allocator.h"
26 #include "ion/base/datacontainer.h"
27 
28 namespace ion {
29 namespace base {
30 
57 template <typename T> class ION_API ScopedAllocation {
58  public:
61  explicit ScopedAllocation(const AllocatorPtr& allocator) {
62  Init(allocator, 1U);
63  }
64 
67  explicit ScopedAllocation(AllocationLifetime lifetime) {
69  }
70 
73  ScopedAllocation(const AllocatorPtr& allocator, size_t count) {
74  Init(allocator, count);
75  }
76 
79  ScopedAllocation(AllocationLifetime lifetime, size_t count) {
81  }
82 
85  DeleteData(instance_ptr_, count_, allocator_, memory_ptr_);
86  }
87 
90  T* Get() const { return instance_ptr_; }
91 
96  return DataContainer::Create<T>(
97  Release(),
98  std::bind(&ScopedAllocation<T>::Deleter, std::placeholders::_1, count_,
99  allocator_, memory_ptr_),
100  is_wipeable, allocator_);
101  }
102 
103  private:
105  void Init(const AllocatorPtr& allocator, size_t count) {
107  count_ = count;
108  if (count_) {
111  memory_ptr_ =
112  allocator_->AllocateMemory(sizeof(size_t) + count_ * sizeof(T));
113  instance_ptr_ = new(memory_ptr_) T[count_];
114  } else {
115  memory_ptr_ = NULL;
116  instance_ptr_ = NULL;
117  }
118  }
119 
124  T* Release() {
125  T* ptr = instance_ptr_;
126  instance_ptr_ = NULL;
128  return ptr;
129  }
130 
132  static void Deleter(void* data, size_t count, AllocatorPtr allocator,
133  void* memory_ptr) {
134  DeleteData(reinterpret_cast<T*>(data), count, allocator, memory_ptr);
135  }
136 
139  static void DeleteData(T* data, size_t count, const AllocatorPtr& allocator,
140  void* memory_ptr) {
141  if (data) {
143  for (size_t i = 0; i < count; ++i)
144  data[i].~T();
146  if (allocator.Get())
147  allocator->DeallocateMemory(memory_ptr);
148  }
149  }
150 
154  void* memory_ptr_;
157  T* instance_ptr_;
159  size_t count_;
160 
161  DISALLOW_COPY_AND_ASSIGN(ScopedAllocation);
162 };
163 
164 } // namespace base
165 } // namespace ion
166 
167 #endif // ION_BASE_SCOPEDALLOCATION_H_
DataContainerPtr TransferToDataContainer(bool is_wipeable)
Creates a DataContainer of the same type and transfers the data from the ScopedAllocation to it...
T * Get() const
Returns a pointer to the allocated T instance(s).
AllocationLifetime
All memory allocated within Ion uses an Allocator chosen based on the predicted lifetime of the targe...
Definition: allocator.h:33
ScopedAllocation(AllocationLifetime lifetime)
This constructor allocates a single T instance using the default allocator for lifetime.
base::AllocatorPtr allocator_
The Allocator for the FreeTypeManager and all its Fonts.
ScopedAllocation(AllocationLifetime lifetime, size_t count)
This constructor allocates count T instances the default allocator for lifetime.
SharedPtr< Allocator > AllocatorPtr
Definition: allocator.h:51
static const AllocatorPtr & GetNonNullAllocator(const AllocatorPtr &allocator)
This convenience function can be used where a non-NULL Allocator pointer is needed.
~ScopedAllocation()
The destructor deletes the instance(s) using the allocator.
ScopedAllocation(const AllocatorPtr &allocator)
This constructor allocates a single T instance using allocator, or the default allocator if a NULL po...
This template class can be used in situations where you want to allocate an object that is not necess...
ScopedAllocation(const AllocatorPtr &allocator, size_t count)
This constructor allocates count T instances using allocator, or the default allocator if a NULL poin...
static const AllocatorPtr & GetDefaultAllocatorForLifetime(AllocationLifetime lifetime)