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>
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkTypes.h
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
199aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com/** See SkGraphics::GetVersion() to retrieve these at runtime
209aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com */
219aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MAJOR  1
229aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MINOR  0
239aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_PATCH  0
249aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memory wrappers to be implemented by the porting layer (platform)
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we run out of memory. The platform implementation must
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    not return, but should either throw an exception or otherwise exit.
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
32de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_out_of_memory(void);
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we hit an unrecoverable error.
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The platform implementation must not return, but should either throw
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    an exception or otherwise exit.
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
37de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_throw(void);
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum {
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MALLOC_TEMP  = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MALLOC_THROW = 0x02  //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a block of memory (at least 4-byte aligned) of at least the
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    specified size. If the requested memory cannot be returned, either
45519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com    return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
487ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
51de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_malloc_throw(size_t size);
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as standard realloc(), but this one never returns null on failure. It will throw
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    an exception if it fails.
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
55de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_realloc_throw(void* buffer, size_t size);
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Free memory returned by sk_malloc(). It is safe to pass null.
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
58de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_free(void*);
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
60519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
61519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com */
62519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.comSK_API extern void* sk_calloc(size_t size);
63519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com
64519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
65519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com */
66519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.comSK_API extern void* sk_calloc_throw(size_t size);
67519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com
684516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
694516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.comstatic inline void sk_bzero(void* buffer, size_t size) {
704516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com    memset(buffer, 0, size);
714516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com}
724516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com
73bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com///////////////////////////////////////////////////////////////////////////////
74bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
75bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#ifdef SK_OVERRIDE_GLOBAL_NEW
76bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#include <new>
77bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
78bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void* operator new(size_t size) {
79bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com    return sk_malloc_throw(size);
80bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com}
81bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
82bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void operator delete(void* p) {
83bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com    sk_free(p);
84bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com}
85bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#endif
86bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
87bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com///////////////////////////////////////////////////////////////////////////////
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_INIT_TO_AVOID_WARNING    = 0
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDebugf
92c4ade57cd4ce5083e0fac19111a6ee6e32e18df4george    SK_API void SkDebugf(const char format[], ...);
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
9645d1d1d9a71253e0ed28ba87f5f946b5845f1d6dcommit-bot@chromium.org    #define SkASSERT(cond)              SK_ALWAYSBREAK(cond)
970c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com    #define SkDEBUGFAIL(message)        SkASSERT(false && message)
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGCODE(code)           code
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDECLAREPARAM(type, var)   , type var
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkPARAM(var)                , var
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  #define SkDEBUGF(args       )       SkDebugf##args
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGF(args       )       SkDebugf args
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkAssertResult(cond)        SkASSERT(cond)
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkASSERT(cond)
1060c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com    #define SkDEBUGFAIL(message)
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGCODE(code)
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGF(args)
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDECLAREPARAM(type, var)
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkPARAM(var)
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // unlike SkASSERT, this guy executes its condition in the non-debug build
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkAssertResult(cond)        cond
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11645d1d1d9a71253e0ed28ba87f5f946b5845f1d6dcommit-bot@chromium.org#define SkFAIL(message)                 SK_ALWAYSBREAK(false && message)
11788cb22b6b4816c7a9ca6c5b795965b4606f9eb7bcommit-bot@chromium.org
118b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form.
119b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond,
120b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein// and if it's true the assert passes; if it's false, we'll print the message and the assert fails.
121b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein#define SkASSERTF(cond, fmt, ...)       SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false))
122b59161f0000eb4aca3dcef29f27ffd0fb5a568e5mtklein
12376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#ifdef SK_DEVELOPER
12476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    #define SkDEVCODE(code)             code
12576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#else
12676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    #define SkDEVCODE(code)
1270f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#endif
1280f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org
1290f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifdef SK_IGNORE_TO_STRING
130bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com    #define SK_TO_STRING_NONVIRT()
131bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com    #define SK_TO_STRING_VIRT()
1320f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_PUREVIRT()
1330f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_OVERRIDE()
1340f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#else
1350f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    // the 'toString' helper functions convert Sk* objects to human-readable
1360f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    // form in developer mode
1370f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
1380f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
1390f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
1400f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_OVERRIDE() virtual void toString(SkString* str) const SK_OVERRIDE;
14176f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif
14276f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
14328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgtemplate <bool>
14428be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgstruct SkCompileAssert {
14528be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org};
14628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
147562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com// Uses static_cast<bool>(expr) instead of bool(expr) due to
148562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com// https://connect.microsoft.com/VisualStudio/feedback/details/832915
1499c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com
1509c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// The extra parentheses in SkCompileAssert<(...)> are a work around for
1519c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57771
1529c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// which was fixed in gcc 4.8.2.
15328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org#define SK_COMPILE_ASSERT(expr, msg) \
1542f5820903e98580ed9349fc5e3621f745911ad17bungeman@google.com    typedef SkCompileAssert<(static_cast<bool>(expr))> \
155562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com            msg[static_cast<bool>(expr) ? 1 : -1] SK_UNUSED
15628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
15749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/*
15849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  Usage:  SK_MACRO_CONCAT(a, b)   to construct the symbol ab
15949a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *
16049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
16149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *
16249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */
16349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT(X, Y)           SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
16449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y)  X ## Y
16549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com
16649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/*
16749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  Usage: SK_MACRO_APPEND_LINE(foo)    to make foo123, where 123 is the current
16849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      line number. Easy way to construct
16949a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      unique names for local functions or
17049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      variables.
17149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */
17249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_APPEND_LINE(name)  SK_MACRO_CONCAT(name, __LINE__)
17349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com
174e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org/**
175e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * For some classes, it's almost always an error to instantiate one without a name, e.g.
176e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   {
177e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       SkAutoMutexAcquire(&mutex);
178e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <some code>
179e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   }
180e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
181e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * but instead the mutex is acquired and then immediately released.  The correct usage is
182e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   {
183e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       SkAutoMutexAcquire lock(&mutex);
184e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <some code>
185e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   }
186e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *
187e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
188e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * like this:
189e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   class classname {
190e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <your class>
191e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   };
192e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
193e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *
194e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * This won't work with templates, and you must inline the class' constructors and destructors.
195e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
196e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org */
197e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SK_REQUIRE_LOCAL_VAR(classname) \
198e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org    SK_COMPILE_ASSERT(false, missing_name_for_##classname)
199e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
20337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 8 bits. Use for parameter passing and local variables,
20437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage.
20537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
20637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S8CPU;
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
20937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 8 bits. Use for parameter passing and local
21037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
21137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
21237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U8CPU;
21337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
21437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
21537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 16 bits. Use for parameter passing and local variables,
21637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage
21737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
21837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S16CPU;
21937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
22037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
22137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 16 bits. Use for parameter passing and local
22237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
22337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
22437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U16CPU;
22537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
22637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
22737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be faster than bool (doesn't promise to be 0 or 1,
22837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  just 0 or non-zero
22937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
23037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int SkBool;
23137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
23237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
23337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be a small version of bool, for storage purposes. Will be 0 or 1
23437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
23537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef uint8_t SkBool8;
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
238777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API int8_t      SkToS8(intmax_t);
239777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API uint8_t     SkToU8(uintmax_t);
240777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API int16_t     SkToS16(intmax_t);
241777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API uint16_t    SkToU16(uintmax_t);
242777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API int32_t     SkToS32(intmax_t);
243777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API uint32_t    SkToU32(uintmax_t);
244a8c7f7702fb4bbedb615031bc653c5cd161a038ecommit-bot@chromium.org    SK_API int         SkToInt(intmax_t);
2457fa2a65c0cfc714364490cb715171461143024e0reed@google.com    SK_API unsigned    SkToUInt(uintmax_t);
246490fb6b4713463954cc0283a9c30e754c45c6004commit-bot@chromium.org    SK_API size_t      SkToSizeT(uintmax_t);
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS8(x)   ((int8_t)(x))
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU8(x)   ((uint8_t)(x))
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS16(x)  ((int16_t)(x))
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU16(x)  ((uint16_t)(x))
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS32(x)  ((int32_t)(x))
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU32(x)  ((uint32_t)(x))
254a8c7f7702fb4bbedb615031bc653c5cd161a038ecommit-bot@chromium.org    #define SkToInt(x)  ((int)(x))
2557fa2a65c0cfc714364490cb715171461143024e0reed@google.com    #define SkToUInt(x) ((unsigned)(x))
256490fb6b4713463954cc0283a9c30e754c45c6004commit-bot@chromium.org    #define SkToSizeT(x) ((size_t)(x))
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkToBool(cond)  ((cond) != 0)
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16   32767
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16   -32767
2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16   0xFFFF
2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16   0
2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32   0x7FFFFFFF
268594dd3cd78e2f970d53bb0934fbbb63b41e1d40ccaryclark@google.com#define SK_MinS32   -SK_MaxS32
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32   0xFFFFFFFF
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32   0
2710e51577a14f903ffeafa117a75954baeb173ffb9humper@google.com#define SK_NaN32    (1 << 31)
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
273d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits
274d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
27590209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) {
276d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (int16_t)x == x;
277d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
278d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
279d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits
280d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
28190209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) {
282d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (uint16_t)x == x;
283d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
284d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
285d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com//////////////////////////////////////////////////////////////////////////////
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_OFFSETOF
28756d3a23c3e0a53777fb25898951718d44dd9c027reed@google.com    #define SK_OFFSETOF(type, field)    (size_t)((char*)&(((type*)1)->field) - (char*)1)
2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns the number of entries in an array (not a pointer)
2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ARRAY_COUNT(array)       (sizeof(array) / sizeof(array[0]))
2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x)     (((x) + 1) >> 1 << 1)
295c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign2(x)   (0 == ((x) & 1))
296c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x)     (((x) + 3) >> 2 << 2)
298c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign4(x)   (0 == ((x) & 3))
2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
300c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkAlign8(x)     (((x) + 7) >> 3 << 3)
301c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign8(x)   (0 == ((x) & 7))
30201224d5d0a3228fe47e63d8346e0e433a87563a8tomhudson@google.com
3030209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein#define SkAlignPtr(x)   (sizeof(void*) == 8 ?   SkAlign8(x) :   SkAlign4(x))
3040209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
3050209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag;
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d)    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value
3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar;
3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit value to hold a millisecond count
3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec;
3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds
3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** maximum representable milliseconds
3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b)     ((int32_t)(a) - (int32_t)(b) < 0)
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b)     ((int32_t)(a) - (int32_t)(b) <= 0)
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3282b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org/** The generation IDs in Skia reserve 0 has an invalid marker.
3292b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org */
3302b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org#define SK_InvalidGenID     0
3311c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon/** The unique IDs in Skia reserve 0 has an invalid marker.
3321c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon */
3331c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon#define SK_InvalidUniqueID  0
3342b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org
3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/****************************************************************************
3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The rest of these only build with C++
3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
342d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int Sk32ToBool(uint32_t n) {
3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (n | (0-n)) >> 31;
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
346ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com/** Generic swap function. Classes with efficient swaps should specialize this function to take
347ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com    their fast path. This function is used by SkTSort. */
348d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) {
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    T c(a);
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    a = b;
3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    b = c;
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
354d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) {
35538bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value < 0) {
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = -value;
35738bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3612b57dc6bb241a6627c4375ee54b73039983d03dareed@google.comtemplate <typename T> inline T SkTAbs(T value) {
3622b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    if (value < 0) {
3632b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com        value = -value;
3642b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    }
3652b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    return value;
3662b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com}
3672b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com
368d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) {
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a < b)
3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
374d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) {
3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a > b)
3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3803b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.comtemplate <typename T> const T& SkTMin(const T& a, const T& b) {
3813b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com    return (a < b) ? a : b;
3823b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com}
3833b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com
3843b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.comtemplate <typename T> const T& SkTMax(const T& a, const T& b) {
3853b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com    return (b < a) ? a : b;
3863b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com}
3873b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com
388d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) {
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (a >> 31) | ((unsigned) -a >> 31);
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
392d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) {
39338bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value > max) {
3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
39538bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns signed 32 bit value pinned between min and max, inclusively
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
401d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
40238bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value < min) {
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = min;
40438bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
40538bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value > max) {
4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
40738bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
411d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
412d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com                                       unsigned shift) {
4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((int)cond == 0 || (int)cond == 1);
4148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (bits & ~(1 << shift)) | ((int)cond << shift);
4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
417d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
418d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com                                      uint32_t mask) {
4198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return cond ? bits | mask : bits & ~mask;
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4221fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com///////////////////////////////////////////////////////////////////////////////
4231fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
424325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org/** Use to combine multiple bits in a bitmask in a type safe way.
425325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org */
426325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgtemplate <typename T>
427325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgT SkTBitOr(T a, T b) {
428325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org    return (T)(a | b);
429325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org}
430325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org
4311fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/**
4321fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com *  Use to cast a pointer to a different type, and maintaining strict-aliasing
4331fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com */
4341fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.comtemplate <typename Dst> Dst SkTCast(const void* ptr) {
4351fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    union {
4361fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        const void* src;
4371fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        Dst dst;
4381fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    } data;
4391fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    data.src = ptr;
4401fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    return data.dst;
4411fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com}
4421fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
4448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable
4468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkNoncopyable is the base class for objects that may do not want to
4488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator.
4498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
4507ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkNoncopyable {
4518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable() {}
4531fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
4558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable(const SkNoncopyable&);
4568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable& operator=(const SkNoncopyable&);
4578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable {
4608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
4618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree() : fPtr(NULL) {}
4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoFree() { sk_free(fPtr); }
4641fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the currently allocate buffer, or null
4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Assign a new ptr allocated with sk_malloc (or null), and return the
4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        previous ptr. Note it is the caller's responsibility to sk_free the
4718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        returned ptr.
4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* set(void* ptr) {
4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        void* prev = fPtr;
4758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = ptr;
4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return prev;
4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4781fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Transfer ownership of the current ptr to the caller, setting the
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        internal reference to null. Note the caller is reponsible for calling
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free on the returned address.
4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* detach() { return this->set(NULL); }
4848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Free the current buffer, and set the internal reference to NULL. Same
4868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        as calling sk_free(detach())
4878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void free() {
4898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free(fPtr);
4908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = NULL;
4918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* fPtr;
4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // illegal
4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree(const SkAutoFree&);
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree& operator=(const SkAutoFree&);
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
499e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5017d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com/**
5027d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com *  Manage an allocated block of heap memory. This object is the sole manager of
5037d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com *  the lifetime of the block, so the caller must not call sk_free() or delete
5041c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com *  on the block, unless detach() was called.
5057d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */
506e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.orgclass SkAutoMalloc : SkNoncopyable {
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
5083ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com    explicit SkAutoMalloc(size_t size = 0) {
5093ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com        fPtr = size ? sk_malloc_throw(size) : NULL;
5107d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        fSize = size;
5117d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
5128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5137d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    ~SkAutoMalloc() {
5147d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        sk_free(fPtr);
5157d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
5168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5177d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5181c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     *  Passed to reset to specify what happens if the requested size is smaller
5191c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     *  than the current size (and the current block was dynamically allocated).
5201c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     */
5211c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    enum OnShrink {
5221c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        /**
5231c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  If the requested size is smaller than the current size, and the
5241c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  current block is dynamically allocated, free the old block and
5251c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  malloc a new block of the smaller size.
5261c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         */
5271c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        kAlloc_OnShrink,
528fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
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, just return the old
5321c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  block.
5331c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         */
5341f90287df3129cb267422e482c52ebeca6a8990ftomhudson@google.com        kReuse_OnShrink
5351c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    };
5361c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5371c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    /**
5387d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Reallocates the block to a new size. The ptr may or may not change.
5397d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5409eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com    void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink,  bool* didChangeAlloc = NULL) {
5411c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
54249f085dddff10473b6ebf832a974288300224e60bsalomon            if (didChangeAlloc) {
5439eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                *didChangeAlloc = false;
5449eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
5451c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com            return fPtr;
5467d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        }
5471c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5481c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        sk_free(fPtr);
5491c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        fPtr = size ? sk_malloc_throw(size) : NULL;
5501c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        fSize = size;
55149f085dddff10473b6ebf832a974288300224e60bsalomon        if (didChangeAlloc) {
5529eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            *didChangeAlloc = true;
5539eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        }
5541c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5557d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        return fPtr;
5568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5577d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5587d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5597d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Releases the block back to the heap
5607d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5617d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void free() {
5627d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        this->reset(0);
5637d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
5647d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5657d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5667d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Return the allocated block.
5677d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5687d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void* get() { return fPtr; }
5697d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    const void* get() const { return fPtr; }
5707d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5716dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com   /** Transfer ownership of the current ptr to the caller, setting the
5726dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com       internal reference to null. Note the caller is reponsible for calling
5736dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com       sk_free on the returned address.
5746dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    */
5756dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    void* detach() {
5766dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        void* ptr = fPtr;
5776dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        fPtr = NULL;
5786dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        fSize = 0;
5796dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        return ptr;
5806dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    }
5816dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com
5827d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comprivate:
5837d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void*   fPtr;
5841c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    size_t  fSize;  // can be larger than the requested size (see kReuse)
5858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
586e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
5878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
58863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com/**
58963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  Manage an allocated block of memory. If the requested size is <= kSize, then
59063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  the allocation will come from the stack rather than the heap. This object
59163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  is the sole manager of the lifetime of the block, so the caller must not
59263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  call sk_free() or delete on the block.
59363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */
5948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
5958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
59663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
59763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Creates initially empty storage. get() returns a ptr, but it is to
5987d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  a zero-byte allocation. Must call reset(size) to return an allocated
59963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  block.
60063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
60163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    SkAutoSMalloc() {
60263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
6039eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        fSize = kSize;
6048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
60563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
60663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
60763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Allocate a block of the specified size. If size <= kSize, then the
60863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  allocation will come from the stack, otherwise it will be dynamically
60963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  allocated.
61063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
61163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    explicit SkAutoSMalloc(size_t size) {
61263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
6139eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        fSize = kSize;
6147d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        this->reset(size);
61563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
61663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
61763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
61863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Free the allocated block (if any). If the block was small enought to
61963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  have been allocated on the stack (size <= kSize) then this does nothing.
62063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
62163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    ~SkAutoSMalloc() {
62263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        if (fPtr != (void*)fStorage) {
6238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            sk_free(fPtr);
62463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
6258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
62663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
62763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
62863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Return the allocated block. May return non-null even if the block is
62963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  of zero size. Since this may be on the stack or dynamically allocated,
63063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
63163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  to manage it.
63263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
6338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
63463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
63563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
63663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Return a new block of the requested size, freeing (as necessary) any
63763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  previously allocated block. As with the constructor, if size <= kSize
63863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  then the return block may be allocated locally, rather than from the
63963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  heap.
64063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
6411c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    void* reset(size_t size,
6429eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
6439eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                bool* didChangeAlloc = NULL) {
6449eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        size = (size < kSize) ? kSize : size;
6450f2b1953c7a8f8bb5a25d573dd9e215eaa10a2f8robertphillips@google.com        bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
64649f085dddff10473b6ebf832a974288300224e60bsalomon        if (didChangeAlloc) {
6479eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            *didChangeAlloc = alloc;
6481c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        }
6499eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        if (alloc) {
6509eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (fPtr != (void*)fStorage) {
6519eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                sk_free(fPtr);
6529eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
6539eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com
6549eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (size == kSize) {
6559eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
6569eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                fPtr = fStorage;
6579eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            } else {
6589eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
6599eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
6609eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com
6619eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            fSize = size;
66263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
6639eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        SkASSERT(fSize >= size && fSize >= kSize);
6649eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        SkASSERT((fPtr == fStorage) || fSize > kSize);
66563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        return fPtr;
66663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
66763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
6688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
6698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void*       fPtr;
6701c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    size_t      fSize;  // can be larger than the requested size (see kReuse)
6718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fStorage[(kSize + 3) >> 2];
6728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
673e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org// Can't guard the constructor because it's a template class.
6748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */
6768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
678