SkTypes.h revision 6dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project 48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkTypes_DEFINED 118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkTypes_DEFINED 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPreConfig.h" 148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUserConfig.h" 158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPostConfig.h" 168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_IGNORE_STDINT_DOT_H 188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #include <stdint.h> 198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include <stdio.h> 228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkTypes.h 248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 269aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com/** See SkGraphics::GetVersion() to retrieve these at runtime 279aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com */ 289aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MAJOR 1 299aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MINOR 0 309aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_PATCH 0 319aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com 328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memory wrappers to be implemented by the porting layer (platform) 348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we run out of memory. The platform implementation must 378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com not return, but should either throw an exception or otherwise exit. 388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void sk_out_of_memory(void); 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we hit an unrecoverable error. 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The platform implementation must not return, but should either throw 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com an exception or otherwise exit. 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void sk_throw(void); 458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum { 478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame 488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a block of memory (at least 4-byte aligned) of at least the 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com specified size. If the requested memory cannot be returned, either 528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw() 538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 557ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API extern void* sk_malloc_flags(size_t size, unsigned flags); 568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void* sk_malloc_throw(size_t size); 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as standard realloc(), but this one never returns null on failure. It will throw 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com an exception if it fails. 618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void* sk_realloc_throw(void* buffer, size_t size); 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Free memory returned by sk_malloc(). It is safe to pass null. 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 657ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API extern void sk_free(void*); 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 674516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com// bzero is safer than memset, but we can't rely on it, so... sk_bzero() 684516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.comstatic inline void sk_bzero(void* buffer, size_t size) { 694516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com memset(buffer, 0, size); 704516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com} 714516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com 72bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com/////////////////////////////////////////////////////////////////////////////// 73bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 74bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#ifdef SK_OVERRIDE_GLOBAL_NEW 75bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#include <new> 76bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 77bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void* operator new(size_t size) { 78bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com return sk_malloc_throw(size); 79bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com} 80bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 81bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void operator delete(void* p) { 82bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com sk_free(p); 83bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com} 84bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#endif 85bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 86bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com/////////////////////////////////////////////////////////////////////////////// 878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_INIT_TO_AVOID_WARNING = 0 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDebugf 918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void SkDebugf(const char format[], ...); 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkASSERT(cond) SK_DEBUGBREAK(cond) 968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGCODE(code) code 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDECLAREPARAM(type, var) , type var 988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkPARAM(var) , var 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// #define SkDEBUGF(args ) SkDebugf##args 1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGF(args ) SkDebugf args 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkAssertResult(cond) SkASSERT(cond) 1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkASSERT(cond) 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGCODE(code) 1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGF(args) 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDECLAREPARAM(type, var) 1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkPARAM(var) 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // unlike SkASSERT, this guy executes its condition in the non-debug build 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkAssertResult(cond) cond 1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgnamespace { 11428be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 11528be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgtemplate <bool> 11628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgstruct SkCompileAssert { 11728be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org}; 11828be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 11928be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org} // namespace 12028be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 12128be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org#define SK_COMPILE_ASSERT(expr, msg) \ 12228be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] 12328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////// 1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 12637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 12737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for signed 8 bits. Use for parameter passing and local variables, 12837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * not for storage. 12937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 13037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S8CPU; 1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 13237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 13337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for unsigned 8 bits. Use for parameter passing and local 13437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * variables, not for storage 13537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 13637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U8CPU; 13737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 13837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 13937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for signed 16 bits. Use for parameter passing and local variables, 14037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * not for storage 14137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 14237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S16CPU; 14337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 14437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 14537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for unsigned 16 bits. Use for parameter passing and local 14637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * variables, not for storage 14737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 14837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U16CPU; 14937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 15037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 15137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Meant to be faster than bool (doesn't promise to be 0 or 1, 15237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * just 0 or non-zero 15337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 15437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int SkBool; 15537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 15637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 15737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Meant to be a small version of bool, for storage purposes. Will be 0 or 1 15837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 15937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef uint8_t SkBool8; 1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 1627ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org SK_API int8_t SkToS8(long); 1637ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org SK_API uint8_t SkToU8(size_t); 1647ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org SK_API int16_t SkToS16(long); 1657ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org SK_API uint16_t SkToU16(size_t); 1667ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org SK_API int32_t SkToS32(long); 1677ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org SK_API uint32_t SkToU32(size_t); 1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS8(x) ((int8_t)(x)) 1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU8(x) ((uint8_t)(x)) 1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS16(x) ((int16_t)(x)) 1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU16(x) ((uint16_t)(x)) 1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS32(x) ((int32_t)(x)) 1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU32(x) ((uint32_t)(x)) 1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition 1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkToBool(cond) ((cond) != 0) 1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16 32767 1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16 -32767 1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16 0xFFFF 1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16 0 1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32 0x7FFFFFFF 1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS32 0x80000001 1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32 0xFFFFFFFF 1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32 0 1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_NaN32 0x80000000 1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 191d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits 192d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 19390209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) { 194d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (int16_t)x == x; 195d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 196d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 197d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits 198d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 19990209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) { 200d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (uint16_t)x == x; 201d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 202d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 203d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com////////////////////////////////////////////////////////////////////////////// 2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_OFFSETOF 2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1) 2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns the number of entries in an array (not a pointer) 2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) 2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns x rounded up to a multiple of 2 2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x) (((x) + 1) >> 1 << 1) 2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns x rounded up to a multiple of 4 2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x) (((x) + 3) >> 2 << 2) 2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag; 2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) 2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value 2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar; 2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit value to hold a millisecond count 2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec; 2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds 2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000 2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** maximum representable milliseconds 2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF 2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) 2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) 2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/**************************************************************************** 2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The rest of these only build with C++ 2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus 2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1 2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 248d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int Sk32ToBool(uint32_t n) { 2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (n | (0-n)) >> 31; 2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 252d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) { 2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com T c(a); 2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b = c; 2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 258d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) { 2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < 0) 2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = -value; 2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int32_t mask = value >> 31; 2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (value ^ mask) - mask; 2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 269d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) { 2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a < b) 2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 275d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) { 2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a > b) 2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 281d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) { 2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (a >> 31) | ((unsigned) -a >> 31); 2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 285d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) { 2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value > max) 2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int diff = max - value; 2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // clear diff if it is negative (clear if value > max) 2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com diff &= (diff >> 31); 2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value + diff; 2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns signed 32 bit value pinned between min and max, inclusively 2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 300d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { 3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < min) 3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = min; 3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value > max) 3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < min) 3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = min; 3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else if (value > max) 3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 315d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearShift(uint32_t bits, bool cond, 316d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com unsigned shift) { 3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT((int)cond == 0 || (int)cond == 1); 3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (bits & ~(1 << shift)) | ((int)cond << shift); 3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 321d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearMask(uint32_t bits, bool cond, 322d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com uint32_t mask) { 3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return cond ? bits | mask : bits & ~mask; 3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3261fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/////////////////////////////////////////////////////////////////////////////// 3271fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 328325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org/** Use to combine multiple bits in a bitmask in a type safe way. 329325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org */ 330325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgtemplate <typename T> 331325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgT SkTBitOr(T a, T b) { 332325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org return (T)(a | b); 333325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org} 334325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org 3351fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/** 3361fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com * Use to cast a pointer to a different type, and maintaining strict-aliasing 3371fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com */ 3381fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.comtemplate <typename Dst> Dst SkTCast(const void* ptr) { 3391fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com union { 3401fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com const void* src; 3411fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com Dst dst; 3421fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com } data; 3431fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com data.src = ptr; 3441fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com return data.dst; 3451fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com} 3461fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable 3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkNoncopyable is the base class for objects that may do not want to 3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator. 3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3547ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkNoncopyable { 3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable() {} 3571fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable(const SkNoncopyable&); 3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable& operator=(const SkNoncopyable&); 3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable { 3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree() : fPtr(NULL) {} 3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoFree(void* ptr) : fPtr(ptr) {} 3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ~SkAutoFree() { sk_free(fPtr); } 3681fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Return the currently allocate buffer, or null 3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Assign a new ptr allocated with sk_malloc (or null), and return the 3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com previous ptr. Note it is the caller's responsibility to sk_free the 3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com returned ptr. 3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* set(void* ptr) { 3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* prev = fPtr; 3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = ptr; 3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return prev; 3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3821fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Transfer ownership of the current ptr to the caller, setting the 3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com internal reference to null. Note the caller is reponsible for calling 3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free on the returned address. 3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* detach() { return this->set(NULL); } 3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Free the current buffer, and set the internal reference to NULL. Same 3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com as calling sk_free(detach()) 3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void free() { 3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = NULL; 3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // illegal 4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree(const SkAutoFree&); 4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree& operator=(const SkAutoFree&); 4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4047d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com/** 4057d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Manage an allocated block of heap memory. This object is the sole manager of 4067d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * the lifetime of the block, so the caller must not call sk_free() or delete 4077d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * on the block. 4087d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 4097d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comclass SkAutoMalloc : public SkNoncopyable { 4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 4117d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com explicit SkAutoMalloc(size_t size = 0, 4127d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com unsigned flags = SK_MALLOC_THROW | SK_MALLOC_TEMP) { 4137d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com fPtr = size ? sk_malloc_flags(size, flags) : NULL; 4147d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com fSize = size; 4157d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4177d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com ~SkAutoMalloc() { 4187d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com sk_free(fPtr); 4197d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4217d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com /** 4227d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Reallocates the block to a new size. The ptr may or may not change. 4237d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 4247d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com void* reset(size_t size, 4257d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) { 4267d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com if (size != fSize) { 4277d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com sk_free(fPtr); 4287d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com fPtr = size ? sk_malloc_flags(size, flags) : NULL; 4297d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com fSize = size; 4307d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 4317d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com return fPtr; 4328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 4337d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com 4347d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com /** 4357d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Releases the block back to the heap 4367d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 4377d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com void free() { 4387d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com this->reset(0); 4397d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 4407d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com 4417d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com /** 4427d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Return the allocated block. 4437d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 4447d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com void* get() { return fPtr; } 4457d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com const void* get() const { return fPtr; } 4467d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com 4476dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com /** Transfer ownership of the current ptr to the caller, setting the 4486dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com internal reference to null. Note the caller is reponsible for calling 4496dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com sk_free on the returned address. 4506dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com */ 4516dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com void* detach() { 4526dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com void* ptr = fPtr; 4536dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com fPtr = NULL; 4546dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com fSize = 0; 4556dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com return ptr; 4566dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com } 4576dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com 4587d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com /** 4597d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Gets the size of the block in bytes 4607d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 4617d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com size_t getSize() const { return fSize; } 4627d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com 4637d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comprivate: 4647d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com void* fPtr; 4657d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com size_t fSize; 4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 46863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com/** 46963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Manage an allocated block of memory. If the requested size is <= kSize, then 47063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * the allocation will come from the stack rather than the heap. This object 47163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * is the sole manager of the lifetime of the block, so the caller must not 47263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * call sk_free() or delete on the block. 47363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <size_t kSize> class SkAutoSMalloc : SkNoncopyable { 4758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 47663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 47763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Creates initially empty storage. get() returns a ptr, but it is to 4787d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * a zero-byte allocation. Must call reset(size) to return an allocated 47963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * block. 48063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 48163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com SkAutoSMalloc() { 48263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com fPtr = fStorage; 4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 48463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 48563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 48663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Allocate a block of the specified size. If size <= kSize, then the 48763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * allocation will come from the stack, otherwise it will be dynamically 48863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * allocated. 48963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 49063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com explicit SkAutoSMalloc(size_t size) { 49163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com fPtr = fStorage; 4927d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com this->reset(size); 49363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 49463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 49563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 49663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Free the allocated block (if any). If the block was small enought to 49763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * have been allocated on the stack (size <= kSize) then this does nothing. 49863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 49963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com ~SkAutoSMalloc() { 50063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com if (fPtr != (void*)fStorage) { 5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 50263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 50463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 50563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 50663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Return the allocated block. May return non-null even if the block is 50763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * of zero size. Since this may be on the stack or dynamically allocated, 50863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc 50963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * to manage it. 51063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 5118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 51263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 51363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 51463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Return a new block of the requested size, freeing (as necessary) any 51563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * previously allocated block. As with the constructor, if size <= kSize 51663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * then the return block may be allocated locally, rather than from the 51763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * heap. 51863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 5197d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com void* reset(size_t size) { 52063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com if (fPtr != (void*)fStorage) { 52163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com sk_free(fPtr); 52263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 52363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 52463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com if (size <= kSize) { 52563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com fPtr = fStorage; 52663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } else { 52763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); 52863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 52963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com return fPtr; 53063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 53163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 5328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 5338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 5348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint32_t fStorage[(kSize + 3) >> 2]; 5358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // illegal 5368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoSMalloc(const SkAutoSMalloc&); 5378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoSMalloc& operator=(const SkAutoSMalloc&); 5388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 5398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */ 5418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 543