blob: 2e495117d83b5dc80c156dea636882626e1cf22f [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkTypes_DEFINED
9#define SkTypes_DEFINED
10
bungemanf20488b2015-07-29 11:49:40 -070011// IWYU pragma: begin_exports
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkPreConfig.h"
13#include "SkUserConfig.h"
14#include "SkPostConfig.h"
bungemanf20488b2015-07-29 11:49:40 -070015#include <stddef.h>
bungeman@google.comfab44db2013-10-11 18:50:45 +000016#include <stdint.h>
bsalomonef3fcd82014-12-12 08:51:38 -080017#include <sys/types.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
mtklein95cc0122015-04-27 15:11:01 -070019#if defined(SK_ARM_HAS_NEON)
20 #include <arm_neon.h>
21#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
22 #include <immintrin.h>
23#endif
bungemanf20488b2015-07-29 11:49:40 -070024// IWYU pragma: end_exports
25
26#include <stdlib.h>
27#include <string.h>
mtklein95cc0122015-04-27 15:11:01 -070028
reed@android.com8a1c16f2008-12-17 15:59:43 +000029/** \file SkTypes.h
30*/
31
reed@android.com9aa8b322010-04-13 13:22:54 +000032/** See SkGraphics::GetVersion() to retrieve these at runtime
33 */
34#define SKIA_VERSION_MAJOR 1
35#define SKIA_VERSION_MINOR 0
36#define SKIA_VERSION_PATCH 0
37
reed@android.com8a1c16f2008-12-17 15:59:43 +000038/*
39 memory wrappers to be implemented by the porting layer (platform)
40*/
41
42/** Called internally if we run out of memory. The platform implementation must
43 not return, but should either throw an exception or otherwise exit.
44*/
reed@google.comde916c82011-10-19 19:50:48 +000045SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046/** Called internally if we hit an unrecoverable error.
47 The platform implementation must not return, but should either throw
48 an exception or otherwise exit.
49*/
reed@google.comde916c82011-10-19 19:50:48 +000050SK_API extern void sk_throw(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051
52enum {
53 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
54 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
55};
56/** Return a block of memory (at least 4-byte aligned) of at least the
57 specified size. If the requested memory cannot be returned, either
mtklein@google.com519f9672013-09-20 14:31:45 +000058 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
60*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000061SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000062/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
63*/
reed@google.comde916c82011-10-19 19:50:48 +000064SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000065/** Same as standard realloc(), but this one never returns null on failure. It will throw
66 an exception if it fails.
67*/
reed@google.comde916c82011-10-19 19:50:48 +000068SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000069/** Free memory returned by sk_malloc(). It is safe to pass null.
70*/
reed@google.comde916c82011-10-19 19:50:48 +000071SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
mtklein@google.com519f9672013-09-20 14:31:45 +000073/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
74 */
75SK_API extern void* sk_calloc(size_t size);
76
77/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
78 */
79SK_API extern void* sk_calloc_throw(size_t size);
80
reed@android.com4516f472009-06-29 16:25:36 +000081// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
82static inline void sk_bzero(void* buffer, size_t size) {
83 memset(buffer, 0, size);
84}
85
reed@google.combdf73612011-09-06 14:56:20 +000086///////////////////////////////////////////////////////////////////////////////
87
mtklein36352bf2015-03-25 18:17:31 -070088#ifdef override_GLOBAL_NEW
reed@google.combdf73612011-09-06 14:56:20 +000089#include <new>
90
91inline void* operator new(size_t size) {
92 return sk_malloc_throw(size);
93}
94
95inline void operator delete(void* p) {
96 sk_free(p);
97}
98#endif
99
100///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101
102#define SK_INIT_TO_AVOID_WARNING = 0
103
104#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -0700105 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106#endif
107
108#ifdef SK_DEBUG
commit-bot@chromium.org45d1d1d2014-04-30 18:24:16 +0000109 #define SkASSERT(cond) SK_ALWAYSBREAK(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000110 #define SkDEBUGFAIL(message) SkASSERT(false && message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 #define SkDEBUGCODE(code) code
112 #define SkDECLAREPARAM(type, var) , type var
113 #define SkPARAM(var) , var
114// #define SkDEBUGF(args ) SkDebugf##args
115 #define SkDEBUGF(args ) SkDebugf args
116 #define SkAssertResult(cond) SkASSERT(cond)
117#else
118 #define SkASSERT(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000119 #define SkDEBUGFAIL(message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 #define SkDEBUGCODE(code)
121 #define SkDEBUGF(args)
122 #define SkDECLAREPARAM(type, var)
123 #define SkPARAM(var)
124
125 // unlike SkASSERT, this guy executes its condition in the non-debug build
126 #define SkAssertResult(cond) cond
127#endif
128
commit-bot@chromium.org45d1d1d2014-04-30 18:24:16 +0000129#define SkFAIL(message) SK_ALWAYSBREAK(false && message)
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000130
mtkleinb59161f2014-06-18 07:54:47 -0700131// We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form.
132// So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond,
133// and if it's true the assert passes; if it's false, we'll print the message and the assert fails.
134#define SkASSERTF(cond, fmt, ...) SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false))
135
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000136#ifdef SK_DEVELOPER
137 #define SkDEVCODE(code) code
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000138#else
139 #define SkDEVCODE(code)
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000140#endif
141
142#ifdef SK_IGNORE_TO_STRING
skia.committer@gmail.combc3d92a2014-03-14 03:02:26 +0000143 #define SK_TO_STRING_NONVIRT()
144 #define SK_TO_STRING_VIRT()
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000145 #define SK_TO_STRING_PUREVIRT()
146 #define SK_TO_STRING_OVERRIDE()
147#else
bungemand3ebb482015-08-05 13:57:49 -0700148 class SkString;
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000149 // the 'toString' helper functions convert Sk* objects to human-readable
150 // form in developer mode
151 #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
152 #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
153 #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
mtklein36352bf2015-03-25 18:17:31 -0700154 #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000155#endif
156
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000157template <bool>
158struct SkCompileAssert {
159};
160
bungeman@google.com562b2e62014-03-12 21:41:06 +0000161// Uses static_cast<bool>(expr) instead of bool(expr) due to
162// https://connect.microsoft.com/VisualStudio/feedback/details/832915
bungeman@google.com9c28fa52014-03-13 17:51:05 +0000163
164// The extra parentheses in SkCompileAssert<(...)> are a work around for
165// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57771
166// which was fixed in gcc 4.8.2.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000167#define SK_COMPILE_ASSERT(expr, msg) \
bungeman@google.com2f582092014-03-12 21:56:23 +0000168 typedef SkCompileAssert<(static_cast<bool>(expr))> \
bungeman@google.com562b2e62014-03-12 21:41:06 +0000169 msg[static_cast<bool>(expr) ? 1 : -1] SK_UNUSED
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000170
reed@google.com49a5b192012-10-25 17:31:39 +0000171/*
172 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
173 *
174 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
175 *
176 */
177#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
178#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
179
180/*
181 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
182 * line number. Easy way to construct
183 * unique names for local functions or
184 * variables.
185 */
186#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
187
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000188/**
189 * For some classes, it's almost always an error to instantiate one without a name, e.g.
190 * {
191 * SkAutoMutexAcquire(&mutex);
192 * <some code>
193 * }
194 * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
195 * but instead the mutex is acquired and then immediately released. The correct usage is
196 * {
197 * SkAutoMutexAcquire lock(&mutex);
198 * <some code>
199 * }
200 *
201 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
202 * like this:
203 * class classname {
204 * <your class>
205 * };
206 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
207 *
208 * This won't work with templates, and you must inline the class' constructors and destructors.
209 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
210 */
211#define SK_REQUIRE_LOCAL_VAR(classname) \
212 SK_COMPILE_ASSERT(false, missing_name_for_##classname)
213
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214///////////////////////////////////////////////////////////////////////
215
reed@google.com37a31332011-01-25 14:55:42 +0000216/**
217 * Fast type for signed 8 bits. Use for parameter passing and local variables,
218 * not for storage.
219 */
220typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221
reed@google.com37a31332011-01-25 14:55:42 +0000222/**
223 * Fast type for unsigned 8 bits. Use for parameter passing and local
224 * variables, not for storage
225 */
226typedef unsigned U8CPU;
227
228/**
229 * Fast type for signed 16 bits. Use for parameter passing and local variables,
230 * not for storage
231 */
232typedef int S16CPU;
233
234/**
235 * Fast type for unsigned 16 bits. Use for parameter passing and local
236 * variables, not for storage
237 */
238typedef unsigned U16CPU;
239
240/**
241 * Meant to be faster than bool (doesn't promise to be 0 or 1,
242 * just 0 or non-zero
243 */
244typedef int SkBool;
245
246/**
247 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
248 */
249typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250
251#ifdef SK_DEBUG
bungeman@google.com777ded02013-07-19 22:30:11 +0000252 SK_API int8_t SkToS8(intmax_t);
253 SK_API uint8_t SkToU8(uintmax_t);
254 SK_API int16_t SkToS16(intmax_t);
255 SK_API uint16_t SkToU16(uintmax_t);
256 SK_API int32_t SkToS32(intmax_t);
257 SK_API uint32_t SkToU32(uintmax_t);
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000258 SK_API int SkToInt(intmax_t);
reed@google.com7fa2a652014-01-27 13:42:58 +0000259 SK_API unsigned SkToUInt(uintmax_t);
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000260 SK_API size_t SkToSizeT(uintmax_t);
bsalomonef3fcd82014-12-12 08:51:38 -0800261 SK_API off_t SkToOffT(intmax_t x);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262#else
263 #define SkToS8(x) ((int8_t)(x))
264 #define SkToU8(x) ((uint8_t)(x))
265 #define SkToS16(x) ((int16_t)(x))
266 #define SkToU16(x) ((uint16_t)(x))
267 #define SkToS32(x) ((int32_t)(x))
268 #define SkToU32(x) ((uint32_t)(x))
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000269 #define SkToInt(x) ((int)(x))
reed@google.com7fa2a652014-01-27 13:42:58 +0000270 #define SkToUInt(x) ((unsigned)(x))
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000271 #define SkToSizeT(x) ((size_t)(x))
bsalomonef3fcd82014-12-12 08:51:38 -0800272 #define SkToOffT(x) ((off_t)(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273#endif
274
275/** Returns 0 or 1 based on the condition
276*/
277#define SkToBool(cond) ((cond) != 0)
278
279#define SK_MaxS16 32767
280#define SK_MinS16 -32767
281#define SK_MaxU16 0xFFFF
282#define SK_MinU16 0
283#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000284#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285#define SK_MaxU32 0xFFFFFFFF
286#define SK_MinU32 0
humper@google.com0e515772013-01-07 19:54:40 +0000287#define SK_NaN32 (1 << 31)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288
reed@android.comd4577752009-11-21 02:48:11 +0000289/** Returns true if the value can be represented with signed 16bits
290 */
reed@android.com90209ca2009-11-21 19:58:04 +0000291static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000292 return (int16_t)x == x;
293}
294
295/** Returns true if the value can be represented with unsigned 16bits
296 */
reed@android.com90209ca2009-11-21 19:58:04 +0000297static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000298 return (uint16_t)x == x;
299}
300
301//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302#ifndef SK_OFFSETOF
reed@google.com56d3a232012-03-21 12:25:48 +0000303 #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304#endif
305
mtkleinfc00a7c2015-05-07 10:58:44 -0700306/** Returns the number of entries in an array (not a pointer) */
307template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
caryclark95b96d62015-08-19 10:12:59 -0700308#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310#define SkAlign2(x) (((x) + 1) >> 1 << 1)
reed@google.comc6faa5a2012-06-27 15:07:11 +0000311#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312
reed@google.comc6faa5a2012-06-27 15:07:11 +0000313#define SkAlign4(x) (((x) + 3) >> 2 << 2)
314#define SkIsAlign4(x) (0 == ((x) & 3))
315
316#define SkAlign8(x) (((x) + 7) >> 3 << 3)
317#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000318
mtklein0209e952014-08-28 14:10:05 -0700319#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x))
320#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
321
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322typedef uint32_t SkFourByteTag;
323#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
324
325/** 32 bit integer to hold a unicode value
326*/
327typedef int32_t SkUnichar;
328/** 32 bit value to hold a millisecond count
329*/
330typedef uint32_t SkMSec;
331/** 1 second measured in milliseconds
332*/
333#define SK_MSec1 1000
334/** maximum representable milliseconds
335*/
336#define SK_MSecMax 0x7FFFFFFF
337/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
338*/
339#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
340/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
341*/
342#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
343
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000344/** The generation IDs in Skia reserve 0 has an invalid marker.
345 */
346#define SK_InvalidGenID 0
bsalomon1c63bf62014-07-22 13:09:46 -0700347/** The unique IDs in Skia reserve 0 has an invalid marker.
348 */
349#define SK_InvalidUniqueID 0
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000350
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351/****************************************************************************
352 The rest of these only build with C++
353*/
354#ifdef __cplusplus
355
356/** Faster than SkToBool for integral conditions. Returns 0 or 1
357*/
reed@android.comd4577752009-11-21 02:48:11 +0000358static inline int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 return (n | (0-n)) >> 31;
360}
361
bsalomon@google.comff436612013-02-27 19:07:32 +0000362/** Generic swap function. Classes with efficient swaps should specialize this function to take
363 their fast path. This function is used by SkTSort. */
reed@android.comd4577752009-11-21 02:48:11 +0000364template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 T c(a);
366 a = b;
367 b = c;
368}
369
reed@android.comd4577752009-11-21 02:48:11 +0000370static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800371 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000372 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000374 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376}
377
reed@google.com2b57dc62013-01-08 13:23:32 +0000378template <typename T> inline T SkTAbs(T value) {
379 if (value < 0) {
380 value = -value;
381 }
382 return value;
383}
384
reed@android.comd4577752009-11-21 02:48:11 +0000385static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 if (a < b)
387 a = b;
388 return a;
389}
390
reed@android.comd4577752009-11-21 02:48:11 +0000391static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392 if (a > b)
393 a = b;
394 return a;
395}
396
caryclark@google.com3b97af52013-04-23 11:56:44 +0000397template <typename T> const T& SkTMin(const T& a, const T& b) {
398 return (a < b) ? a : b;
399}
400
401template <typename T> const T& SkTMax(const T& a, const T& b) {
402 return (b < a) ? a : b;
403}
404
reed@android.comd4577752009-11-21 02:48:11 +0000405static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406 return (a >> 31) | ((unsigned) -a >> 31);
407}
408
reed@android.comd4577752009-11-21 02:48:11 +0000409static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000410 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000411 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000412 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000413 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414}
415
bungemanfd0ecf42015-04-16 12:18:28 -0700416template <typename T> static inline const T& SkTPin(const T& x, const T& min, const T& max) {
417 return SkTMax(SkTMin(x, max), min);
418}
419
420/** Returns signed 32 bit value pinned between min and max, inclusively. */
reed@android.comd4577752009-11-21 02:48:11 +0000421static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
bungemanfd0ecf42015-04-16 12:18:28 -0700422 return SkTPin(value, min, max);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000423}
424
reed@android.comd4577752009-11-21 02:48:11 +0000425static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
426 unsigned shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427 SkASSERT((int)cond == 0 || (int)cond == 1);
428 return (bits & ~(1 << shift)) | ((int)cond << shift);
429}
430
reed@android.comd4577752009-11-21 02:48:11 +0000431static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
432 uint32_t mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433 return cond ? bits | mask : bits & ~mask;
434}
435
reed@google.com1fcd51e2011-01-05 15:50:27 +0000436///////////////////////////////////////////////////////////////////////////////
437
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000438/** Use to combine multiple bits in a bitmask in a type safe way.
439 */
440template <typename T>
441T SkTBitOr(T a, T b) {
442 return (T)(a | b);
443}
444
reed@google.com1fcd51e2011-01-05 15:50:27 +0000445/**
446 * Use to cast a pointer to a different type, and maintaining strict-aliasing
447 */
448template <typename Dst> Dst SkTCast(const void* ptr) {
449 union {
450 const void* src;
451 Dst dst;
452 } data;
453 data.src = ptr;
454 return data.dst;
455}
456
reed@android.com8a1c16f2008-12-17 15:59:43 +0000457//////////////////////////////////////////////////////////////////////////////
458
459/** \class SkNoncopyable
460
fmalita055f6b52015-04-09 08:49:32 -0700461SkNoncopyable is the base class for objects that do not want to
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462be copied. It hides its copy-constructor and its assignment-operator.
463*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000464class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000465public:
466 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000467
reed@android.com8a1c16f2008-12-17 15:59:43 +0000468private:
469 SkNoncopyable(const SkNoncopyable&);
470 SkNoncopyable& operator=(const SkNoncopyable&);
471};
472
473class SkAutoFree : SkNoncopyable {
474public:
475 SkAutoFree() : fPtr(NULL) {}
476 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
477 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000478
reed@android.com8a1c16f2008-12-17 15:59:43 +0000479 /** Return the currently allocate buffer, or null
480 */
481 void* get() const { return fPtr; }
482
483 /** Assign a new ptr allocated with sk_malloc (or null), and return the
484 previous ptr. Note it is the caller's responsibility to sk_free the
485 returned ptr.
486 */
487 void* set(void* ptr) {
488 void* prev = fPtr;
489 fPtr = ptr;
490 return prev;
491 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000492
reed@android.com8a1c16f2008-12-17 15:59:43 +0000493 /** Transfer ownership of the current ptr to the caller, setting the
494 internal reference to null. Note the caller is reponsible for calling
495 sk_free on the returned address.
496 */
497 void* detach() { return this->set(NULL); }
498
499 /** Free the current buffer, and set the internal reference to NULL. Same
500 as calling sk_free(detach())
501 */
502 void free() {
503 sk_free(fPtr);
504 fPtr = NULL;
505 }
506
507private:
508 void* fPtr;
509 // illegal
510 SkAutoFree(const SkAutoFree&);
511 SkAutoFree& operator=(const SkAutoFree&);
512};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000513#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000515/**
516 * Manage an allocated block of heap memory. This object is the sole manager of
517 * the lifetime of the block, so the caller must not call sk_free() or delete
reed@google.com1c401d82011-10-18 18:52:03 +0000518 * on the block, unless detach() was called.
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000519 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +0000520class SkAutoMalloc : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521public:
reed@google.com3ab41952011-10-18 18:32:46 +0000522 explicit SkAutoMalloc(size_t size = 0) {
523 fPtr = size ? sk_malloc_throw(size) : NULL;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000524 fSize = size;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000525 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000526
527 ~SkAutoMalloc() {
528 sk_free(fPtr);
529 }
530
531 /**
reed@google.com1c401d82011-10-18 18:52:03 +0000532 * Passed to reset to specify what happens if the requested size is smaller
533 * than the current size (and the current block was dynamically allocated).
534 */
535 enum OnShrink {
536 /**
537 * If the requested size is smaller than the current size, and the
538 * current block is dynamically allocated, free the old block and
539 * malloc a new block of the smaller size.
540 */
541 kAlloc_OnShrink,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000542
reed@google.com1c401d82011-10-18 18:52:03 +0000543 /**
544 * If the requested size is smaller than the current size, and the
545 * current block is dynamically allocated, just return the old
546 * block.
547 */
tomhudson@google.com1f902872012-06-01 13:15:47 +0000548 kReuse_OnShrink
reed@google.com1c401d82011-10-18 18:52:03 +0000549 };
550
551 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000552 * Reallocates the block to a new size. The ptr may or may not change.
553 */
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000554 void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) {
reed@google.com1c401d82011-10-18 18:52:03 +0000555 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
bsalomon49f085d2014-09-05 13:34:00 -0700556 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000557 *didChangeAlloc = false;
558 }
reed@google.com1c401d82011-10-18 18:52:03 +0000559 return fPtr;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000560 }
reed@google.com1c401d82011-10-18 18:52:03 +0000561
562 sk_free(fPtr);
563 fPtr = size ? sk_malloc_throw(size) : NULL;
564 fSize = size;
bsalomon49f085d2014-09-05 13:34:00 -0700565 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000566 *didChangeAlloc = true;
567 }
reed@google.com1c401d82011-10-18 18:52:03 +0000568
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000569 return fPtr;
570 }
571
572 /**
573 * Releases the block back to the heap
574 */
575 void free() {
576 this->reset(0);
577 }
578
579 /**
580 * Return the allocated block.
581 */
582 void* get() { return fPtr; }
583 const void* get() const { return fPtr; }
584
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000585 /** Transfer ownership of the current ptr to the caller, setting the
586 internal reference to null. Note the caller is reponsible for calling
587 sk_free on the returned address.
588 */
589 void* detach() {
590 void* ptr = fPtr;
591 fPtr = NULL;
592 fSize = 0;
593 return ptr;
594 }
595
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000596private:
597 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000598 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000600#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000601
reed@google.com63a60602011-03-10 13:07:35 +0000602/**
603 * Manage an allocated block of memory. If the requested size is <= kSize, then
604 * the allocation will come from the stack rather than the heap. This object
605 * is the sole manager of the lifetime of the block, so the caller must not
606 * call sk_free() or delete on the block.
607 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000608template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
609public:
reed@google.com63a60602011-03-10 13:07:35 +0000610 /**
611 * Creates initially empty storage. get() returns a ptr, but it is to
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000612 * a zero-byte allocation. Must call reset(size) to return an allocated
reed@google.com63a60602011-03-10 13:07:35 +0000613 * block.
614 */
615 SkAutoSMalloc() {
616 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000617 fSize = kSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000618 }
reed@google.com63a60602011-03-10 13:07:35 +0000619
620 /**
621 * Allocate a block of the specified size. If size <= kSize, then the
622 * allocation will come from the stack, otherwise it will be dynamically
623 * allocated.
624 */
625 explicit SkAutoSMalloc(size_t size) {
626 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000627 fSize = kSize;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000628 this->reset(size);
reed@google.com63a60602011-03-10 13:07:35 +0000629 }
630
631 /**
632 * Free the allocated block (if any). If the block was small enought to
633 * have been allocated on the stack (size <= kSize) then this does nothing.
634 */
635 ~SkAutoSMalloc() {
636 if (fPtr != (void*)fStorage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637 sk_free(fPtr);
reed@google.com63a60602011-03-10 13:07:35 +0000638 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000639 }
reed@google.com63a60602011-03-10 13:07:35 +0000640
641 /**
642 * Return the allocated block. May return non-null even if the block is
643 * of zero size. Since this may be on the stack or dynamically allocated,
644 * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
645 * to manage it.
646 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000647 void* get() const { return fPtr; }
reed@google.com63a60602011-03-10 13:07:35 +0000648
649 /**
650 * Return a new block of the requested size, freeing (as necessary) any
651 * previously allocated block. As with the constructor, if size <= kSize
652 * then the return block may be allocated locally, rather than from the
653 * heap.
654 */
reed@google.com1c401d82011-10-18 18:52:03 +0000655 void* reset(size_t size,
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000656 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
657 bool* didChangeAlloc = NULL) {
658 size = (size < kSize) ? kSize : size;
robertphillips@google.com0f2b1952013-05-23 14:59:40 +0000659 bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
bsalomon49f085d2014-09-05 13:34:00 -0700660 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000661 *didChangeAlloc = alloc;
reed@google.com1c401d82011-10-18 18:52:03 +0000662 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000663 if (alloc) {
664 if (fPtr != (void*)fStorage) {
665 sk_free(fPtr);
666 }
reed@google.com1c401d82011-10-18 18:52:03 +0000667
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000668 if (size == kSize) {
669 SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
670 fPtr = fStorage;
671 } else {
672 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
673 }
reed@google.com63a60602011-03-10 13:07:35 +0000674
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000675 fSize = size;
reed@google.com63a60602011-03-10 13:07:35 +0000676 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000677 SkASSERT(fSize >= size && fSize >= kSize);
678 SkASSERT((fPtr == fStorage) || fSize > kSize);
reed@google.com63a60602011-03-10 13:07:35 +0000679 return fPtr;
680 }
681
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682private:
683 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000684 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685 uint32_t fStorage[(kSize + 3) >> 2];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000686};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000687// Can't guard the constructor because it's a template class.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688
689#endif /* C++ */
690
691#endif