SkTypes.h revision 7a5bcc5f59bdf122217bc8ca7e756e8c76bae9e1
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> 178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1895cc012ccaea20f372893ae277ea0a8a6339d094mtklein#if defined(SK_ARM_HAS_NEON) 1995cc012ccaea20f372893ae277ea0a8a6339d094mtklein #include <arm_neon.h> 2095cc012ccaea20f372893ae277ea0a8a6339d094mtklein#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 2195cc012ccaea20f372893ae277ea0a8a6339d094mtklein #include <immintrin.h> 2295cc012ccaea20f372893ae277ea0a8a6339d094mtklein#endif 23f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman// IWYU pragma: end_exports 24f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman 25f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman#include <string.h> 2695cc012ccaea20f372893ae277ea0a8a6339d094mtklein 27cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein/** 28cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior. 29cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * 30cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * It is undefined behavior to call memcpy() with null dst or src, even if len is 0. 31cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * If an optimizer is "smart" enough, it can exploit this to do unexpected things. 32cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * memcpy(dst, src, 0); 33cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * if (src) { 34cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * printf("%x\n", *src); 35cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * } 36cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * In this code the compiler can assume src is not null and omit the if (src) {...} check, 37cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * unconditionally running the printf, crashing the program if src really is null. 38cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * Of the compilers we pay attention to only GCC performs this optimization in practice. 39cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein */ 40cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtkleinstatic inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) { 41cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein // When we pass >0 len we had better already be passing valid pointers. 42cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein // So we just need to skip calling memcpy when len == 0. 43cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein if (len) { 44cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein memcpy(dst,src,len); 45cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein } 46cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein return dst; 47cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein} 48cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkTypes.h 508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 529aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com/** See SkGraphics::GetVersion() to retrieve these at runtime 539aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com */ 549aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MAJOR 1 559aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MINOR 0 569aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_PATCH 0 579aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memory wrappers to be implemented by the porting layer (platform) 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we run out of memory. The platform implementation must 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com not return, but should either throw an exception or otherwise exit. 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 65de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_out_of_memory(void); 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we hit an unrecoverable error. 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The platform implementation must not return, but should either throw 688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com an exception or otherwise exit. 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 70f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollenSK_API extern void sk_abort_no_print(void); 718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum { 738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame 748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. 758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a block of memory (at least 4-byte aligned) of at least the 778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com specified size. If the requested memory cannot be returned, either 78519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com return null (if SK_MALLOC_TEMP bit is clear) or throw an exception 798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). 808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 817ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API extern void* sk_malloc_flags(size_t size, unsigned flags); 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag 838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 84de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_malloc_throw(size_t size); 858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as standard realloc(), but this one never returns null on failure. It will throw 868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com an exception if it fails. 878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 88de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_realloc_throw(void* buffer, size_t size); 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Free memory returned by sk_malloc(). It is safe to pass null. 908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 91de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_free(void*); 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 93519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure. 94519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com */ 95519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.comSK_API extern void* sk_calloc(size_t size); 96519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com 97519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com/** Same as sk_calloc, but throws an exception instead of returning NULL on failure. 98519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com */ 99519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.comSK_API extern void* sk_calloc_throw(size_t size); 100519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com 1014516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com// bzero is safer than memset, but we can't rely on it, so... sk_bzero() 1024516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.comstatic inline void sk_bzero(void* buffer, size_t size) { 10302046c50b294ae2b28e562b0e6e281e4ef823352mtklein // Please c.f. sk_careful_memcpy. It's undefined behavior to call memset(null, 0, 0). 10402046c50b294ae2b28e562b0e6e281e4ef823352mtklein if (size) { 10502046c50b294ae2b28e562b0e6e281e4ef823352mtklein memset(buffer, 0, size); 10602046c50b294ae2b28e562b0e6e281e4ef823352mtklein } 1074516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com} 1084516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com 109bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com/////////////////////////////////////////////////////////////////////////////// 110bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 11136352bf5e38f45a70ee4f4fc132a38048d38206dmtklein#ifdef override_GLOBAL_NEW 112bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#include <new> 113bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 114bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void* operator new(size_t size) { 115bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com return sk_malloc_throw(size); 116bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com} 117bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 118bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void operator delete(void* p) { 119bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com sk_free(p); 120bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com} 121bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#endif 122bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com 123bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com/////////////////////////////////////////////////////////////////////////////// 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_INIT_TO_AVOID_WARNING = 0 1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDebugf 128c4ade57cd4ce5083e0fac19111a6ee6e32e18df4george SK_API void SkDebugf(const char format[], ...); 1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 131f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollen#define SkASSERT_RELEASE(cond) if(!(cond)) { SK_ABORT(#cond); } 132f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollen 1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 134f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollen #define SkASSERT(cond) SkASSERT_RELEASE(cond) 1350c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com #define SkDEBUGFAIL(message) SkASSERT(false && message) 136966e3d30baf676dc399dc9c97ef15cc2c4f8d461herb #define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__) 1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGCODE(code) code 1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDECLAREPARAM(type, var) , type var 1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkPARAM(var) , var 1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// #define SkDEBUGF(args ) SkDebugf##args 1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGF(args ) SkDebugf args 1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkAssertResult(cond) SkASSERT(cond) 1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkASSERT(cond) 1450c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com #define SkDEBUGFAIL(message) 1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGCODE(code) 1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGF(args) 1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDECLAREPARAM(type, var) 1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkPARAM(var) 1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1519daa4b92d73a1ae441c864a0ae73dda6c3397acbbsalomon // unlike SkASSERT, this guy executes its condition in the non-debug build. 1521b4c01c66081cd163535eab21b29a3996b01a6cfbsalomon // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT. 1531b4c01c66081cd163535eab21b29a3996b01a6cfbsalomon #define SkAssertResult(cond) if (cond) {} do {} while(false) 1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 156f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollen// Legacy macro names for SK_ABORT 157f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollen#define SkFAIL(message) SK_ABORT(message) 158f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollen#define sk_throw() SK_ABORT("sk_throw") 15988cb22b6b4816c7a9ca6c5b795965b4606f9eb7bcommit-bot@chromium.org 160b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form. 161b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond, 162b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// and if it's true the assert passes; if it's false, we'll print the message and the assert fails. 163b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein#define SkASSERTF(cond, fmt, ...) SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false)) 164b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein 1650f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifdef SK_IGNORE_TO_STRING 166bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com #define SK_TO_STRING_NONVIRT() 167bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com #define SK_TO_STRING_VIRT() 1680f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org #define SK_TO_STRING_PUREVIRT() 1690f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org #define SK_TO_STRING_OVERRIDE() 1700f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#else 171d3ebb48320cf1b7e969974673e4bd7743816985ebungeman class SkString; 1720f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org // the 'toString' helper functions convert Sk* objects to human-readable 1730f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org // form in developer mode 1740f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const; 1750f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const; 1760f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0; 17736352bf5e38f45a70ee4f4fc132a38048d38206dmtklein #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override; 17876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif 17976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com 18049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/* 18149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab 18249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * 18349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly 18449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * 18549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */ 18649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y) 18749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y 18849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com 18949a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/* 19049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current 19149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * line number. Easy way to construct 19249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * unique names for local functions or 19349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com * variables. 19449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */ 19549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__) 19649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com 197e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org/** 198e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * For some classes, it's almost always an error to instantiate one without a name, e.g. 199e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * { 200e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * SkAutoMutexAcquire(&mutex); 201e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * <some code> 202e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * } 203e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * In this case, the writer meant to hold mutex while the rest of the code in the block runs, 204e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * but instead the mutex is acquired and then immediately released. The correct usage is 205e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * { 206e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * SkAutoMutexAcquire lock(&mutex); 207e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * <some code> 208e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * } 209e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * 210e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR 211e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * like this: 212e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * class classname { 213e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * <your class> 214e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * }; 215e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname) 216e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * 217e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * This won't work with templates, and you must inline the class' constructors and destructors. 218e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * Take a look at SkAutoFree and SkAutoMalloc in this file for examples. 219e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org */ 220e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SK_REQUIRE_LOCAL_VAR(classname) \ 22199fe82260633fcf5d92cca38d12ef0937ecca61cbungeman static_assert(false, "missing name for " #classname) 222e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org 2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////// 2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 22537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 22637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for signed 8 bits. Use for parameter passing and local variables, 22737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * not for storage. 22837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 22937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S8CPU; 2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 23137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 23237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for unsigned 8 bits. Use for parameter passing and local 23337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * variables, not for storage 23437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 23537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U8CPU; 23637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 23737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 23837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for signed 16 bits. Use for parameter passing and local variables, 23937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * not for storage 24037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 24137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S16CPU; 24237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 24337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 24437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Fast type for unsigned 16 bits. Use for parameter passing and local 24537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * variables, not for storage 24637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 24737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U16CPU; 24837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com 24937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/** 25037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com * Meant to be a small version of bool, for storage purposes. Will be 0 or 1 25137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */ 25237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef uint8_t SkBool8; 2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 25468c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#include "../private/SkTFitsIn.h" 25568c14d9b3244ef9d58727ff11f2742be9236f46ebungemantemplate <typename D, typename S> D SkTo(S s) { 25668c14d9b3244ef9d58727ff11f2742be9236f46ebungeman SkASSERT(SkTFitsIn<D>(s)); 25768c14d9b3244ef9d58727ff11f2742be9236f46ebungeman return static_cast<D>(s); 25868c14d9b3244ef9d58727ff11f2742be9236f46ebungeman} 25968c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToS8(x) SkTo<int8_t>(x) 26068c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToU8(x) SkTo<uint8_t>(x) 26168c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToS16(x) SkTo<int16_t>(x) 26268c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToU16(x) SkTo<uint16_t>(x) 26368c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToS32(x) SkTo<int32_t>(x) 26468c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToU32(x) SkTo<uint32_t>(x) 26568c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToInt(x) SkTo<int>(x) 26668c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToUInt(x) SkTo<unsigned>(x) 26768c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToSizeT(x) SkTo<size_t>(x) 2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition 2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2715c05d10511610c35f7912d48ede509c7e32e0a3cmtklein#define SkToBool(cond) ((cond) != 0) 2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16 32767 2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16 -32767 2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16 0xFFFF 2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16 0 2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32 0x7FFFFFFF 278594dd3cd78e2f970d53bb0934fbbb63b41e1d40ccaryclark@google.com#define SK_MinS32 -SK_MaxS32 2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32 0xFFFFFFFF 2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32 0 281952538ed50661ad7dff6ec2b7af3f921e1d91b52caryclark#define SK_NaN32 ((int) (1U << 31)) 2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 283d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits 284d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 28590209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) { 286d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (int16_t)x == x; 287d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 288d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 289d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits 290d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 29190209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) { 292d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (uint16_t)x == x; 293d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 294d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 2953127c99986dc932343aae5ccc575237d99c3aaeccaryclarkstatic inline int32_t SkLeftShift(int32_t value, int32_t shift) { 2963127c99986dc932343aae5ccc575237d99c3aaeccaryclark return (int32_t) ((uint32_t) value << shift); 2973127c99986dc932343aae5ccc575237d99c3aaeccaryclark} 2983127c99986dc932343aae5ccc575237d99c3aaeccaryclark 2993127c99986dc932343aae5ccc575237d99c3aaeccaryclarkstatic inline int64_t SkLeftShift(int64_t value, int32_t shift) { 3003127c99986dc932343aae5ccc575237d99c3aaeccaryclark return (int64_t) ((uint64_t) value << shift); 3013127c99986dc932343aae5ccc575237d99c3aaeccaryclark} 3023127c99986dc932343aae5ccc575237d99c3aaeccaryclark 303d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com////////////////////////////////////////////////////////////////////////////// 3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 305fc00a7c5015ea534b0f28f8115bfb6253d275e1emtklein/** Returns the number of entries in an array (not a pointer) */ 306fc00a7c5015ea534b0f28f8115bfb6253d275e1emtkleintemplate <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N]; 30795b96d649547c6b89ae0eca0f88f965d90c531a5caryclark#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array))) 3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 309b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein// Can be used to bracket data types that must be dense, e.g. hash keys. 310b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein#if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work! 311b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \ 312b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein _Pragma("GCC diagnostic error \"-Wpadded\"") 313b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein #define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop") 314b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein#else 315b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein #define SK_BEGIN_REQUIRE_DENSE 316b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein #define SK_END_REQUIRE_DENSE 317b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein#endif 318b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein 3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x) (((x) + 1) >> 1 << 1) 320c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign2(x) (0 == ((x) & 1)) 321c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com 3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x) (((x) + 3) >> 2 << 2) 323c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign4(x) (0 == ((x) & 3)) 3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 325c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkAlign8(x) (((x) + 7) >> 3 << 3) 326c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign8(x) (0 == ((x) & 7)) 32701224d5d0a3228fe47e63d8346e0e433a87563a8tomhudson@google.com 328e2b0a0a0349b1c2acbd1eb5c1da7eed012f0980dreed#define SkAlign16(x) (((x) + 15) >> 4 << 4) 329e2b0a0a0349b1c2acbd1eb5c1da7eed012f0980dreed#define SkIsAlign16(x) (0 == ((x) & 15)) 330e2b0a0a0349b1c2acbd1eb5c1da7eed012f0980dreed 3310209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x)) 3320209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x)) 3330209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein 3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag; 3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) 3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value 3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar; 340ec4d4d784dbb250e572f8e04d18d0fd2ebeee851benjaminwagner 341ec4d4d784dbb250e572f8e04d18d0fd2ebeee851benjaminwagner/** 32 bit value to hold a millisecond duration 342ec4d4d784dbb250e572f8e04d18d0fd2ebeee851benjaminwagner * Note that SK_MSecMax is about 25 days. 343ec4d4d784dbb250e572f8e04d18d0fd2ebeee851benjaminwagner */ 3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec; 3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds 3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000 348ec4d4d784dbb250e572f8e04d18d0fd2ebeee851benjaminwagner/** maximum representable milliseconds; 24d 20h 31m 23.647s. 3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF 3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) 3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) 3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3582b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org/** The generation IDs in Skia reserve 0 has an invalid marker. 3592b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org */ 3602b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org#define SK_InvalidGenID 0 3611c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon/** The unique IDs in Skia reserve 0 has an invalid marker. 3621c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon */ 3631c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon#define SK_InvalidUniqueID 0 3642b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org 3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/**************************************************************************** 3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The rest of these only build with C++ 3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus 3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1 3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3727a5bcc5f59bdf122217bc8ca7e756e8c76bae9e1bsalomonstatic inline constexpr int Sk32ToBool(uint32_t n) { 3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (n | (0-n)) >> 31; 3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 376ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com/** Generic swap function. Classes with efficient swaps should specialize this function to take 377ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com their fast path. This function is used by SkTSort. */ 378d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) { 3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com T c(a); 3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b = c; 3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 384d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) { 38509a22e9597e271c44dc7c2b71c72cf62a7de1e19mtklein SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated. 38638bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org if (value < 0) { 3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = -value; 38838bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org } 3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3922b57dc6bb241a6627c4375ee54b73039983d03dareed@google.comtemplate <typename T> inline T SkTAbs(T value) { 3932b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com if (value < 0) { 3942b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com value = -value; 3952b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com } 3962b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com return value; 3972b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com} 3982b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com 399d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) { 4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a < b) 4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 405d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) { 4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a > b) 4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 411a0af771612db5ae36b74f1f536bfb335ecd0ec99halcanarytemplate <typename T> constexpr const T& SkTMin(const T& a, const T& b) { 4123b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com return (a < b) ? a : b; 4133b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com} 4143b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com 415a0af771612db5ae36b74f1f536bfb335ecd0ec99halcanarytemplate <typename T> constexpr const T& SkTMax(const T& a, const T& b) { 4163b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com return (b < a) ? a : b; 4173b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com} 4183b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com 419d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) { 4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (a >> 31) | ((unsigned) -a >> 31); 4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 423d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) { 42438bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org if (value > max) { 4258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 42638bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org } 4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 4288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 43062ce0303fb3f9857ee3ab8df05934642c226b1a3bungeman/** Returns value pinned between min and max, inclusively. */ 431a0af771612db5ae36b74f1f536bfb335ecd0ec99halcanarytemplate <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) { 43262ce0303fb3f9857ee3ab8df05934642c226b1a3bungeman return SkTMax(SkTMin(value, max), min); 4338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4355ec26ae9bfca635ccc98283aad5deda11519d826bsalomon 4365ec26ae9bfca635ccc98283aad5deda11519d826bsalomon/////////////////////////////////////////////////////////////////////////////// 4375ec26ae9bfca635ccc98283aad5deda11519d826bsalomon 4385ec26ae9bfca635ccc98283aad5deda11519d826bsalomon/** 4395ec26ae9bfca635ccc98283aad5deda11519d826bsalomon * Indicates whether an allocation should count against a cache budget. 4405ec26ae9bfca635ccc98283aad5deda11519d826bsalomon */ 4415ec26ae9bfca635ccc98283aad5deda11519d826bsalomonenum class SkBudgeted : bool { 4425ec26ae9bfca635ccc98283aad5deda11519d826bsalomon kNo = false, 4435ec26ae9bfca635ccc98283aad5deda11519d826bsalomon kYes = true 4445ec26ae9bfca635ccc98283aad5deda11519d826bsalomon}; 4455ec26ae9bfca635ccc98283aad5deda11519d826bsalomon 44676948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips/** 44776948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips * Indicates whether a backing store needs to be an exact match or can be larger 44876948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips * than is strictly necessary 44976948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips */ 45076948d4faaca9fd7730576e2f79790ca8d93c10brobertphillipsenum class SkBackingFit { 45176948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips kApprox, 45276948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips kExact 45376948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips}; 45476948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips 4551fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/////////////////////////////////////////////////////////////////////////////// 4561fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 457325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org/** Use to combine multiple bits in a bitmask in a type safe way. 458325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org */ 459325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgtemplate <typename T> 460325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgT SkTBitOr(T a, T b) { 461325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org return (T)(a | b); 462325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org} 463325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org 4641fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/** 4651fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com * Use to cast a pointer to a different type, and maintaining strict-aliasing 4661fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com */ 4671fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.comtemplate <typename Dst> Dst SkTCast(const void* ptr) { 4681fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com union { 4691fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com const void* src; 4701fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com Dst dst; 4711fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com } data; 4721fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com data.src = ptr; 4731fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com return data.dst; 4741fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com} 4751fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable 4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 480055f6b59d879b2adac52748ea5a58c8a05bf501cfmalitaSkNoncopyable is the base class for objects that do not want to 4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator. 4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 4837ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkNoncopyable { 4848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 4858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable() {} 4861fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 4878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 4888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable(const SkNoncopyable&); 4898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable& operator=(const SkNoncopyable&); 4908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 4918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable { 4938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree() : fPtr(NULL) {} 4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoFree(void* ptr) : fPtr(ptr) {} 4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ~SkAutoFree() { sk_free(fPtr); } 4971fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Return the currently allocate buffer, or null 4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Assign a new ptr allocated with sk_malloc (or null), and return the 5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com previous ptr. Note it is the caller's responsibility to sk_free the 5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com returned ptr. 5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* set(void* ptr) { 5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* prev = fPtr; 5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = ptr; 5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return prev; 5108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 5111fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com 5128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Transfer ownership of the current ptr to the caller, setting the 5138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com internal reference to null. Note the caller is reponsible for calling 5148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free on the returned address. 5158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 51618300a3aa7cb6eb55d21bb0450dffa58b6fc062cmtklein void* release() { return this->set(NULL); } 5178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Free the current buffer, and set the internal reference to NULL. Same 51918300a3aa7cb6eb55d21bb0450dffa58b6fc062cmtklein as calling sk_free(release()) 5208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 521852f15da7ceb53cfb49b9f728baa6dbc53b27694mtklein void reset() { 5228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 5238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = NULL; 5248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 5258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 5278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 5288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // illegal 5298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree(const SkAutoFree&); 5308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree& operator=(const SkAutoFree&); 5318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 532e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree) 5338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5347d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com/** 5357d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Manage an allocated block of heap memory. This object is the sole manager of 5367d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * the lifetime of the block, so the caller must not call sk_free() or delete 53718300a3aa7cb6eb55d21bb0450dffa58b6fc062cmtklein * on the block, unless release() was called. 5387d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 539e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.orgclass SkAutoMalloc : SkNoncopyable { 5408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 5413ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com explicit SkAutoMalloc(size_t size = 0) { 5423ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com fPtr = size ? sk_malloc_throw(size) : NULL; 5437d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com fSize = size; 5447d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 5458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5467d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com ~SkAutoMalloc() { 5477d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com sk_free(fPtr); 5487d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 5498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5507d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com /** 5511c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * Passed to reset to specify what happens if the requested size is smaller 5521c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * than the current size (and the current block was dynamically allocated). 5531c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com */ 5541c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com enum OnShrink { 5551c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com /** 5561c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * If the requested size is smaller than the current size, and the 5571c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * current block is dynamically allocated, free the old block and 5581c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * malloc a new block of the smaller size. 5591c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com */ 5601c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com kAlloc_OnShrink, 561fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 5621c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com /** 5631c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * If the requested size is smaller than the current size, and the 5641c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * current block is dynamically allocated, just return the old 5651c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com * block. 5661c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com */ 5671f90287df3129cb267422e482c52ebeca6a8990ftomhudson@google.com kReuse_OnShrink 5681c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com }; 5691c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com 5701c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com /** 5717d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Reallocates the block to a new size. The ptr may or may not change. 5727d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 573852f15da7ceb53cfb49b9f728baa6dbc53b27694mtklein void* reset(size_t size = 0, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) { 5741c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) { 57549f085dddff10473b6ebf832a974288300224e60bsalomon if (didChangeAlloc) { 5769eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com *didChangeAlloc = false; 5779eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com } 5781c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com return fPtr; 5797d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com } 5801c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com 5811c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com sk_free(fPtr); 5821c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com fPtr = size ? sk_malloc_throw(size) : NULL; 5831c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com fSize = size; 58449f085dddff10473b6ebf832a974288300224e60bsalomon if (didChangeAlloc) { 5859eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com *didChangeAlloc = true; 5869eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com } 5871c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com 5887d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com return fPtr; 5898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 5907d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com 5917d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com /** 5927d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com * Return the allocated block. 5937d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */ 5947d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com void* get() { return fPtr; } 5957d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com const void* get() const { return fPtr; } 5967d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com 5976dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com /** Transfer ownership of the current ptr to the caller, setting the 5986dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com internal reference to null. Note the caller is reponsible for calling 5996dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com sk_free on the returned address. 6006dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com */ 60118300a3aa7cb6eb55d21bb0450dffa58b6fc062cmtklein void* release() { 6026dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com void* ptr = fPtr; 6036dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com fPtr = NULL; 6046dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com fSize = 0; 6056dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com return ptr; 6066dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com } 6076dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com 6087d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comprivate: 6097d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com void* fPtr; 6101c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com size_t fSize; // can be larger than the requested size (see kReuse) 6118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 612e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc) 6138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 61463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com/** 615f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * Manage an allocated block of memory. If the requested size is <= kSizeRequested (or slightly 616f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * more), then the allocation will come from the stack rather than the heap. This object is the 617f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * sole manager of the lifetime of the block, so the caller must not call sk_free() or delete on 618f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * the block. 61963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 620f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagnertemplate <size_t kSizeRequested> class SkAutoSMalloc : SkNoncopyable { 6218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 62263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 623f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * Creates initially empty storage. get() returns a ptr, but it is to a zero-byte allocation. 624f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * Must call reset(size) to return an allocated block. 62563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 62663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com SkAutoSMalloc() { 62763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com fPtr = fStorage; 6289eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com fSize = kSize; 6298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 63063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 63163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 632f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * Allocate a block of the specified size. If size <= kSizeRequested (or slightly more), then 633f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * the allocation will come from the stack, otherwise it will be dynamically allocated. 63463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 63563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com explicit SkAutoSMalloc(size_t size) { 63663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com fPtr = fStorage; 6379eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com fSize = kSize; 6387d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com this->reset(size); 63963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 64063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 64163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 642f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * Free the allocated block (if any). If the block was small enough to have been allocated on 643f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * the stack, then this does nothing. 64463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 64563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com ~SkAutoSMalloc() { 64663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com if (fPtr != (void*)fStorage) { 6478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 64863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 6498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 65063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 65163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 652f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * Return the allocated block. May return non-null even if the block is of zero size. Since 653f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * this may be on the stack or dynamically allocated, the caller must not call sk_free() on it, 654f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * but must rely on SkAutoSMalloc to manage it. 65563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 6568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 65763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 65863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com /** 659f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * Return a new block of the requested size, freeing (as necessary) any previously allocated 660f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * block. As with the constructor, if size <= kSizeRequested (or slightly more) then the return 661f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner * block may be allocated locally, rather than from the heap. 66263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */ 6631c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com void* reset(size_t size, 6649eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink, 6659eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com bool* didChangeAlloc = NULL) { 6669eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com size = (size < kSize) ? kSize : size; 6670f2b1953c7a8f8bb5a25d573dd9e215eaa10a2f8robertphillips@google.com bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize); 66849f085dddff10473b6ebf832a974288300224e60bsalomon if (didChangeAlloc) { 6699eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com *didChangeAlloc = alloc; 6701c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com } 6719eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com if (alloc) { 6729eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com if (fPtr != (void*)fStorage) { 6739eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com sk_free(fPtr); 6749eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com } 6759eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com 6769eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com if (size == kSize) { 6779eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc. 6789eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com fPtr = fStorage; 6799eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com } else { 6809eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); 6819eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com } 6829eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com 6839eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com fSize = size; 68463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 6859eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com SkASSERT(fSize >= size && fSize >= kSize); 6869eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com SkASSERT((fPtr == fStorage) || fSize > kSize); 68763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com return fPtr; 68863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com } 68963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com 6908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 691f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner // Align up to 32 bits. 692f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner static const size_t kSizeAlign4 = SkAlign4(kSizeRequested); 693f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner#if defined(GOOGLE3) 694f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner // Stack frame size is limited for GOOGLE3. 4k is less than the actual max, but some functions 695f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner // have multiple large stack allocations. 696f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner static const size_t kMaxBytes = 4 * 1024; 697f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner static const size_t kSize = kSizeRequested > kMaxBytes ? kMaxBytes : kSizeAlign4; 698f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner#else 699f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner static const size_t kSize = kSizeAlign4; 700f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner#endif 701f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner 7028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 7031c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com size_t fSize; // can be larger than the requested size (see kReuse) 704f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner uint32_t fStorage[kSize >> 2]; 7058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 706e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org// Can't guard the constructor because it's a template class. 7078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 7088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */ 7098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 7108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 711