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