reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkTypes_DEFINED |
| 9 | #define SkTypes_DEFINED |
| 10 | |
| 11 | #include "SkPreConfig.h" |
| 12 | #include "SkUserConfig.h" |
| 13 | #include "SkPostConfig.h" |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 14 | #include <stdint.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
| 16 | /** \file SkTypes.h |
| 17 | */ |
| 18 | |
reed@android.com | 9aa8b32 | 2010-04-13 13:22:54 +0000 | [diff] [blame] | 19 | /** See SkGraphics::GetVersion() to retrieve these at runtime |
| 20 | */ |
| 21 | #define SKIA_VERSION_MAJOR 1 |
| 22 | #define SKIA_VERSION_MINOR 0 |
| 23 | #define SKIA_VERSION_PATCH 0 |
| 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | /* |
| 26 | memory wrappers to be implemented by the porting layer (platform) |
| 27 | */ |
| 28 | |
| 29 | /** Called internally if we run out of memory. The platform implementation must |
| 30 | not return, but should either throw an exception or otherwise exit. |
| 31 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 32 | SK_API extern void sk_out_of_memory(void); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | /** Called internally if we hit an unrecoverable error. |
| 34 | The platform implementation must not return, but should either throw |
| 35 | an exception or otherwise exit. |
| 36 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 37 | SK_API extern void sk_throw(void); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | |
| 39 | enum { |
| 40 | SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame |
| 41 | SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. |
| 42 | }; |
| 43 | /** Return a block of memory (at least 4-byte aligned) of at least the |
| 44 | specified size. If the requested memory cannot be returned, either |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 45 | return null (if SK_MALLOC_TEMP bit is clear) or throw an exception |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). |
| 47 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 48 | SK_API extern void* sk_malloc_flags(size_t size, unsigned flags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | /** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag |
| 50 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 51 | SK_API extern void* sk_malloc_throw(size_t size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | /** Same as standard realloc(), but this one never returns null on failure. It will throw |
| 53 | an exception if it fails. |
| 54 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 55 | SK_API extern void* sk_realloc_throw(void* buffer, size_t size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | /** Free memory returned by sk_malloc(). It is safe to pass null. |
| 57 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 58 | SK_API extern void sk_free(void*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 60 | /** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure. |
| 61 | */ |
| 62 | SK_API extern void* sk_calloc(size_t size); |
| 63 | |
| 64 | /** Same as sk_calloc, but throws an exception instead of returning NULL on failure. |
| 65 | */ |
| 66 | SK_API extern void* sk_calloc_throw(size_t size); |
| 67 | |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 68 | // bzero is safer than memset, but we can't rely on it, so... sk_bzero() |
| 69 | static inline void sk_bzero(void* buffer, size_t size) { |
| 70 | memset(buffer, 0, size); |
| 71 | } |
| 72 | |
reed@google.com | bdf7361 | 2011-09-06 14:56:20 +0000 | [diff] [blame] | 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
| 75 | #ifdef SK_OVERRIDE_GLOBAL_NEW |
| 76 | #include <new> |
| 77 | |
| 78 | inline void* operator new(size_t size) { |
| 79 | return sk_malloc_throw(size); |
| 80 | } |
| 81 | |
| 82 | inline void operator delete(void* p) { |
| 83 | sk_free(p); |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | |
| 89 | #define SK_INIT_TO_AVOID_WARNING = 0 |
| 90 | |
| 91 | #ifndef SkDebugf |
george | c4ade57 | 2014-08-01 12:02:07 -0700 | [diff] [blame] | 92 | SK_API void SkDebugf(const char format[], ...); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | #endif |
| 94 | |
| 95 | #ifdef SK_DEBUG |
commit-bot@chromium.org | 45d1d1d | 2014-04-30 18:24:16 +0000 | [diff] [blame] | 96 | #define SkASSERT(cond) SK_ALWAYSBREAK(cond) |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 97 | #define SkDEBUGFAIL(message) SkASSERT(false && message) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | #define SkDEBUGCODE(code) code |
| 99 | #define SkDECLAREPARAM(type, var) , type var |
| 100 | #define SkPARAM(var) , var |
| 101 | // #define SkDEBUGF(args ) SkDebugf##args |
| 102 | #define SkDEBUGF(args ) SkDebugf args |
| 103 | #define SkAssertResult(cond) SkASSERT(cond) |
| 104 | #else |
| 105 | #define SkASSERT(cond) |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 106 | #define SkDEBUGFAIL(message) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | #define SkDEBUGCODE(code) |
| 108 | #define SkDEBUGF(args) |
| 109 | #define SkDECLAREPARAM(type, var) |
| 110 | #define SkPARAM(var) |
| 111 | |
| 112 | // unlike SkASSERT, this guy executes its condition in the non-debug build |
| 113 | #define SkAssertResult(cond) cond |
| 114 | #endif |
| 115 | |
commit-bot@chromium.org | 45d1d1d | 2014-04-30 18:24:16 +0000 | [diff] [blame] | 116 | #define SkFAIL(message) SK_ALWAYSBREAK(false && message) |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 117 | |
mtklein | b59161f | 2014-06-18 07:54:47 -0700 | [diff] [blame] | 118 | // We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form. |
| 119 | // So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond, |
| 120 | // and if it's true the assert passes; if it's false, we'll print the message and the assert fails. |
| 121 | #define SkASSERTF(cond, fmt, ...) SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false)) |
| 122 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 123 | #ifdef SK_DEVELOPER |
| 124 | #define SkDEVCODE(code) code |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 125 | #else |
| 126 | #define SkDEVCODE(code) |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
| 129 | #ifdef SK_IGNORE_TO_STRING |
skia.committer@gmail.com | bc3d92a | 2014-03-14 03:02:26 +0000 | [diff] [blame] | 130 | #define SK_TO_STRING_NONVIRT() |
| 131 | #define SK_TO_STRING_VIRT() |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 132 | #define SK_TO_STRING_PUREVIRT() |
| 133 | #define SK_TO_STRING_OVERRIDE() |
| 134 | #else |
| 135 | // the 'toString' helper functions convert Sk* objects to human-readable |
| 136 | // form in developer mode |
| 137 | #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const; |
| 138 | #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const; |
| 139 | #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0; |
| 140 | #define SK_TO_STRING_OVERRIDE() virtual void toString(SkString* str) const SK_OVERRIDE; |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 143 | template <bool> |
| 144 | struct SkCompileAssert { |
| 145 | }; |
| 146 | |
bungeman@google.com | 562b2e6 | 2014-03-12 21:41:06 +0000 | [diff] [blame] | 147 | // Uses static_cast<bool>(expr) instead of bool(expr) due to |
| 148 | // https://connect.microsoft.com/VisualStudio/feedback/details/832915 |
bungeman@google.com | 9c28fa5 | 2014-03-13 17:51:05 +0000 | [diff] [blame] | 149 | |
| 150 | // The extra parentheses in SkCompileAssert<(...)> are a work around for |
| 151 | // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57771 |
| 152 | // which was fixed in gcc 4.8.2. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 153 | #define SK_COMPILE_ASSERT(expr, msg) \ |
bungeman@google.com | 2f58209 | 2014-03-12 21:56:23 +0000 | [diff] [blame] | 154 | typedef SkCompileAssert<(static_cast<bool>(expr))> \ |
bungeman@google.com | 562b2e6 | 2014-03-12 21:41:06 +0000 | [diff] [blame] | 155 | msg[static_cast<bool>(expr) ? 1 : -1] SK_UNUSED |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 156 | |
reed@google.com | 49a5b19 | 2012-10-25 17:31:39 +0000 | [diff] [blame] | 157 | /* |
| 158 | * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab |
| 159 | * |
| 160 | * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly |
| 161 | * |
| 162 | */ |
| 163 | #define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y) |
| 164 | #define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y |
| 165 | |
| 166 | /* |
| 167 | * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current |
| 168 | * line number. Easy way to construct |
| 169 | * unique names for local functions or |
| 170 | * variables. |
| 171 | */ |
| 172 | #define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__) |
| 173 | |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 174 | /** |
| 175 | * For some classes, it's almost always an error to instantiate one without a name, e.g. |
| 176 | * { |
| 177 | * SkAutoMutexAcquire(&mutex); |
| 178 | * <some code> |
| 179 | * } |
| 180 | * In this case, the writer meant to hold mutex while the rest of the code in the block runs, |
| 181 | * but instead the mutex is acquired and then immediately released. The correct usage is |
| 182 | * { |
| 183 | * SkAutoMutexAcquire lock(&mutex); |
| 184 | * <some code> |
| 185 | * } |
| 186 | * |
| 187 | * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR |
| 188 | * like this: |
| 189 | * class classname { |
| 190 | * <your class> |
| 191 | * }; |
| 192 | * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname) |
| 193 | * |
| 194 | * This won't work with templates, and you must inline the class' constructors and destructors. |
| 195 | * Take a look at SkAutoFree and SkAutoMalloc in this file for examples. |
| 196 | */ |
| 197 | #define SK_REQUIRE_LOCAL_VAR(classname) \ |
| 198 | SK_COMPILE_ASSERT(false, missing_name_for_##classname) |
| 199 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | /////////////////////////////////////////////////////////////////////// |
| 201 | |
reed@google.com | 37a3133 | 2011-01-25 14:55:42 +0000 | [diff] [blame] | 202 | /** |
| 203 | * Fast type for signed 8 bits. Use for parameter passing and local variables, |
| 204 | * not for storage. |
| 205 | */ |
| 206 | typedef int S8CPU; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | |
reed@google.com | 37a3133 | 2011-01-25 14:55:42 +0000 | [diff] [blame] | 208 | /** |
| 209 | * Fast type for unsigned 8 bits. Use for parameter passing and local |
| 210 | * variables, not for storage |
| 211 | */ |
| 212 | typedef unsigned U8CPU; |
| 213 | |
| 214 | /** |
| 215 | * Fast type for signed 16 bits. Use for parameter passing and local variables, |
| 216 | * not for storage |
| 217 | */ |
| 218 | typedef int S16CPU; |
| 219 | |
| 220 | /** |
| 221 | * Fast type for unsigned 16 bits. Use for parameter passing and local |
| 222 | * variables, not for storage |
| 223 | */ |
| 224 | typedef unsigned U16CPU; |
| 225 | |
| 226 | /** |
| 227 | * Meant to be faster than bool (doesn't promise to be 0 or 1, |
| 228 | * just 0 or non-zero |
| 229 | */ |
| 230 | typedef int SkBool; |
| 231 | |
| 232 | /** |
| 233 | * Meant to be a small version of bool, for storage purposes. Will be 0 or 1 |
| 234 | */ |
| 235 | typedef uint8_t SkBool8; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | |
| 237 | #ifdef SK_DEBUG |
bungeman@google.com | 777ded0 | 2013-07-19 22:30:11 +0000 | [diff] [blame] | 238 | SK_API int8_t SkToS8(intmax_t); |
| 239 | SK_API uint8_t SkToU8(uintmax_t); |
| 240 | SK_API int16_t SkToS16(intmax_t); |
| 241 | SK_API uint16_t SkToU16(uintmax_t); |
| 242 | SK_API int32_t SkToS32(intmax_t); |
| 243 | SK_API uint32_t SkToU32(uintmax_t); |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 244 | SK_API int SkToInt(intmax_t); |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 245 | SK_API unsigned SkToUInt(uintmax_t); |
commit-bot@chromium.org | 490fb6b | 2014-03-06 17:16:26 +0000 | [diff] [blame] | 246 | SK_API size_t SkToSizeT(uintmax_t); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | #else |
| 248 | #define SkToS8(x) ((int8_t)(x)) |
| 249 | #define SkToU8(x) ((uint8_t)(x)) |
| 250 | #define SkToS16(x) ((int16_t)(x)) |
| 251 | #define SkToU16(x) ((uint16_t)(x)) |
| 252 | #define SkToS32(x) ((int32_t)(x)) |
| 253 | #define SkToU32(x) ((uint32_t)(x)) |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 254 | #define SkToInt(x) ((int)(x)) |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 255 | #define SkToUInt(x) ((unsigned)(x)) |
commit-bot@chromium.org | 490fb6b | 2014-03-06 17:16:26 +0000 | [diff] [blame] | 256 | #define SkToSizeT(x) ((size_t)(x)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 257 | #endif |
| 258 | |
| 259 | /** Returns 0 or 1 based on the condition |
| 260 | */ |
| 261 | #define SkToBool(cond) ((cond) != 0) |
| 262 | |
| 263 | #define SK_MaxS16 32767 |
| 264 | #define SK_MinS16 -32767 |
| 265 | #define SK_MaxU16 0xFFFF |
| 266 | #define SK_MinU16 0 |
| 267 | #define SK_MaxS32 0x7FFFFFFF |
caryclark@google.com | 594dd3c | 2012-09-24 19:33:57 +0000 | [diff] [blame] | 268 | #define SK_MinS32 -SK_MaxS32 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | #define SK_MaxU32 0xFFFFFFFF |
| 270 | #define SK_MinU32 0 |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 271 | #define SK_NaN32 (1 << 31) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 272 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 273 | /** Returns true if the value can be represented with signed 16bits |
| 274 | */ |
reed@android.com | 90209ca | 2009-11-21 19:58:04 +0000 | [diff] [blame] | 275 | static inline bool SkIsS16(long x) { |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 276 | return (int16_t)x == x; |
| 277 | } |
| 278 | |
| 279 | /** Returns true if the value can be represented with unsigned 16bits |
| 280 | */ |
reed@android.com | 90209ca | 2009-11-21 19:58:04 +0000 | [diff] [blame] | 281 | static inline bool SkIsU16(long x) { |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 282 | return (uint16_t)x == x; |
| 283 | } |
| 284 | |
| 285 | ////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | #ifndef SK_OFFSETOF |
reed@google.com | 56d3a23 | 2012-03-21 12:25:48 +0000 | [diff] [blame] | 287 | #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 288 | #endif |
| 289 | |
| 290 | /** Returns the number of entries in an array (not a pointer) |
| 291 | */ |
| 292 | #define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) |
| 293 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | #define SkAlign2(x) (((x) + 1) >> 1 << 1) |
reed@google.com | c6faa5a | 2012-06-27 15:07:11 +0000 | [diff] [blame] | 295 | #define SkIsAlign2(x) (0 == ((x) & 1)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 296 | |
reed@google.com | c6faa5a | 2012-06-27 15:07:11 +0000 | [diff] [blame] | 297 | #define SkAlign4(x) (((x) + 3) >> 2 << 2) |
| 298 | #define SkIsAlign4(x) (0 == ((x) & 3)) |
| 299 | |
| 300 | #define SkAlign8(x) (((x) + 7) >> 3 << 3) |
| 301 | #define SkIsAlign8(x) (0 == ((x) & 7)) |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 302 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | typedef uint32_t SkFourByteTag; |
| 304 | #define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) |
| 305 | |
| 306 | /** 32 bit integer to hold a unicode value |
| 307 | */ |
| 308 | typedef int32_t SkUnichar; |
| 309 | /** 32 bit value to hold a millisecond count |
| 310 | */ |
| 311 | typedef uint32_t SkMSec; |
| 312 | /** 1 second measured in milliseconds |
| 313 | */ |
| 314 | #define SK_MSec1 1000 |
| 315 | /** maximum representable milliseconds |
| 316 | */ |
| 317 | #define SK_MSecMax 0x7FFFFFFF |
| 318 | /** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 |
| 319 | */ |
| 320 | #define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) |
| 321 | /** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 |
| 322 | */ |
| 323 | #define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) |
| 324 | |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 325 | /** The generation IDs in Skia reserve 0 has an invalid marker. |
| 326 | */ |
| 327 | #define SK_InvalidGenID 0 |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 328 | /** The unique IDs in Skia reserve 0 has an invalid marker. |
| 329 | */ |
| 330 | #define SK_InvalidUniqueID 0 |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 331 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | /**************************************************************************** |
| 333 | The rest of these only build with C++ |
| 334 | */ |
| 335 | #ifdef __cplusplus |
| 336 | |
| 337 | /** Faster than SkToBool for integral conditions. Returns 0 or 1 |
| 338 | */ |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 339 | static inline int Sk32ToBool(uint32_t n) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | return (n | (0-n)) >> 31; |
| 341 | } |
| 342 | |
bsalomon@google.com | ff43661 | 2013-02-27 19:07:32 +0000 | [diff] [blame] | 343 | /** Generic swap function. Classes with efficient swaps should specialize this function to take |
| 344 | their fast path. This function is used by SkTSort. */ |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 345 | template <typename T> inline void SkTSwap(T& a, T& b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | T c(a); |
| 347 | a = b; |
| 348 | b = c; |
| 349 | } |
| 350 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 351 | static inline int32_t SkAbs32(int32_t value) { |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 352 | if (value < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | value = -value; |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 354 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 355 | return value; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 356 | } |
| 357 | |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 358 | template <typename T> inline T SkTAbs(T value) { |
| 359 | if (value < 0) { |
| 360 | value = -value; |
| 361 | } |
| 362 | return value; |
| 363 | } |
| 364 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 365 | static inline int32_t SkMax32(int32_t a, int32_t b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | if (a < b) |
| 367 | a = b; |
| 368 | return a; |
| 369 | } |
| 370 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 371 | static inline int32_t SkMin32(int32_t a, int32_t b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 372 | if (a > b) |
| 373 | a = b; |
| 374 | return a; |
| 375 | } |
| 376 | |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 377 | template <typename T> const T& SkTMin(const T& a, const T& b) { |
| 378 | return (a < b) ? a : b; |
| 379 | } |
| 380 | |
| 381 | template <typename T> const T& SkTMax(const T& a, const T& b) { |
| 382 | return (b < a) ? a : b; |
| 383 | } |
| 384 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 385 | static inline int32_t SkSign32(int32_t a) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 386 | return (a >> 31) | ((unsigned) -a >> 31); |
| 387 | } |
| 388 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 389 | static inline int32_t SkFastMin32(int32_t value, int32_t max) { |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 390 | if (value > max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 391 | value = max; |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 392 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 393 | return value; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /** Returns signed 32 bit value pinned between min and max, inclusively |
| 397 | */ |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 398 | static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 399 | if (value < min) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 400 | value = min; |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 401 | } |
| 402 | if (value > max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 403 | value = max; |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 404 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 405 | return value; |
| 406 | } |
| 407 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 408 | static inline uint32_t SkSetClearShift(uint32_t bits, bool cond, |
| 409 | unsigned shift) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 410 | SkASSERT((int)cond == 0 || (int)cond == 1); |
| 411 | return (bits & ~(1 << shift)) | ((int)cond << shift); |
| 412 | } |
| 413 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 414 | static inline uint32_t SkSetClearMask(uint32_t bits, bool cond, |
| 415 | uint32_t mask) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 416 | return cond ? bits | mask : bits & ~mask; |
| 417 | } |
| 418 | |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 419 | /////////////////////////////////////////////////////////////////////////////// |
| 420 | |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 421 | /** Use to combine multiple bits in a bitmask in a type safe way. |
| 422 | */ |
| 423 | template <typename T> |
| 424 | T SkTBitOr(T a, T b) { |
| 425 | return (T)(a | b); |
| 426 | } |
| 427 | |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 428 | /** |
| 429 | * Use to cast a pointer to a different type, and maintaining strict-aliasing |
| 430 | */ |
| 431 | template <typename Dst> Dst SkTCast(const void* ptr) { |
| 432 | union { |
| 433 | const void* src; |
| 434 | Dst dst; |
| 435 | } data; |
| 436 | data.src = ptr; |
| 437 | return data.dst; |
| 438 | } |
| 439 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 440 | ////////////////////////////////////////////////////////////////////////////// |
| 441 | |
| 442 | /** \class SkNoncopyable |
| 443 | |
| 444 | SkNoncopyable is the base class for objects that may do not want to |
| 445 | be copied. It hides its copy-constructor and its assignment-operator. |
| 446 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 447 | class SK_API SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 448 | public: |
| 449 | SkNoncopyable() {} |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 450 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 451 | private: |
| 452 | SkNoncopyable(const SkNoncopyable&); |
| 453 | SkNoncopyable& operator=(const SkNoncopyable&); |
| 454 | }; |
| 455 | |
| 456 | class SkAutoFree : SkNoncopyable { |
| 457 | public: |
| 458 | SkAutoFree() : fPtr(NULL) {} |
| 459 | explicit SkAutoFree(void* ptr) : fPtr(ptr) {} |
| 460 | ~SkAutoFree() { sk_free(fPtr); } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 461 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 462 | /** Return the currently allocate buffer, or null |
| 463 | */ |
| 464 | void* get() const { return fPtr; } |
| 465 | |
| 466 | /** Assign a new ptr allocated with sk_malloc (or null), and return the |
| 467 | previous ptr. Note it is the caller's responsibility to sk_free the |
| 468 | returned ptr. |
| 469 | */ |
| 470 | void* set(void* ptr) { |
| 471 | void* prev = fPtr; |
| 472 | fPtr = ptr; |
| 473 | return prev; |
| 474 | } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 475 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 476 | /** Transfer ownership of the current ptr to the caller, setting the |
| 477 | internal reference to null. Note the caller is reponsible for calling |
| 478 | sk_free on the returned address. |
| 479 | */ |
| 480 | void* detach() { return this->set(NULL); } |
| 481 | |
| 482 | /** Free the current buffer, and set the internal reference to NULL. Same |
| 483 | as calling sk_free(detach()) |
| 484 | */ |
| 485 | void free() { |
| 486 | sk_free(fPtr); |
| 487 | fPtr = NULL; |
| 488 | } |
| 489 | |
| 490 | private: |
| 491 | void* fPtr; |
| 492 | // illegal |
| 493 | SkAutoFree(const SkAutoFree&); |
| 494 | SkAutoFree& operator=(const SkAutoFree&); |
| 495 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 496 | #define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 497 | |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 498 | /** |
| 499 | * Manage an allocated block of heap memory. This object is the sole manager of |
| 500 | * the lifetime of the block, so the caller must not call sk_free() or delete |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 501 | * on the block, unless detach() was called. |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 502 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 503 | class SkAutoMalloc : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 504 | public: |
reed@google.com | 3ab4195 | 2011-10-18 18:32:46 +0000 | [diff] [blame] | 505 | explicit SkAutoMalloc(size_t size = 0) { |
| 506 | fPtr = size ? sk_malloc_throw(size) : NULL; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 507 | fSize = size; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 508 | } |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 509 | |
| 510 | ~SkAutoMalloc() { |
| 511 | sk_free(fPtr); |
| 512 | } |
| 513 | |
| 514 | /** |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 515 | * Passed to reset to specify what happens if the requested size is smaller |
| 516 | * than the current size (and the current block was dynamically allocated). |
| 517 | */ |
| 518 | enum OnShrink { |
| 519 | /** |
| 520 | * If the requested size is smaller than the current size, and the |
| 521 | * current block is dynamically allocated, free the old block and |
| 522 | * malloc a new block of the smaller size. |
| 523 | */ |
| 524 | kAlloc_OnShrink, |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 525 | |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 526 | /** |
| 527 | * If the requested size is smaller than the current size, and the |
| 528 | * current block is dynamically allocated, just return the old |
| 529 | * block. |
| 530 | */ |
tomhudson@google.com | 1f90287 | 2012-06-01 13:15:47 +0000 | [diff] [blame] | 531 | kReuse_OnShrink |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 532 | }; |
| 533 | |
| 534 | /** |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 535 | * Reallocates the block to a new size. The ptr may or may not change. |
| 536 | */ |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 537 | void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) { |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 538 | if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) { |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 539 | if (NULL != didChangeAlloc) { |
| 540 | *didChangeAlloc = false; |
| 541 | } |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 542 | return fPtr; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 543 | } |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 544 | |
| 545 | sk_free(fPtr); |
| 546 | fPtr = size ? sk_malloc_throw(size) : NULL; |
| 547 | fSize = size; |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 548 | if (NULL != didChangeAlloc) { |
| 549 | *didChangeAlloc = true; |
| 550 | } |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 551 | |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 552 | return fPtr; |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Releases the block back to the heap |
| 557 | */ |
| 558 | void free() { |
| 559 | this->reset(0); |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Return the allocated block. |
| 564 | */ |
| 565 | void* get() { return fPtr; } |
| 566 | const void* get() const { return fPtr; } |
| 567 | |
bsalomon@google.com | 6dcd27c | 2011-09-06 15:02:33 +0000 | [diff] [blame] | 568 | /** Transfer ownership of the current ptr to the caller, setting the |
| 569 | internal reference to null. Note the caller is reponsible for calling |
| 570 | sk_free on the returned address. |
| 571 | */ |
| 572 | void* detach() { |
| 573 | void* ptr = fPtr; |
| 574 | fPtr = NULL; |
| 575 | fSize = 0; |
| 576 | return ptr; |
| 577 | } |
| 578 | |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 579 | private: |
| 580 | void* fPtr; |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 581 | size_t fSize; // can be larger than the requested size (see kReuse) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 582 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 583 | #define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 584 | |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 585 | /** |
| 586 | * Manage an allocated block of memory. If the requested size is <= kSize, then |
| 587 | * the allocation will come from the stack rather than the heap. This object |
| 588 | * is the sole manager of the lifetime of the block, so the caller must not |
| 589 | * call sk_free() or delete on the block. |
| 590 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 591 | template <size_t kSize> class SkAutoSMalloc : SkNoncopyable { |
| 592 | public: |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 593 | /** |
| 594 | * Creates initially empty storage. get() returns a ptr, but it is to |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 595 | * a zero-byte allocation. Must call reset(size) to return an allocated |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 596 | * block. |
| 597 | */ |
| 598 | SkAutoSMalloc() { |
| 599 | fPtr = fStorage; |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 600 | fSize = kSize; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 601 | } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 602 | |
| 603 | /** |
| 604 | * Allocate a block of the specified size. If size <= kSize, then the |
| 605 | * allocation will come from the stack, otherwise it will be dynamically |
| 606 | * allocated. |
| 607 | */ |
| 608 | explicit SkAutoSMalloc(size_t size) { |
| 609 | fPtr = fStorage; |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 610 | fSize = kSize; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 611 | this->reset(size); |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Free the allocated block (if any). If the block was small enought to |
| 616 | * have been allocated on the stack (size <= kSize) then this does nothing. |
| 617 | */ |
| 618 | ~SkAutoSMalloc() { |
| 619 | if (fPtr != (void*)fStorage) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 620 | sk_free(fPtr); |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 621 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 622 | } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 623 | |
| 624 | /** |
| 625 | * Return the allocated block. May return non-null even if the block is |
| 626 | * of zero size. Since this may be on the stack or dynamically allocated, |
| 627 | * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc |
| 628 | * to manage it. |
| 629 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 630 | void* get() const { return fPtr; } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 631 | |
| 632 | /** |
| 633 | * Return a new block of the requested size, freeing (as necessary) any |
| 634 | * previously allocated block. As with the constructor, if size <= kSize |
| 635 | * then the return block may be allocated locally, rather than from the |
| 636 | * heap. |
| 637 | */ |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 638 | void* reset(size_t size, |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 639 | SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink, |
| 640 | bool* didChangeAlloc = NULL) { |
| 641 | size = (size < kSize) ? kSize : size; |
robertphillips@google.com | 0f2b195 | 2013-05-23 14:59:40 +0000 | [diff] [blame] | 642 | bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize); |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 643 | if (NULL != didChangeAlloc) { |
| 644 | *didChangeAlloc = alloc; |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 645 | } |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 646 | if (alloc) { |
| 647 | if (fPtr != (void*)fStorage) { |
| 648 | sk_free(fPtr); |
| 649 | } |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 650 | |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 651 | if (size == kSize) { |
| 652 | SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc. |
| 653 | fPtr = fStorage; |
| 654 | } else { |
| 655 | fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); |
| 656 | } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 657 | |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 658 | fSize = size; |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 659 | } |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 660 | SkASSERT(fSize >= size && fSize >= kSize); |
| 661 | SkASSERT((fPtr == fStorage) || fSize > kSize); |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 662 | return fPtr; |
| 663 | } |
| 664 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 665 | private: |
| 666 | void* fPtr; |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 667 | size_t fSize; // can be larger than the requested size (see kReuse) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 668 | uint32_t fStorage[(kSize + 3) >> 2]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 669 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 670 | // Can't guard the constructor because it's a template class. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 671 | |
| 672 | #endif /* C++ */ |
| 673 | |
| 674 | #endif |