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