SkTypes.h revision 6f4293af6906721f09a12818ff3adc767badb3c7
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkTypes_DEFINED
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkTypes_DEFINED
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman// IWYU pragma: begin_exports
12f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon
13f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon// In at least two known scenarios when using GCC with libc++:
14f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon//  * GCC 4.8 targeting ARMv7 with NEON
15f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon//  * GCC 4.9 targeting ARMv8 64 bit
16f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon// we need to typedef float float32_t (or include <arm_neon.h> which does that)
17f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon// before #including <memory>. This makes no sense.  I'm not very interested in
18f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon// understanding why... these are old, bizarre platform configuration that we
19f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon// should just let die.
207ad42cfe87d07f20e629be5685b1507df931dd9ebungeman// See https://llvm.org/bugs/show_bug.cgi?id=25608 .
21f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon#include <ciso646>  // Include something innocuous to define _LIBCPP_VERISON if it's libc++.
22f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon#if defined(__GNUC__) && __GNUC__ == 4 \
237ad42cfe87d07f20e629be5685b1507df931dd9ebungeman && ((defined(__arm__) && (defined(__ARM_NEON__) || defined(__ARM_NEON))) || defined(__aarch64__)) \
24f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon && defined(_LIBCPP_VERSION)
25f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon    typedef float float32_t;
26f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon    #include <memory>
27f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon#endif
28f48c62fa6ae703f0e4fa7b97a381eb06afaadc4bbsalomon
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPreConfig.h"
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUserConfig.h"
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPostConfig.h"
32f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman#include <stddef.h>
33fab44db294846ff05d837b9cf0bf97a073891da7bungeman@google.com#include <stdint.h>
34f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman// IWYU pragma: end_exports
35f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman
36f20488b4f2139e6ca09fee7e39b731dd8ab467dbbungeman#include <string.h>
3795cc012ccaea20f372893ae277ea0a8a6339d094mtklein
38cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein/**
39cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein *  sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.
40cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein *
41cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * It is undefined behavior to call memcpy() with null dst or src, even if len is 0.
42cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * If an optimizer is "smart" enough, it can exploit this to do unexpected things.
43cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein *     memcpy(dst, src, 0);
44cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein *     if (src) {
45cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein *         printf("%x\n", *src);
46cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein *     }
47cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * In this code the compiler can assume src is not null and omit the if (src) {...} check,
48cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * unconditionally running the printf, crashing the program if src really is null.
49cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein * Of the compilers we pay attention to only GCC performs this optimization in practice.
50cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein */
51cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtkleinstatic inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
52cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein    // When we pass >0 len we had better already be passing valid pointers.
53cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein    // So we just need to skip calling memcpy when len == 0.
54cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein    if (len) {
55cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein        memcpy(dst,src,len);
56cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein    }
57cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein    return dst;
58cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein}
59cc881dafcbd00e8a811c47c14b472acdba5dd6c6mtklein
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkTypes.h
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
639aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com/** See SkGraphics::GetVersion() to retrieve these at runtime
649aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com */
659aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MAJOR  1
669aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MINOR  0
679aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_PATCH  0
689aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memory wrappers to be implemented by the porting layer (platform)
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we run out of memory. The platform implementation must
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    not return, but should either throw an exception or otherwise exit.
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
76de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_out_of_memory(void);
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we hit an unrecoverable error.
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The platform implementation must not return, but should either throw
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    an exception or otherwise exit.
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
81f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollenSK_API extern void sk_abort_no_print(void);
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum {
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MALLOC_TEMP  = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MALLOC_THROW = 0x02  //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a block of memory (at least 4-byte aligned) of at least the
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    specified size. If the requested memory cannot be returned, either
89519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com    return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
927ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
95de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_malloc_throw(size_t size);
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as standard realloc(), but this one never returns null on failure. It will throw
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    an exception if it fails.
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
99de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void* sk_realloc_throw(void* buffer, size_t size);
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Free memory returned by sk_malloc(). It is safe to pass null.
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
102de916c8ac866378cee9af2bf161c79f528d9ccd5reed@google.comSK_API extern void sk_free(void*);
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
104519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
105519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com */
106519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.comSK_API extern void* sk_calloc(size_t size);
107519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com
108519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
109519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com */
110519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.comSK_API extern void* sk_calloc_throw(size_t size);
111519f9677a41239808f41a7c13ef1f6e05eb1ed50mtklein@google.com
1124516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
1134516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.comstatic inline void sk_bzero(void* buffer, size_t size) {
11402046c50b294ae2b28e562b0e6e281e4ef823352mtklein    // Please c.f. sk_careful_memcpy.  It's undefined behavior to call memset(null, 0, 0).
11502046c50b294ae2b28e562b0e6e281e4ef823352mtklein    if (size) {
11602046c50b294ae2b28e562b0e6e281e4ef823352mtklein        memset(buffer, 0, size);
11702046c50b294ae2b28e562b0e6e281e4ef823352mtklein    }
1184516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com}
1194516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com
120bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com///////////////////////////////////////////////////////////////////////////////
121bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
12236352bf5e38f45a70ee4f4fc132a38048d38206dmtklein#ifdef override_GLOBAL_NEW
123bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#include <new>
124bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
125bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void* operator new(size_t size) {
126bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com    return sk_malloc_throw(size);
127bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com}
128bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
129bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.cominline void operator delete(void* p) {
130bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com    sk_free(p);
131bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com}
132bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com#endif
133bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com
134bdf736133b513bb13f7c66e01c8c37ac526ce8d4reed@google.com///////////////////////////////////////////////////////////////////////////////
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_INIT_TO_AVOID_WARNING    = 0
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDebugf
139c4ade57cd4ce5083e0fac19111a6ee6e32e18df4george    SK_API void SkDebugf(const char format[], ...);
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
142d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark#define SkREQUIRE_SEMICOLON_AFTER(code) do { code } while (false)
143d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark
144d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark#define SkASSERT_RELEASE(cond) \
145d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark    SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { SK_ABORT(#cond); } )
146f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollen
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
148d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark    #define SkASSERT(cond) \
149d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark        SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { SK_ABORT("assert(" #cond ")"); })
150d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark    #define SkASSERTF(cond, fmt, ...) \
151d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark        SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { \
152d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark                                      SkDebugf(fmt"\n", __VA_ARGS__); \
153d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark                                      SK_ABORT("assert(" #cond ")"); \
154d6562000efca50bc2bfddae8dcb69dce6b8c0950caryclark                                  })
1551f790aaeef47b02fe72b9b62d1e8c7ead85ae442bungeman    #define SkDEBUGFAIL(message)        SK_ABORT(message)
156966e3d30baf676dc399dc9c97ef15cc2c4f8d461herb    #define SkDEBUGFAILF(fmt, ...)      SkASSERTF(false, fmt, ##__VA_ARGS__)
157ceeaa78713dde9cc6e3ccd688aca6021b260af4dcsmartdalton    #define SkDEBUGCODE(...)            __VA_ARGS__
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDECLAREPARAM(type, var)   , type var
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkPARAM(var)                , var
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGF(args       )       SkDebugf args
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkAssertResult(cond)        SkASSERT(cond)
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkASSERT(cond)
1641f790aaeef47b02fe72b9b62d1e8c7ead85ae442bungeman    #define SkASSERTF(cond, fmt, ...)
1650c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com    #define SkDEBUGFAIL(message)
166e6f8ff00136b589c5ee1fc04de5d910d188fa98dhstern    #define SkDEBUGFAILF(fmt, ...)
167ceeaa78713dde9cc6e3ccd688aca6021b260af4dcsmartdalton    #define SkDEBUGCODE(...)
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDEBUGF(args)
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDECLAREPARAM(type, var)
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkPARAM(var)
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1729daa4b92d73a1ae441c864a0ae73dda6c3397acbbsalomon    // unlike SkASSERT, this guy executes its condition in the non-debug build.
1731b4c01c66081cd163535eab21b29a3996b01a6cfbsalomon    // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT.
1741b4c01c66081cd163535eab21b29a3996b01a6cfbsalomon    #define SkAssertResult(cond)         if (cond) {} do {} while(false)
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
177f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollen// Legacy macro names for SK_ABORT
178f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollen#define SkFAIL(message)                 SK_ABORT(message)
179f2b340fc885ad2a12d2d73974eff9c8f4c94192cdjsollen#define sk_throw()                      SK_ABORT("sk_throw")
18088cb22b6b4816c7a9ca6c5b795965b4606f9eb7bcommit-bot@chromium.org
1810f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifdef SK_IGNORE_TO_STRING
182bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com    #define SK_TO_STRING_NONVIRT()
183bc3d92a7d84b56eb235d6c2d9b7de00625200713skia.committer@gmail.com    #define SK_TO_STRING_VIRT()
1840f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_PUREVIRT()
1850f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_OVERRIDE()
1860f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#else
187d3ebb48320cf1b7e969974673e4bd7743816985ebungeman    class SkString;
1880f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    // the 'toString' helper functions convert Sk* objects to human-readable
1890f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    // form in developer mode
1900f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
1910f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
1920f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
19336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
19476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif
19576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
19649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/*
19749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  Usage:  SK_MACRO_CONCAT(a, b)   to construct the symbol ab
19849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *
19949a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
20049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *
20149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */
20249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT(X, Y)           SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
20349a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y)  X ## Y
20449a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com
20549a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com/*
20649a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *  Usage: SK_MACRO_APPEND_LINE(foo)    to make foo123, where 123 is the current
20749a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      line number. Easy way to construct
20849a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      unique names for local functions or
20949a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com *                                      variables.
21049a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com */
21149a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com#define SK_MACRO_APPEND_LINE(name)  SK_MACRO_CONCAT(name, __LINE__)
21249a5b1967ac5bbc6699bee9a2c66088c42aef6fdreed@google.com
213e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org/**
214e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * For some classes, it's almost always an error to instantiate one without a name, e.g.
215e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   {
216e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       SkAutoMutexAcquire(&mutex);
217e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <some code>
218e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   }
219e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
220e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * but instead the mutex is acquired and then immediately released.  The correct usage is
221e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   {
222e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       SkAutoMutexAcquire lock(&mutex);
223e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <some code>
224e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   }
225e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *
226e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
227e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * like this:
228e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   class classname {
229e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *       <your class>
230e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   };
231e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *   #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
232e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org *
233e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * This won't work with templates, and you must inline the class' constructors and destructors.
234e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
235e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org */
236e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SK_REQUIRE_LOCAL_VAR(classname) \
23799fe82260633fcf5d92cca38d12ef0937ecca61cbungeman    static_assert(false, "missing name for " #classname)
238e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
24237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 8 bits. Use for parameter passing and local variables,
24337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage.
24437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
24537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S8CPU;
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
24837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 8 bits. Use for parameter passing and local
24937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
25037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
25137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U8CPU;
25237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
25337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
25437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for signed 16 bits. Use for parameter passing and local variables,
25537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  not for storage
25637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
25737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef int S16CPU;
25837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
25937a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
26037a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Fast type for unsigned 16 bits. Use for parameter passing and local
26137a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  variables, not for storage
26237a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
26337a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef unsigned U16CPU;
26437a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com
26537a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com/**
26637a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com *  Meant to be a small version of bool, for storage purposes. Will be 0 or 1
26737a3133d2009b276f2126ccb61be1f69074cacb9reed@google.com */
26837a3133d2009b276f2126ccb61be1f69074cacb9reed@google.comtypedef uint8_t SkBool8;
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27068c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#include "../private/SkTFitsIn.h"
27168c14d9b3244ef9d58727ff11f2742be9236f46ebungemantemplate <typename D, typename S> D SkTo(S s) {
27268c14d9b3244ef9d58727ff11f2742be9236f46ebungeman    SkASSERT(SkTFitsIn<D>(s));
27368c14d9b3244ef9d58727ff11f2742be9236f46ebungeman    return static_cast<D>(s);
27468c14d9b3244ef9d58727ff11f2742be9236f46ebungeman}
27568c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToS8(x)    SkTo<int8_t>(x)
27668c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToU8(x)    SkTo<uint8_t>(x)
27768c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToS16(x)   SkTo<int16_t>(x)
27868c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToU16(x)   SkTo<uint16_t>(x)
27968c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToS32(x)   SkTo<int32_t>(x)
28068c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToU32(x)   SkTo<uint32_t>(x)
28168c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToInt(x)   SkTo<int>(x)
28268c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToUInt(x)  SkTo<unsigned>(x)
28368c14d9b3244ef9d58727ff11f2742be9236f46ebungeman#define SkToSizeT(x) SkTo<size_t>(x)
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2875c05d10511610c35f7912d48ede509c7e32e0a3cmtklein#define SkToBool(cond)  ((cond) != 0)
2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16   32767
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16   -32767
2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16   0xFFFF
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16   0
2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32   0x7FFFFFFF
294594dd3cd78e2f970d53bb0934fbbb63b41e1d40ccaryclark@google.com#define SK_MinS32   -SK_MaxS32
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32   0xFFFFFFFF
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32   0
297952538ed50661ad7dff6ec2b7af3f921e1d91b52caryclark#define SK_NaN32    ((int) (1U << 31))
2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
299d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits
300d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
30190209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) {
302d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (int16_t)x == x;
303d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
304d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
305d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits
306d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */
30790209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) {
308d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com    return (uint16_t)x == x;
309d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com}
310d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com
3113127c99986dc932343aae5ccc575237d99c3aaeccaryclarkstatic inline int32_t SkLeftShift(int32_t value, int32_t shift) {
3123127c99986dc932343aae5ccc575237d99c3aaeccaryclark    return (int32_t) ((uint32_t) value << shift);
3133127c99986dc932343aae5ccc575237d99c3aaeccaryclark}
3143127c99986dc932343aae5ccc575237d99c3aaeccaryclark
3153127c99986dc932343aae5ccc575237d99c3aaeccaryclarkstatic inline int64_t SkLeftShift(int64_t value, int32_t shift) {
3163127c99986dc932343aae5ccc575237d99c3aaeccaryclark    return (int64_t) ((uint64_t) value << shift);
3173127c99986dc932343aae5ccc575237d99c3aaeccaryclark}
3183127c99986dc932343aae5ccc575237d99c3aaeccaryclark
319d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com//////////////////////////////////////////////////////////////////////////////
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
321fc00a7c5015ea534b0f28f8115bfb6253d275e1emtklein/** Returns the number of entries in an array (not a pointer) */
322fc00a7c5015ea534b0f28f8115bfb6253d275e1emtkleintemplate <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
32395b96d649547c6b89ae0eca0f88f965d90c531a5caryclark#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
325b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein// Can be used to bracket data types that must be dense, e.g. hash keys.
326b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein#if defined(__clang__)  // This should work on GCC too, but GCC diagnostic pop didn't seem to work!
327b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein    #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \
328b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein                                   _Pragma("GCC diagnostic error \"-Wpadded\"")
329b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein    #define SK_END_REQUIRE_DENSE   _Pragma("GCC diagnostic pop")
330b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein#else
331b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein    #define SK_BEGIN_REQUIRE_DENSE
332b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein    #define SK_END_REQUIRE_DENSE
333b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein#endif
334b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtklein
3356a259bfcc80a7b4765b99c7503e5a5b98b7e1523mtklein#define SkAlign2(x)     (((x) + 1) >> 1 << 1)
3366a259bfcc80a7b4765b99c7503e5a5b98b7e1523mtklein#define SkIsAlign2(x)   (0 == ((x) & 1))
337c6faa5a0c46004115739cf34e29f4a3aa56a2adcreed@google.com
3386a259bfcc80a7b4765b99c7503e5a5b98b7e1523mtklein#define SkAlign4(x)     (((x) + 3) >> 2 << 2)
3396a259bfcc80a7b4765b99c7503e5a5b98b7e1523mtklein#define SkIsAlign4(x)   (0 == ((x) & 3))
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3416a259bfcc80a7b4765b99c7503e5a5b98b7e1523mtklein#define SkAlign8(x)     (((x) + 7) >> 3 << 3)
3426a259bfcc80a7b4765b99c7503e5a5b98b7e1523mtklein#define SkIsAlign8(x)   (0 == ((x) & 7))
34301224d5d0a3228fe47e63d8346e0e433a87563a8tomhudson@google.com
3446a259bfcc80a7b4765b99c7503e5a5b98b7e1523mtklein#define SkAlign16(x)     (((x) + 15) >> 4 << 4)
3456a259bfcc80a7b4765b99c7503e5a5b98b7e1523mtklein#define SkIsAlign16(x)   (0 == ((x) & 15))
346e2b0a0a0349b1c2acbd1eb5c1da7eed012f0980dreed
3476a259bfcc80a7b4765b99c7503e5a5b98b7e1523mtklein#define SkAlignPtr(x)   (sizeof(void*) == 8 ?   SkAlign8(x) :   SkAlign4(x))
3486a259bfcc80a7b4765b99c7503e5a5b98b7e1523mtklein#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
3490209e95cc2625a445c1cb6c4213d2182e5c832d7mtklein
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag;
3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d)    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value
3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar;
356ec4d4d784dbb250e572f8e04d18d0fd2ebeee851benjaminwagner
357d0e95a524c20932e0f4e68bab43995188a281395halcanary/** 16 bit unsigned integer to hold a glyph index
358d0e95a524c20932e0f4e68bab43995188a281395halcanary*/
359d0e95a524c20932e0f4e68bab43995188a281395halcanarytypedef uint16_t SkGlyphID;
360d0e95a524c20932e0f4e68bab43995188a281395halcanary
361ec4d4d784dbb250e572f8e04d18d0fd2ebeee851benjaminwagner/** 32 bit value to hold a millisecond duration
362ec4d4d784dbb250e572f8e04d18d0fd2ebeee851benjaminwagner *  Note that SK_MSecMax is about 25 days.
363ec4d4d784dbb250e572f8e04d18d0fd2ebeee851benjaminwagner */
3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec;
3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds
3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000
368ec4d4d784dbb250e572f8e04d18d0fd2ebeee851benjaminwagner/** maximum representable milliseconds; 24d 20h 31m 23.647s.
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF
3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b)     ((int32_t)(a) - (int32_t)(b) < 0)
3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b)     ((int32_t)(a) - (int32_t)(b) <= 0)
3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3782b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org/** The generation IDs in Skia reserve 0 has an invalid marker.
3792b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org */
3802b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org#define SK_InvalidGenID     0
3811c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon/** The unique IDs in Skia reserve 0 has an invalid marker.
3821c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon */
3831c63bf6e90f160c9a0d7484dedfaf87c0aa341e9bsalomon#define SK_InvalidUniqueID  0
3842b4e370a2fe00168838e43f5a78ccc3b371609f5commit-bot@chromium.org
3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/****************************************************************************
3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    The rest of these only build with C++
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3927a5bcc5f59bdf122217bc8ca7e756e8c76bae9e1bsalomonstatic inline constexpr int Sk32ToBool(uint32_t n) {
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (n | (0-n)) >> 31;
3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
396ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com/** Generic swap function. Classes with efficient swaps should specialize this function to take
397ff436617d8f11297f0eff93ddd49fb9022d0843bbsalomon@google.com    their fast path. This function is used by SkTSort. */
398d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) {
3996f4293af6906721f09a12818ff3adc767badb3c7bungeman    T c(std::move(a));
4006f4293af6906721f09a12818ff3adc767badb3c7bungeman    a = std::move(b);
4016f4293af6906721f09a12818ff3adc767badb3c7bungeman    b = std::move(c);
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
404d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) {
40509a22e9597e271c44dc7c2b71c72cf62a7de1e19mtklein    SkASSERT(value != SK_NaN32);  // The most negative int32_t can't be negated.
40638bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value < 0) {
4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = -value;
40838bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4122b57dc6bb241a6627c4375ee54b73039983d03dareed@google.comtemplate <typename T> inline T SkTAbs(T value) {
4132b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    if (value < 0) {
4142b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com        value = -value;
4152b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    }
4162b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com    return value;
4172b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com}
4182b57dc6bb241a6627c4375ee54b73039983d03dareed@google.com
419d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) {
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a < b)
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
4238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
425d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) {
4268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a > b)
4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a = b;
4288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return a;
4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
431a0af771612db5ae36b74f1f536bfb335ecd0ec99halcanarytemplate <typename T> constexpr const T& SkTMin(const T& a, const T& b) {
4323b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com    return (a < b) ? a : b;
4333b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com}
4343b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com
435a0af771612db5ae36b74f1f536bfb335ecd0ec99halcanarytemplate <typename T> constexpr const T& SkTMax(const T& a, const T& b) {
4363b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com    return (b < a) ? a : b;
4373b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com}
4383b97af5add04489d57c7926ba6dc6f0013daf40fcaryclark@google.com
439d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) {
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (a >> 31) | ((unsigned) -a >> 31);
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
443d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) {
44438bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    if (value > max) {
4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = max;
44638bad32cf5297ec6908620fd174cd08c937d331acommit-bot@chromium.org    }
4478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return value;
4488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
45062ce0303fb3f9857ee3ab8df05934642c226b1a3bungeman/** Returns value pinned between min and max, inclusively. */
451a0af771612db5ae36b74f1f536bfb335ecd0ec99halcanarytemplate <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) {
45262ce0303fb3f9857ee3ab8df05934642c226b1a3bungeman    return SkTMax(SkTMin(value, max), min);
4538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4555ec26ae9bfca635ccc98283aad5deda11519d826bsalomon
4565ec26ae9bfca635ccc98283aad5deda11519d826bsalomon///////////////////////////////////////////////////////////////////////////////
4575ec26ae9bfca635ccc98283aad5deda11519d826bsalomon
4585ec26ae9bfca635ccc98283aad5deda11519d826bsalomon/**
4595ec26ae9bfca635ccc98283aad5deda11519d826bsalomon *  Indicates whether an allocation should count against a cache budget.
4605ec26ae9bfca635ccc98283aad5deda11519d826bsalomon */
4615ec26ae9bfca635ccc98283aad5deda11519d826bsalomonenum class SkBudgeted : bool {
4625ec26ae9bfca635ccc98283aad5deda11519d826bsalomon    kNo  = false,
4635ec26ae9bfca635ccc98283aad5deda11519d826bsalomon    kYes = true
4645ec26ae9bfca635ccc98283aad5deda11519d826bsalomon};
4655ec26ae9bfca635ccc98283aad5deda11519d826bsalomon
46676948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips/**
46776948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips * Indicates whether a backing store needs to be an exact match or can be larger
46876948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips * than is strictly necessary
46976948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips */
47076948d4faaca9fd7730576e2f79790ca8d93c10brobertphillipsenum class SkBackingFit {
47176948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips    kApprox,
47276948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips    kExact
47376948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips};
47476948d4faaca9fd7730576e2f79790ca8d93c10brobertphillips
4751fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com///////////////////////////////////////////////////////////////////////////////
4761fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
477325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org/** Use to combine multiple bits in a bitmask in a type safe way.
478325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org */
479325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgtemplate <typename T>
480325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.orgT SkTBitOr(T a, T b) {
481325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org    return (T)(a | b);
482325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org}
483325cb9aa17b94258b362082eb3a799524f4345f3vandebo@chromium.org
4841fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com/**
4851fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com *  Use to cast a pointer to a different type, and maintaining strict-aliasing
4861fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com */
4871fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.comtemplate <typename Dst> Dst SkTCast(const void* ptr) {
4881fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    union {
4891fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        const void* src;
4901fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com        Dst dst;
4911fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    } data;
4921fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    data.src = ptr;
4931fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com    return data.dst;
4941fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com}
4951fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable
4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
500055f6b59d879b2adac52748ea5a58c8a05bf501cfmalitaSkNoncopyable is the base class for objects that do not want to
5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator.
5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
5037ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkNoncopyable {
5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable() {}
5061fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable(const SkNoncopyable&);
5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkNoncopyable& operator=(const SkNoncopyable&);
5108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
5118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable {
5138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
5148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree() : fPtr(NULL) {}
5158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
5168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkAutoFree() { sk_free(fPtr); }
5171fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
5188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the currently allocate buffer, or null
5198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
5208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
5218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Assign a new ptr allocated with sk_malloc (or null), and return the
5238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        previous ptr. Note it is the caller's responsibility to sk_free the
5248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        returned ptr.
5258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
5268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* set(void* ptr) {
5278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        void* prev = fPtr;
5288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = ptr;
5298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return prev;
5308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5311fcd51e6b2a210a37b9b9c2cfb82e1be7196e42areed@google.com
5328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Transfer ownership of the current ptr to the caller, setting the
5338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        internal reference to null. Note the caller is reponsible for calling
5348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free on the returned address.
5358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
53618300a3aa7cb6eb55d21bb0450dffa58b6fc062cmtklein    void* release() { return this->set(NULL); }
5378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Free the current buffer, and set the internal reference to NULL. Same
53918300a3aa7cb6eb55d21bb0450dffa58b6fc062cmtklein        as calling sk_free(release())
5408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
541852f15da7ceb53cfb49b9f728baa6dbc53b27694mtklein    void reset() {
5428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sk_free(fPtr);
5438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPtr = NULL;
5448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
5478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* fPtr;
5488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // illegal
5498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree(const SkAutoFree&);
5508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkAutoFree& operator=(const SkAutoFree&);
5518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
552e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5547d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com/**
5557d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com *  Manage an allocated block of heap memory. This object is the sole manager of
5567d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com *  the lifetime of the block, so the caller must not call sk_free() or delete
55718300a3aa7cb6eb55d21bb0450dffa58b6fc062cmtklein *  on the block, unless release() was called.
5587d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com */
559e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.orgclass SkAutoMalloc : SkNoncopyable {
5608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
5613ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com    explicit SkAutoMalloc(size_t size = 0) {
5623ab4195445d8541b2950bd30ccaefe1e345afa25reed@google.com        fPtr = size ? sk_malloc_throw(size) : NULL;
5637d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        fSize = size;
5647d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
5658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5667d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    ~SkAutoMalloc() {
5677d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        sk_free(fPtr);
5687d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    }
5698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5707d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
5711c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     *  Passed to reset to specify what happens if the requested size is smaller
5721c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     *  than the current size (and the current block was dynamically allocated).
5731c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com     */
5741c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    enum OnShrink {
5751c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        /**
5761c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  If the requested size is smaller than the current size, and the
5771c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  current block is dynamically allocated, free the old block and
5781c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  malloc a new block of the smaller size.
5791c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         */
5801c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        kAlloc_OnShrink,
581fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
5821c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        /**
5831c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  If the requested size is smaller than the current size, and the
5841c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  current block is dynamically allocated, just return the old
5851c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         *  block.
5861c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com         */
5871f90287df3129cb267422e482c52ebeca6a8990ftomhudson@google.com        kReuse_OnShrink
5881c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    };
5891c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
5901c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    /**
5917d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Reallocates the block to a new size. The ptr may or may not change.
5927d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
593852f15da7ceb53cfb49b9f728baa6dbc53b27694mtklein    void* reset(size_t size = 0, OnShrink shrink = kAlloc_OnShrink,  bool* didChangeAlloc = NULL) {
5941c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
59549f085dddff10473b6ebf832a974288300224e60bsalomon            if (didChangeAlloc) {
5969eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                *didChangeAlloc = false;
5979eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
5981c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com            return fPtr;
5997d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        }
6001c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
6011c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        sk_free(fPtr);
6021c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        fPtr = size ? sk_malloc_throw(size) : NULL;
6031c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        fSize = size;
60449f085dddff10473b6ebf832a974288300224e60bsalomon        if (didChangeAlloc) {
6059eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            *didChangeAlloc = true;
6069eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        }
6071c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com
6087d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        return fPtr;
6098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
6107d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
6117d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    /**
6127d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     *  Return the allocated block.
6137d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com     */
6147d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void* get() { return fPtr; }
6157d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    const void* get() const { return fPtr; }
6167d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com
6176dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com   /** Transfer ownership of the current ptr to the caller, setting the
6186dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com       internal reference to null. Note the caller is reponsible for calling
6196dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com       sk_free on the returned address.
6206dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    */
62118300a3aa7cb6eb55d21bb0450dffa58b6fc062cmtklein    void* release() {
6226dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        void* ptr = fPtr;
6236dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        fPtr = NULL;
6246dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        fSize = 0;
6256dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com        return ptr;
6266dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com    }
6276dcd27cd5e2e1f7a1f4d85bd546586a4d2bd4f41bsalomon@google.com
6287d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.comprivate:
6297d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com    void*   fPtr;
6301c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    size_t  fSize;  // can be larger than the requested size (see kReuse)
6318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
632e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
6338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
63463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com/**
635f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner *  Manage an allocated block of memory. If the requested size is <= kSizeRequested (or slightly
636f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner *  more), then the allocation will come from the stack rather than the heap. This object is the
637f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner *  sole manager of the lifetime of the block, so the caller must not call sk_free() or delete on
638f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner *  the block.
63963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com */
640f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagnertemplate <size_t kSizeRequested> class SkAutoSMalloc : SkNoncopyable {
6418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
64263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
643f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  Creates initially empty storage. get() returns a ptr, but it is to a zero-byte allocation.
644f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  Must call reset(size) to return an allocated block.
64563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
64663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    SkAutoSMalloc() {
64763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
6489eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        fSize = kSize;
6498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
65063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
65163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
652f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  Allocate a block of the specified size. If size <= kSizeRequested (or slightly more), then
653f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  the allocation will come from the stack, otherwise it will be dynamically allocated.
65463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
65563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    explicit SkAutoSMalloc(size_t size) {
65663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        fPtr = fStorage;
6579eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        fSize = kSize;
6587d4679a2e1aaa1953bc20d668135c517ee488c11bsalomon@google.com        this->reset(size);
65963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
66063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
66163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
662f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  Free the allocated block (if any). If the block was small enough to have been allocated on
663f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  the stack, then this does nothing.
66463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
66563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    ~SkAutoSMalloc() {
66663a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        if (fPtr != (void*)fStorage) {
6678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            sk_free(fPtr);
66863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
6698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
67063a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
67163a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
672f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  Return the allocated block. May return non-null even if the block is of zero size. Since
673f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  this may be on the stack or dynamically allocated, the caller must not call sk_free() on it,
674f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  but must rely on SkAutoSMalloc to manage it.
67563a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
6768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* get() const { return fPtr; }
67763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
67863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    /**
679f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  Return a new block of the requested size, freeing (as necessary) any previously allocated
680f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  block. As with the constructor, if size <= kSizeRequested (or slightly more) then the return
681f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner     *  block may be allocated locally, rather than from the heap.
68263a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com     */
6831c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    void* reset(size_t size,
6849eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
6859eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                bool* didChangeAlloc = NULL) {
6869eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        size = (size < kSize) ? kSize : size;
6870f2b1953c7a8f8bb5a25d573dd9e215eaa10a2f8robertphillips@google.com        bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
68849f085dddff10473b6ebf832a974288300224e60bsalomon        if (didChangeAlloc) {
6899eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            *didChangeAlloc = alloc;
6901c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com        }
6919eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        if (alloc) {
6929eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (fPtr != (void*)fStorage) {
6939eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                sk_free(fPtr);
6949eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
6959eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com
6969eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            if (size == kSize) {
6979eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
6989eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                fPtr = fStorage;
6999eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            } else {
7009eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com                fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
7019eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            }
7029eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com
7039eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com            fSize = size;
70463a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        }
7059eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        SkASSERT(fSize >= size && fSize >= kSize);
7069eb6645956a46e81336a2ed9e705a5360163c4f9bsalomon@google.com        SkASSERT((fPtr == fStorage) || fSize > kSize);
70763a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com        return fPtr;
70863a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com    }
70963a6060fcdc16245ea3958e7fd88ae32c5e631a3reed@google.com
7108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
711f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner    // Align up to 32 bits.
712f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner    static const size_t kSizeAlign4 = SkAlign4(kSizeRequested);
713f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner#if defined(GOOGLE3)
714f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner    // Stack frame size is limited for GOOGLE3. 4k is less than the actual max, but some functions
715f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner    // have multiple large stack allocations.
716f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner    static const size_t kMaxBytes = 4 * 1024;
717f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner    static const size_t kSize = kSizeRequested > kMaxBytes ? kMaxBytes : kSizeAlign4;
718f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner#else
719f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner    static const size_t kSize = kSizeAlign4;
720f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner#endif
721f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner
7228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void*       fPtr;
7231c401d8f42dbb3d2fd8f249a2acbe4bac829ff00reed@google.com    size_t      fSize;  // can be larger than the requested size (see kReuse)
724f49c75a8f1eacf8e6cb19ce0dcc1cc9bcbf1f96ebenjaminwagner    uint32_t    fStorage[kSize >> 2];
7258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
726e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org// Can't guard the constructor because it's a template class.
7278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */
7298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
731