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