epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkTemplates_DEFINED |
| 11 | #define SkTemplates_DEFINED |
| 12 | |
sugoi | 23d4320 | 2015-01-07 13:28:08 -0800 | [diff] [blame] | 13 | #include "SkMath.h" |
Herb Derby | b549cc3 | 2017-03-27 13:35:15 -0400 | [diff] [blame] | 14 | #include "SkMalloc.h" |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 15 | #include "SkTLogic.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkTypes.h" |
bungeman@google.com | 562b2e6 | 2014-03-12 21:41:06 +0000 | [diff] [blame] | 17 | #include <limits.h> |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 18 | #include <memory> |
bungeman@google.com | 7103f18 | 2012-10-31 20:53:49 +0000 | [diff] [blame] | 19 | #include <new> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | |
| 21 | /** \file SkTemplates.h |
| 22 | |
| 23 | This file contains light-weight template classes for type-safe and exception-safe |
| 24 | resource management. |
| 25 | */ |
| 26 | |
bungeman@google.com | 9120892 | 2012-07-30 15:03:59 +0000 | [diff] [blame] | 27 | /** |
bungeman@google.com | 7de18e5 | 2013-02-04 15:58:08 +0000 | [diff] [blame] | 28 | * Marks a local variable as known to be unused (to avoid warnings). |
| 29 | * Note that this does *not* prevent the local variable from being optimized away. |
| 30 | */ |
| 31 | template<typename T> inline void sk_ignore_unused_variable(const T&) { } |
| 32 | |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 33 | /** |
| 34 | * Returns a pointer to a D which comes immediately after S[count]. |
| 35 | */ |
| 36 | template <typename D, typename S> static D* SkTAfter(S* ptr, size_t count = 1) { |
| 37 | return reinterpret_cast<D*>(ptr + count); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns a pointer to a D which comes byteOffset bytes after S. |
| 42 | */ |
| 43 | template <typename D, typename S> static D* SkTAddOffset(S* ptr, size_t byteOffset) { |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 44 | // The intermediate char* has the same cv-ness as D as this produces better error messages. |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 45 | // This relies on the fact that reinterpret_cast can add constness, but cannot remove it. |
bungeman | 761cf61 | 2015-08-28 07:09:20 -0700 | [diff] [blame] | 46 | return reinterpret_cast<D*>(reinterpret_cast<sknonstd::same_cv_t<char, D>*>(ptr) + byteOffset); |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 47 | } |
| 48 | |
bungeman | a3434d8 | 2015-09-07 12:45:52 -0700 | [diff] [blame] | 49 | template <typename R, typename T, R (*P)(T*)> struct SkFunctionWrapper { |
| 50 | R operator()(T* t) { return P(t); } |
| 51 | }; |
| 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | /** \class SkAutoTCallVProc |
| 54 | |
| 55 | Call a function when this goes out of scope. The template uses two |
| 56 | parameters, the object, and a function that is to be called in the destructor. |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 57 | If release() is called, the object reference is set to null. If the object |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | reference is null when the destructor is called, we do not call the |
| 59 | function. |
| 60 | */ |
mtklein | 1138be4 | 2016-01-24 19:49:24 -0800 | [diff] [blame] | 61 | template <typename T, void (*P)(T*)> class SkAutoTCallVProc |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 62 | : public std::unique_ptr<T, SkFunctionWrapper<void, T, P>> { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | public: |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 64 | SkAutoTCallVProc(T* obj): std::unique_ptr<T, SkFunctionWrapper<void, T, P>>(obj) {} |
bungeman | a6785cc | 2014-08-25 12:00:49 -0700 | [diff] [blame] | 65 | |
mtklein | 1138be4 | 2016-01-24 19:49:24 -0800 | [diff] [blame] | 66 | operator T*() const { return this->get(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /** \class SkAutoTCallIProc |
| 70 | |
| 71 | Call a function when this goes out of scope. The template uses two |
| 72 | parameters, the object, and a function that is to be called in the destructor. |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 73 | If release() is called, the object reference is set to null. If the object |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | reference is null when the destructor is called, we do not call the |
| 75 | function. |
| 76 | */ |
mtklein | 1138be4 | 2016-01-24 19:49:24 -0800 | [diff] [blame] | 77 | template <typename T, int (*P)(T*)> class SkAutoTCallIProc |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 78 | : public std::unique_ptr<T, SkFunctionWrapper<int, T, P>> { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | public: |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 80 | SkAutoTCallIProc(T* obj): std::unique_ptr<T, SkFunctionWrapper<int, T, P>>(obj) {} |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 81 | |
mtklein | 1138be4 | 2016-01-24 19:49:24 -0800 | [diff] [blame] | 82 | operator T*() const { return this->get(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | /** Allocate an array of T elements, and free the array in the destructor |
| 86 | */ |
| 87 | template <typename T> class SkAutoTArray : SkNoncopyable { |
| 88 | public: |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 89 | SkAutoTArray() { |
| 90 | fArray = NULL; |
| 91 | SkDEBUGCODE(fCount = 0;) |
| 92 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | /** Allocate count number of T elements |
| 94 | */ |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 95 | explicit SkAutoTArray(int count) { |
| 96 | SkASSERT(count >= 0); |
| 97 | fArray = NULL; |
| 98 | if (count) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 99 | fArray = new T[count]; |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 100 | } |
| 101 | SkDEBUGCODE(fCount = count;) |
| 102 | } |
| 103 | |
| 104 | /** Reallocates given a new count. Reallocation occurs even if new count equals old count. |
| 105 | */ |
| 106 | void reset(int count) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 107 | delete[] fArray; |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 108 | SkASSERT(count >= 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | fArray = NULL; |
| 110 | if (count) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 111 | fArray = new T[count]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | } |
| 113 | SkDEBUGCODE(fCount = count;) |
| 114 | } |
| 115 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 116 | ~SkAutoTArray() { delete[] fArray; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | |
| 118 | /** Return the array of T elements. Will be NULL if count == 0 |
| 119 | */ |
| 120 | T* get() const { return fArray; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 121 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | /** Return the nth element in the array |
| 123 | */ |
| 124 | T& operator[](int index) const { |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 125 | SkASSERT((unsigned)index < (unsigned)fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | return fArray[index]; |
| 127 | } |
| 128 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 129 | void swap(SkAutoTArray& other) { |
| 130 | SkTSwap(fArray, other.fArray); |
| 131 | SkDEBUGCODE(SkTSwap(fCount, other.fCount)); |
| 132 | } |
| 133 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | private: |
| 135 | T* fArray; |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 136 | SkDEBUGCODE(int fCount;) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 139 | /** Wraps SkAutoTArray, with room for kCountRequested elements preallocated. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | */ |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 141 | template <int kCountRequested, typename T> class SkAutoSTArray : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | public: |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 143 | /** Initialize with no objects */ |
| 144 | SkAutoSTArray() { |
| 145 | fArray = NULL; |
| 146 | fCount = 0; |
| 147 | } |
| 148 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | /** Allocate count number of T elements |
| 150 | */ |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 151 | SkAutoSTArray(int count) { |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 152 | fArray = NULL; |
| 153 | fCount = 0; |
| 154 | this->reset(count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 156 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | ~SkAutoSTArray() { |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 158 | this->reset(0); |
| 159 | } |
| 160 | |
| 161 | /** Destroys previous objects in the array and default constructs count number of objects */ |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 162 | void reset(int count) { |
scroggo@google.com | 1198e74 | 2013-05-29 20:10:25 +0000 | [diff] [blame] | 163 | T* start = fArray; |
| 164 | T* iter = start + fCount; |
| 165 | while (iter > start) { |
| 166 | (--iter)->~T(); |
| 167 | } |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 168 | |
csmartdalton | d0e402f | 2016-06-23 12:55:14 -0700 | [diff] [blame] | 169 | SkASSERT(count >= 0); |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 170 | if (fCount != count) { |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 171 | if (fCount > kCount) { |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 172 | // 'fArray' was allocated last time so free it now |
| 173 | SkASSERT((T*) fStorage != fArray); |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 174 | sk_free(fArray); |
| 175 | } |
| 176 | |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 177 | if (count > kCount) { |
sugoi | 23d4320 | 2015-01-07 13:28:08 -0800 | [diff] [blame] | 178 | const uint64_t size64 = sk_64_mul(count, sizeof(T)); |
| 179 | const size_t size = static_cast<size_t>(size64); |
| 180 | if (size != size64) { |
| 181 | sk_out_of_memory(); |
| 182 | } |
| 183 | fArray = (T*) sk_malloc_throw(size); |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 184 | } else if (count > 0) { |
| 185 | fArray = (T*) fStorage; |
| 186 | } else { |
| 187 | fArray = NULL; |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | fCount = count; |
| 191 | } |
| 192 | |
| 193 | iter = fArray; |
| 194 | T* stop = fArray + count; |
| 195 | while (iter < stop) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 196 | new (iter++) T; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 199 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | /** Return the number of T elements in the array |
| 201 | */ |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 202 | int count() const { return fCount; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 203 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | /** Return the array of T elements. Will be NULL if count == 0 |
| 205 | */ |
| 206 | T* get() const { return fArray; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 207 | |
Brian Salomon | 92ce594 | 2017-01-18 11:01:10 -0500 | [diff] [blame] | 208 | T* begin() { return fArray; } |
| 209 | |
| 210 | const T* begin() const { return fArray; } |
| 211 | |
| 212 | T* end() { return fArray + fCount; } |
| 213 | |
| 214 | const T* end() const { return fArray + fCount; } |
| 215 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | /** Return the nth element in the array |
| 217 | */ |
| 218 | T& operator[](int index) const { |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 219 | SkASSERT(index < fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | return fArray[index]; |
| 221 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 222 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 223 | private: |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 224 | #if defined(GOOGLE3) |
| 225 | // Stack frame size is limited for GOOGLE3. 4k is less than the actual max, but some functions |
| 226 | // have multiple large stack allocations. |
| 227 | static const int kMaxBytes = 4 * 1024; |
| 228 | static const int kCount = kCountRequested * sizeof(T) > kMaxBytes |
| 229 | ? kMaxBytes / sizeof(T) |
| 230 | : kCountRequested; |
| 231 | #else |
| 232 | static const int kCount = kCountRequested; |
| 233 | #endif |
| 234 | |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 235 | int fCount; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | T* fArray; |
| 237 | // since we come right after fArray, fStorage should be properly aligned |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 238 | char fStorage[kCount * sizeof(T)]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 241 | /** Manages an array of T elements, freeing the array in the destructor. |
| 242 | * Does NOT call any constructors/destructors on T (T must be POD). |
| 243 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | template <typename T> class SkAutoTMalloc : SkNoncopyable { |
| 245 | public: |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 246 | /** Takes ownership of the ptr. The ptr must be a value which can be passed to sk_free. */ |
| 247 | explicit SkAutoTMalloc(T* ptr = NULL) { |
| 248 | fPtr = ptr; |
| 249 | } |
| 250 | |
| 251 | /** Allocates space for 'count' Ts. */ |
| 252 | explicit SkAutoTMalloc(size_t count) { |
csmartdalton | d0e402f | 2016-06-23 12:55:14 -0700 | [diff] [blame] | 253 | fPtr = count ? (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW) : nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | } |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 255 | |
csmartdalton | 320ade4 | 2017-02-13 11:54:37 -0700 | [diff] [blame] | 256 | SkAutoTMalloc(SkAutoTMalloc<T>&& that) : fPtr(that.release()) {} |
| 257 | |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 258 | ~SkAutoTMalloc() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | sk_free(fPtr); |
| 260 | } |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 261 | |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 262 | /** Resize the memory area pointed to by the current ptr preserving contents. */ |
| 263 | void realloc(size_t count) { |
csmartdalton | d0e402f | 2016-06-23 12:55:14 -0700 | [diff] [blame] | 264 | if (count) { |
| 265 | fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T))); |
| 266 | } else { |
| 267 | this->reset(0); |
| 268 | } |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** Resize the memory area pointed to by the current ptr without preserving contents. */ |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 272 | T* reset(size_t count = 0) { |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 273 | sk_free(fPtr); |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 274 | fPtr = count ? (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW) : nullptr; |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 275 | return fPtr; |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 276 | } |
| 277 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 278 | T* get() const { return fPtr; } |
| 279 | |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 280 | operator T*() { |
| 281 | return fPtr; |
| 282 | } |
| 283 | |
| 284 | operator const T*() const { |
| 285 | return fPtr; |
| 286 | } |
| 287 | |
| 288 | T& operator[](int index) { |
| 289 | return fPtr[index]; |
| 290 | } |
| 291 | |
| 292 | const T& operator[](int index) const { |
| 293 | return fPtr[index]; |
| 294 | } |
| 295 | |
csmartdalton | 320ade4 | 2017-02-13 11:54:37 -0700 | [diff] [blame] | 296 | SkAutoTMalloc& operator=(SkAutoTMalloc<T>&& that) { |
Mike Klein | 149e42e | 2017-04-07 14:18:29 -0400 | [diff] [blame] | 297 | if (this != &that) { |
| 298 | sk_free(fPtr); |
| 299 | fPtr = that.release(); |
| 300 | } |
csmartdalton | 320ade4 | 2017-02-13 11:54:37 -0700 | [diff] [blame] | 301 | return *this; |
| 302 | } |
| 303 | |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 304 | /** |
| 305 | * Transfer ownership of the ptr to the caller, setting the internal |
| 306 | * pointer to NULL. Note that this differs from get(), which also returns |
| 307 | * the pointer, but it does not transfer ownership. |
| 308 | */ |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 309 | T* release() { |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 310 | T* ptr = fPtr; |
| 311 | fPtr = NULL; |
| 312 | return ptr; |
| 313 | } |
| 314 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 315 | private: |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 316 | T* fPtr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 319 | template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 320 | public: |
joshualitt | f1f8895 | 2015-04-08 07:33:33 -0700 | [diff] [blame] | 321 | SkAutoSTMalloc() : fPtr(fTStorage) {} |
bungeman@google.com | 7103344 | 2013-05-01 14:21:20 +0000 | [diff] [blame] | 322 | |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 323 | SkAutoSTMalloc(size_t count) { |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 324 | if (count > kCount) { |
bungeman@google.com | 7103344 | 2013-05-01 14:21:20 +0000 | [diff] [blame] | 325 | fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP); |
csmartdalton | d0e402f | 2016-06-23 12:55:14 -0700 | [diff] [blame] | 326 | } else if (count) { |
joshualitt | f1f8895 | 2015-04-08 07:33:33 -0700 | [diff] [blame] | 327 | fPtr = fTStorage; |
csmartdalton | d0e402f | 2016-06-23 12:55:14 -0700 | [diff] [blame] | 328 | } else { |
| 329 | fPtr = nullptr; |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 330 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 331 | } |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 332 | |
| 333 | ~SkAutoSTMalloc() { |
| 334 | if (fPtr != fTStorage) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 335 | sk_free(fPtr); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 336 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 337 | } |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 338 | |
| 339 | // doesn't preserve contents |
reed@google.com | 4e05fd2 | 2013-06-10 18:58:11 +0000 | [diff] [blame] | 340 | T* reset(size_t count) { |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 341 | if (fPtr != fTStorage) { |
| 342 | sk_free(fPtr); |
| 343 | } |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 344 | if (count > kCount) { |
joshualitt | f1f8895 | 2015-04-08 07:33:33 -0700 | [diff] [blame] | 345 | fPtr = (T*)sk_malloc_throw(count * sizeof(T)); |
csmartdalton | d0e402f | 2016-06-23 12:55:14 -0700 | [diff] [blame] | 346 | } else if (count) { |
joshualitt | f1f8895 | 2015-04-08 07:33:33 -0700 | [diff] [blame] | 347 | fPtr = fTStorage; |
csmartdalton | d0e402f | 2016-06-23 12:55:14 -0700 | [diff] [blame] | 348 | } else { |
| 349 | fPtr = nullptr; |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 350 | } |
reed@google.com | 4e05fd2 | 2013-06-10 18:58:11 +0000 | [diff] [blame] | 351 | return fPtr; |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 352 | } |
| 353 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 354 | T* get() const { return fPtr; } |
| 355 | |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 356 | operator T*() { |
| 357 | return fPtr; |
| 358 | } |
| 359 | |
| 360 | operator const T*() const { |
| 361 | return fPtr; |
| 362 | } |
| 363 | |
| 364 | T& operator[](int index) { |
| 365 | return fPtr[index]; |
| 366 | } |
| 367 | |
| 368 | const T& operator[](int index) const { |
| 369 | return fPtr[index]; |
| 370 | } |
| 371 | |
joshualitt | f1f8895 | 2015-04-08 07:33:33 -0700 | [diff] [blame] | 372 | // Reallocs the array, can be used to shrink the allocation. Makes no attempt to be intelligent |
| 373 | void realloc(size_t count) { |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 374 | if (count > kCount) { |
joshualitt | f1f8895 | 2015-04-08 07:33:33 -0700 | [diff] [blame] | 375 | if (fPtr == fTStorage) { |
| 376 | fPtr = (T*)sk_malloc_throw(count * sizeof(T)); |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 377 | memcpy(fPtr, fTStorage, kCount * sizeof(T)); |
joshualitt | f1f8895 | 2015-04-08 07:33:33 -0700 | [diff] [blame] | 378 | } else { |
| 379 | fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); |
| 380 | } |
csmartdalton | d0e402f | 2016-06-23 12:55:14 -0700 | [diff] [blame] | 381 | } else if (count) { |
| 382 | if (fPtr != fTStorage) { |
| 383 | fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); |
| 384 | } |
| 385 | } else { |
| 386 | this->reset(0); |
joshualitt | f1f8895 | 2015-04-08 07:33:33 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 390 | private: |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 391 | // Since we use uint32_t storage, we might be able to get more elements for free. |
| 392 | static const size_t kCountWithPadding = SkAlign4(kCountRequested*sizeof(T)) / sizeof(T); |
| 393 | #if defined(GOOGLE3) |
| 394 | // Stack frame size is limited for GOOGLE3. 4k is less than the actual max, but some functions |
| 395 | // have multiple large stack allocations. |
| 396 | static const size_t kMaxBytes = 4 * 1024; |
| 397 | static const size_t kCount = kCountRequested * sizeof(T) > kMaxBytes |
| 398 | ? kMaxBytes / sizeof(T) |
| 399 | : kCountWithPadding; |
| 400 | #else |
| 401 | static const size_t kCount = kCountWithPadding; |
| 402 | #endif |
| 403 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 404 | T* fPtr; |
| 405 | union { |
benjaminwagner | f49c75a | 2016-02-05 07:02:38 -0800 | [diff] [blame] | 406 | uint32_t fStorage32[SkAlign4(kCount*sizeof(T)) >> 2]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 407 | T fTStorage[1]; // do NOT want to invoke T::T() |
| 408 | }; |
| 409 | }; |
| 410 | |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 411 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 412 | |
| 413 | /** |
| 414 | * Pass the object and the storage that was offered during SkInPlaceNewCheck, and this will |
| 415 | * safely destroy (and free if it was dynamically allocated) the object. |
| 416 | */ |
| 417 | template <typename T> void SkInPlaceDeleteCheck(T* obj, void* storage) { |
| 418 | if (storage == obj) { |
| 419 | obj->~T(); |
| 420 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 421 | delete obj; |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Allocates T, using storage if it is large enough, and allocating on the heap (via new) if |
| 427 | * storage is not large enough. |
| 428 | * |
| 429 | * obj = SkInPlaceNewCheck<Type>(storage, size); |
| 430 | * ... |
| 431 | * SkInPlaceDeleteCheck(obj, storage); |
| 432 | */ |
| 433 | template <typename T> T* SkInPlaceNewCheck(void* storage, size_t size) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 434 | return (sizeof(T) <= size) ? new (storage) T : new T; |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | template <typename T, typename A1, typename A2, typename A3> |
| 438 | T* SkInPlaceNewCheck(void* storage, size_t size, const A1& a1, const A2& a2, const A3& a3) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 439 | return (sizeof(T) <= size) ? new (storage) T(a1, a2, a3) : new T(a1, a2, a3); |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 440 | } |
| 441 | |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 442 | template <typename T, typename A1, typename A2, typename A3, typename A4> |
| 443 | T* SkInPlaceNewCheck(void* storage, size_t size, |
| 444 | const A1& a1, const A2& a2, const A3& a3, const A4& a4) { |
| 445 | return (sizeof(T) <= size) ? new (storage) T(a1, a2, a3, a4) : new T(a1, a2, a3, a4); |
| 446 | } |
| 447 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 448 | /** |
| 449 | * Reserves memory that is aligned on double and pointer boundaries. |
| 450 | * Hopefully this is sufficient for all practical purposes. |
| 451 | */ |
| 452 | template <size_t N> class SkAlignedSStorage : SkNoncopyable { |
| 453 | public: |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 454 | size_t size() const { return N; } |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 455 | void* get() { return fData; } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 456 | const void* get() const { return fData; } |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 457 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 458 | private: |
| 459 | union { |
| 460 | void* fPtr; |
| 461 | double fDouble; |
| 462 | char fData[N]; |
| 463 | }; |
| 464 | }; |
| 465 | |
| 466 | /** |
| 467 | * Reserves memory that is aligned on double and pointer boundaries. |
| 468 | * Hopefully this is sufficient for all practical purposes. Otherwise, |
| 469 | * we have to do some arcane trickery to determine alignment of non-POD |
| 470 | * types. Lifetime of the memory is the lifetime of the object. |
| 471 | */ |
| 472 | template <int N, typename T> class SkAlignedSTStorage : SkNoncopyable { |
| 473 | public: |
| 474 | /** |
| 475 | * Returns void* because this object does not initialize the |
| 476 | * memory. Use placement new for types that require a cons. |
| 477 | */ |
| 478 | void* get() { return fStorage.get(); } |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 479 | const void* get() const { return fStorage.get(); } |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 480 | private: |
| 481 | SkAlignedSStorage<sizeof(T)*N> fStorage; |
| 482 | }; |
| 483 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 484 | using SkAutoFree = std::unique_ptr<void, SkFunctionWrapper<void, void, sk_free>>; |
| 485 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 486 | #endif |