Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
datacontainer.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_DATACONTAINER_H_
19 #define ION_BASE_DATACONTAINER_H_
20 
21 #include <cstring>
22 #include <functional>
23 
24 #if ION_DEBUG
25 #include <set>
26 #endif
27 
28 #include "base/integral_types.h"
29 #include "base/macros.h"
30 #include "ion/base/logging.h"
31 #include "ion/base/notifier.h"
32 #include "ion/port/nullptr.h" // For kNullFunction.
33 
34 namespace ion {
35 namespace base {
36 
41 
74 class ION_API DataContainer : public base::Notifier {
75  public:
77  typedef std::function<void(void* data_to_delete)> Deleter;
78 
81  template <typename T>
82  static void ArrayDeleter(void* data_to_delete) {
83  T* data = reinterpret_cast<T*>(data_to_delete);
84  delete [] data;
85  }
86  template <typename T>
87  static void PointerDeleter(void* data_to_delete) {
88  T* data = reinterpret_cast<T*>(data_to_delete);
89  delete data;
90  }
94  static void AllocatorDeleter(AllocatorPtr allocator, void* data_to_delete) {
95  DCHECK(allocator.Get());
96  allocator->DeallocateMemory(data_to_delete);
97  }
98 
100  bool IsWipeable() const { return is_wipeable_; }
101 
103  template <typename T>
104  const T* GetData() const {
105  return reinterpret_cast<const T*>(GetDataPtr());
106  }
107 
109  const void* GetData() const {
110  return reinterpret_cast<const void*>(GetDataPtr());
111  }
112 
114  template <typename T>
115  T* GetMutableData() const {
116  T* data = reinterpret_cast<T*>(GetDataPtr());
117  if (data)
118  this->Notify();
119  else
120  LOG(ERROR) << "GetMutableData() called on NULL (or wiped) DataContainer. "
121  "The contents of the original buffer will not be returned "
122  "and any data in GPU memory will likely be cleared. This "
123  "is probably not what you want.";
124  return data;
125  }
126 
128  template <typename T>
130  T* data, const Deleter& data_deleter, bool is_wipeable,
131  const AllocatorPtr& container_allocator) {
132  if (data_deleter && !AddOrRemoveDataFromCheck(data, true))
133  return DataContainerPtr(NULL);
134  DataContainer* container =
135  Allocate(0, data_deleter, is_wipeable, container_allocator);
136  container->data_ = data;
137  return DataContainerPtr(container);
138  }
139 
141  template <typename T>
143  const T* data, size_t count, bool is_wipeable,
144  const AllocatorPtr& container_and_data_allocator) {
145  DataContainer* container =
146  Allocate(0, kNullFunction, is_wipeable, container_and_data_allocator);
149  if (is_wipeable) {
150  container->data_allocator_ =
151  container->GetAllocator().Get()
154  } else {
155  container->data_allocator_ = container->GetAllocator();
156  }
157  container->deleter_ =
158  std::bind(DataContainer::AllocatorDeleter, container->data_allocator_,
159  std::placeholders::_1);
160  container->data_ =
161  container->data_allocator_->AllocateMemory(sizeof(T) * count);
163  if (data)
164  memcpy(container->data_, data, sizeof(T) * count);
165  return DataContainerPtr(container);
166  }
167 
169  template <typename T>
171  size_t count, const T* data, const AllocatorPtr& container_allocator) {
174  DataContainer* container = Allocate(sizeof(T) * count + 16, kNullFunction,
175  false, container_allocator);
176  uint8* ptr = reinterpret_cast<uint8*>(container) + sizeof(DataContainer);
178  container->data_ = ptr + 16 - (reinterpret_cast<size_t>(ptr) % 16);
180  if (data)
181  memcpy(container->data_, data, sizeof(T) * count);
182  return DataContainerPtr(container);
183  }
184 
188  void WipeData();
189 
190  protected:
193  ~DataContainer() override;
194 
197  DataContainer(const Deleter& deleter, bool is_wipeable);
198 
199  private:
202  static DataContainer* Allocate(size_t extra_bytes, const Deleter& deleter,
203  bool is_wipeable,
204  const AllocatorPtr& allocator);
205 
210  static bool AddOrRemoveDataFromCheck(void* data, bool is_add);
211 
213  virtual void InternalWipeData();
214 
216  virtual void* GetDataPtr() const { return data_; }
217 
219  void* data_;
222  bool is_wipeable_;
225  Deleter deleter_;
226 
228  base::AllocatorPtr data_allocator_;
229 
230  DISALLOW_IMPLICIT_CONSTRUCTORS(DataContainer);
231 };
232 
233 } // namespace base
234 } // namespace ion
235 
236 #endif // ION_BASE_DATACONTAINER_H_
kShortTerm is used for objects that are very transient in nature, such as scratch memory used to comp...
Definition: allocator.h:36
#define DCHECK(expr)
Definition: logging.h:331
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
A Notifier both sends notifications to and receives notifications from other Notifiers.
Definition: notifier.h:35
base::WeakReferentPtr< DataContainer > DataContainerWeakPtr
Definition: datacontainer.h:40
virtual const AllocatorPtr & GetAllocatorForLifetime(AllocationLifetime lifetime) const
Returns the correct Allocator to use to allocate memory with a specific lifetime. ...
Definition: allocator.cc:27
static DataContainerPtr CreateOverAllocated(size_t count, const T *data, const AllocatorPtr &container_allocator)
See class comment for documentation.
static void PointerDeleter(void *data_to_delete)
Definition: datacontainer.h:87
The DataContainer class encapsulates arbitrary user data passed to Ion.
Definition: datacontainer.h:74
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
std::function< void(void *data_to_delete)> Deleter
Generic delete function.
Definition: datacontainer.h:77
static void ArrayDeleter(void *data_to_delete)
Generic deleters that perform the most common deletion operations.
Definition: datacontainer.h:82
static void AllocatorDeleter(AllocatorPtr allocator, void *data_to_delete)
A deleter for data allocated by an Allocator.
Definition: datacontainer.h:94
const void * GetData() const
Default GetData() returns a const void pointer.
Copyright 2016 Google Inc.
void DeallocateMemory(void *p)
Deallocates a previously-allocated memory block.
Definition: allocator.cc:39
#define kNullFunction
Copyright 2016 Google Inc.
Definition: nullptr.h:38
void * AllocateMemory(size_t size)
Allocates memory of the given size.
Definition: allocator.cc:32
static DataContainerPtr CreateAndCopy(const T *data, size_t count, bool is_wipeable, const AllocatorPtr &container_and_data_allocator)
See class comment for documentation.
base::ReferentPtr< DataContainer >::Type DataContainerPtr
Definition: datacontainer.h:38
const AllocatorPtr & GetAllocator() const
Returns the Allocator that was used for the instance.
Definition: allocatable.h:68
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
static DataContainerPtr Create(T *data, const Deleter &data_deleter, bool is_wipeable, const AllocatorPtr &container_allocator)
See class comment for documentation.
T * GetMutableData() const
Returns a non-const data pointer.
const T * GetData() const
Returns a const data pointer.
A WeakReferentPtr is a weak reference to an instance of some class derived from Referent.
Definition: weakreferent.h:182
bool IsWipeable() const
Returns the is_wipeable setting passed to the constructor.
static const AllocatorPtr & GetDefaultAllocatorForLifetime(AllocationLifetime lifetime)