blob: 261fcaea4210893cbedb8a61076b4358b5e5400e [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>
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
mtklein95cc0122015-04-27 15:11:01 -070018#if defined(SK_ARM_HAS_NEON)
19 #include <arm_neon.h>
20#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
21 #include <immintrin.h>
22#endif
bungemanf20488b2015-07-29 11:49:40 -070023// IWYU pragma: end_exports
24
bungemanf20488b2015-07-29 11:49:40 -070025#include <string.h>
mtklein95cc0122015-04-27 15:11:01 -070026
mtkleincc881da2015-12-08 11:55:17 -080027/**
28 * sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.
29 *
30 * It is undefined behavior to call memcpy() with null dst or src, even if len is 0.
31 * If an optimizer is "smart" enough, it can exploit this to do unexpected things.
32 * memcpy(dst, src, 0);
33 * if (src) {
34 * printf("%x\n", *src);
35 * }
36 * In this code the compiler can assume src is not null and omit the if (src) {...} check,
37 * unconditionally running the printf, crashing the program if src really is null.
38 * Of the compilers we pay attention to only GCC performs this optimization in practice.
39 */
40static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
41 // When we pass >0 len we had better already be passing valid pointers.
42 // So we just need to skip calling memcpy when len == 0.
43 if (len) {
44 memcpy(dst,src,len);
45 }
46 return dst;
47}
48
reed@android.com8a1c16f2008-12-17 15:59:43 +000049/** \file SkTypes.h
50*/
51
reed@android.com9aa8b322010-04-13 13:22:54 +000052/** See SkGraphics::GetVersion() to retrieve these at runtime
53 */
54#define SKIA_VERSION_MAJOR 1
55#define SKIA_VERSION_MINOR 0
56#define SKIA_VERSION_PATCH 0
57
reed@android.com8a1c16f2008-12-17 15:59:43 +000058/*
59 memory wrappers to be implemented by the porting layer (platform)
60*/
61
62/** Called internally if we run out of memory. The platform implementation must
63 not return, but should either throw an exception or otherwise exit.
64*/
reed@google.comde916c82011-10-19 19:50:48 +000065SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000066/** Called internally if we hit an unrecoverable error.
67 The platform implementation must not return, but should either throw
68 an exception or otherwise exit.
69*/
reed@google.comde916c82011-10-19 19:50:48 +000070SK_API extern void sk_throw(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000071
72enum {
73 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
74 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
75};
76/** Return a block of memory (at least 4-byte aligned) of at least the
77 specified size. If the requested memory cannot be returned, either
mtklein@google.com519f9672013-09-20 14:31:45 +000078 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
80*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000081SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
83*/
reed@google.comde916c82011-10-19 19:50:48 +000084SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085/** Same as standard realloc(), but this one never returns null on failure. It will throw
86 an exception if it fails.
87*/
reed@google.comde916c82011-10-19 19:50:48 +000088SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000089/** Free memory returned by sk_malloc(). It is safe to pass null.
90*/
reed@google.comde916c82011-10-19 19:50:48 +000091SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
mtklein@google.com519f9672013-09-20 14:31:45 +000093/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
94 */
95SK_API extern void* sk_calloc(size_t size);
96
97/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
98 */
99SK_API extern void* sk_calloc_throw(size_t size);
100
reed@android.com4516f472009-06-29 16:25:36 +0000101// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
102static inline void sk_bzero(void* buffer, size_t size) {
mtklein02046c52015-12-09 10:02:14 -0800103 // Please c.f. sk_careful_memcpy. It's undefined behavior to call memset(null, 0, 0).
104 if (size) {
105 memset(buffer, 0, size);
106 }
reed@android.com4516f472009-06-29 16:25:36 +0000107}
108
reed@google.combdf73612011-09-06 14:56:20 +0000109///////////////////////////////////////////////////////////////////////////////
110
mtklein36352bf2015-03-25 18:17:31 -0700111#ifdef override_GLOBAL_NEW
reed@google.combdf73612011-09-06 14:56:20 +0000112#include <new>
113
114inline void* operator new(size_t size) {
115 return sk_malloc_throw(size);
116}
117
118inline void operator delete(void* p) {
119 sk_free(p);
120}
121#endif
122
123///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124
125#define SK_INIT_TO_AVOID_WARNING = 0
126
127#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -0700128 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129#endif
130
131#ifdef SK_DEBUG
commit-bot@chromium.org45d1d1d2014-04-30 18:24:16 +0000132 #define SkASSERT(cond) SK_ALWAYSBREAK(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000133 #define SkDEBUGFAIL(message) SkASSERT(false && message)
herb966e3d32015-09-18 07:00:48 -0700134 #define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 #define SkDEBUGCODE(code) code
136 #define SkDECLAREPARAM(type, var) , type var
137 #define SkPARAM(var) , var
138// #define SkDEBUGF(args ) SkDebugf##args
139 #define SkDEBUGF(args ) SkDebugf args
140 #define SkAssertResult(cond) SkASSERT(cond)
141#else
142 #define SkASSERT(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000143 #define SkDEBUGFAIL(message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 #define SkDEBUGCODE(code)
145 #define SkDEBUGF(args)
146 #define SkDECLAREPARAM(type, var)
147 #define SkPARAM(var)
148
149 // unlike SkASSERT, this guy executes its condition in the non-debug build
150 #define SkAssertResult(cond) cond
151#endif
152
commit-bot@chromium.org45d1d1d2014-04-30 18:24:16 +0000153#define SkFAIL(message) SK_ALWAYSBREAK(false && message)
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000154
mtkleinb59161f2014-06-18 07:54:47 -0700155// We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form.
156// So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond,
157// and if it's true the assert passes; if it's false, we'll print the message and the assert fails.
158#define SkASSERTF(cond, fmt, ...) SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false))
159
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000160#ifdef SK_DEVELOPER
161 #define SkDEVCODE(code) code
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000162#else
163 #define SkDEVCODE(code)
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000164#endif
165
166#ifdef SK_IGNORE_TO_STRING
skia.committer@gmail.combc3d92a2014-03-14 03:02:26 +0000167 #define SK_TO_STRING_NONVIRT()
168 #define SK_TO_STRING_VIRT()
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000169 #define SK_TO_STRING_PUREVIRT()
170 #define SK_TO_STRING_OVERRIDE()
171#else
bungemand3ebb482015-08-05 13:57:49 -0700172 class SkString;
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000173 // the 'toString' helper functions convert Sk* objects to human-readable
174 // form in developer mode
175 #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
176 #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
177 #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
mtklein36352bf2015-03-25 18:17:31 -0700178 #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000179#endif
180
reed@google.com49a5b192012-10-25 17:31:39 +0000181/*
182 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
183 *
184 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
185 *
186 */
187#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
188#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
189
190/*
191 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
192 * line number. Easy way to construct
193 * unique names for local functions or
194 * variables.
195 */
196#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
197
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000198/**
199 * For some classes, it's almost always an error to instantiate one without a name, e.g.
200 * {
201 * SkAutoMutexAcquire(&mutex);
202 * <some code>
203 * }
204 * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
205 * but instead the mutex is acquired and then immediately released. The correct usage is
206 * {
207 * SkAutoMutexAcquire lock(&mutex);
208 * <some code>
209 * }
210 *
211 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
212 * like this:
213 * class classname {
214 * <your class>
215 * };
216 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
217 *
218 * This won't work with templates, and you must inline the class' constructors and destructors.
219 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
220 */
221#define SK_REQUIRE_LOCAL_VAR(classname) \
bungeman99fe8222015-08-20 07:57:51 -0700222 static_assert(false, "missing name for " #classname)
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000223
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224///////////////////////////////////////////////////////////////////////
225
reed@google.com37a31332011-01-25 14:55:42 +0000226/**
227 * Fast type for signed 8 bits. Use for parameter passing and local variables,
228 * not for storage.
229 */
230typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231
reed@google.com37a31332011-01-25 14:55:42 +0000232/**
233 * Fast type for unsigned 8 bits. Use for parameter passing and local
234 * variables, not for storage
235 */
236typedef unsigned U8CPU;
237
238/**
239 * Fast type for signed 16 bits. Use for parameter passing and local variables,
240 * not for storage
241 */
242typedef int S16CPU;
243
244/**
245 * Fast type for unsigned 16 bits. Use for parameter passing and local
246 * variables, not for storage
247 */
248typedef unsigned U16CPU;
249
250/**
251 * Meant to be faster than bool (doesn't promise to be 0 or 1,
252 * just 0 or non-zero
253 */
254typedef int SkBool;
255
256/**
257 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
258 */
259typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260
261#ifdef SK_DEBUG
bungeman@google.com777ded02013-07-19 22:30:11 +0000262 SK_API int8_t SkToS8(intmax_t);
263 SK_API uint8_t SkToU8(uintmax_t);
264 SK_API int16_t SkToS16(intmax_t);
265 SK_API uint16_t SkToU16(uintmax_t);
266 SK_API int32_t SkToS32(intmax_t);
267 SK_API uint32_t SkToU32(uintmax_t);
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000268 SK_API int SkToInt(intmax_t);
reed@google.com7fa2a652014-01-27 13:42:58 +0000269 SK_API unsigned SkToUInt(uintmax_t);
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000270 SK_API size_t SkToSizeT(uintmax_t);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271#else
272 #define SkToS8(x) ((int8_t)(x))
273 #define SkToU8(x) ((uint8_t)(x))
274 #define SkToS16(x) ((int16_t)(x))
275 #define SkToU16(x) ((uint16_t)(x))
276 #define SkToS32(x) ((int32_t)(x))
277 #define SkToU32(x) ((uint32_t)(x))
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000278 #define SkToInt(x) ((int)(x))
reed@google.com7fa2a652014-01-27 13:42:58 +0000279 #define SkToUInt(x) ((unsigned)(x))
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000280 #define SkToSizeT(x) ((size_t)(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281#endif
282
283/** Returns 0 or 1 based on the condition
284*/
mtklein5c05d102015-12-02 12:32:02 -0800285#define SkToBool(cond) ((cond) != 0)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286
287#define SK_MaxS16 32767
288#define SK_MinS16 -32767
289#define SK_MaxU16 0xFFFF
290#define SK_MinU16 0
291#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000292#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293#define SK_MaxU32 0xFFFFFFFF
294#define SK_MinU32 0
humper@google.com0e515772013-01-07 19:54:40 +0000295#define SK_NaN32 (1 << 31)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296
reed@android.comd4577752009-11-21 02:48:11 +0000297/** Returns true if the value can be represented with signed 16bits
298 */
reed@android.com90209ca2009-11-21 19:58:04 +0000299static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000300 return (int16_t)x == x;
301}
302
303/** Returns true if the value can be represented with unsigned 16bits
304 */
reed@android.com90209ca2009-11-21 19:58:04 +0000305static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000306 return (uint16_t)x == x;
307}
308
caryclark3127c992015-12-09 12:02:30 -0800309static inline int32_t SkLeftShift(int32_t value, int32_t shift) {
310 return (int32_t) ((uint32_t) value << shift);
311}
312
313static inline int64_t SkLeftShift(int64_t value, int32_t shift) {
314 return (int64_t) ((uint64_t) value << shift);
315}
316
reed@android.comd4577752009-11-21 02:48:11 +0000317//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318
mtkleinfc00a7c2015-05-07 10:58:44 -0700319/** Returns the number of entries in an array (not a pointer) */
320template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
caryclark95b96d62015-08-19 10:12:59 -0700321#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322
mtkleinb68ce742015-11-24 05:35:58 -0800323// Can be used to bracket data types that must be dense, e.g. hash keys.
324#if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work!
325 #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \
326 _Pragma("GCC diagnostic error \"-Wpadded\"")
327 #define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop")
328#else
329 #define SK_BEGIN_REQUIRE_DENSE
330 #define SK_END_REQUIRE_DENSE
331#endif
332
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333#define SkAlign2(x) (((x) + 1) >> 1 << 1)
reed@google.comc6faa5a2012-06-27 15:07:11 +0000334#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335
reed@google.comc6faa5a2012-06-27 15:07:11 +0000336#define SkAlign4(x) (((x) + 3) >> 2 << 2)
337#define SkIsAlign4(x) (0 == ((x) & 3))
338
339#define SkAlign8(x) (((x) + 7) >> 3 << 3)
340#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000341
mtklein0209e952014-08-28 14:10:05 -0700342#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x))
343#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
344
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345typedef uint32_t SkFourByteTag;
346#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
347
348/** 32 bit integer to hold a unicode value
349*/
350typedef int32_t SkUnichar;
351/** 32 bit value to hold a millisecond count
352*/
353typedef uint32_t SkMSec;
354/** 1 second measured in milliseconds
355*/
356#define SK_MSec1 1000
357/** maximum representable milliseconds
358*/
359#define SK_MSecMax 0x7FFFFFFF
360/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
361*/
362#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
363/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
364*/
365#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
366
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000367/** The generation IDs in Skia reserve 0 has an invalid marker.
368 */
369#define SK_InvalidGenID 0
bsalomon1c63bf62014-07-22 13:09:46 -0700370/** The unique IDs in Skia reserve 0 has an invalid marker.
371 */
372#define SK_InvalidUniqueID 0
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000373
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374/****************************************************************************
375 The rest of these only build with C++
376*/
377#ifdef __cplusplus
378
379/** Faster than SkToBool for integral conditions. Returns 0 or 1
380*/
reed@android.comd4577752009-11-21 02:48:11 +0000381static inline int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000382 return (n | (0-n)) >> 31;
383}
384
bsalomon@google.comff436612013-02-27 19:07:32 +0000385/** Generic swap function. Classes with efficient swaps should specialize this function to take
386 their fast path. This function is used by SkTSort. */
reed@android.comd4577752009-11-21 02:48:11 +0000387template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388 T c(a);
389 a = b;
390 b = c;
391}
392
reed@android.comd4577752009-11-21 02:48:11 +0000393static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800394 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000395 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000397 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000399}
400
reed@google.com2b57dc62013-01-08 13:23:32 +0000401template <typename T> inline T SkTAbs(T value) {
402 if (value < 0) {
403 value = -value;
404 }
405 return value;
406}
407
reed@android.comd4577752009-11-21 02:48:11 +0000408static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 if (a < b)
410 a = b;
411 return a;
412}
413
reed@android.comd4577752009-11-21 02:48:11 +0000414static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000415 if (a > b)
416 a = b;
417 return a;
418}
419
caryclark@google.com3b97af52013-04-23 11:56:44 +0000420template <typename T> const T& SkTMin(const T& a, const T& b) {
421 return (a < b) ? a : b;
422}
423
424template <typename T> const T& SkTMax(const T& a, const T& b) {
425 return (b < a) ? a : b;
426}
427
reed@android.comd4577752009-11-21 02:48:11 +0000428static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429 return (a >> 31) | ((unsigned) -a >> 31);
430}
431
reed@android.comd4577752009-11-21 02:48:11 +0000432static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000433 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000435 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437}
438
bungeman62ce0302015-08-28 09:09:32 -0700439/** Returns value pinned between min and max, inclusively. */
440template <typename T> static inline const T& SkTPin(const T& value, const T& min, const T& max) {
441 return SkTMax(SkTMin(value, max), min);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000442}
443
reed@android.comd4577752009-11-21 02:48:11 +0000444static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
445 unsigned shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446 SkASSERT((int)cond == 0 || (int)cond == 1);
447 return (bits & ~(1 << shift)) | ((int)cond << shift);
448}
449
reed@android.comd4577752009-11-21 02:48:11 +0000450static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
451 uint32_t mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452 return cond ? bits | mask : bits & ~mask;
453}
454
reed@google.com1fcd51e2011-01-05 15:50:27 +0000455///////////////////////////////////////////////////////////////////////////////
456
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000457/** Use to combine multiple bits in a bitmask in a type safe way.
458 */
459template <typename T>
460T SkTBitOr(T a, T b) {
461 return (T)(a | b);
462}
463
reed@google.com1fcd51e2011-01-05 15:50:27 +0000464/**
465 * Use to cast a pointer to a different type, and maintaining strict-aliasing
466 */
467template <typename Dst> Dst SkTCast(const void* ptr) {
468 union {
469 const void* src;
470 Dst dst;
471 } data;
472 data.src = ptr;
473 return data.dst;
474}
475
reed@android.com8a1c16f2008-12-17 15:59:43 +0000476//////////////////////////////////////////////////////////////////////////////
477
478/** \class SkNoncopyable
479
fmalita055f6b52015-04-09 08:49:32 -0700480SkNoncopyable is the base class for objects that do not want to
reed@android.com8a1c16f2008-12-17 15:59:43 +0000481be copied. It hides its copy-constructor and its assignment-operator.
482*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000483class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000484public:
485 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000486
reed@android.com8a1c16f2008-12-17 15:59:43 +0000487private:
488 SkNoncopyable(const SkNoncopyable&);
489 SkNoncopyable& operator=(const SkNoncopyable&);
490};
491
492class SkAutoFree : SkNoncopyable {
493public:
494 SkAutoFree() : fPtr(NULL) {}
495 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
496 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000497
reed@android.com8a1c16f2008-12-17 15:59:43 +0000498 /** Return the currently allocate buffer, or null
499 */
500 void* get() const { return fPtr; }
501
502 /** Assign a new ptr allocated with sk_malloc (or null), and return the
503 previous ptr. Note it is the caller's responsibility to sk_free the
504 returned ptr.
505 */
506 void* set(void* ptr) {
507 void* prev = fPtr;
508 fPtr = ptr;
509 return prev;
510 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000511
reed@android.com8a1c16f2008-12-17 15:59:43 +0000512 /** Transfer ownership of the current ptr to the caller, setting the
513 internal reference to null. Note the caller is reponsible for calling
514 sk_free on the returned address.
515 */
516 void* detach() { return this->set(NULL); }
517
518 /** Free the current buffer, and set the internal reference to NULL. Same
519 as calling sk_free(detach())
520 */
521 void free() {
522 sk_free(fPtr);
523 fPtr = NULL;
524 }
525
526private:
527 void* fPtr;
528 // illegal
529 SkAutoFree(const SkAutoFree&);
530 SkAutoFree& operator=(const SkAutoFree&);
531};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000532#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000534/**
535 * Manage an allocated block of heap memory. This object is the sole manager of
536 * the lifetime of the block, so the caller must not call sk_free() or delete
reed@google.com1c401d82011-10-18 18:52:03 +0000537 * on the block, unless detach() was called.
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000538 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +0000539class SkAutoMalloc : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000540public:
reed@google.com3ab41952011-10-18 18:32:46 +0000541 explicit SkAutoMalloc(size_t size = 0) {
542 fPtr = size ? sk_malloc_throw(size) : NULL;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000543 fSize = size;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000545
546 ~SkAutoMalloc() {
547 sk_free(fPtr);
548 }
549
550 /**
reed@google.com1c401d82011-10-18 18:52:03 +0000551 * Passed to reset to specify what happens if the requested size is smaller
552 * than the current size (and the current block was dynamically allocated).
553 */
554 enum OnShrink {
555 /**
556 * If the requested size is smaller than the current size, and the
557 * current block is dynamically allocated, free the old block and
558 * malloc a new block of the smaller size.
559 */
560 kAlloc_OnShrink,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000561
reed@google.com1c401d82011-10-18 18:52:03 +0000562 /**
563 * If the requested size is smaller than the current size, and the
564 * current block is dynamically allocated, just return the old
565 * block.
566 */
tomhudson@google.com1f902872012-06-01 13:15:47 +0000567 kReuse_OnShrink
reed@google.com1c401d82011-10-18 18:52:03 +0000568 };
569
570 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000571 * Reallocates the block to a new size. The ptr may or may not change.
572 */
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000573 void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) {
reed@google.com1c401d82011-10-18 18:52:03 +0000574 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
bsalomon49f085d2014-09-05 13:34:00 -0700575 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000576 *didChangeAlloc = false;
577 }
reed@google.com1c401d82011-10-18 18:52:03 +0000578 return fPtr;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000579 }
reed@google.com1c401d82011-10-18 18:52:03 +0000580
581 sk_free(fPtr);
582 fPtr = size ? sk_malloc_throw(size) : NULL;
583 fSize = size;
bsalomon49f085d2014-09-05 13:34:00 -0700584 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000585 *didChangeAlloc = true;
586 }
reed@google.com1c401d82011-10-18 18:52:03 +0000587
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000588 return fPtr;
589 }
590
591 /**
592 * Releases the block back to the heap
593 */
594 void free() {
595 this->reset(0);
596 }
597
598 /**
599 * Return the allocated block.
600 */
601 void* get() { return fPtr; }
602 const void* get() const { return fPtr; }
603
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000604 /** Transfer ownership of the current ptr to the caller, setting the
605 internal reference to null. Note the caller is reponsible for calling
606 sk_free on the returned address.
607 */
608 void* detach() {
609 void* ptr = fPtr;
610 fPtr = NULL;
611 fSize = 0;
612 return ptr;
613 }
614
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000615private:
616 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000617 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000618};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000619#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000620
reed@google.com63a60602011-03-10 13:07:35 +0000621/**
622 * Manage an allocated block of memory. If the requested size is <= kSize, then
623 * the allocation will come from the stack rather than the heap. This object
624 * is the sole manager of the lifetime of the block, so the caller must not
625 * call sk_free() or delete on the block.
626 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
628public:
reed@google.com63a60602011-03-10 13:07:35 +0000629 /**
630 * Creates initially empty storage. get() returns a ptr, but it is to
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000631 * a zero-byte allocation. Must call reset(size) to return an allocated
reed@google.com63a60602011-03-10 13:07:35 +0000632 * block.
633 */
634 SkAutoSMalloc() {
635 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000636 fSize = kSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637 }
reed@google.com63a60602011-03-10 13:07:35 +0000638
639 /**
640 * Allocate a block of the specified size. If size <= kSize, then the
641 * allocation will come from the stack, otherwise it will be dynamically
642 * allocated.
643 */
644 explicit SkAutoSMalloc(size_t size) {
645 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000646 fSize = kSize;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000647 this->reset(size);
reed@google.com63a60602011-03-10 13:07:35 +0000648 }
649
650 /**
651 * Free the allocated block (if any). If the block was small enought to
652 * have been allocated on the stack (size <= kSize) then this does nothing.
653 */
654 ~SkAutoSMalloc() {
655 if (fPtr != (void*)fStorage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000656 sk_free(fPtr);
reed@google.com63a60602011-03-10 13:07:35 +0000657 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658 }
reed@google.com63a60602011-03-10 13:07:35 +0000659
660 /**
661 * Return the allocated block. May return non-null even if the block is
662 * of zero size. Since this may be on the stack or dynamically allocated,
663 * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
664 * to manage it.
665 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000666 void* get() const { return fPtr; }
reed@google.com63a60602011-03-10 13:07:35 +0000667
668 /**
669 * Return a new block of the requested size, freeing (as necessary) any
670 * previously allocated block. As with the constructor, if size <= kSize
671 * then the return block may be allocated locally, rather than from the
672 * heap.
673 */
reed@google.com1c401d82011-10-18 18:52:03 +0000674 void* reset(size_t size,
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000675 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
676 bool* didChangeAlloc = NULL) {
677 size = (size < kSize) ? kSize : size;
robertphillips@google.com0f2b1952013-05-23 14:59:40 +0000678 bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
bsalomon49f085d2014-09-05 13:34:00 -0700679 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000680 *didChangeAlloc = alloc;
reed@google.com1c401d82011-10-18 18:52:03 +0000681 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000682 if (alloc) {
683 if (fPtr != (void*)fStorage) {
684 sk_free(fPtr);
685 }
reed@google.com1c401d82011-10-18 18:52:03 +0000686
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000687 if (size == kSize) {
688 SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
689 fPtr = fStorage;
690 } else {
691 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
692 }
reed@google.com63a60602011-03-10 13:07:35 +0000693
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000694 fSize = size;
reed@google.com63a60602011-03-10 13:07:35 +0000695 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000696 SkASSERT(fSize >= size && fSize >= kSize);
697 SkASSERT((fPtr == fStorage) || fSize > kSize);
reed@google.com63a60602011-03-10 13:07:35 +0000698 return fPtr;
699 }
700
reed@android.com8a1c16f2008-12-17 15:59:43 +0000701private:
702 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000703 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704 uint32_t fStorage[(kSize + 3) >> 2];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000706// Can't guard the constructor because it's a template class.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000707
708#endif /* C++ */
709
710#endif