| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef PPAPI_CPP_DEV_ARRAY_DEV_H_ |
| 6 #define PPAPI_CPP_DEV_ARRAY_DEV_H_ |
| 7 |
| 8 #include <cstdlib> |
| 9 |
| 10 #include <vector> |
| 11 |
| 12 #include "ppapi/cpp/dev/may_own_ptr_dev.h" |
| 13 #include "ppapi/cpp/logging.h" |
| 14 |
| 15 namespace pp { |
| 16 namespace internal { |
| 17 |
| 18 class ArrayAllocator { |
| 19 public: |
| 20 // Allocates memory and zero-fills it. |
| 21 static void* Alloc(uint32_t count, uint32_t size) { |
| 22 if (count == 0 || size == 0) |
| 23 return NULL; |
| 24 |
| 25 return calloc(count, size); |
| 26 } |
| 27 |
| 28 static void Free(void* mem) { free(mem); } |
| 29 |
| 30 static PP_ArrayOutput Get() { |
| 31 PP_ArrayOutput array_output = { &ArrayAllocator::GetDataBuffer, NULL }; |
| 32 return array_output; |
| 33 } |
| 34 |
| 35 private: |
| 36 static void* GetDataBuffer(void* /* user_data */, |
| 37 uint32_t element_count, |
| 38 uint32_t element_size) { |
| 39 return Alloc(element_count, element_size); |
| 40 } |
| 41 }; |
| 42 |
| 43 } // namespace internal |
| 44 |
| 45 // Generic array for struct wrappers. |
| 46 template <class T> |
| 47 class Array { |
| 48 public: |
| 49 typedef typename T::CArrayType CArrayType; |
| 50 typedef typename T::CType CType; |
| 51 typedef CArrayType* COutputType; |
| 52 |
| 53 Array() {} |
| 54 |
| 55 explicit Array(uint32_t size) { Reset(size); } |
| 56 |
| 57 // It doesn't take ownership of |storage|, therefore |storage| must live |
| 58 // longer than this object. |storage| must be zero-initialized. |
| 59 Array(CArrayType* storage, NotOwned) : storage_(storage, NOT_OWNED) { |
| 60 PP_DCHECK(storage_->size == 0); |
| 61 PP_DCHECK(storage_->elements == NULL); |
| 62 } |
| 63 |
| 64 Array(const Array<T>& other) { DeepCopy(*other.storage_); } |
| 65 |
| 66 ~Array() { Reset(0); } |
| 67 |
| 68 Array<T>& operator=(const Array<T>& other) { |
| 69 return operator=(*other.storage_); |
| 70 } |
| 71 |
| 72 Array<T>& operator=(const CArrayType& other) { |
| 73 if (storage_.get() == &other) |
| 74 return *this; |
| 75 |
| 76 Reset(0); |
| 77 DeepCopy(other); |
| 78 |
| 79 return *this; |
| 80 } |
| 81 |
| 82 uint32_t size() const { return storage_->size; } |
| 83 |
| 84 T& operator[](uint32_t index) { |
| 85 PP_DCHECK(storage_->size == wrappers_.size()); |
| 86 PP_DCHECK(index < storage_->size); |
| 87 PP_DCHECK(wrappers_[index]->ToStruct() == storage_->elements + index); |
| 88 return *wrappers_[index]; |
| 89 } |
| 90 |
| 91 const T& operator[](uint32_t index) const { |
| 92 PP_DCHECK(storage_->size == wrappers_.size()); |
| 93 PP_DCHECK(index < storage_->size); |
| 94 PP_DCHECK(wrappers_[index]->ToStruct() == storage_->elements + index); |
| 95 return *wrappers_[index]; |
| 96 } |
| 97 |
| 98 void Reset(uint32_t size) { |
| 99 // Wrappers must be destroyed before we free |storage_->elements|, because |
| 100 // they may try to access the memory in their destructors. |
| 101 DeleteWrappers(); |
| 102 |
| 103 internal::ArrayAllocator::Free(storage_->elements); |
| 104 storage_->elements = NULL; |
| 105 |
| 106 storage_->size = size; |
| 107 if (size > 0) { |
| 108 storage_->elements = static_cast<CType*>( |
| 109 internal::ArrayAllocator::Alloc(size, sizeof(CType))); |
| 110 } |
| 111 |
| 112 CreateWrappers(); |
| 113 } |
| 114 |
| 115 CArrayType* StartRawUpdate() { |
| 116 Reset(0); |
| 117 return storage_.get(); |
| 118 } |
| 119 |
| 120 void EndRawUpdate() { CreateWrappers(); } |
| 121 |
| 122 private: |
| 123 void DeepCopy(const CArrayType& other) { |
| 124 storage_->size = other.size; |
| 125 |
| 126 if (storage_->size > 0) { |
| 127 storage_->elements = static_cast<CType*>( |
| 128 internal::ArrayAllocator::Alloc(storage_->size, sizeof(CType))); |
| 129 } |
| 130 |
| 131 CreateWrappers(); |
| 132 |
| 133 for (size_t i = 0; i < storage_->size; ++i) |
| 134 wrappers_[i] = other.elements[i]; |
| 135 } |
| 136 |
| 137 void DeleteWrappers() { |
| 138 for (typename std::vector<T*>::iterator iter = wrappers_.begin(); |
| 139 iter != wrappers_.end(); |
| 140 ++iter) { |
| 141 delete *iter; |
| 142 } |
| 143 wrappers_.clear(); |
| 144 } |
| 145 |
| 146 void CreateWrappers() { |
| 147 PP_DCHECK(wrappers_.empty()); |
| 148 |
| 149 uint32_t size = storage_->size; |
| 150 if (size == 0) |
| 151 return; |
| 152 |
| 153 wrappers_.reserve(size); |
| 154 for (size_t i = 0; i < size; ++i) |
| 155 wrappers_.push_back(new T(&storage_->elements[i], NOT_OWNED)); |
| 156 } |
| 157 |
| 158 internal::MayOwnPtr<CArrayType> storage_; |
| 159 std::vector<T*> wrappers_; |
| 160 }; |
| 161 |
| 162 } // namespace pp |
| 163 |
| 164 #endif // PPAPI_CPP_DEV_ARRAY_DEV_H_ |
| OLD | NEW |