SkTypes.h revision 562b2e67a29f24db4c258aa2fa59cd7b4ee15174
11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/* 21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * Copyright 2006 The Android Open Source Project 31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * 41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * Use of this source code is governed by a BSD-style license that can be 51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * found in the LICENSE file. 61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci */ 71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef SkTypes_DEFINED 91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define SkTypes_DEFINED 101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "SkPreConfig.h" 121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "SkUserConfig.h" 131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "SkPostConfig.h" 141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include <stdint.h> 151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/** \file SkTypes.h 171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci*/ 181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/** See SkGraphics::GetVersion() to retrieve these at runtime 201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci */ 211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define SKIA_VERSION_MAJOR 1 221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define SKIA_VERSION_MINOR 0 231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define SKIA_VERSION_PATCH 0 241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/* 261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci memory wrappers to be implemented by the porting layer (platform) 271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci*/ 281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/** Called internally if we run out of memory. The platform implementation must 301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci not return, but should either throw an exception or otherwise exit. 311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci*/ 321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciSK_API extern void sk_out_of_memory(void); 331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/** Called internally if we hit an unrecoverable error. 341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci The platform implementation must not return, but should either throw 351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci an exception or otherwise exit. 361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci*/ 371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciSK_API extern void sk_throw(void); 381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccienum { 401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame 411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. 421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}; 431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/** Return a block of memory (at least 4-byte aligned) of at least the 441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci specified size. If the requested memory cannot be returned, either 451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci return null (if SK_MALLOC_TEMP bit is clear) or throw an exception 461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). 471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci*/ 481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciSK_API extern void* sk_malloc_flags(size_t size, unsigned flags); 491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag 501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci*/ 511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciSK_API extern void* sk_malloc_throw(size_t size); 521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/** Same as standard realloc(), but this one never returns null on failure. It will throw 531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci an exception if it fails. 541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci*/ 551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciSK_API extern void* sk_realloc_throw(void* buffer, size_t size); 561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/** Free memory returned by sk_malloc(). It is safe to pass null. 571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci*/ 581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciSK_API extern void sk_free(void*); 591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure. 611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci */ 621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciSK_API extern void* sk_calloc(size_t size); 631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/** Same as sk_calloc, but throws an exception instead of returning NULL on failure. 651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci */ 661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciSK_API extern void* sk_calloc_throw(size_t size); 671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// bzero is safer than memset, but we can't rely on it, so... sk_bzero() 691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccistatic inline void sk_bzero(void* buffer, size_t size) { 701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci memset(buffer, 0, size); 711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci} 721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/////////////////////////////////////////////////////////////////////////////// 741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifdef SK_OVERRIDE_GLOBAL_NEW 761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include <new> 771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciinline void* operator new(size_t size) { 791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci return sk_malloc_throw(size); 801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci} 811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciinline void operator delete(void* p) { 831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci sk_free(p); 841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci} 851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif 861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/////////////////////////////////////////////////////////////////////////////// 881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define SK_INIT_TO_AVOID_WARNING = 0 901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef SkDebugf 921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void SkDebugf(const char format[], ...); 931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif 941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifdef SK_DEBUG 961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkASSERT(cond) SK_DEBUGBREAK(cond) 971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkDEBUGFAIL(message) SkASSERT(false && message) 981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkDEBUGCODE(code) code 991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkDECLAREPARAM(type, var) , type var 1001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkPARAM(var) , var 1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// #define SkDEBUGF(args ) SkDebugf##args 1021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkDEBUGF(args ) SkDebugf args 1031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkAssertResult(cond) SkASSERT(cond) 1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#else 1051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkASSERT(cond) 1061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkDEBUGFAIL(message) 1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkDEBUGCODE(code) 1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkDEBUGF(args) 1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkDECLAREPARAM(type, var) 1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkPARAM(var) 1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // unlike SkASSERT, this guy executes its condition in the non-debug build 1131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkAssertResult(cond) cond 1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif 1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifdef SK_DEVELOPER 1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkDEVCODE(code) code 1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // the 'toString' helper functions convert Sk* objects to human-readable 1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // form in developer mode 1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SK_DEVELOPER_TO_STRING() virtual void toString(SkString* str) const SK_OVERRIDE; 1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#else 1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SkDEVCODE(code) 1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci #define SK_DEVELOPER_TO_STRING() 1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif 1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccitemplate <bool> 1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccistruct SkCompileAssert { 1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}; 1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Uses static_cast<bool>(expr) instead of bool(expr) due to 1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// https://connect.microsoft.com/VisualStudio/feedback/details/832915 1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define SK_COMPILE_ASSERT(expr, msg) \ 1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci typedef SkCompileAssert<static_cast<bool>(expr)> \ 1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci msg[static_cast<bool>(expr) ? 1 : -1] SK_UNUSED 1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/* 1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab 1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * 1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly 1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * 141 */ 142#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y) 143#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y 144 145/* 146 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current 147 * line number. Easy way to construct 148 * unique names for local functions or 149 * variables. 150 */ 151#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__) 152 153/** 154 * For some classes, it's almost always an error to instantiate one without a name, e.g. 155 * { 156 * SkAutoMutexAcquire(&mutex); 157 * <some code> 158 * } 159 * In this case, the writer meant to hold mutex while the rest of the code in the block runs, 160 * but instead the mutex is acquired and then immediately released. The correct usage is 161 * { 162 * SkAutoMutexAcquire lock(&mutex); 163 * <some code> 164 * } 165 * 166 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR 167 * like this: 168 * class classname { 169 * <your class> 170 * }; 171 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname) 172 * 173 * This won't work with templates, and you must inline the class' constructors and destructors. 174 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples. 175 */ 176#define SK_REQUIRE_LOCAL_VAR(classname) \ 177 SK_COMPILE_ASSERT(false, missing_name_for_##classname) 178 179/////////////////////////////////////////////////////////////////////// 180 181/** 182 * Fast type for signed 8 bits. Use for parameter passing and local variables, 183 * not for storage. 184 */ 185typedef int S8CPU; 186 187/** 188 * Fast type for unsigned 8 bits. Use for parameter passing and local 189 * variables, not for storage 190 */ 191typedef unsigned U8CPU; 192 193/** 194 * Fast type for signed 16 bits. Use for parameter passing and local variables, 195 * not for storage 196 */ 197typedef int S16CPU; 198 199/** 200 * Fast type for unsigned 16 bits. Use for parameter passing and local 201 * variables, not for storage 202 */ 203typedef unsigned U16CPU; 204 205/** 206 * Meant to be faster than bool (doesn't promise to be 0 or 1, 207 * just 0 or non-zero 208 */ 209typedef int SkBool; 210 211/** 212 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1 213 */ 214typedef uint8_t SkBool8; 215 216#ifdef SK_DEBUG 217 SK_API int8_t SkToS8(intmax_t); 218 SK_API uint8_t SkToU8(uintmax_t); 219 SK_API int16_t SkToS16(intmax_t); 220 SK_API uint16_t SkToU16(uintmax_t); 221 SK_API int32_t SkToS32(intmax_t); 222 SK_API uint32_t SkToU32(uintmax_t); 223 SK_API int SkToInt(intmax_t); 224 SK_API unsigned SkToUInt(uintmax_t); 225 SK_API size_t SkToSizeT(uintmax_t); 226#else 227 #define SkToS8(x) ((int8_t)(x)) 228 #define SkToU8(x) ((uint8_t)(x)) 229 #define SkToS16(x) ((int16_t)(x)) 230 #define SkToU16(x) ((uint16_t)(x)) 231 #define SkToS32(x) ((int32_t)(x)) 232 #define SkToU32(x) ((uint32_t)(x)) 233 #define SkToInt(x) ((int)(x)) 234 #define SkToUInt(x) ((unsigned)(x)) 235 #define SkToSizeT(x) ((size_t)(x)) 236#endif 237 238/** Returns 0 or 1 based on the condition 239*/ 240#define SkToBool(cond) ((cond) != 0) 241 242#define SK_MaxS16 32767 243#define SK_MinS16 -32767 244#define SK_MaxU16 0xFFFF 245#define SK_MinU16 0 246#define SK_MaxS32 0x7FFFFFFF 247#define SK_MinS32 -SK_MaxS32 248#define SK_MaxU32 0xFFFFFFFF 249#define SK_MinU32 0 250#define SK_NaN32 (1 << 31) 251 252/** Returns true if the value can be represented with signed 16bits 253 */ 254static inline bool SkIsS16(long x) { 255 return (int16_t)x == x; 256} 257 258/** Returns true if the value can be represented with unsigned 16bits 259 */ 260static inline bool SkIsU16(long x) { 261 return (uint16_t)x == x; 262} 263 264////////////////////////////////////////////////////////////////////////////// 265#ifndef SK_OFFSETOF 266 #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1) 267#endif 268 269/** Returns the number of entries in an array (not a pointer) 270*/ 271#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) 272 273#define SkAlign2(x) (((x) + 1) >> 1 << 1) 274#define SkIsAlign2(x) (0 == ((x) & 1)) 275 276#define SkAlign4(x) (((x) + 3) >> 2 << 2) 277#define SkIsAlign4(x) (0 == ((x) & 3)) 278 279#define SkAlign8(x) (((x) + 7) >> 3 << 3) 280#define SkIsAlign8(x) (0 == ((x) & 7)) 281 282typedef uint32_t SkFourByteTag; 283#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) 284 285/** 32 bit integer to hold a unicode value 286*/ 287typedef int32_t SkUnichar; 288/** 32 bit value to hold a millisecond count 289*/ 290typedef uint32_t SkMSec; 291/** 1 second measured in milliseconds 292*/ 293#define SK_MSec1 1000 294/** maximum representable milliseconds 295*/ 296#define SK_MSecMax 0x7FFFFFFF 297/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 298*/ 299#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) 300/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 301*/ 302#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) 303 304/**************************************************************************** 305 The rest of these only build with C++ 306*/ 307#ifdef __cplusplus 308 309/** Faster than SkToBool for integral conditions. Returns 0 or 1 310*/ 311static inline int Sk32ToBool(uint32_t n) { 312 return (n | (0-n)) >> 31; 313} 314 315/** Generic swap function. Classes with efficient swaps should specialize this function to take 316 their fast path. This function is used by SkTSort. */ 317template <typename T> inline void SkTSwap(T& a, T& b) { 318 T c(a); 319 a = b; 320 b = c; 321} 322 323static inline int32_t SkAbs32(int32_t value) { 324 if (value < 0) { 325 value = -value; 326 } 327 return value; 328} 329 330template <typename T> inline T SkTAbs(T value) { 331 if (value < 0) { 332 value = -value; 333 } 334 return value; 335} 336 337static inline int32_t SkMax32(int32_t a, int32_t b) { 338 if (a < b) 339 a = b; 340 return a; 341} 342 343static inline int32_t SkMin32(int32_t a, int32_t b) { 344 if (a > b) 345 a = b; 346 return a; 347} 348 349template <typename T> const T& SkTMin(const T& a, const T& b) { 350 return (a < b) ? a : b; 351} 352 353template <typename T> const T& SkTMax(const T& a, const T& b) { 354 return (b < a) ? a : b; 355} 356 357static inline int32_t SkSign32(int32_t a) { 358 return (a >> 31) | ((unsigned) -a >> 31); 359} 360 361static inline int32_t SkFastMin32(int32_t value, int32_t max) { 362 if (value > max) { 363 value = max; 364 } 365 return value; 366} 367 368/** Returns signed 32 bit value pinned between min and max, inclusively 369*/ 370static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { 371 if (value < min) { 372 value = min; 373 } 374 if (value > max) { 375 value = max; 376 } 377 return value; 378} 379 380static inline uint32_t SkSetClearShift(uint32_t bits, bool cond, 381 unsigned shift) { 382 SkASSERT((int)cond == 0 || (int)cond == 1); 383 return (bits & ~(1 << shift)) | ((int)cond << shift); 384} 385 386static inline uint32_t SkSetClearMask(uint32_t bits, bool cond, 387 uint32_t mask) { 388 return cond ? bits | mask : bits & ~mask; 389} 390 391/////////////////////////////////////////////////////////////////////////////// 392 393/** Use to combine multiple bits in a bitmask in a type safe way. 394 */ 395template <typename T> 396T SkTBitOr(T a, T b) { 397 return (T)(a | b); 398} 399 400/** 401 * Use to cast a pointer to a different type, and maintaining strict-aliasing 402 */ 403template <typename Dst> Dst SkTCast(const void* ptr) { 404 union { 405 const void* src; 406 Dst dst; 407 } data; 408 data.src = ptr; 409 return data.dst; 410} 411 412////////////////////////////////////////////////////////////////////////////// 413 414/** \class SkNoncopyable 415 416SkNoncopyable is the base class for objects that may do not want to 417be copied. It hides its copy-constructor and its assignment-operator. 418*/ 419class SK_API SkNoncopyable { 420public: 421 SkNoncopyable() {} 422 423private: 424 SkNoncopyable(const SkNoncopyable&); 425 SkNoncopyable& operator=(const SkNoncopyable&); 426}; 427 428class SkAutoFree : SkNoncopyable { 429public: 430 SkAutoFree() : fPtr(NULL) {} 431 explicit SkAutoFree(void* ptr) : fPtr(ptr) {} 432 ~SkAutoFree() { sk_free(fPtr); } 433 434 /** Return the currently allocate buffer, or null 435 */ 436 void* get() const { return fPtr; } 437 438 /** Assign a new ptr allocated with sk_malloc (or null), and return the 439 previous ptr. Note it is the caller's responsibility to sk_free the 440 returned ptr. 441 */ 442 void* set(void* ptr) { 443 void* prev = fPtr; 444 fPtr = ptr; 445 return prev; 446 } 447 448 /** Transfer ownership of the current ptr to the caller, setting the 449 internal reference to null. Note the caller is reponsible for calling 450 sk_free on the returned address. 451 */ 452 void* detach() { return this->set(NULL); } 453 454 /** Free the current buffer, and set the internal reference to NULL. Same 455 as calling sk_free(detach()) 456 */ 457 void free() { 458 sk_free(fPtr); 459 fPtr = NULL; 460 } 461 462private: 463 void* fPtr; 464 // illegal 465 SkAutoFree(const SkAutoFree&); 466 SkAutoFree& operator=(const SkAutoFree&); 467}; 468#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree) 469 470/** 471 * Manage an allocated block of heap memory. This object is the sole manager of 472 * the lifetime of the block, so the caller must not call sk_free() or delete 473 * on the block, unless detach() was called. 474 */ 475class SkAutoMalloc : public SkNoncopyable { 476public: 477 explicit SkAutoMalloc(size_t size = 0) { 478 fPtr = size ? sk_malloc_throw(size) : NULL; 479 fSize = size; 480 } 481 482 ~SkAutoMalloc() { 483 sk_free(fPtr); 484 } 485 486 /** 487 * Passed to reset to specify what happens if the requested size is smaller 488 * than the current size (and the current block was dynamically allocated). 489 */ 490 enum OnShrink { 491 /** 492 * If the requested size is smaller than the current size, and the 493 * current block is dynamically allocated, free the old block and 494 * malloc a new block of the smaller size. 495 */ 496 kAlloc_OnShrink, 497 498 /** 499 * If the requested size is smaller than the current size, and the 500 * current block is dynamically allocated, just return the old 501 * block. 502 */ 503 kReuse_OnShrink 504 }; 505 506 /** 507 * Reallocates the block to a new size. The ptr may or may not change. 508 */ 509 void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) { 510 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) { 511 if (NULL != didChangeAlloc) { 512 *didChangeAlloc = false; 513 } 514 return fPtr; 515 } 516 517 sk_free(fPtr); 518 fPtr = size ? sk_malloc_throw(size) : NULL; 519 fSize = size; 520 if (NULL != didChangeAlloc) { 521 *didChangeAlloc = true; 522 } 523 524 return fPtr; 525 } 526 527 /** 528 * Releases the block back to the heap 529 */ 530 void free() { 531 this->reset(0); 532 } 533 534 /** 535 * Return the allocated block. 536 */ 537 void* get() { return fPtr; } 538 const void* get() const { return fPtr; } 539 540 /** Transfer ownership of the current ptr to the caller, setting the 541 internal reference to null. Note the caller is reponsible for calling 542 sk_free on the returned address. 543 */ 544 void* detach() { 545 void* ptr = fPtr; 546 fPtr = NULL; 547 fSize = 0; 548 return ptr; 549 } 550 551private: 552 void* fPtr; 553 size_t fSize; // can be larger than the requested size (see kReuse) 554}; 555#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc) 556 557/** 558 * Manage an allocated block of memory. If the requested size is <= kSize, then 559 * the allocation will come from the stack rather than the heap. This object 560 * is the sole manager of the lifetime of the block, so the caller must not 561 * call sk_free() or delete on the block. 562 */ 563template <size_t kSize> class SkAutoSMalloc : SkNoncopyable { 564public: 565 /** 566 * Creates initially empty storage. get() returns a ptr, but it is to 567 * a zero-byte allocation. Must call reset(size) to return an allocated 568 * block. 569 */ 570 SkAutoSMalloc() { 571 fPtr = fStorage; 572 fSize = kSize; 573 } 574 575 /** 576 * Allocate a block of the specified size. If size <= kSize, then the 577 * allocation will come from the stack, otherwise it will be dynamically 578 * allocated. 579 */ 580 explicit SkAutoSMalloc(size_t size) { 581 fPtr = fStorage; 582 fSize = kSize; 583 this->reset(size); 584 } 585 586 /** 587 * Free the allocated block (if any). If the block was small enought to 588 * have been allocated on the stack (size <= kSize) then this does nothing. 589 */ 590 ~SkAutoSMalloc() { 591 if (fPtr != (void*)fStorage) { 592 sk_free(fPtr); 593 } 594 } 595 596 /** 597 * Return the allocated block. May return non-null even if the block is 598 * of zero size. Since this may be on the stack or dynamically allocated, 599 * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc 600 * to manage it. 601 */ 602 void* get() const { return fPtr; } 603 604 /** 605 * Return a new block of the requested size, freeing (as necessary) any 606 * previously allocated block. As with the constructor, if size <= kSize 607 * then the return block may be allocated locally, rather than from the 608 * heap. 609 */ 610 void* reset(size_t size, 611 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink, 612 bool* didChangeAlloc = NULL) { 613 size = (size < kSize) ? kSize : size; 614 bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize); 615 if (NULL != didChangeAlloc) { 616 *didChangeAlloc = alloc; 617 } 618 if (alloc) { 619 if (fPtr != (void*)fStorage) { 620 sk_free(fPtr); 621 } 622 623 if (size == kSize) { 624 SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc. 625 fPtr = fStorage; 626 } else { 627 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); 628 } 629 630 fSize = size; 631 } 632 SkASSERT(fSize >= size && fSize >= kSize); 633 SkASSERT((fPtr == fStorage) || fSize > kSize); 634 return fPtr; 635 } 636 637private: 638 void* fPtr; 639 size_t fSize; // can be larger than the requested size (see kReuse) 640 uint32_t fStorage[(kSize + 3) >> 2]; 641}; 642// Can't guard the constructor because it's a template class. 643 644#endif /* C++ */ 645 646#endif 647