SkTypes.h revision 7ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Copyright (C) 2006 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Licensed under the Apache License, Version 2.0 (the "License");
58a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * you may not use this file except in compliance with the License.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * You may obtain a copy of the License at
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *      http://www.apache.org/licenses/LICENSE-2.0
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Unless required by applicable law or agreed to in writing, software
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * distributed under the License is distributed on an "AS IS" BASIS,
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * See the License for the specific language governing permissions and
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * limitations under the License.
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkTypes_DEFINED
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkTypes_DEFINED
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPreConfig.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUserConfig.h"
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPostConfig.h"
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_IGNORE_STDINT_DOT_H
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #include <stdint.h>
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include <stdio.h>
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkTypes.h
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
339aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com/** See SkGraphics::GetVersion() to retrieve these at runtime
349aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com */
359aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MAJOR  1
369aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MINOR  0
379aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_PATCH  0
389aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memory wrappers to be implemented by the porting layer (platform)
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we run out of memory. The platform implementation must
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    not return, but should either throw an exception or otherwise exit.
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void  sk_out_of_memory(void);
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we hit an unrecoverable error.
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The platform implementation must not return, but should either throw
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    an exception or otherwise exit.
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void  sk_throw(void);
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum {
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MALLOC_TEMP  = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MALLOC_THROW = 0x02  //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a block of memory (at least 4-byte aligned) of at least the
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    specified size. If the requested memory cannot be returned, either
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw()
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
627ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void* sk_malloc_throw(size_t size);
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as standard realloc(), but this one never returns null on failure. It will throw
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    an exception if it fails.
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void* sk_realloc_throw(void* buffer, size_t size);
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Free memory returned by sk_malloc(). It is safe to pass null.
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
727ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API extern void  sk_free(void*);
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
744516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
754516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.comstatic inline void sk_bzero(void* buffer, size_t size) {
764516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com    memset(buffer, 0, size);
774516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com}
784516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_INIT_TO_AVOID_WARNING    = 0
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDebugf
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void SkDebugf(const char format[], ...);
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkASSERT(cond)              SK_DEBUGBREAK(cond)
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGCODE(code)           code
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDECLAREPARAM(type, var)   , type var
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkPARAM(var)                , var
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  #define SkDEBUGF(args       )       SkDebugf##args
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGF(args       )       SkDebugf args
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkAssertResult(cond)        SkASSERT(cond)
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkASSERT(cond)
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGCODE(code)
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGF(args)
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDECLAREPARAM(type, var)
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkPARAM(var)
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // unlike SkASSERT, this guy executes its condition in the non-debug build
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkAssertResult(cond)        cond
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgnamespace {
10728be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
10828be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgtemplate <bool>
10928be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgstruct SkCompileAssert {
11028be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org};
11128be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
11228be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org}  // namespace
11328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
11428be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org#define SK_COMPILE_ASSERT(expr, msg) \
11528be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
11628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
12037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 8 bits. Use for parameter passing and local variables,
12137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage.
12237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
12337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S8CPU;
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
12637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 8 bits. Use for parameter passing and local
12737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
12837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
12937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U8CPU;
13037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
13137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
13237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 16 bits. Use for parameter passing and local variables,
13337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage
13437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
13537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S16CPU;
13637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
13737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
13837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 16 bits. Use for parameter passing and local
13937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
14037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
14137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U16CPU;
14237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
14337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
14437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be faster than bool (doesn't promise to be 0 or 1,
14537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  just 0 or non-zero
14637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
14737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int SkBool;
14837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
14937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
15037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be a small version of bool, for storage purposes. Will be 0 or 1
15137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
15237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef uint8_t SkBool8;
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
1557ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API int8_t      SkToS8(long);
1567ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API uint8_t     SkToU8(size_t);
1577ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API int16_t     SkToS16(long);
1587ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API uint16_t    SkToU16(size_t);
1597ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API int32_t     SkToS32(long);
1607ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API uint32_t    SkToU32(size_t);
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS8(x)   ((int8_t)(x))
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU8(x)   ((uint8_t)(x))
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS16(x)  ((int16_t)(x))
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU16(x)  ((uint16_t)(x))
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS32(x)  ((int32_t)(x))
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU32(x)  ((uint32_t)(x))
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkToBool(cond)  ((cond) != 0)
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16   32767
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16   -32767
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16   0xFFFF
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16   0
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32   0x7FFFFFFF
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS32   0x80000001
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32   0xFFFFFFFF
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32   0
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_NaN32    0x80000000
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
184d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits
185d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
18690209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) {
187d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (int16_t)x == x;
188d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
189d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
190d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits
191d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
19290209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) {
193d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (uint16_t)x == x;
194d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
195d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
196d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com//////////////////////////////////////////////////////////////////////////////
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_OFFSETOF
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_OFFSETOF(type, field)    ((char*)&(((type*)1)->field) - (char*)1)
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns the number of entries in an array (not a pointer)
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ARRAY_COUNT(array)       (sizeof(array) / sizeof(array[0]))
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns x rounded up to a multiple of 2
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x)     (((x) + 1) >> 1 << 1)
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns x rounded up to a multiple of 4
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x)     (((x) + 3) >> 2 << 2)
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag;
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d)    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar;
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit value to hold a millisecond count
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec;
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** maximum representable milliseconds
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b)     ((int32_t)(a) - (int32_t)(b) < 0)
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b)     ((int32_t)(a) - (int32_t)(b) <= 0)
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/****************************************************************************
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The rest of these only build with C++
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
241d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int Sk32ToBool(uint32_t n) {
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (n | (0-n)) >> 31;
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
245d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) {
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    T c(a);
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    a = b;
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    b = c;
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
251d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) {
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (value < 0)
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = -value;
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int32_t mask = value >> 31;
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (value ^ mask) - mask;
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
262d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) {
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a < b)
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
268d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) {
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a > b)
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
274d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) {
2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (a >> 31) | ((unsigned) -a >> 31);
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
278d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) {
2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (value > max)
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int diff = max - value;
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // clear diff if it is negative (clear if value > max)
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    diff &= (diff >> 31);
2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value + diff;
2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns signed 32 bit value pinned between min and max, inclusively
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
293d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (value < min)
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = min;
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (value > max)
2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (value < min)
3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = min;
3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    else if (value > max)
3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
308d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
309d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com                                       unsigned shift) {
3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((int)cond == 0 || (int)cond == 1);
3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (bits & ~(1 << shift)) | ((int)cond << shift);
3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
314d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
315d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com                                      uint32_t mask) {
3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return cond ? bits | mask : bits & ~mask;
3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3191fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com///////////////////////////////////////////////////////////////////////////////
3201fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
3211fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/**
3221fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com *  Use to cast a pointer to a different type, and maintaining strict-aliasing
3231fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com */
3241fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.comtemplate <typename Dst> Dst SkTCast(const void* ptr) {
3251fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    union {
3261fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        const void* src;
3271fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        Dst dst;
3281fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    } data;
3291fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    data.src = ptr;
3301fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    return data.dst;
3311fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com}
3321fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable
3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkNoncopyable is the base class for objects that may do not want to
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator.
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3407ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkNoncopyable {
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable() {}
3431fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable(const SkNoncopyable&);
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable& operator=(const SkNoncopyable&);
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable {
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree() : fPtr(NULL) {}
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoFree() { sk_free(fPtr); }
3541fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the currently allocate buffer, or null
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Assign a new ptr allocated with sk_malloc (or null), and return the
3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        previous ptr. Note it is the caller's responsibility to sk_free the
3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        returned ptr.
3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* set(void* ptr) {
3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        void* prev = fPtr;
3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = ptr;
3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return prev;
3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3681fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Transfer ownership of the current ptr to the caller, setting the
3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        internal reference to null. Note the caller is reponsible for calling
3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free on the returned address.
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* detach() { return this->set(NULL); }
3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Free the current buffer, and set the internal reference to NULL. Same
3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        as calling sk_free(detach())
3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void free() {
3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free(fPtr);
3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = NULL;
3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* fPtr;
3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // illegal
3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree(const SkAutoFree&);
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree& operator=(const SkAutoFree&);
3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoMalloc : public SkAutoFree {
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    explicit SkAutoMalloc(size_t size)
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {}
3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoMalloc(size_t size, unsigned flags)
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        : SkAutoFree(sk_malloc_flags(size, flags)) {}
3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoMalloc() {}
3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* alloc(size_t size,
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) {
4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free(set(sk_malloc_flags(size, flags)));
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return get();
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
4058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
40663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com/**
40763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  Manage an allocated block of memory. If the requested size is <= kSize, then
40863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  the allocation will come from the stack rather than the heap. This object
40963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  is the sole manager of the lifetime of the block, so the caller must not
41063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  call sk_free() or delete on the block.
41163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */
4128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
41463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
41563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Creates initially empty storage. get() returns a ptr, but it is to
41663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  a zero-byte allocation. Must call realloc(size) to return an allocated
41763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  block.
41863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
41963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    SkAutoSMalloc() {
42063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
42263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
42363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
42463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Allocate a block of the specified size. If size <= kSize, then the
42563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  allocation will come from the stack, otherwise it will be dynamically
42663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  allocated.
42763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
42863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    explicit SkAutoSMalloc(size_t size) {
42963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
43063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        this->realloc(size);
43163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
43263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
43363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
43463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Free the allocated block (if any). If the block was small enought to
43563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  have been allocated on the stack (size <= kSize) then this does nothing.
43663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
43763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    ~SkAutoSMalloc() {
43863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        if (fPtr != (void*)fStorage) {
4398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            sk_free(fPtr);
44063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
44263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
44363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
44463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Return the allocated block. May return non-null even if the block is
44563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  of zero size. Since this may be on the stack or dynamically allocated,
44663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
44763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  to manage it.
44863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
4498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
45063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
45163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
45263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Return a new block of the requested size, freeing (as necessary) any
45363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  previously allocated block. As with the constructor, if size <= kSize
45463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  then the return block may be allocated locally, rather than from the
45563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  heap.
45663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
45763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    void* realloc(size_t size) {
45863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        if (fPtr != (void*)fStorage) {
45963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com            sk_free(fPtr);
46063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
46163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
46263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        if (size <= kSize) {
46363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com            fPtr = fStorage;
46463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        } else {
46563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com            fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
46663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
46763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        return fPtr;
46863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
46963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
4718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void*       fPtr;
4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fStorage[(kSize + 3) >> 2];
4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // illegal
4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoSMalloc(const SkAutoSMalloc&);
4758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoSMalloc& operator=(const SkAutoSMalloc&);
4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */
4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
482