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