epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkTypes_DEFINED |
| 11 | #define SkTypes_DEFINED |
| 12 | |
| 13 | #include "SkPreConfig.h" |
| 14 | #include "SkUserConfig.h" |
| 15 | #include "SkPostConfig.h" |
| 16 | |
| 17 | #ifndef SK_IGNORE_STDINT_DOT_H |
| 18 | #include <stdint.h> |
| 19 | #endif |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | /** \file SkTypes.h |
| 24 | */ |
| 25 | |
reed@android.com | 9aa8b32 | 2010-04-13 13:22:54 +0000 | [diff] [blame] | 26 | /** See SkGraphics::GetVersion() to retrieve these at runtime |
| 27 | */ |
| 28 | #define SKIA_VERSION_MAJOR 1 |
| 29 | #define SKIA_VERSION_MINOR 0 |
| 30 | #define SKIA_VERSION_PATCH 0 |
| 31 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | /* |
| 33 | memory wrappers to be implemented by the porting layer (platform) |
| 34 | */ |
| 35 | |
| 36 | /** Called internally if we run out of memory. The platform implementation must |
| 37 | not return, but should either throw an exception or otherwise exit. |
| 38 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 39 | SK_API extern void sk_out_of_memory(void); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | /** Called internally if we hit an unrecoverable error. |
| 41 | The platform implementation must not return, but should either throw |
| 42 | an exception or otherwise exit. |
| 43 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 44 | SK_API extern void sk_throw(void); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | |
| 46 | enum { |
| 47 | SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame |
| 48 | SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. |
| 49 | }; |
| 50 | /** Return a block of memory (at least 4-byte aligned) of at least the |
| 51 | specified size. If the requested memory cannot be returned, either |
| 52 | return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw() |
| 53 | (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). |
| 54 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 55 | 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] | 56 | /** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag |
| 57 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 58 | SK_API extern void* sk_malloc_throw(size_t size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | /** Same as standard realloc(), but this one never returns null on failure. It will throw |
| 60 | an exception if it fails. |
| 61 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 62 | 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] | 63 | /** Free memory returned by sk_malloc(). It is safe to pass null. |
| 64 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 65 | SK_API extern void sk_free(void*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 67 | // bzero is safer than memset, but we can't rely on it, so... sk_bzero() |
| 68 | static inline void sk_bzero(void* buffer, size_t size) { |
| 69 | memset(buffer, 0, size); |
| 70 | } |
| 71 | |
reed@google.com | bdf7361 | 2011-09-06 14:56:20 +0000 | [diff] [blame] | 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 | |
| 74 | #ifdef SK_OVERRIDE_GLOBAL_NEW |
| 75 | #include <new> |
| 76 | |
| 77 | inline void* operator new(size_t size) { |
| 78 | return sk_malloc_throw(size); |
| 79 | } |
| 80 | |
| 81 | inline void operator delete(void* p) { |
| 82 | sk_free(p); |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | |
| 88 | #define SK_INIT_TO_AVOID_WARNING = 0 |
| 89 | |
| 90 | #ifndef SkDebugf |
| 91 | void SkDebugf(const char format[], ...); |
| 92 | #endif |
| 93 | |
| 94 | #ifdef SK_DEBUG |
| 95 | #define SkASSERT(cond) SK_DEBUGBREAK(cond) |
| 96 | #define SkDEBUGCODE(code) code |
| 97 | #define SkDECLAREPARAM(type, var) , type var |
| 98 | #define SkPARAM(var) , var |
| 99 | // #define SkDEBUGF(args ) SkDebugf##args |
| 100 | #define SkDEBUGF(args ) SkDebugf args |
| 101 | #define SkAssertResult(cond) SkASSERT(cond) |
| 102 | #else |
| 103 | #define SkASSERT(cond) |
| 104 | #define SkDEBUGCODE(code) |
| 105 | #define SkDEBUGF(args) |
| 106 | #define SkDECLAREPARAM(type, var) |
| 107 | #define SkPARAM(var) |
| 108 | |
| 109 | // unlike SkASSERT, this guy executes its condition in the non-debug build |
| 110 | #define SkAssertResult(cond) cond |
| 111 | #endif |
| 112 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 113 | namespace { |
| 114 | |
| 115 | template <bool> |
| 116 | struct SkCompileAssert { |
| 117 | }; |
| 118 | |
| 119 | } // namespace |
| 120 | |
| 121 | #define SK_COMPILE_ASSERT(expr, msg) \ |
| 122 | typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] |
| 123 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | /////////////////////////////////////////////////////////////////////// |
| 125 | |
reed@google.com | 37a3133 | 2011-01-25 14:55:42 +0000 | [diff] [blame] | 126 | /** |
| 127 | * Fast type for signed 8 bits. Use for parameter passing and local variables, |
| 128 | * not for storage. |
| 129 | */ |
| 130 | typedef int S8CPU; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | |
reed@google.com | 37a3133 | 2011-01-25 14:55:42 +0000 | [diff] [blame] | 132 | /** |
| 133 | * Fast type for unsigned 8 bits. Use for parameter passing and local |
| 134 | * variables, not for storage |
| 135 | */ |
| 136 | typedef unsigned U8CPU; |
| 137 | |
| 138 | /** |
| 139 | * Fast type for signed 16 bits. Use for parameter passing and local variables, |
| 140 | * not for storage |
| 141 | */ |
| 142 | typedef int S16CPU; |
| 143 | |
| 144 | /** |
| 145 | * Fast type for unsigned 16 bits. Use for parameter passing and local |
| 146 | * variables, not for storage |
| 147 | */ |
| 148 | typedef unsigned U16CPU; |
| 149 | |
| 150 | /** |
| 151 | * Meant to be faster than bool (doesn't promise to be 0 or 1, |
| 152 | * just 0 or non-zero |
| 153 | */ |
| 154 | typedef int SkBool; |
| 155 | |
| 156 | /** |
| 157 | * Meant to be a small version of bool, for storage purposes. Will be 0 or 1 |
| 158 | */ |
| 159 | typedef uint8_t SkBool8; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | |
| 161 | #ifdef SK_DEBUG |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 162 | SK_API int8_t SkToS8(long); |
| 163 | SK_API uint8_t SkToU8(size_t); |
| 164 | SK_API int16_t SkToS16(long); |
| 165 | SK_API uint16_t SkToU16(size_t); |
| 166 | SK_API int32_t SkToS32(long); |
| 167 | SK_API uint32_t SkToU32(size_t); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | #else |
| 169 | #define SkToS8(x) ((int8_t)(x)) |
| 170 | #define SkToU8(x) ((uint8_t)(x)) |
| 171 | #define SkToS16(x) ((int16_t)(x)) |
| 172 | #define SkToU16(x) ((uint16_t)(x)) |
| 173 | #define SkToS32(x) ((int32_t)(x)) |
| 174 | #define SkToU32(x) ((uint32_t)(x)) |
| 175 | #endif |
| 176 | |
| 177 | /** Returns 0 or 1 based on the condition |
| 178 | */ |
| 179 | #define SkToBool(cond) ((cond) != 0) |
| 180 | |
| 181 | #define SK_MaxS16 32767 |
| 182 | #define SK_MinS16 -32767 |
| 183 | #define SK_MaxU16 0xFFFF |
| 184 | #define SK_MinU16 0 |
| 185 | #define SK_MaxS32 0x7FFFFFFF |
| 186 | #define SK_MinS32 0x80000001 |
| 187 | #define SK_MaxU32 0xFFFFFFFF |
| 188 | #define SK_MinU32 0 |
| 189 | #define SK_NaN32 0x80000000 |
| 190 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 191 | /** Returns true if the value can be represented with signed 16bits |
| 192 | */ |
reed@android.com | 90209ca | 2009-11-21 19:58:04 +0000 | [diff] [blame] | 193 | static inline bool SkIsS16(long x) { |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 194 | return (int16_t)x == x; |
| 195 | } |
| 196 | |
| 197 | /** Returns true if the value can be represented with unsigned 16bits |
| 198 | */ |
reed@android.com | 90209ca | 2009-11-21 19:58:04 +0000 | [diff] [blame] | 199 | static inline bool SkIsU16(long x) { |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 200 | return (uint16_t)x == x; |
| 201 | } |
| 202 | |
| 203 | ////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | #ifndef SK_OFFSETOF |
| 205 | #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1) |
| 206 | #endif |
| 207 | |
| 208 | /** Returns the number of entries in an array (not a pointer) |
| 209 | */ |
| 210 | #define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) |
| 211 | |
| 212 | /** Returns x rounded up to a multiple of 2 |
| 213 | */ |
| 214 | #define SkAlign2(x) (((x) + 1) >> 1 << 1) |
| 215 | /** Returns x rounded up to a multiple of 4 |
| 216 | */ |
| 217 | #define SkAlign4(x) (((x) + 3) >> 2 << 2) |
| 218 | |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame^] | 219 | #define SkIsAlign4(x) (((x) & 3) == 0) |
| 220 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | typedef uint32_t SkFourByteTag; |
| 222 | #define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) |
| 223 | |
| 224 | /** 32 bit integer to hold a unicode value |
| 225 | */ |
| 226 | typedef int32_t SkUnichar; |
| 227 | /** 32 bit value to hold a millisecond count |
| 228 | */ |
| 229 | typedef uint32_t SkMSec; |
| 230 | /** 1 second measured in milliseconds |
| 231 | */ |
| 232 | #define SK_MSec1 1000 |
| 233 | /** maximum representable milliseconds |
| 234 | */ |
| 235 | #define SK_MSecMax 0x7FFFFFFF |
| 236 | /** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 |
| 237 | */ |
| 238 | #define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) |
| 239 | /** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 |
| 240 | */ |
| 241 | #define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) |
| 242 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | /**************************************************************************** |
| 244 | The rest of these only build with C++ |
| 245 | */ |
| 246 | #ifdef __cplusplus |
| 247 | |
| 248 | /** Faster than SkToBool for integral conditions. Returns 0 or 1 |
| 249 | */ |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 250 | static inline int Sk32ToBool(uint32_t n) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | return (n | (0-n)) >> 31; |
| 252 | } |
| 253 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 254 | template <typename T> inline void SkTSwap(T& a, T& b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | T c(a); |
| 256 | a = b; |
| 257 | b = c; |
| 258 | } |
| 259 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 260 | static inline int32_t SkAbs32(int32_t value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 261 | #ifdef SK_CPU_HAS_CONDITIONAL_INSTR |
| 262 | if (value < 0) |
| 263 | value = -value; |
| 264 | return value; |
| 265 | #else |
| 266 | int32_t mask = value >> 31; |
| 267 | return (value ^ mask) - mask; |
| 268 | #endif |
| 269 | } |
| 270 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 271 | static inline int32_t SkMax32(int32_t a, int32_t b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 272 | if (a < b) |
| 273 | a = b; |
| 274 | return a; |
| 275 | } |
| 276 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 277 | static inline int32_t SkMin32(int32_t a, int32_t b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 278 | if (a > b) |
| 279 | a = b; |
| 280 | return a; |
| 281 | } |
| 282 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 283 | static inline int32_t SkSign32(int32_t a) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | return (a >> 31) | ((unsigned) -a >> 31); |
| 285 | } |
| 286 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 287 | static inline int32_t SkFastMin32(int32_t value, int32_t max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 288 | #ifdef SK_CPU_HAS_CONDITIONAL_INSTR |
| 289 | if (value > max) |
| 290 | value = max; |
| 291 | return value; |
| 292 | #else |
| 293 | int diff = max - value; |
| 294 | // clear diff if it is negative (clear if value > max) |
| 295 | diff &= (diff >> 31); |
| 296 | return value + diff; |
| 297 | #endif |
| 298 | } |
| 299 | |
| 300 | /** Returns signed 32 bit value pinned between min and max, inclusively |
| 301 | */ |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 302 | static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | #ifdef SK_CPU_HAS_CONDITIONAL_INSTR |
| 304 | if (value < min) |
| 305 | value = min; |
| 306 | if (value > max) |
| 307 | value = max; |
| 308 | #else |
| 309 | if (value < min) |
| 310 | value = min; |
| 311 | else if (value > max) |
| 312 | value = max; |
| 313 | #endif |
| 314 | return value; |
| 315 | } |
| 316 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 317 | static inline uint32_t SkSetClearShift(uint32_t bits, bool cond, |
| 318 | unsigned shift) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 319 | SkASSERT((int)cond == 0 || (int)cond == 1); |
| 320 | return (bits & ~(1 << shift)) | ((int)cond << shift); |
| 321 | } |
| 322 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 323 | static inline uint32_t SkSetClearMask(uint32_t bits, bool cond, |
| 324 | uint32_t mask) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 325 | return cond ? bits | mask : bits & ~mask; |
| 326 | } |
| 327 | |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 328 | /////////////////////////////////////////////////////////////////////////////// |
| 329 | |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 330 | /** Use to combine multiple bits in a bitmask in a type safe way. |
| 331 | */ |
| 332 | template <typename T> |
| 333 | T SkTBitOr(T a, T b) { |
| 334 | return (T)(a | b); |
| 335 | } |
| 336 | |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 337 | /** |
| 338 | * Use to cast a pointer to a different type, and maintaining strict-aliasing |
| 339 | */ |
| 340 | template <typename Dst> Dst SkTCast(const void* ptr) { |
| 341 | union { |
| 342 | const void* src; |
| 343 | Dst dst; |
| 344 | } data; |
| 345 | data.src = ptr; |
| 346 | return data.dst; |
| 347 | } |
| 348 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | ////////////////////////////////////////////////////////////////////////////// |
| 350 | |
| 351 | /** \class SkNoncopyable |
| 352 | |
| 353 | SkNoncopyable is the base class for objects that may do not want to |
| 354 | be copied. It hides its copy-constructor and its assignment-operator. |
| 355 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 356 | class SK_API SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 357 | public: |
| 358 | SkNoncopyable() {} |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 359 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | private: |
| 361 | SkNoncopyable(const SkNoncopyable&); |
| 362 | SkNoncopyable& operator=(const SkNoncopyable&); |
| 363 | }; |
| 364 | |
| 365 | class SkAutoFree : SkNoncopyable { |
| 366 | public: |
| 367 | SkAutoFree() : fPtr(NULL) {} |
| 368 | explicit SkAutoFree(void* ptr) : fPtr(ptr) {} |
| 369 | ~SkAutoFree() { sk_free(fPtr); } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 370 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 371 | /** Return the currently allocate buffer, or null |
| 372 | */ |
| 373 | void* get() const { return fPtr; } |
| 374 | |
| 375 | /** Assign a new ptr allocated with sk_malloc (or null), and return the |
| 376 | previous ptr. Note it is the caller's responsibility to sk_free the |
| 377 | returned ptr. |
| 378 | */ |
| 379 | void* set(void* ptr) { |
| 380 | void* prev = fPtr; |
| 381 | fPtr = ptr; |
| 382 | return prev; |
| 383 | } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 384 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 385 | /** Transfer ownership of the current ptr to the caller, setting the |
| 386 | internal reference to null. Note the caller is reponsible for calling |
| 387 | sk_free on the returned address. |
| 388 | */ |
| 389 | void* detach() { return this->set(NULL); } |
| 390 | |
| 391 | /** Free the current buffer, and set the internal reference to NULL. Same |
| 392 | as calling sk_free(detach()) |
| 393 | */ |
| 394 | void free() { |
| 395 | sk_free(fPtr); |
| 396 | fPtr = NULL; |
| 397 | } |
| 398 | |
| 399 | private: |
| 400 | void* fPtr; |
| 401 | // illegal |
| 402 | SkAutoFree(const SkAutoFree&); |
| 403 | SkAutoFree& operator=(const SkAutoFree&); |
| 404 | }; |
| 405 | |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 406 | /** |
| 407 | * Manage an allocated block of heap memory. This object is the sole manager of |
| 408 | * 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] | 409 | * on the block, unless detach() was called. |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 410 | */ |
| 411 | class SkAutoMalloc : public SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 412 | public: |
reed@google.com | 3ab4195 | 2011-10-18 18:32:46 +0000 | [diff] [blame] | 413 | explicit SkAutoMalloc(size_t size = 0) { |
| 414 | fPtr = size ? sk_malloc_throw(size) : NULL; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 415 | fSize = size; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 416 | } |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 417 | |
| 418 | ~SkAutoMalloc() { |
| 419 | sk_free(fPtr); |
| 420 | } |
| 421 | |
| 422 | /** |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 423 | * Passed to reset to specify what happens if the requested size is smaller |
| 424 | * than the current size (and the current block was dynamically allocated). |
| 425 | */ |
| 426 | enum OnShrink { |
| 427 | /** |
| 428 | * If the requested size is smaller than the current size, and the |
| 429 | * current block is dynamically allocated, free the old block and |
| 430 | * malloc a new block of the smaller size. |
| 431 | */ |
| 432 | kAlloc_OnShrink, |
| 433 | |
| 434 | /** |
| 435 | * If the requested size is smaller than the current size, and the |
| 436 | * current block is dynamically allocated, just return the old |
| 437 | * block. |
| 438 | */ |
| 439 | kReuse_OnShrink, |
| 440 | }; |
| 441 | |
| 442 | /** |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 443 | * Reallocates the block to a new size. The ptr may or may not change. |
| 444 | */ |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 445 | void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink) { |
| 446 | if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) { |
| 447 | return fPtr; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 448 | } |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 449 | |
| 450 | sk_free(fPtr); |
| 451 | fPtr = size ? sk_malloc_throw(size) : NULL; |
| 452 | fSize = size; |
| 453 | |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 454 | return fPtr; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Releases the block back to the heap |
| 459 | */ |
| 460 | void free() { |
| 461 | this->reset(0); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Return the allocated block. |
| 466 | */ |
| 467 | void* get() { return fPtr; } |
| 468 | const void* get() const { return fPtr; } |
| 469 | |
bsalomon@google.com | 6dcd27c | 2011-09-06 15:02:33 +0000 | [diff] [blame] | 470 | /** Transfer ownership of the current ptr to the caller, setting the |
| 471 | internal reference to null. Note the caller is reponsible for calling |
| 472 | sk_free on the returned address. |
| 473 | */ |
| 474 | void* detach() { |
| 475 | void* ptr = fPtr; |
| 476 | fPtr = NULL; |
| 477 | fSize = 0; |
| 478 | return ptr; |
| 479 | } |
| 480 | |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 481 | private: |
| 482 | void* fPtr; |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 483 | 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] | 484 | }; |
| 485 | |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 486 | /** |
| 487 | * Manage an allocated block of memory. If the requested size is <= kSize, then |
| 488 | * the allocation will come from the stack rather than the heap. This object |
| 489 | * is the sole manager of the lifetime of the block, so the caller must not |
| 490 | * call sk_free() or delete on the block. |
| 491 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 492 | template <size_t kSize> class SkAutoSMalloc : SkNoncopyable { |
| 493 | public: |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 494 | /** |
| 495 | * 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] | 496 | * 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] | 497 | * block. |
| 498 | */ |
| 499 | SkAutoSMalloc() { |
| 500 | fPtr = fStorage; |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 501 | fSize = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 502 | } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 503 | |
| 504 | /** |
| 505 | * Allocate a block of the specified size. If size <= kSize, then the |
| 506 | * allocation will come from the stack, otherwise it will be dynamically |
| 507 | * allocated. |
| 508 | */ |
| 509 | explicit SkAutoSMalloc(size_t size) { |
| 510 | fPtr = fStorage; |
reed@google.com | 7a17e3a | 2011-10-18 18:58:06 +0000 | [diff] [blame] | 511 | fSize = 0; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 512 | this->reset(size); |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Free the allocated block (if any). If the block was small enought to |
| 517 | * have been allocated on the stack (size <= kSize) then this does nothing. |
| 518 | */ |
| 519 | ~SkAutoSMalloc() { |
| 520 | if (fPtr != (void*)fStorage) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 521 | sk_free(fPtr); |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 522 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 523 | } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 524 | |
| 525 | /** |
| 526 | * Return the allocated block. May return non-null even if the block is |
| 527 | * of zero size. Since this may be on the stack or dynamically allocated, |
| 528 | * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc |
| 529 | * to manage it. |
| 530 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 531 | void* get() const { return fPtr; } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 532 | |
| 533 | /** |
| 534 | * Return a new block of the requested size, freeing (as necessary) any |
| 535 | * previously allocated block. As with the constructor, if size <= kSize |
| 536 | * then the return block may be allocated locally, rather than from the |
| 537 | * heap. |
| 538 | */ |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 539 | void* reset(size_t size, |
| 540 | SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink) { |
| 541 | if (size == fSize || (SkAutoMalloc::kReuse_OnShrink == shrink && |
| 542 | size < fSize)) { |
| 543 | return fPtr; |
| 544 | } |
| 545 | |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 546 | if (fPtr != (void*)fStorage) { |
| 547 | sk_free(fPtr); |
| 548 | } |
| 549 | |
| 550 | if (size <= kSize) { |
| 551 | fPtr = fStorage; |
| 552 | } else { |
| 553 | fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); |
| 554 | } |
| 555 | return fPtr; |
| 556 | } |
| 557 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 558 | private: |
| 559 | void* fPtr; |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 560 | 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] | 561 | uint32_t fStorage[(kSize + 3) >> 2]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 562 | }; |
| 563 | |
| 564 | #endif /* C++ */ |
| 565 | |
| 566 | #endif |