SkTypes.h revision 0f2b1953c7a8f8bb5a25d573dd9e215eaa10a2f8
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkTypes_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkTypes_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPreConfig.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUserConfig.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPostConfig.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_IGNORE_STDINT_DOT_H
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #include <stdint.h>
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include <stdio.h>
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkTypes.h
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
269aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com/** See SkGraphics::GetVersion() to retrieve these at runtime
279aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com */
289aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MAJOR  1
299aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MINOR  0
309aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_PATCH  0
319aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memory wrappers to be implemented by the porting layer (platform)
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we run out of memory. The platform implementation must
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    not return, but should either throw an exception or otherwise exit.
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
39de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_out_of_memory(void);
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we hit an unrecoverable error.
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The platform implementation must not return, but should either throw
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    an exception or otherwise exit.
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
44de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_throw(void);
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum {
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MALLOC_TEMP  = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MALLOC_THROW = 0x02  //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a block of memory (at least 4-byte aligned) of at least the
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    specified size. If the requested memory cannot be returned, either
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw()
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
557ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
58de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_malloc_throw(size_t size);
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as standard realloc(), but this one never returns null on failure. It will throw
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    an exception if it fails.
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
62de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_realloc_throw(void* buffer, size_t size);
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Free memory returned by sk_malloc(). It is safe to pass null.
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
65de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_free(void*);
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
674516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
684516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.comstatic inline void sk_bzero(void* buffer, size_t size) {
694516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com    memset(buffer, 0, size);
704516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com}
714516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com
72bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com///////////////////////////////////////////////////////////////////////////////
73bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
74bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#ifdef SK_OVERRIDE_GLOBAL_NEW
75bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#include <new>
76bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
77bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void* operator new(size_t size) {
78bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com    return sk_malloc_throw(size);
79bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com}
80bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
81bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void operator delete(void* p) {
82bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com    sk_free(p);
83bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com}
84bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#endif
85bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
86bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com///////////////////////////////////////////////////////////////////////////////
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_INIT_TO_AVOID_WARNING    = 0
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDebugf
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void SkDebugf(const char format[], ...);
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkASSERT(cond)              SK_DEBUGBREAK(cond)
960c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com    #define SkDEBUGFAIL(message)        SkASSERT(false && message)
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGCODE(code)           code
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDECLAREPARAM(type, var)   , type var
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkPARAM(var)                , var
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  #define SkDEBUGF(args       )       SkDebugf##args
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGF(args       )       SkDebugf args
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkAssertResult(cond)        SkASSERT(cond)
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkASSERT(cond)
1050c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com    #define SkDEBUGFAIL(message)
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGCODE(code)
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGF(args)
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDECLAREPARAM(type, var)
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkPARAM(var)
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // unlike SkASSERT, this guy executes its condition in the non-debug build
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkAssertResult(cond)        cond
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#ifdef SK_DEVELOPER
11676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    #define SkDEVCODE(code)             code
11776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    // the 'toString' helper functions convert Sk* objects to human-readable
11876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    // form in developer mode
11976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    #define SK_DEVELOPER_TO_STRING()    virtual void toString(SkString* str) const SK_OVERRIDE;
12076f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#else
12176f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    #define SkDEVCODE(code)
12276f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    #define SK_DEVELOPER_TO_STRING()
12376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif
12476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
12528be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgtemplate <bool>
12628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgstruct SkCompileAssert {
12728be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org};
12828be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
12928be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org#define SK_COMPILE_ASSERT(expr, msg) \
13028be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
13128be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
13249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/*
13349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  Usage:  SK_MACRO_CONCAT(a, b)   to construct the symbol ab
13449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *
13549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
13649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *
13749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */
13849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT(X, Y)           SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
13949a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y)  X ## Y
14049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com
14149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/*
14249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  Usage: SK_MACRO_APPEND_LINE(foo)    to make foo123, where 123 is the current
14349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      line number. Easy way to construct
14449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      unique names for local functions or
14549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      variables.
14649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */
14749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_APPEND_LINE(name)  SK_MACRO_CONCAT(name, __LINE__)
14849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
15237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 8 bits. Use for parameter passing and local variables,
15337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage.
15437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
15537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S8CPU;
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
15837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 8 bits. Use for parameter passing and local
15937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
16037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
16137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U8CPU;
16237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
16337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
16437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 16 bits. Use for parameter passing and local variables,
16537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage
16637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
16737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S16CPU;
16837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
16937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
17037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 16 bits. Use for parameter passing and local
17137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
17237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
17337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U16CPU;
17437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
17537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
17637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be faster than bool (doesn't promise to be 0 or 1,
17737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  just 0 or non-zero
17837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
17937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int SkBool;
18037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
18137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
18237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be a small version of bool, for storage purposes. Will be 0 or 1
18337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
18437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef uint8_t SkBool8;
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
1877ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API int8_t      SkToS8(long);
1887ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API uint8_t     SkToU8(size_t);
1897ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API int16_t     SkToS16(long);
1907ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API uint16_t    SkToU16(size_t);
1917ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API int32_t     SkToS32(long);
1927ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org    SK_API uint32_t    SkToU32(size_t);
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS8(x)   ((int8_t)(x))
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU8(x)   ((uint8_t)(x))
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS16(x)  ((int16_t)(x))
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU16(x)  ((uint16_t)(x))
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToS32(x)  ((int32_t)(x))
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkToU32(x)  ((uint32_t)(x))
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkToBool(cond)  ((cond) != 0)
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16   32767
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16   -32767
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16   0xFFFF
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16   0
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32   0x7FFFFFFF
211594dd3cd78e2f970d53bb0934fbbb63b41e1d40ccaryclark@google.com#define SK_MinS32   -SK_MaxS32
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32   0xFFFFFFFF
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32   0
2140e51577a14f903ffeafa117a75954baeb173ffb9humper@google.com#define SK_NaN32    (1 << 31)
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
216d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits
217d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
21890209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) {
219d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (int16_t)x == x;
220d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
221d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
222d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits
223d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
22490209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) {
225d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (uint16_t)x == x;
226d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
227d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
228d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com//////////////////////////////////////////////////////////////////////////////
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_OFFSETOF
23056d3a23c3e0a53777fb25898951718d44dd9c027reed@google.com    #define SK_OFFSETOF(type, field)    (size_t)((char*)&(((type*)1)->field) - (char*)1)
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns the number of entries in an array (not a pointer)
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ARRAY_COUNT(array)       (sizeof(array) / sizeof(array[0]))
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x)     (((x) + 1) >> 1 << 1)
238c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign2(x)   (0 == ((x) & 1))
239c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x)     (((x) + 3) >> 2 << 2)
241c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign4(x)   (0 == ((x) & 3))
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
243c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkAlign8(x)     (((x) + 7) >> 3 << 3)
244c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com#define SkIsAlign8(x)   (0 == ((x) & 7))
24501224d5d0a3228fe47e63d8346e0e433a87563a8tomhudson@google.com
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag;
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d)    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar;
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit value to hold a millisecond count
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec;
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** maximum representable milliseconds
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b)     ((int32_t)(a) - (int32_t)(b) < 0)
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b)     ((int32_t)(a) - (int32_t)(b) <= 0)
2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/****************************************************************************
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The rest of these only build with C++
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
275d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int Sk32ToBool(uint32_t n) {
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (n | (0-n)) >> 31;
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
279ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com/** Generic swap function. Classes with efficient swaps should specialize this function to take
280ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com    their fast path. This function is used by SkTSort. */
281d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) {
2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    T c(a);
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    a = b;
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    b = c;
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
287d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) {
2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (value < 0)
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = -value;
2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int32_t mask = value >> 31;
2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (value ^ mask) - mask;
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2982b57dc6bb241a6627c4375ee54b73039983d03dareed@google.comtemplate <typename T> inline T SkTAbs(T value) {
2992b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    if (value < 0) {
3002b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com        value = -value;
3012b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    }
3022b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    return value;
3032b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com}
3042b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com
305d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) {
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a < b)
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
311d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) {
3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a > b)
3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3173b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.comtemplate <typename T> const T& SkTMin(const T& a, const T& b) {
3183b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com    return (a < b) ? a : b;
3193b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com}
3203b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com
3213b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.comtemplate <typename T> const T& SkTMax(const T& a, const T& b) {
3223b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com    return (b < a) ? a : b;
3233b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com}
3243b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com
325d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) {
3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (a >> 31) | ((unsigned) -a >> 31);
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
329d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) {
3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (value > max)
3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int diff = max - value;
3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // clear diff if it is negative (clear if value > max)
3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    diff &= (diff >> 31);
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value + diff;
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns signed 32 bit value pinned between min and max, inclusively
3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
344d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (value < min)
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = min;
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (value > max)
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (value < min)
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = min;
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    else if (value > max)
3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
359d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
360d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com                                       unsigned shift) {
3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((int)cond == 0 || (int)cond == 1);
3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (bits & ~(1 << shift)) | ((int)cond << shift);
3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
365d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
366d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com                                      uint32_t mask) {
3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return cond ? bits | mask : bits & ~mask;
3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3701fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com///////////////////////////////////////////////////////////////////////////////
3711fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
372325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org/** Use to combine multiple bits in a bitmask in a type safe way.
373325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org */
374325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgtemplate <typename T>
375325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgT SkTBitOr(T a, T b) {
376325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org    return (T)(a | b);
377325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org}
378325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org
3791fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/**
3801fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com *  Use to cast a pointer to a different type, and maintaining strict-aliasing
3811fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com */
3821fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.comtemplate <typename Dst> Dst SkTCast(const void* ptr) {
3831fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    union {
3841fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        const void* src;
3851fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        Dst dst;
3861fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    } data;
3871fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    data.src = ptr;
3881fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    return data.dst;
3891fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com}
3901fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable
3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkNoncopyable is the base class for objects that may do not want to
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator.
3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3987ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkNoncopyable {
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable() {}
4011fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable(const SkNoncopyable&);
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable& operator=(const SkNoncopyable&);
4058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable {
4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree() : fPtr(NULL) {}
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoFree() { sk_free(fPtr); }
4121fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the currently allocate buffer, or null
4148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Assign a new ptr allocated with sk_malloc (or null), and return the
4188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        previous ptr. Note it is the caller's responsibility to sk_free the
4198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        returned ptr.
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* set(void* ptr) {
4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        void* prev = fPtr;
4238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = ptr;
4248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return prev;
4258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4261fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Transfer ownership of the current ptr to the caller, setting the
4288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        internal reference to null. Note the caller is reponsible for calling
4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free on the returned address.
4308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* detach() { return this->set(NULL); }
4328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Free the current buffer, and set the internal reference to NULL. Same
4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        as calling sk_free(detach())
4358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void free() {
4378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free(fPtr);
4388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = NULL;
4398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* fPtr;
4438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // illegal
4448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree(const SkAutoFree&);
4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree& operator=(const SkAutoFree&);
4468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
4478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4487d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com/**
4497d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com *  Manage an allocated block of heap memory. This object is the sole manager of
4507d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com *  the lifetime of the block, so the caller must not call sk_free() or delete
4511c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com *  on the block, unless detach() was called.
4527d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */
4537d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comclass SkAutoMalloc : public SkNoncopyable {
4548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
4553ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com    explicit SkAutoMalloc(size_t size = 0) {
4563ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com        fPtr = size ? sk_malloc_throw(size) : NULL;
4577d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        fSize = size;
4587d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4607d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    ~SkAutoMalloc() {
4617d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        sk_free(fPtr);
4627d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4647d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
4651c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     *  Passed to reset to specify what happens if the requested size is smaller
4661c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     *  than the current size (and the current block was dynamically allocated).
4671c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     */
4681c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    enum OnShrink {
4691c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        /**
4701c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  If the requested size is smaller than the current size, and the
4711c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  current block is dynamically allocated, free the old block and
4721c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  malloc a new block of the smaller size.
4731c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         */
4741c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        kAlloc_OnShrink,
475fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
4761c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        /**
4771c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  If the requested size is smaller than the current size, and the
4781c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  current block is dynamically allocated, just return the old
4791c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  block.
4801c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         */
4811f90287df3129cb267422e482c52ebeca6a8990ftomhudson@google.com        kReuse_OnShrink
4821c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    };
4831c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
4841c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    /**
4857d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Reallocates the block to a new size. The ptr may or may not change.
4867d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
4879eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com    void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink,  bool* didChangeAlloc = NULL) {
4881c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
4899eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (NULL != didChangeAlloc) {
4909eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                *didChangeAlloc = false;
4919eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
4921c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com            return fPtr;
4937d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        }
4941c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
4951c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        sk_free(fPtr);
4961c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        fPtr = size ? sk_malloc_throw(size) : NULL;
4971c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        fSize = size;
4989eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        if (NULL != didChangeAlloc) {
4999eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            *didChangeAlloc = true;
5009eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        }
5011c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5027d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        return fPtr;
5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5047d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5057d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5067d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Releases the block back to the heap
5077d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5087d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void free() {
5097d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        this->reset(0);
5107d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
5117d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5127d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5137d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Return the allocated block.
5147d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
5157d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void* get() { return fPtr; }
5167d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    const void* get() const { return fPtr; }
5177d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
5186dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com   /** Transfer ownership of the current ptr to the caller, setting the
5196dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com       internal reference to null. Note the caller is reponsible for calling
5206dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com       sk_free on the returned address.
5216dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    */
5226dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    void* detach() {
5236dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        void* ptr = fPtr;
5246dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        fPtr = NULL;
5256dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        fSize = 0;
5266dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        return ptr;
5276dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    }
5286dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com
5297d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comprivate:
5307d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void*   fPtr;
5311c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    size_t  fSize;  // can be larger than the requested size (see kReuse)
5328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
5338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
53463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com/**
53563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  Manage an allocated block of memory. If the requested size is <= kSize, then
53663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  the allocation will come from the stack rather than the heap. This object
53763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  is the sole manager of the lifetime of the block, so the caller must not
53863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com *  call sk_free() or delete on the block.
53963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */
5408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
5418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
54263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
54363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Creates initially empty storage. get() returns a ptr, but it is to
5447d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  a zero-byte allocation. Must call reset(size) to return an allocated
54563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  block.
54663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
54763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    SkAutoSMalloc() {
54863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
5499eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        fSize = kSize;
5508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
55163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
55263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
55363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Allocate a block of the specified size. If size <= kSize, then the
55463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  allocation will come from the stack, otherwise it will be dynamically
55563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  allocated.
55663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
55763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    explicit SkAutoSMalloc(size_t size) {
55863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
5599eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        fSize = kSize;
5607d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        this->reset(size);
56163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
56263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
56363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
56463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Free the allocated block (if any). If the block was small enought to
56563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  have been allocated on the stack (size <= kSize) then this does nothing.
56663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
56763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    ~SkAutoSMalloc() {
56863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        if (fPtr != (void*)fStorage) {
5698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            sk_free(fPtr);
57063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
5718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
57263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
57363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
57463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Return the allocated block. May return non-null even if the block is
57563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  of zero size. Since this may be on the stack or dynamically allocated,
57663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
57763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  to manage it.
57863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
5798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
58063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
58163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
58263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  Return a new block of the requested size, freeing (as necessary) any
58363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  previously allocated block. As with the constructor, if size <= kSize
58463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  then the return block may be allocated locally, rather than from the
58563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     *  heap.
58663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
5871c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    void* reset(size_t size,
5889eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
5899eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                bool* didChangeAlloc = NULL) {
5909eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        size = (size < kSize) ? kSize : size;
5910f2b1953c7a8f8bb5a25d573dd9e215eaa10a2f8robertphillips@google.com        bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
5929eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        if (NULL != didChangeAlloc) {
5939eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            *didChangeAlloc = alloc;
5941c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        }
5959eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        if (alloc) {
5969eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (fPtr != (void*)fStorage) {
5979eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                sk_free(fPtr);
5989eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
5999eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com
6009eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (size == kSize) {
6019eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
6029eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                fPtr = fStorage;
6039eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            } else {
6049eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
6059eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
6069eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com
6079eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            fSize = size;
60863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
6099eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        SkASSERT(fSize >= size && fSize >= kSize);
6109eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        SkASSERT((fPtr == fStorage) || fSize > kSize);
61163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        return fPtr;
61263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
61363a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
6148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
6158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void*       fPtr;
6161c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    size_t      fSize;  // can be larger than the requested size (see kReuse)
6178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fStorage[(kSize + 3) >> 2];
6188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
6198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */
6218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
623