SkTypes.h revision 95cc012ccaea20f372893ae277ea0a8a6339d094
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
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPreConfig.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUserConfig.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPostConfig.h"
14fab44db294846ff05d837b9cf0bf97a073891da7bungeman@google.com#include <stdint.h>
15ef3fcd877aa78c1d0ac802043cd8785180304c12bsalomon#include <sys/types.h>
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1795cc012ccaea20f372893ae277ea0a8a6339d094mtklein#if defined(SK_ARM_HAS_NEON)
1895cc012ccaea20f372893ae277ea0a8a6339d094mtklein    #include <arm_neon.h>
1995cc012ccaea20f372893ae277ea0a8a6339d094mtklein#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
2095cc012ccaea20f372893ae277ea0a8a6339d094mtklein    #include <immintrin.h>
2195cc012ccaea20f372893ae277ea0a8a6339d094mtklein#endif
2295cc012ccaea20f372893ae277ea0a8a6339d094mtklein
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkTypes.h
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
269aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com/** See SkGraphics::GetVersion() to retrieve these at runtime
279aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com */
289aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MAJOR  1
299aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MINOR  0
309aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_PATCH  0
319aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memory wrappers to be implemented by the porting layer (platform)
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we run out of memory. The platform implementation must
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    not return, but should either throw an exception or otherwise exit.
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
39de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_out_of_memory(void);
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we hit an unrecoverable error.
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The platform implementation must not return, but should either throw
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    an exception or otherwise exit.
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
44de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_throw(void);
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum {
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MALLOC_TEMP  = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MALLOC_THROW = 0x02  //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a block of memory (at least 4-byte aligned) of at least the
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    specified size. If the requested memory cannot be returned, either
52519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com    return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
557ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
58de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_malloc_throw(size_t size);
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as standard realloc(), but this one never returns null on failure. It will throw
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    an exception if it fails.
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
62de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_realloc_throw(void* buffer, size_t size);
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Free memory returned by sk_malloc(). It is safe to pass null.
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
65de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_free(void*);
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
67519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
68519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com */
69519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.comSK_API extern void* sk_calloc(size_t size);
70519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com
71519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
72519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com */
73519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.comSK_API extern void* sk_calloc_throw(size_t size);
74519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com
754516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
764516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.comstatic inline void sk_bzero(void* buffer, size_t size) {
774516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com    memset(buffer, 0, size);
784516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com}
794516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com
80bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com///////////////////////////////////////////////////////////////////////////////
81bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
8236352bf5e38f45a70ee4f4fc132a38048d38206dmtklein#ifdef override_GLOBAL_NEW
83bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#include <new>
84bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
85bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void* operator new(size_t size) {
86bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com    return sk_malloc_throw(size);
87bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com}
88bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
89bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void operator delete(void* p) {
90bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com    sk_free(p);
91bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com}
92bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#endif
93bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
94bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com///////////////////////////////////////////////////////////////////////////////
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_INIT_TO_AVOID_WARNING    = 0
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDebugf
99c4ade57cd4ce5083e0fac19111a6ee6e32e18df4george    SK_API void SkDebugf(const char format[], ...);
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
10345d1d1d9a71253e0ed28ba87f5f946b5845f1d6dcommit-bot@chromium.org    #define SkASSERT(cond)              SK_ALWAYSBREAK(cond)
1040c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com    #define SkDEBUGFAIL(message)        SkASSERT(false && message)
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGCODE(code)           code
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDECLAREPARAM(type, var)   , type var
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkPARAM(var)                , var
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  #define SkDEBUGF(args       )       SkDebugf##args
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGF(args       )       SkDebugf args
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkAssertResult(cond)        SkASSERT(cond)
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkASSERT(cond)
1130c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com    #define SkDEBUGFAIL(message)
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGCODE(code)
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGF(args)
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDECLAREPARAM(type, var)
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkPARAM(var)
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // unlike SkASSERT, this guy executes its condition in the non-debug build
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkAssertResult(cond)        cond
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12345d1d1d9a71253e0ed28ba87f5f946b5845f1d6dcommit-bot@chromium.org#define SkFAIL(message)                 SK_ALWAYSBREAK(false && message)
12488cb22b6b4816c7a9ca6c5b795965b4606f9eb7bcommit-bot@chromium.org
125b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form.
126b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond,
127b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// and if it's true the assert passes; if it's false, we'll print the message and the assert fails.
128b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein#define SkASSERTF(cond, fmt, ...)       SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false))
129b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein
13076f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#ifdef SK_DEVELOPER
13176f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    #define SkDEVCODE(code)             code
13276f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#else
13376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    #define SkDEVCODE(code)
1340f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#endif
1350f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org
1360f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifdef SK_IGNORE_TO_STRING
137bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com    #define SK_TO_STRING_NONVIRT()
138bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com    #define SK_TO_STRING_VIRT()
1390f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_PUREVIRT()
1400f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_OVERRIDE()
1410f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#else
1420f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    // the 'toString' helper functions convert Sk* objects to human-readable
1430f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    // form in developer mode
1440f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
1450f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
1460f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
14736352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
14876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif
14976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
15028be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgtemplate <bool>
15128be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgstruct SkCompileAssert {
15228be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org};
15328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
154562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com// Uses static_cast<bool>(expr) instead of bool(expr) due to
155562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com// https://connect.microsoft.com/VisualStudio/feedback/details/832915
1569c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com
1579c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// The extra parentheses in SkCompileAssert<(...)> are a work around for
1589c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57771
1599c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// which was fixed in gcc 4.8.2.
16028be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org#define SK_COMPILE_ASSERT(expr, msg) \
1612f5820903e98580ed9349fc5e3621f745911ad17bungeman@google.com    typedef SkCompileAssert<(static_cast<bool>(expr))> \
162562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com            msg[static_cast<bool>(expr) ? 1 : -1] SK_UNUSED
16328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
16449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/*
16549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  Usage:  SK_MACRO_CONCAT(a, b)   to construct the symbol ab
16649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *
16749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
16849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *
16949a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */
17049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT(X, Y)           SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
17149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y)  X ## Y
17249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com
17349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/*
17449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  Usage: SK_MACRO_APPEND_LINE(foo)    to make foo123, where 123 is the current
17549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      line number. Easy way to construct
17649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      unique names for local functions or
17749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      variables.
17849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */
17949a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_APPEND_LINE(name)  SK_MACRO_CONCAT(name, __LINE__)
18049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com
181e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org/**
182e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * For some classes, it's almost always an error to instantiate one without a name, e.g.
183e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   {
184e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       SkAutoMutexAcquire(&mutex);
185e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <some code>
186e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   }
187e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
188e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * but instead the mutex is acquired and then immediately released.  The correct usage is
189e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   {
190e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       SkAutoMutexAcquire lock(&mutex);
191e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <some code>
192e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   }
193e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *
194e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
195e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * like this:
196e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   class classname {
197e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <your class>
198e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   };
199e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
200e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *
201e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * This won't work with templates, and you must inline the class' constructors and destructors.
202e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
203e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org */
204e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SK_REQUIRE_LOCAL_VAR(classname) \
205e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org    SK_COMPILE_ASSERT(false, missing_name_for_##classname)
206e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
21037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 8 bits. Use for parameter passing and local variables,
21137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage.
21237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
21337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S8CPU;
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
21637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 8 bits. Use for parameter passing and local
21737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
21837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
21937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U8CPU;
22037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
22137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
22237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 16 bits. Use for parameter passing and local variables,
22337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage
22437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
22537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S16CPU;
22637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
22737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
22837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 16 bits. Use for parameter passing and local
22937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
23037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
23137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U16CPU;
23237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
23337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
23437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be faster than bool (doesn't promise to be 0 or 1,
23537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  just 0 or non-zero
23637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
23737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int SkBool;
23837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
23937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
24037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be a small version of bool, for storage purposes. Will be 0 or 1
24137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
24237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef uint8_t SkBool8;
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
245777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API int8_t      SkToS8(intmax_t);
246777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API uint8_t     SkToU8(uintmax_t);
247777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API int16_t     SkToS16(intmax_t);
248777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API uint16_t    SkToU16(uintmax_t);
249777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API int32_t     SkToS32(intmax_t);
250777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API uint32_t    SkToU32(uintmax_t);
251a8c7f7702fb4bbedb615031bc653c5cd161a038ecommit-bot@chromium.org    SK_API int         SkToInt(intmax_t);
2527fa2a65c0cfc714364490cb715171461143024e0reed@google.com    SK_API unsigned    SkToUInt(uintmax_t);
253490fb6b4713463954cc0283a9c30e754c45c6004commit-bot@chromium.org    SK_API size_t      SkToSizeT(uintmax_t);
254ef3fcd877aa78c1d0ac802043cd8785180304c12bsalomon    SK_API off_t       SkToOffT(intmax_t x);
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS8(x)   ((int8_t)(x))
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU8(x)   ((uint8_t)(x))
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS16(x)  ((int16_t)(x))
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU16(x)  ((uint16_t)(x))
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS32(x)  ((int32_t)(x))
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU32(x)  ((uint32_t)(x))
262a8c7f7702fb4bbedb615031bc653c5cd161a038ecommit-bot@chromium.org    #define SkToInt(x)  ((int)(x))
2637fa2a65c0cfc714364490cb715171461143024e0reed@google.com    #define SkToUInt(x) ((unsigned)(x))
264490fb6b4713463954cc0283a9c30e754c45c6004commit-bot@chromium.org    #define SkToSizeT(x) ((size_t)(x))
265ef3fcd877aa78c1d0ac802043cd8785180304c12bsalomon    #define SkToOffT(x) ((off_t)(x))
2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkToBool(cond)  ((cond) != 0)
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16   32767
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16   -32767
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16   0xFFFF
2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16   0
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32   0x7FFFFFFF
277594dd3cd78e2f970d53bb0934fbbb63b41e1d40ccaryclark@google.com#define SK_MinS32   -SK_MaxS32
2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32   0xFFFFFFFF
2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32   0
2800e51577a14f903ffeafa117a75954baeb173ffb9humper@google.com#define SK_NaN32    (1 << 31)
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
282d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits
283d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
28490209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) {
285d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (int16_t)x == x;
286d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
287d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
288d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits
289d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
29090209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) {
291d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (uint16_t)x == x;
292d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
293d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
294d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com//////////////////////////////////////////////////////////////////////////////
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_OFFSETOF
29656d3a23c3e0a53777fb25898951718d44dd9c027reed@google.com    #define SK_OFFSETOF(type, field)    (size_t)((char*)&(((type*)1)->field) - (char*)1)
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns the number of entries in an array (not a pointer)
3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ARRAY_COUNT(array)       (sizeof(array) / sizeof(array[0]))
3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x)     (((x) + 1) >> 1 << 1)
304c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign2(x)   (0 == ((x) & 1))
305c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x)     (((x) + 3) >> 2 << 2)
307c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign4(x)   (0 == ((x) & 3))
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
309c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkAlign8(x)     (((x) + 7) >> 3 << 3)
310c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign8(x)   (0 == ((x) & 7))
31101224d5d0a3228fe47e63d8346e0e433a87563a8tomhudson@google.com
3120209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein#define SkAlignPtr(x)   (sizeof(void*) == 8 ?   SkAlign8(x) :   SkAlign4(x))
3130209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
3140209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein
3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag;
3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d)    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value
3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar;
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit value to hold a millisecond count
3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec;
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds
3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** maximum representable milliseconds
3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF
3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b)     ((int32_t)(a) - (int32_t)(b) < 0)
3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b)     ((int32_t)(a) - (int32_t)(b) <= 0)
3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3372b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org/** The generation IDs in Skia reserve 0 has an invalid marker.
3382b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org */
3392b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org#define SK_InvalidGenID     0
3401c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon/** The unique IDs in Skia reserve 0 has an invalid marker.
3411c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon */
3421c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon#define SK_InvalidUniqueID  0
3432b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/****************************************************************************
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The rest of these only build with C++
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
351d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int Sk32ToBool(uint32_t n) {
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (n | (0-n)) >> 31;
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
355ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com/** Generic swap function. Classes with efficient swaps should specialize this function to take
356ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com    their fast path. This function is used by SkTSort. */
357d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) {
3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    T c(a);
3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    a = b;
3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    b = c;
3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
363d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) {
36409a22e9597e271c44dc7c2b71c72cf62a7de1e19mtklein    SkASSERT(value != SK_NaN32);  // The most negative int32_t can't be negated.
36538bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value < 0) {
3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = -value;
36738bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3712b57dc6bb241a6627c4375ee54b73039983d03dareed@google.comtemplate <typename T> inline T SkTAbs(T value) {
3722b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    if (value < 0) {
3732b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com        value = -value;
3742b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    }
3752b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    return value;
3762b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com}
3772b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com
378d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) {
3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a < b)
3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
384d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) {
3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a > b)
3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3903b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.comtemplate <typename T> const T& SkTMin(const T& a, const T& b) {
3913b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com    return (a < b) ? a : b;
3923b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com}
3933b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com
3943b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.comtemplate <typename T> const T& SkTMax(const T& a, const T& b) {
3953b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com    return (b < a) ? a : b;
3963b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com}
3973b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com
398d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) {
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (a >> 31) | ((unsigned) -a >> 31);
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
402d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) {
40338bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value > max) {
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
40538bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
409fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungemantemplate <typename T> static inline const T& SkTPin(const T& x, const T& min, const T& max) {
410fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungeman    return SkTMax(SkTMin(x, max), min);
411fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungeman}
412fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungeman
413fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungeman/** Returns signed 32 bit value pinned between min and max, inclusively. */
414d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
415fd0ecf46ce7736a86dd38d8cfc96078e588d10a3bungeman    return SkTPin(value, min, max);
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
418d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
419d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com                                       unsigned shift) {
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((int)cond == 0 || (int)cond == 1);
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (bits & ~(1 << shift)) | ((int)cond << shift);
4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
424d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
425d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com                                      uint32_t mask) {
4268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return cond ? bits | mask : bits & ~mask;
4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4291fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com///////////////////////////////////////////////////////////////////////////////
4301fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
431325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org/** Use to combine multiple bits in a bitmask in a type safe way.
432325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org */
433325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgtemplate <typename T>
434325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgT SkTBitOr(T a, T b) {
435325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org    return (T)(a | b);
436325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org}
437325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org
4381fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/**
4391fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com *  Use to cast a pointer to a different type, and maintaining strict-aliasing
4401fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com */
4411fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.comtemplate <typename Dst> Dst SkTCast(const void* ptr) {
4421fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    union {
4431fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        const void* src;
4441fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        Dst dst;
4451fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    } data;
4461fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    data.src = ptr;
4471fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    return data.dst;
4481fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com}
4491fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
4518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable
4538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
454055f6b59d879b2adac52748ea5a58c8a05bf501cfmalitaSkNoncopyable is the base class for objects that do not want to
4558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator.
4568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
4577ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkNoncopyable {
4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable() {}
4601fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable(const SkNoncopyable&);
4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable& operator=(const SkNoncopyable&);
4648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable {
4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree() : fPtr(NULL) {}
4698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoFree() { sk_free(fPtr); }
4711fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the currently allocate buffer, or null
4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
4758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Assign a new ptr allocated with sk_malloc (or null), and return the
4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        previous ptr. Note it is the caller's responsibility to sk_free the
4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        returned ptr.
4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* set(void* ptr) {
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        void* prev = fPtr;
4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = ptr;
4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return prev;
4848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4851fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Transfer ownership of the current ptr to the caller, setting the
4878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        internal reference to null. Note the caller is reponsible for calling
4888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free on the returned address.
4898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* detach() { return this->set(NULL); }
4918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Free the current buffer, and set the internal reference to NULL. Same
4938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        as calling sk_free(detach())
4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void free() {
4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free(fPtr);
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = NULL;
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* fPtr;
5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // illegal
5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree(const SkAutoFree&);
5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree& operator=(const SkAutoFree&);
5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
506e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5087d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com/**
5097d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com *  Manage an allocated block of heap memory. This object is the sole manager of
5107d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com *  the lifetime of the block, so the caller must not call sk_free() or delete
5111c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com *  on the block, unless detach() was called.
5127d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */
513e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.orgclass SkAutoMalloc : SkNoncopyable {
5148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
5153ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com    explicit SkAutoMalloc(size_t size = 0) {
5163ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com        fPtr = size ? sk_malloc_throw(size) : NULL;
5177d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        fSize = size;
5187d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
5198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5207d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    ~SkAutoMalloc() {
5217d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        sk_free(fPtr);
5227d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
5238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5247d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5251c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     *  Passed to reset to specify what happens if the requested size is smaller
5261c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     *  than the current size (and the current block was dynamically allocated).
5271c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     */
5281c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    enum OnShrink {
5291c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        /**
5301c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  If the requested size is smaller than the current size, and the
5311c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  current block is dynamically allocated, free the old block and
5321c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  malloc a new block of the smaller size.
5331c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         */
5341c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        kAlloc_OnShrink,
535fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
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, just return the old
5391c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  block.
5401c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         */
5411f90287df3129cb267422e482c52ebeca6a8990ftomhudson@google.com        kReuse_OnShrink
5421c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    };
5431c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5441c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    /**
5457d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Reallocates the block to a new size. The ptr may or may not change.
5467d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5479eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com    void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink,  bool* didChangeAlloc = NULL) {
5481c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
54949f085dddff10473b6ebf832a974288300224e60bsalomon            if (didChangeAlloc) {
5509eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                *didChangeAlloc = false;
5519eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
5521c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com            return fPtr;
5537d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        }
5541c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5551c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        sk_free(fPtr);
5561c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        fPtr = size ? sk_malloc_throw(size) : NULL;
5571c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        fSize = size;
55849f085dddff10473b6ebf832a974288300224e60bsalomon        if (didChangeAlloc) {
5599eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            *didChangeAlloc = true;
5609eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        }
5611c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5627d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        return fPtr;
5638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5647d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5657d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5667d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Releases the block back to the heap
5677d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5687d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void free() {
5697d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        this->reset(0);
5707d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
5717d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5727d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5737d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Return the allocated block.
5747d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5757d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void* get() { return fPtr; }
5767d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    const void* get() const { return fPtr; }
5777d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5786dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com   /** Transfer ownership of the current ptr to the caller, setting the
5796dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com       internal reference to null. Note the caller is reponsible for calling
5806dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com       sk_free on the returned address.
5816dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    */
5826dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    void* detach() {
5836dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        void* ptr = fPtr;
5846dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        fPtr = NULL;
5856dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        fSize = 0;
5866dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        return ptr;
5876dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    }
5886dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com
5897d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comprivate:
5907d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void*   fPtr;
5911c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    size_t  fSize;  // can be larger than the requested size (see kReuse)
5928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
593e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
5948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
59563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com/**
59663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  Manage an allocated block of memory. If the requested size is <= kSize, then
59763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  the allocation will come from the stack rather than the heap. This object
59863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  is the sole manager of the lifetime of the block, so the caller must not
59963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  call sk_free() or delete on the block.
60063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */
6018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
6028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
60363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
60463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Creates initially empty storage. get() returns a ptr, but it is to
6057d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  a zero-byte allocation. Must call reset(size) to return an allocated
60663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  block.
60763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
60863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    SkAutoSMalloc() {
60963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
6109eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        fSize = kSize;
6118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
61263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
61363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
61463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Allocate a block of the specified size. If size <= kSize, then the
61563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  allocation will come from the stack, otherwise it will be dynamically
61663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  allocated.
61763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
61863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    explicit SkAutoSMalloc(size_t size) {
61963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
6209eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        fSize = kSize;
6217d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        this->reset(size);
62263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
62363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
62463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
62563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Free the allocated block (if any). If the block was small enought to
62663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  have been allocated on the stack (size <= kSize) then this does nothing.
62763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
62863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    ~SkAutoSMalloc() {
62963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        if (fPtr != (void*)fStorage) {
6308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            sk_free(fPtr);
63163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
6328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
63363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
63463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
63563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Return the allocated block. May return non-null even if the block is
63663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  of zero size. Since this may be on the stack or dynamically allocated,
63763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
63863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  to manage it.
63963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
6408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
64163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
64263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
64363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Return a new block of the requested size, freeing (as necessary) any
64463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  previously allocated block. As with the constructor, if size <= kSize
64563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  then the return block may be allocated locally, rather than from the
64663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  heap.
64763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
6481c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    void* reset(size_t size,
6499eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
6509eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                bool* didChangeAlloc = NULL) {
6519eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        size = (size < kSize) ? kSize : size;
6520f2b1953c7a8f8bb5a25d573dd9e215eaa10a2f8robertphillips@google.com        bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
65349f085dddff10473b6ebf832a974288300224e60bsalomon        if (didChangeAlloc) {
6549eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            *didChangeAlloc = alloc;
6551c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        }
6569eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        if (alloc) {
6579eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (fPtr != (void*)fStorage) {
6589eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                sk_free(fPtr);
6599eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
6609eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com
6619eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (size == kSize) {
6629eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
6639eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                fPtr = fStorage;
6649eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            } else {
6659eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
6669eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
6679eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com
6689eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            fSize = size;
66963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
6709eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        SkASSERT(fSize >= size && fSize >= kSize);
6719eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        SkASSERT((fPtr == fStorage) || fSize > kSize);
67263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        return fPtr;
67363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
67463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
6758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
6768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void*       fPtr;
6771c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    size_t      fSize;  // can be larger than the requested size (see kReuse)
6788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fStorage[(kSize + 3) >> 2];
6798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
680e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org// Can't guard the constructor because it's a template class.
6818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */
6838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
685