SkTypes.h revision bc3d92a7d84b56eb235d6c2d9b7de00625200713
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
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void SkDebugf(const char format[], ...);
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkASSERT(cond)              SK_DEBUGBREAK(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
11676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#ifdef SK_DEVELOPER
11776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    #define SkDEVCODE(code)             code
11876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#else
11976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    #define SkDEVCODE(code)
1200f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#endif
1210f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org
1220f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifdef SK_IGNORE_TO_STRING
123bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com    #define SK_TO_STRING_NONVIRT()
124bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com    #define SK_TO_STRING_VIRT()
1250f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_PUREVIRT()
1260f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_OVERRIDE()
1270f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#else
1280f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    // the 'toString' helper functions convert Sk* objects to human-readable
1290f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    // form in developer mode
1300f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
1310f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
1320f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
1330f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_OVERRIDE() virtual void toString(SkString* str) const SK_OVERRIDE;
13476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif
13576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
13628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgtemplate <bool>
13728be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgstruct SkCompileAssert {
13828be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org};
13928be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
140562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com// Uses static_cast<bool>(expr) instead of bool(expr) due to
141562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com// https://connect.microsoft.com/VisualStudio/feedback/details/832915
1429c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com
1439c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// The extra parentheses in SkCompileAssert<(...)> are a work around for
1449c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57771
1459c28fa5579734129f1bad026b8b6aea3bf76da5fbungeman@google.com// which was fixed in gcc 4.8.2.
14628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org#define SK_COMPILE_ASSERT(expr, msg) \
1472f5820903e98580ed9349fc5e3621f745911ad17bungeman@google.com    typedef SkCompileAssert<(static_cast<bool>(expr))> \
148562b2e67a29f24db4c258aa2fa59cd7b4ee15174bungeman@google.com            msg[static_cast<bool>(expr) ? 1 : -1] SK_UNUSED
14928be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
15049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/*
15149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  Usage:  SK_MACRO_CONCAT(a, b)   to construct the symbol ab
15249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *
15349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
15449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *
15549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */
15649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT(X, Y)           SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
15749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y)  X ## Y
15849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com
15949a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/*
16049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  Usage: SK_MACRO_APPEND_LINE(foo)    to make foo123, where 123 is the current
16149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      line number. Easy way to construct
16249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      unique names for local functions or
16349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      variables.
16449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */
16549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_APPEND_LINE(name)  SK_MACRO_CONCAT(name, __LINE__)
16649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com
167e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org/**
168e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * For some classes, it's almost always an error to instantiate one without a name, e.g.
169e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   {
170e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       SkAutoMutexAcquire(&mutex);
171e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <some code>
172e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   }
173e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
174e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * but instead the mutex is acquired and then immediately released.  The correct usage is
175e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   {
176e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       SkAutoMutexAcquire lock(&mutex);
177e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <some code>
178e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   }
179e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *
180e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
181e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * like this:
182e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   class classname {
183e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <your class>
184e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   };
185e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
186e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *
187e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * This won't work with templates, and you must inline the class' constructors and destructors.
188e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
189e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org */
190e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SK_REQUIRE_LOCAL_VAR(classname) \
191e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org    SK_COMPILE_ASSERT(false, missing_name_for_##classname)
192e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
19637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 8 bits. Use for parameter passing and local variables,
19737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage.
19837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
19937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S8CPU;
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
20237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 8 bits. Use for parameter passing and local
20337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
20437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
20537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U8CPU;
20637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
20737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
20837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 16 bits. Use for parameter passing and local variables,
20937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage
21037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
21137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S16CPU;
21237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
21337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
21437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 16 bits. Use for parameter passing and local
21537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
21637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
21737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U16CPU;
21837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
21937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
22037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be faster than bool (doesn't promise to be 0 or 1,
22137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  just 0 or non-zero
22237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
22337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int SkBool;
22437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
22537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
22637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be a small version of bool, for storage purposes. Will be 0 or 1
22737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
22837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef uint8_t SkBool8;
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
231777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API int8_t      SkToS8(intmax_t);
232777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API uint8_t     SkToU8(uintmax_t);
233777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API int16_t     SkToS16(intmax_t);
234777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API uint16_t    SkToU16(uintmax_t);
235777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API int32_t     SkToS32(intmax_t);
236777ded06fd655aad3f8d8ce1eb434374f153f234bungeman@google.com    SK_API uint32_t    SkToU32(uintmax_t);
237a8c7f7702fb4bbedb615031bc653c5cd161a038ecommit-bot@chromium.org    SK_API int         SkToInt(intmax_t);
2387fa2a65c0cfc714364490cb715171461143024e0reed@google.com    SK_API unsigned    SkToUInt(uintmax_t);
239490fb6b4713463954cc0283a9c30e754c45c6004commit-bot@chromium.org    SK_API size_t      SkToSizeT(uintmax_t);
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS8(x)   ((int8_t)(x))
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU8(x)   ((uint8_t)(x))
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS16(x)  ((int16_t)(x))
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU16(x)  ((uint16_t)(x))
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS32(x)  ((int32_t)(x))
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU32(x)  ((uint32_t)(x))
247a8c7f7702fb4bbedb615031bc653c5cd161a038ecommit-bot@chromium.org    #define SkToInt(x)  ((int)(x))
2487fa2a65c0cfc714364490cb715171461143024e0reed@google.com    #define SkToUInt(x) ((unsigned)(x))
249490fb6b4713463954cc0283a9c30e754c45c6004commit-bot@chromium.org    #define SkToSizeT(x) ((size_t)(x))
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkToBool(cond)  ((cond) != 0)
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16   32767
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16   -32767
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16   0xFFFF
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16   0
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32   0x7FFFFFFF
261594dd3cd78e2f970d53bb0934fbbb63b41e1d40ccaryclark@google.com#define SK_MinS32   -SK_MaxS32
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32   0xFFFFFFFF
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32   0
2640e51577a14f903ffeafa117a75954baeb173ffb9humper@google.com#define SK_NaN32    (1 << 31)
2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
266d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits
267d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
26890209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) {
269d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (int16_t)x == x;
270d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
271d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
272d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits
273d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
27490209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) {
275d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (uint16_t)x == x;
276d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
277d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
278d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com//////////////////////////////////////////////////////////////////////////////
2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_OFFSETOF
28056d3a23c3e0a53777fb25898951718d44dd9c027reed@google.com    #define SK_OFFSETOF(type, field)    (size_t)((char*)&(((type*)1)->field) - (char*)1)
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns the number of entries in an array (not a pointer)
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ARRAY_COUNT(array)       (sizeof(array) / sizeof(array[0]))
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x)     (((x) + 1) >> 1 << 1)
288c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign2(x)   (0 == ((x) & 1))
289c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x)     (((x) + 3) >> 2 << 2)
291c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign4(x)   (0 == ((x) & 3))
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
293c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkAlign8(x)     (((x) + 7) >> 3 << 3)
294c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign8(x)   (0 == ((x) & 7))
29501224d5d0a3228fe47e63d8346e0e433a87563a8tomhudson@google.com
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag;
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d)    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value
3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar;
3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit value to hold a millisecond count
3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec;
3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** maximum representable milliseconds
3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF
3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b)     ((int32_t)(a) - (int32_t)(b) < 0)
3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b)     ((int32_t)(a) - (int32_t)(b) <= 0)
3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/****************************************************************************
3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The rest of these only build with C++
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus
3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
325d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int Sk32ToBool(uint32_t n) {
3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (n | (0-n)) >> 31;
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
329ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com/** Generic swap function. Classes with efficient swaps should specialize this function to take
330ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com    their fast path. This function is used by SkTSort. */
331d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) {
3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    T c(a);
3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    a = b;
3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    b = c;
3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
337d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) {
33838bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value < 0) {
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = -value;
34038bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3442b57dc6bb241a6627c4375ee54b73039983d03dareed@google.comtemplate <typename T> inline T SkTAbs(T value) {
3452b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    if (value < 0) {
3462b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com        value = -value;
3472b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    }
3482b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    return value;
3492b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com}
3502b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com
351d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) {
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a < b)
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
357d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) {
3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a > b)
3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3633b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.comtemplate <typename T> const T& SkTMin(const T& a, const T& b) {
3643b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com    return (a < b) ? a : b;
3653b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com}
3663b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com
3673b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.comtemplate <typename T> const T& SkTMax(const T& a, const T& b) {
3683b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com    return (b < a) ? a : b;
3693b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com}
3703b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com
371d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) {
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (a >> 31) | ((unsigned) -a >> 31);
3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
375d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) {
37638bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value > max) {
3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
37838bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns signed 32 bit value pinned between min and max, inclusively
3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
384d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
38538bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value < min) {
3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = min;
38738bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
38838bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value > max) {
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
39038bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
394d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
395d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com                                       unsigned shift) {
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((int)cond == 0 || (int)cond == 1);
3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (bits & ~(1 << shift)) | ((int)cond << shift);
3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
400d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
401d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com                                      uint32_t mask) {
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return cond ? bits | mask : bits & ~mask;
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4051fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com///////////////////////////////////////////////////////////////////////////////
4061fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
407325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org/** Use to combine multiple bits in a bitmask in a type safe way.
408325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org */
409325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgtemplate <typename T>
410325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgT SkTBitOr(T a, T b) {
411325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org    return (T)(a | b);
412325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org}
413325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org
4141fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/**
4151fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com *  Use to cast a pointer to a different type, and maintaining strict-aliasing
4161fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com */
4171fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.comtemplate <typename Dst> Dst SkTCast(const void* ptr) {
4181fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    union {
4191fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        const void* src;
4201fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        Dst dst;
4211fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    } data;
4221fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    data.src = ptr;
4231fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    return data.dst;
4241fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com}
4251fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable
4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkNoncopyable is the base class for objects that may do not want to
4318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator.
4328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
4337ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkNoncopyable {
4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
4358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable() {}
4361fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
4388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable(const SkNoncopyable&);
4398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable& operator=(const SkNoncopyable&);
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable {
4438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
4448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree() : fPtr(NULL) {}
4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
4468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoFree() { sk_free(fPtr); }
4471fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the currently allocate buffer, or null
4498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
4518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Assign a new ptr allocated with sk_malloc (or null), and return the
4538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        previous ptr. Note it is the caller's responsibility to sk_free the
4548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        returned ptr.
4558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* set(void* ptr) {
4578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        void* prev = fPtr;
4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = ptr;
4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return prev;
4608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4611fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Transfer ownership of the current ptr to the caller, setting the
4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        internal reference to null. Note the caller is reponsible for calling
4648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free on the returned address.
4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* detach() { return this->set(NULL); }
4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Free the current buffer, and set the internal reference to NULL. Same
4698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        as calling sk_free(detach())
4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void free() {
4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free(fPtr);
4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = NULL;
4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* fPtr;
4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // illegal
4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree(const SkAutoFree&);
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree& operator=(const SkAutoFree&);
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
482e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4847d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com/**
4857d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com *  Manage an allocated block of heap memory. This object is the sole manager of
4867d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com *  the lifetime of the block, so the caller must not call sk_free() or delete
4871c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com *  on the block, unless detach() was called.
4887d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */
4897d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comclass SkAutoMalloc : public SkNoncopyable {
4908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
4913ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com    explicit SkAutoMalloc(size_t size = 0) {
4923ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com        fPtr = size ? sk_malloc_throw(size) : NULL;
4937d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        fSize = size;
4947d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4967d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    ~SkAutoMalloc() {
4977d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        sk_free(fPtr);
4987d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5007d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5011c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     *  Passed to reset to specify what happens if the requested size is smaller
5021c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     *  than the current size (and the current block was dynamically allocated).
5031c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     */
5041c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    enum OnShrink {
5051c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        /**
5061c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  If the requested size is smaller than the current size, and the
5071c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  current block is dynamically allocated, free the old block and
5081c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  malloc a new block of the smaller size.
5091c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         */
5101c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        kAlloc_OnShrink,
511fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
5121c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        /**
5131c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  If the requested size is smaller than the current size, and the
5141c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  current block is dynamically allocated, just return the old
5151c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  block.
5161c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         */
5171f90287df3129cb267422e482c52ebeca6a8990ftomhudson@google.com        kReuse_OnShrink
5181c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    };
5191c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5201c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    /**
5217d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Reallocates the block to a new size. The ptr may or may not change.
5227d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5239eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com    void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink,  bool* didChangeAlloc = NULL) {
5241c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
5259eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (NULL != didChangeAlloc) {
5269eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                *didChangeAlloc = false;
5279eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
5281c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com            return fPtr;
5297d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        }
5301c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5311c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        sk_free(fPtr);
5321c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        fPtr = size ? sk_malloc_throw(size) : NULL;
5331c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        fSize = size;
5349eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        if (NULL != didChangeAlloc) {
5359eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            *didChangeAlloc = true;
5369eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        }
5371c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5387d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        return fPtr;
5398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5407d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5417d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5427d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Releases the block back to the heap
5437d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5447d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void free() {
5457d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        this->reset(0);
5467d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
5477d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5487d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5497d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Return the allocated block.
5507d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5517d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void* get() { return fPtr; }
5527d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    const void* get() const { return fPtr; }
5537d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5546dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com   /** Transfer ownership of the current ptr to the caller, setting the
5556dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com       internal reference to null. Note the caller is reponsible for calling
5566dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com       sk_free on the returned address.
5576dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    */
5586dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    void* detach() {
5596dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        void* ptr = fPtr;
5606dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        fPtr = NULL;
5616dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        fSize = 0;
5626dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        return ptr;
5636dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    }
5646dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com
5657d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comprivate:
5667d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void*   fPtr;
5671c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    size_t  fSize;  // can be larger than the requested size (see kReuse)
5688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
569e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
5708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
57163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com/**
57263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  Manage an allocated block of memory. If the requested size is <= kSize, then
57363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  the allocation will come from the stack rather than the heap. This object
57463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  is the sole manager of the lifetime of the block, so the caller must not
57563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  call sk_free() or delete on the block.
57663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */
5778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
5788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
57963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
58063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Creates initially empty storage. get() returns a ptr, but it is to
5817d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  a zero-byte allocation. Must call reset(size) to return an allocated
58263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  block.
58363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
58463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    SkAutoSMalloc() {
58563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
5869eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        fSize = kSize;
5878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
58863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
58963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
59063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Allocate a block of the specified size. If size <= kSize, then the
59163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  allocation will come from the stack, otherwise it will be dynamically
59263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  allocated.
59363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
59463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    explicit SkAutoSMalloc(size_t size) {
59563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
5969eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        fSize = kSize;
5977d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        this->reset(size);
59863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
59963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
60063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
60163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Free the allocated block (if any). If the block was small enought to
60263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  have been allocated on the stack (size <= kSize) then this does nothing.
60363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
60463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    ~SkAutoSMalloc() {
60563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        if (fPtr != (void*)fStorage) {
6068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            sk_free(fPtr);
60763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
6088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
60963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
61063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
61163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Return the allocated block. May return non-null even if the block is
61263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  of zero size. Since this may be on the stack or dynamically allocated,
61363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
61463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  to manage it.
61563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
6168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
61763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
61863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
61963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Return a new block of the requested size, freeing (as necessary) any
62063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  previously allocated block. As with the constructor, if size <= kSize
62163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  then the return block may be allocated locally, rather than from the
62263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  heap.
62363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
6241c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    void* reset(size_t size,
6259eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
6269eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                bool* didChangeAlloc = NULL) {
6279eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        size = (size < kSize) ? kSize : size;
6280f2b1953c7a8f8bb5a25d573dd9e215eaa10a2f8robertphillips@google.com        bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
6299eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        if (NULL != didChangeAlloc) {
6309eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            *didChangeAlloc = alloc;
6311c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        }
6329eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        if (alloc) {
6339eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (fPtr != (void*)fStorage) {
6349eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                sk_free(fPtr);
6359eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
6369eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com
6379eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (size == kSize) {
6389eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
6399eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                fPtr = fStorage;
6409eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            } else {
6419eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
6429eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
6439eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com
6449eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            fSize = size;
64563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
6469eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        SkASSERT(fSize >= size && fSize >= kSize);
6479eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        SkASSERT((fPtr == fStorage) || fSize > kSize);
64863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        return fPtr;
64963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
65063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
6518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
6528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void*       fPtr;
6531c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    size_t      fSize;  // can be larger than the requested size (see kReuse)
6548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fStorage[(kSize + 3) >> 2];
6558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
656e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org// Can't guard the constructor because it's a template class.
6578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */
6598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
661