Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
datacontainer.cc
Go to the documentation of this file.
1 
18 #include "ion/base/datacontainer.h"
19 
21 #include "ion/base/lockguards.h"
23 #include "ion/port/mutex.h"
24 
25 namespace ion {
26 namespace base {
27 
28 DataContainer::DataContainer(const Deleter& deleter, bool is_wipeable)
29  : data_(NULL),
30  is_wipeable_(is_wipeable),
31  deleter_(deleter) {
32 }
33 
35  AddOrRemoveDataFromCheck(data_, false);
36  InternalWipeData();
37 }
38 
40  AddOrRemoveDataFromCheck(data_, false);
41  if (is_wipeable_)
42  InternalWipeData();
43 }
44 
45 DataContainer* DataContainer::Allocate(
46  size_t extra_bytes, const Deleter& deleter, bool is_wipeable,
47  const AllocatorPtr& allocator) {
48  const AllocatorPtr& alloc = allocator.Get()
49  ? allocator
51 
52  void* ptr =
53  DataContainer::operator new(sizeof(DataContainer) + extra_bytes, alloc);
54  DataContainer* container = new(ptr) DataContainer(deleter, is_wipeable);
55  return container;
56 }
57 
58 bool DataContainer::AddOrRemoveDataFromCheck(void* data, bool is_add) {
59 #if ION_DEBUG
60  ION_DECLARE_SAFE_STATIC_POINTER(std::set<void*>, client_pointers_used);
62 
64  static port::Mutex mutex;
65  LockGuard guard(&mutex);
66 
69  if (is_add) {
70  if (client_pointers_used->count(data)) {
71  LOG(ERROR) << "Duplicate client-space pointer passed to "
72  << "DataContainer::Create(). This is very dangerous and may "
73  << "result in double-deletion! It is much safer to simply "
74  << "use the same DataContainerPtr.";
75  return false;
76  }
77  client_pointers_used->insert(data);
78  } else {
79  client_pointers_used->erase(data);
80  }
81 #endif
82  return true;
83 }
84 
85 void DataContainer::InternalWipeData() {
86  if (data_ != NULL && deleter_) {
87  deleter_(data_);
88  data_ = NULL;
89  }
90 }
91 
92 } // namespace base
93 } // namespace ion
#define ION_DECLARE_SAFE_STATIC_POINTER(type, variable)
Declare a static non-array pointer and calls a default constructor.
void WipeData()
Informs the DataContainer that the data is no longer needed and can be deleted.
GenericLockGuard< port::Mutex > LockGuard
Convenient typedefs for ion::port::Mutex.
Definition: lockguards.h:192
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
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
~DataContainer() override
The destructor is protected because all base::Referent classes must have protected or private destruc...
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
kMediumTerm is used for objects that don't fall into the kShortTerm or kLongTerm categories.
Definition: allocator.h:40
DataContainer(const Deleter &deleter, bool is_wipeable)
The constructor is protected because all allocation of DataContainers should be through the Create() ...
A Mutex is used to ensure that only one thread or process can access a block of code at one time...
Definition: mutex.h:34
static const AllocatorPtr & GetDefaultAllocatorForLifetime(AllocationLifetime lifetime)