SkTypes.h revision 95b96d649547c6b89ae0eca0f88f965d90c531a5
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project 38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkTypes_DEFINED 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkTypes_DEFINED 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman// IWYU pragma: begin_exports 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPreConfig.h" 138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUserConfig.h" 148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPostConfig.h" 15f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman#include <stddef.h> 16fab44db294846ff05d837b9cf0bf97a073891da7bungeman@google.com#include <stdint.h> 17ef3fcd877aa78c1d0ac802043cd8785180304c12bsalomon#include <sys/types.h> 188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1995cc012ccaea20f372893ae277ea0a8a6339d094mtklein#if defined(SK_ARM_HAS_NEON) 2095cc012ccaea20f372893ae277ea0a8a6339d094mtklein #include <arm_neon.h> 2195cc012ccaea20f372893ae277ea0a8a6339d094mtklein#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 2295cc012ccaea20f372893ae277ea0a8a6339d094mtklein #include <immintrin.h> 2395cc012ccaea20f372893ae277ea0a8a6339d094mtklein#endif 24f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman// IWYU pragma: end_exports 25f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman 26f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman#include <stdlib.h> 27f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman#include <string.h> 2895cc012ccaea20f372893ae277ea0a8a6339d094mtklein 298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkTypes.h 308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 329aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com/** See SkGraphics::GetVersion() to retrieve these at runtime 339aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com */ 349aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MAJOR 1 359aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MINOR 0 369aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_PATCH 0 379aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com 388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memory wrappers to be implemented by the porting layer (platform) 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we run out of memory. The platform implementation must 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com not return, but should either throw an exception or otherwise exit. 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 45de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_out_of_memory(void); 468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we hit an unrecoverable error. 478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The platform implementation must not return, but should either throw 488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com an exception or otherwise exit. 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 50de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_throw(void); 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum { 538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. 558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a block of memory (at least 4-byte aligned) of at least the 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com specified size. If the requested memory cannot be returned, either 58519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com return null (if SK_MALLOC_TEMP bit is clear) or throw an exception 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 617ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API extern void* sk_malloc_flags(size_t size, unsigned flags); 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 64de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_malloc_throw(size_t size); 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as standard realloc(), but this one never returns null on failure. It will throw 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com an exception if it fails. 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 68de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_realloc_throw(void* buffer, size_t size); 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Free memory returned by sk_malloc(). It is safe to pass null. 708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 71de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_free(void*); 728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 73519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure. 74519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com */ 75519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.comSK_API extern void* sk_calloc(size_t size); 76519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com 77519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com/** Same as sk_calloc, but throws an exception instead of returning NULL on failure. 78519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com */ 79519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.comSK_API extern void* sk_calloc_throw(size_t size); 80519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com 814516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com// bzero is safer than memset, but we can't rely on it, so... sk_bzero() 824516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.comstatic inline void sk_bzero(void* buffer, size_t size) { 834516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com memset(buffer, 0, size); 844516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com} 854516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com 86bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com/////////////////////////////////////////////////////////////////////////////// 87bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 8836352bf5e38f45a70ee4f4fc132a38048d38206dmtklein#ifdef override_GLOBAL_NEW 89bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#include <new> 90bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 91bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void* operator new(size_t size) { 92bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com return sk_malloc_throw(size); 93bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com} 94bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 95bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void operator delete(void* p) { 96bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com sk_free(p); 97bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com} 98bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#endif 99bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 100bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com/////////////////////////////////////////////////////////////////////////////// 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_INIT_TO_AVOID_WARNING = 0 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDebugf 105c4ade57cd4ce5083e0fac19111a6ee6e32e18df4george SK_API void SkDebugf(const char format[], ...); 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 10945d1d1d9a71253e0ed28ba87f5f946b5845f1d6dcommit-bot@chromium.org #define SkASSERT(cond) SK_ALWAYSBREAK(cond) 1100c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com #define SkDEBUGFAIL(message) SkASSERT(false && message) 1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGCODE(code) code 1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDECLAREPARAM(type, var) , type var 1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkPARAM(var) , var 1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// #define SkDEBUGF(args ) SkDebugf##args 1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGF(args ) SkDebugf args 1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkAssertResult(cond) SkASSERT(cond) 1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkASSERT(cond) 1190c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com #define SkDEBUGFAIL(message) 1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGCODE(code) 1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGF(args) 1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDECLAREPARAM(type, var) 1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkPARAM(var) 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // unlike SkASSERT, this guy executes its condition in the non-debug build 1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkAssertResult(cond) cond 1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 12945d1d1d9a71253e0ed28ba87f5f946b5845f1d6dcommit-bot@chromium.org#define SkFAIL(message) SK_ALWAYSBREAK(false && message) 13088cb22b6b4816c7a9ca6c5b795965b4606f9eb7bcommit-bot@chromium.org 131b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form. 132b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond, 133b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// and if it's true the assert passes; if it's false, we'll print the message and the assert fails. 134b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein#define SkASSERTF(cond, fmt, ...) SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false)) 135b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein 13676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#ifdef SK_DEVELOPER 13776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com #define SkDEVCODE(code) code 13876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#else 13976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com #define SkDEVCODE(code) 1400f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#endif 1410f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org 1420f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifdef SK_IGNORE_TO_STRING 143bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com #define SK_TO_STRING_NONVIRT() 144bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com #define SK_TO_STRING_VIRT() 1450f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org #define SK_TO_STRING_PUREVIRT() 1460f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org #define SK_TO_STRING_OVERRIDE() 1470f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#else 148d3ebb48320cf1b7e969974673e4bd7743816985ebungeman class SkString; 1490f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org // the 'toString' helper functions convert Sk* objects to human-readable 1500f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org // form in developer mode 1510f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const; 1520f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const; 1530f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0; 15436352bf5e38f45a70ee4f4fc132a38048d38206dmtklein #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override; 15576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif 15676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com 15728be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgtemplate <bool> 15828be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgstruct SkCompileAssert { 15928be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org}; 16028be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 161562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com// Uses static_cast<bool>(expr) instead of bool(expr) due to 162562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com// https://connect.microsoft.com/VisualStudio/feedback/details/832915 1639c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com 1649c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// The extra parentheses in SkCompileAssert<(...)> are a work around for 1659c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57771 1669c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// which was fixed in gcc 4.8.2. 16728be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org#define SK_COMPILE_ASSERT(expr, msg) \ 1682f5820903e98580ed9349fc5e3621f745911ad17bungeman@google.com typedef SkCompileAssert<(static_cast<bool>(expr))> \ 169562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com msg[static_cast<bool>(expr) ? 1 : -1] SK_UNUSED 17028be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 17149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/* 17249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab 17349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * 17449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly 17549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * 17649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */ 17749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y) 17849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y 17949a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com 18049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/* 18149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current 18249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * line number. Easy way to construct 18349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * unique names for local functions or 18449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * variables. 18549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */ 18649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__) 18749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com 188e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org/** 189e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * For some classes, it's almost always an error to instantiate one without a name, e.g. 190e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * { 191e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * SkAutoMutexAcquire(&mutex); 192e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * <some code> 193e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * } 194e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * In this case, the writer meant to hold mutex while the rest of the code in the block runs, 195e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * but instead the mutex is acquired and then immediately released. The correct usage is 196e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * { 197e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * SkAutoMutexAcquire lock(&mutex); 198e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * <some code> 199e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * } 200e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * 201e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR 202e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * like this: 203e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * class classname { 204e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * <your class> 205e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * }; 206e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname) 207e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * 208e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * This won't work with templates, and you must inline the class' constructors and destructors. 209e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * Take a look at SkAutoFree and SkAutoMalloc in this file for examples. 210e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org */ 211e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SK_REQUIRE_LOCAL_VAR(classname) \ 212e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org SK_COMPILE_ASSERT(false, missing_name_for_##classname) 213e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org 2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////// 2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 21637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 21737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for signed 8 bits. Use for parameter passing and local variables, 21837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * not for storage. 21937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 22037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S8CPU; 2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 22237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 22337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for unsigned 8 bits. Use for parameter passing and local 22437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * variables, not for storage 22537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 22637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U8CPU; 22737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 22837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 22937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for signed 16 bits. Use for parameter passing and local variables, 23037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * not for storage 23137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 23237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S16CPU; 23337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 23437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 23537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for unsigned 16 bits. Use for parameter passing and local 23637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * variables, not for storage 23737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 23837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U16CPU; 23937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 24037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 24137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Meant to be faster than bool (doesn't promise to be 0 or 1, 24237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * just 0 or non-zero 24337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 24437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int SkBool; 24537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 24637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 24737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Meant to be a small version of bool, for storage purposes. Will be 0 or 1 24837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 24937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef uint8_t SkBool8; 2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 252777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com SK_API int8_t SkToS8(intmax_t); 253777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com SK_API uint8_t SkToU8(uintmax_t); 254777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com SK_API int16_t SkToS16(intmax_t); 255777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com SK_API uint16_t SkToU16(uintmax_t); 256777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com SK_API int32_t SkToS32(intmax_t); 257777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com SK_API uint32_t SkToU32(uintmax_t); 258a8c7f7702fb4bbedb615031bc653c5cd161a038ecommit-bot@chromium.org SK_API int SkToInt(intmax_t); 2597fa2a65c0cfc714364490cb715171461143024e0reed@google.com SK_API unsigned SkToUInt(uintmax_t); 260490fb6b4713463954cc0283a9c30e754c45c6004commit-bot@chromium.org SK_API size_t SkToSizeT(uintmax_t); 261ef3fcd877aa78c1d0ac802043cd8785180304c12bsalomon SK_API off_t SkToOffT(intmax_t x); 2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS8(x) ((int8_t)(x)) 2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU8(x) ((uint8_t)(x)) 2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS16(x) ((int16_t)(x)) 2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU16(x) ((uint16_t)(x)) 2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS32(x) ((int32_t)(x)) 2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU32(x) ((uint32_t)(x)) 269a8c7f7702fb4bbedb615031bc653c5cd161a038ecommit-bot@chromium.org #define SkToInt(x) ((int)(x)) 2707fa2a65c0cfc714364490cb715171461143024e0reed@google.com #define SkToUInt(x) ((unsigned)(x)) 271490fb6b4713463954cc0283a9c30e754c45c6004commit-bot@chromium.org #define SkToSizeT(x) ((size_t)(x)) 272ef3fcd877aa78c1d0ac802043cd8785180304c12bsalomon #define SkToOffT(x) ((off_t)(x)) 2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition 2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkToBool(cond) ((cond) != 0) 2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16 32767 2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16 -32767 2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16 0xFFFF 2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16 0 2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32 0x7FFFFFFF 284594dd3cd78e2f970d53bb0934fbbb63b41e1d40ccaryclark@google.com#define SK_MinS32 -SK_MaxS32 2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32 0xFFFFFFFF 2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32 0 2870e51577a14f903ffeafa117a75954baeb173ffb9humper@google.com#define SK_NaN32 (1 << 31) 2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 289d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits 290d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 29190209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) { 292d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (int16_t)x == x; 293d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 294d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 295d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits 296d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 29790209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) { 298d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (uint16_t)x == x; 299d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 300d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 301d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com////////////////////////////////////////////////////////////////////////////// 3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_OFFSETOF 30356d3a23c3e0a53777fb25898951718d44dd9c027reed@google.com #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1) 3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 306fc00a7c5015ea534b0f28f8115bfb6253d275e1emtklein/** Returns the number of entries in an array (not a pointer) */ 307fc00a7c5015ea534b0f28f8115bfb6253d275e1emtkleintemplate <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N]; 30895b96d649547c6b89ae0eca0f88f965d90c531a5caryclark#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array))) 3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x) (((x) + 1) >> 1 << 1) 311c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign2(x) (0 == ((x) & 1)) 312c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com 3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x) (((x) + 3) >> 2 << 2) 314c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign4(x) (0 == ((x) & 3)) 3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 316c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkAlign8(x) (((x) + 7) >> 3 << 3) 317c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign8(x) (0 == ((x) & 7)) 31801224d5d0a3228fe47e63d8346e0e433a87563a8tomhudson@google.com 3190209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x)) 3200209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x)) 3210209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein 3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag; 3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) 3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value 3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar; 3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit value to hold a millisecond count 3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec; 3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds 3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000 3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** maximum representable milliseconds 3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF 3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) 3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) 3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3442b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org/** The generation IDs in Skia reserve 0 has an invalid marker. 3452b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org */ 3462b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org#define SK_InvalidGenID 0 3471c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon/** The unique IDs in Skia reserve 0 has an invalid marker. 3481c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon */ 3491c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon#define SK_InvalidUniqueID 0 3502b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org 3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/**************************************************************************** 3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The rest of these only build with C++ 3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus 3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1 3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 358d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int Sk32ToBool(uint32_t n) { 3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (n | (0-n)) >> 31; 3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 362ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com/** Generic swap function. Classes with efficient swaps should specialize this function to take 363ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com their fast path. This function is used by SkTSort. */ 364d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) { 3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com T c(a); 3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b = c; 3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 370d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) { 37109a22e9597e271c44dc7c2b71c72cf62a7de1e19mtklein SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated. 37238bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org if (value < 0) { 3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = -value; 37438bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org } 3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3782b57dc6bb241a6627c4375ee54b73039983d03dareed@google.comtemplate <typename T> inline T SkTAbs(T value) { 3792b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com if (value < 0) { 3802b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com value = -value; 3812b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com } 3822b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com return value; 3832b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com} 3842b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com 385d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) { 3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a < b) 3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 391d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) { 3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a > b) 3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3973b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.comtemplate <typename T> const T& SkTMin(const T& a, const T& b) { 3983b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com return (a < b) ? a : b; 3993b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com} 4003b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com 4013b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.comtemplate <typename T> const T& SkTMax(const T& a, const T& b) { 4023b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com return (b < a) ? a : b; 4033b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com} 4043b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com 405d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) { 4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (a >> 31) | ((unsigned) -a >> 31); 4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 409d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) { 41038bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org if (value > max) { 4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 41238bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org } 4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 4148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 416fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungemantemplate <typename T> static inline const T& SkTPin(const T& x, const T& min, const T& max) { 417fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungeman return SkTMax(SkTMin(x, max), min); 418fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungeman} 419fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungeman 420fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungeman/** Returns signed 32 bit value pinned between min and max, inclusively. */ 421d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { 422fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungeman return SkTPin(value, min, max); 4238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 425d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearShift(uint32_t bits, bool cond, 426d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com unsigned shift) { 4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT((int)cond == 0 || (int)cond == 1); 4288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (bits & ~(1 << shift)) | ((int)cond << shift); 4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 431d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearMask(uint32_t bits, bool cond, 432d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com uint32_t mask) { 4338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return cond ? bits | mask : bits & ~mask; 4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4361fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/////////////////////////////////////////////////////////////////////////////// 4371fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 438325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org/** Use to combine multiple bits in a bitmask in a type safe way. 439325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org */ 440325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgtemplate <typename T> 441325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgT SkTBitOr(T a, T b) { 442325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org return (T)(a | b); 443325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org} 444325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org 4451fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/** 4461fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com * Use to cast a pointer to a different type, and maintaining strict-aliasing 4471fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com */ 4481fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.comtemplate <typename Dst> Dst SkTCast(const void* ptr) { 4491fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com union { 4501fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com const void* src; 4511fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com Dst dst; 4521fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com } data; 4531fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com data.src = ptr; 4541fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com return data.dst; 4551fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com} 4561fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 4578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable 4608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 461055f6b59d879b2adac52748ea5a58c8a05bf501cfmalitaSkNoncopyable is the base class for objects that do not want to 4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator. 4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 4647ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkNoncopyable { 4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable() {} 4671fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 4698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable(const SkNoncopyable&); 4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable& operator=(const SkNoncopyable&); 4718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable { 4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 4758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree() : fPtr(NULL) {} 4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoFree(void* ptr) : fPtr(ptr) {} 4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ~SkAutoFree() { sk_free(fPtr); } 4781fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Return the currently allocate buffer, or null 4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Assign a new ptr allocated with sk_malloc (or null), and return the 4848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com previous ptr. Note it is the caller's responsibility to sk_free the 4858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com returned ptr. 4868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 4878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* set(void* ptr) { 4888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* prev = fPtr; 4898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = ptr; 4908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return prev; 4918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 4921fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 4938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Transfer ownership of the current ptr to the caller, setting the 4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com internal reference to null. Note the caller is reponsible for calling 4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free on the returned address. 4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* detach() { return this->set(NULL); } 4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Free the current buffer, and set the internal reference to NULL. Same 5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com as calling sk_free(detach()) 5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void free() { 5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = NULL; 5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // illegal 5108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree(const SkAutoFree&); 5118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree& operator=(const SkAutoFree&); 5128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 513e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree) 5148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5157d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com/** 5167d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Manage an allocated block of heap memory. This object is the sole manager of 5177d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * the lifetime of the block, so the caller must not call sk_free() or delete 5181c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * on the block, unless detach() was called. 5197d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 520e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.orgclass SkAutoMalloc : SkNoncopyable { 5218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 5223ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com explicit SkAutoMalloc(size_t size = 0) { 5233ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com fPtr = size ? sk_malloc_throw(size) : NULL; 5247d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com fSize = size; 5257d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 5268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5277d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com ~SkAutoMalloc() { 5287d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com sk_free(fPtr); 5297d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 5308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5317d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com /** 5321c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * Passed to reset to specify what happens if the requested size is smaller 5331c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * than the current size (and the current block was dynamically allocated). 5341c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com */ 5351c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com enum OnShrink { 5361c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com /** 5371c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * If the requested size is smaller than the current size, and the 5381c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * current block is dynamically allocated, free the old block and 5391c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * malloc a new block of the smaller size. 5401c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com */ 5411c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com kAlloc_OnShrink, 542fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 5431c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com /** 5441c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * If the requested size is smaller than the current size, and the 5451c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * current block is dynamically allocated, just return the old 5461c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * block. 5471c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com */ 5481f90287df3129cb267422e482c52ebeca6a8990ftomhudson@google.com kReuse_OnShrink 5491c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com }; 5501c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com 5511c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com /** 5527d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Reallocates the block to a new size. The ptr may or may not change. 5537d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 5549eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) { 5551c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) { 55649f085dddff10473b6ebf832a974288300224e60bsalomon if (didChangeAlloc) { 5579eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com *didChangeAlloc = false; 5589eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com } 5591c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com return fPtr; 5607d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 5611c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com 5621c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com sk_free(fPtr); 5631c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com fPtr = size ? sk_malloc_throw(size) : NULL; 5641c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com fSize = size; 56549f085dddff10473b6ebf832a974288300224e60bsalomon if (didChangeAlloc) { 5669eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com *didChangeAlloc = true; 5679eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com } 5681c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com 5697d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com return fPtr; 5708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 5717d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com 5727d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com /** 5737d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Releases the block back to the heap 5747d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 5757d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com void free() { 5767d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com this->reset(0); 5777d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 5787d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com 5797d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com /** 5807d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Return the allocated block. 5817d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 5827d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com void* get() { return fPtr; } 5837d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com const void* get() const { return fPtr; } 5847d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com 5856dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com /** Transfer ownership of the current ptr to the caller, setting the 5866dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com internal reference to null. Note the caller is reponsible for calling 5876dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com sk_free on the returned address. 5886dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com */ 5896dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com void* detach() { 5906dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com void* ptr = fPtr; 5916dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com fPtr = NULL; 5926dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com fSize = 0; 5936dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com return ptr; 5946dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com } 5956dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com 5967d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comprivate: 5977d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com void* fPtr; 5981c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com size_t fSize; // can be larger than the requested size (see kReuse) 5998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 600e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc) 6018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 60263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com/** 60363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Manage an allocated block of memory. If the requested size is <= kSize, then 60463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * the allocation will come from the stack rather than the heap. This object 60563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * is the sole manager of the lifetime of the block, so the caller must not 60663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * call sk_free() or delete on the block. 60763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 6088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <size_t kSize> class SkAutoSMalloc : SkNoncopyable { 6098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 61063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 61163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Creates initially empty storage. get() returns a ptr, but it is to 6127d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * a zero-byte allocation. Must call reset(size) to return an allocated 61363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * block. 61463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 61563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com SkAutoSMalloc() { 61663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com fPtr = fStorage; 6179eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com fSize = kSize; 6188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 61963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 62063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 62163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Allocate a block of the specified size. If size <= kSize, then the 62263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * allocation will come from the stack, otherwise it will be dynamically 62363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * allocated. 62463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 62563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com explicit SkAutoSMalloc(size_t size) { 62663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com fPtr = fStorage; 6279eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com fSize = kSize; 6287d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com this->reset(size); 62963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 63063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 63163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 63263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Free the allocated block (if any). If the block was small enought to 63363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * have been allocated on the stack (size <= kSize) then this does nothing. 63463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 63563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com ~SkAutoSMalloc() { 63663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com if (fPtr != (void*)fStorage) { 6378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 63863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 6398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 64063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 64163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 64263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Return the allocated block. May return non-null even if the block is 64363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * of zero size. Since this may be on the stack or dynamically allocated, 64463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc 64563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * to manage it. 64663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 6478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 64863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 64963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 65063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * Return a new block of the requested size, freeing (as necessary) any 65163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * previously allocated block. As with the constructor, if size <= kSize 65263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * then the return block may be allocated locally, rather than from the 65363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com * heap. 65463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 6551c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com void* reset(size_t size, 6569eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink, 6579eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com bool* didChangeAlloc = NULL) { 6589eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com size = (size < kSize) ? kSize : size; 6590f2b1953c7a8f8bb5a25d573dd9e215eaa10a2f8robertphillips@google.com bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize); 66049f085dddff10473b6ebf832a974288300224e60bsalomon if (didChangeAlloc) { 6619eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com *didChangeAlloc = alloc; 6621c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com } 6639eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com if (alloc) { 6649eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com if (fPtr != (void*)fStorage) { 6659eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com sk_free(fPtr); 6669eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com } 6679eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com 6689eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com if (size == kSize) { 6699eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc. 6709eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com fPtr = fStorage; 6719eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com } else { 6729eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); 6739eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com } 6749eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com 6759eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com fSize = size; 67663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 6779eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com SkASSERT(fSize >= size && fSize >= kSize); 6789eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com SkASSERT((fPtr == fStorage) || fSize > kSize); 67963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com return fPtr; 68063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 68163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 6828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 6838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 6841c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com size_t fSize; // can be larger than the requested size (see kReuse) 6858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint32_t fStorage[(kSize + 3) >> 2]; 6868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 687e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org// Can't guard the constructor because it's a template class. 6888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */ 6908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 692