OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkTemplates_DEFINED | 10 #ifndef SkTemplates_DEFINED |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 thread-compatible, and once you dereference it, you get the threadsafety | 89 thread-compatible, and once you dereference it, you get the threadsafety |
90 guarantees of T. | 90 guarantees of T. |
91 | 91 |
92 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) | 92 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) |
93 */ | 93 */ |
94 template <typename T> class SkAutoTDelete : public std::unique_ptr<T> { | 94 template <typename T> class SkAutoTDelete : public std::unique_ptr<T> { |
95 public: | 95 public: |
96 SkAutoTDelete(T* obj = NULL) : std::unique_ptr<T>(obj) {} | 96 SkAutoTDelete(T* obj = NULL) : std::unique_ptr<T>(obj) {} |
97 | 97 |
98 operator T*() const { return this->get(); } | 98 operator T*() const { return this->get(); } |
| 99 void free() { this->reset(nullptr); } |
99 | 100 |
100 // See SkAutoTUnref for why we do this. | 101 // See SkAutoTUnref for why we do this. |
101 explicit operator bool() const { return this->get() != nullptr; } | 102 explicit operator bool() const { return this->get() != nullptr; } |
102 }; | 103 }; |
103 | 104 |
104 template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> { | 105 template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> { |
105 public: | 106 public: |
106 SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {} | 107 SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {} |
| 108 |
| 109 void free() { this->reset(nullptr); } |
107 }; | 110 }; |
108 | 111 |
109 /** Allocate an array of T elements, and free the array in the destructor | 112 /** Allocate an array of T elements, and free the array in the destructor |
110 */ | 113 */ |
111 template <typename T> class SkAutoTArray : SkNoncopyable { | 114 template <typename T> class SkAutoTArray : SkNoncopyable { |
112 public: | 115 public: |
113 SkAutoTArray() { | 116 SkAutoTArray() { |
114 fArray = NULL; | 117 fArray = NULL; |
115 SkDEBUGCODE(fCount = 0;) | 118 SkDEBUGCODE(fCount = 0;) |
116 } | 119 } |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 ~SkAutoTMalloc() { | 274 ~SkAutoTMalloc() { |
272 sk_free(fPtr); | 275 sk_free(fPtr); |
273 } | 276 } |
274 | 277 |
275 /** Resize the memory area pointed to by the current ptr preserving contents
. */ | 278 /** Resize the memory area pointed to by the current ptr preserving contents
. */ |
276 void realloc(size_t count) { | 279 void realloc(size_t count) { |
277 fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T))); | 280 fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T))); |
278 } | 281 } |
279 | 282 |
280 /** Resize the memory area pointed to by the current ptr without preserving
contents. */ | 283 /** Resize the memory area pointed to by the current ptr without preserving
contents. */ |
281 T* reset(size_t count = 0) { | 284 T* reset(size_t count) { |
282 sk_free(fPtr); | 285 sk_free(fPtr); |
283 fPtr = count ? (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW) :
nullptr; | 286 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW); |
284 return fPtr; | 287 return fPtr; |
285 } | 288 } |
286 | 289 |
287 T* get() const { return fPtr; } | 290 T* get() const { return fPtr; } |
288 | 291 |
289 operator T*() { | 292 operator T*() { |
290 return fPtr; | 293 return fPtr; |
291 } | 294 } |
292 | 295 |
293 operator const T*() const { | 296 operator const T*() const { |
294 return fPtr; | 297 return fPtr; |
295 } | 298 } |
296 | 299 |
297 T& operator[](int index) { | 300 T& operator[](int index) { |
298 return fPtr[index]; | 301 return fPtr[index]; |
299 } | 302 } |
300 | 303 |
301 const T& operator[](int index) const { | 304 const T& operator[](int index) const { |
302 return fPtr[index]; | 305 return fPtr[index]; |
303 } | 306 } |
304 | 307 |
305 /** | 308 /** |
| 309 * Releases the block back to the heap |
| 310 */ |
| 311 void free() { |
| 312 this->reset(0); |
| 313 } |
| 314 |
| 315 /** |
306 * Transfer ownership of the ptr to the caller, setting the internal | 316 * Transfer ownership of the ptr to the caller, setting the internal |
307 * pointer to NULL. Note that this differs from get(), which also returns | 317 * pointer to NULL. Note that this differs from get(), which also returns |
308 * the pointer, but it does not transfer ownership. | 318 * the pointer, but it does not transfer ownership. |
309 */ | 319 */ |
310 T* release() { | 320 T* release() { |
311 T* ptr = fPtr; | 321 T* ptr = fPtr; |
312 fPtr = NULL; | 322 fPtr = NULL; |
313 return ptr; | 323 return ptr; |
314 } | 324 } |
315 | 325 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 * Returns void* because this object does not initialize the | 472 * Returns void* because this object does not initialize the |
463 * memory. Use placement new for types that require a cons. | 473 * memory. Use placement new for types that require a cons. |
464 */ | 474 */ |
465 void* get() { return fStorage.get(); } | 475 void* get() { return fStorage.get(); } |
466 const void* get() const { return fStorage.get(); } | 476 const void* get() const { return fStorage.get(); } |
467 private: | 477 private: |
468 SkAlignedSStorage<sizeof(T)*N> fStorage; | 478 SkAlignedSStorage<sizeof(T)*N> fStorage; |
469 }; | 479 }; |
470 | 480 |
471 #endif | 481 #endif |
OLD | NEW |