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