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