blob: 9014620695ec83bbaa5a9fbb53b3bdbdb999f194 [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
11#include "SkPreConfig.h"
12#include "SkUserConfig.h"
13#include "SkPostConfig.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +000014#include <stdint.h>
bsalomonef3fcd82014-12-12 08:51:38 -080015#include <sys/types.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
17/** \file SkTypes.h
18*/
19
reed@android.com9aa8b322010-04-13 13:22:54 +000020/** See SkGraphics::GetVersion() to retrieve these at runtime
21 */
22#define SKIA_VERSION_MAJOR 1
23#define SKIA_VERSION_MINOR 0
24#define SKIA_VERSION_PATCH 0
25
reed@android.com8a1c16f2008-12-17 15:59:43 +000026/*
27 memory wrappers to be implemented by the porting layer (platform)
28*/
29
30/** Called internally if we run out of memory. The platform implementation must
31 not return, but should either throw an exception or otherwise exit.
32*/
reed@google.comde916c82011-10-19 19:50:48 +000033SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000034/** Called internally if we hit an unrecoverable error.
35 The platform implementation must not return, but should either throw
36 an exception or otherwise exit.
37*/
reed@google.comde916c82011-10-19 19:50:48 +000038SK_API extern void sk_throw(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
40enum {
41 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
42 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
43};
44/** Return a block of memory (at least 4-byte aligned) of at least the
45 specified size. If the requested memory cannot be returned, either
mtklein@google.com519f9672013-09-20 14:31:45 +000046 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
48*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000049SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000050/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
51*/
reed@google.comde916c82011-10-19 19:50:48 +000052SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000053/** Same as standard realloc(), but this one never returns null on failure. It will throw
54 an exception if it fails.
55*/
reed@google.comde916c82011-10-19 19:50:48 +000056SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000057/** Free memory returned by sk_malloc(). It is safe to pass null.
58*/
reed@google.comde916c82011-10-19 19:50:48 +000059SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000060
mtklein@google.com519f9672013-09-20 14:31:45 +000061/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
62 */
63SK_API extern void* sk_calloc(size_t size);
64
65/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
66 */
67SK_API extern void* sk_calloc_throw(size_t size);
68
reed@android.com4516f472009-06-29 16:25:36 +000069// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
70static inline void sk_bzero(void* buffer, size_t size) {
71 memset(buffer, 0, size);
72}
73
reed@google.combdf73612011-09-06 14:56:20 +000074///////////////////////////////////////////////////////////////////////////////
75
mtklein36352bf2015-03-25 18:17:31 -070076#ifdef override_GLOBAL_NEW
reed@google.combdf73612011-09-06 14:56:20 +000077#include <new>
78
79inline void* operator new(size_t size) {
80 return sk_malloc_throw(size);
81}
82
83inline void operator delete(void* p) {
84 sk_free(p);
85}
86#endif
87
88///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
90#define SK_INIT_TO_AVOID_WARNING = 0
91
92#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -070093 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +000094#endif
95
96#ifdef SK_DEBUG
commit-bot@chromium.org45d1d1d2014-04-30 18:24:16 +000097 #define SkASSERT(cond) SK_ALWAYSBREAK(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +000098 #define SkDEBUGFAIL(message) SkASSERT(false && message)
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 #define SkDEBUGCODE(code) code
100 #define SkDECLAREPARAM(type, var) , type var
101 #define SkPARAM(var) , var
102// #define SkDEBUGF(args ) SkDebugf##args
103 #define SkDEBUGF(args ) SkDebugf args
104 #define SkAssertResult(cond) SkASSERT(cond)
105#else
106 #define SkASSERT(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000107 #define SkDEBUGFAIL(message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 #define SkDEBUGCODE(code)
109 #define SkDEBUGF(args)
110 #define SkDECLAREPARAM(type, var)
111 #define SkPARAM(var)
112
113 // unlike SkASSERT, this guy executes its condition in the non-debug build
114 #define SkAssertResult(cond) cond
115#endif
116
commit-bot@chromium.org45d1d1d2014-04-30 18:24:16 +0000117#define SkFAIL(message) SK_ALWAYSBREAK(false && message)
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000118
mtkleinb59161f2014-06-18 07:54:47 -0700119// We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form.
120// So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond,
121// and if it's true the assert passes; if it's false, we'll print the message and the assert fails.
122#define SkASSERTF(cond, fmt, ...) SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false))
123
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000124#ifdef SK_DEVELOPER
125 #define SkDEVCODE(code) code
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000126#else
127 #define SkDEVCODE(code)
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000128#endif
129
130#ifdef SK_IGNORE_TO_STRING
skia.committer@gmail.combc3d92a2014-03-14 03:02:26 +0000131 #define SK_TO_STRING_NONVIRT()
132 #define SK_TO_STRING_VIRT()
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000133 #define SK_TO_STRING_PUREVIRT()
134 #define SK_TO_STRING_OVERRIDE()
135#else
136 // the 'toString' helper functions convert Sk* objects to human-readable
137 // form in developer mode
138 #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
139 #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
140 #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
mtklein36352bf2015-03-25 18:17:31 -0700141 #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000142#endif
143
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000144template <bool>
145struct SkCompileAssert {
146};
147
bungeman@google.com562b2e62014-03-12 21:41:06 +0000148// Uses static_cast<bool>(expr) instead of bool(expr) due to
149// https://connect.microsoft.com/VisualStudio/feedback/details/832915
bungeman@google.com9c28fa52014-03-13 17:51:05 +0000150
151// The extra parentheses in SkCompileAssert<(...)> are a work around for
152// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57771
153// which was fixed in gcc 4.8.2.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000154#define SK_COMPILE_ASSERT(expr, msg) \
bungeman@google.com2f582092014-03-12 21:56:23 +0000155 typedef SkCompileAssert<(static_cast<bool>(expr))> \
bungeman@google.com562b2e62014-03-12 21:41:06 +0000156 msg[static_cast<bool>(expr) ? 1 : -1] SK_UNUSED
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000157
reed@google.com49a5b192012-10-25 17:31:39 +0000158/*
159 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
160 *
161 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
162 *
163 */
164#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
165#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
166
167/*
168 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
169 * line number. Easy way to construct
170 * unique names for local functions or
171 * variables.
172 */
173#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
174
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000175/**
176 * For some classes, it's almost always an error to instantiate one without a name, e.g.
177 * {
178 * SkAutoMutexAcquire(&mutex);
179 * <some code>
180 * }
181 * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
182 * but instead the mutex is acquired and then immediately released. The correct usage is
183 * {
184 * SkAutoMutexAcquire lock(&mutex);
185 * <some code>
186 * }
187 *
188 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
189 * like this:
190 * class classname {
191 * <your class>
192 * };
193 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
194 *
195 * This won't work with templates, and you must inline the class' constructors and destructors.
196 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
197 */
198#define SK_REQUIRE_LOCAL_VAR(classname) \
199 SK_COMPILE_ASSERT(false, missing_name_for_##classname)
200
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201///////////////////////////////////////////////////////////////////////
202
reed@google.com37a31332011-01-25 14:55:42 +0000203/**
204 * Fast type for signed 8 bits. Use for parameter passing and local variables,
205 * not for storage.
206 */
207typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208
reed@google.com37a31332011-01-25 14:55:42 +0000209/**
210 * Fast type for unsigned 8 bits. Use for parameter passing and local
211 * variables, not for storage
212 */
213typedef unsigned U8CPU;
214
215/**
216 * Fast type for signed 16 bits. Use for parameter passing and local variables,
217 * not for storage
218 */
219typedef int S16CPU;
220
221/**
222 * Fast type for unsigned 16 bits. Use for parameter passing and local
223 * variables, not for storage
224 */
225typedef unsigned U16CPU;
226
227/**
228 * Meant to be faster than bool (doesn't promise to be 0 or 1,
229 * just 0 or non-zero
230 */
231typedef int SkBool;
232
233/**
234 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
235 */
236typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237
238#ifdef SK_DEBUG
bungeman@google.com777ded02013-07-19 22:30:11 +0000239 SK_API int8_t SkToS8(intmax_t);
240 SK_API uint8_t SkToU8(uintmax_t);
241 SK_API int16_t SkToS16(intmax_t);
242 SK_API uint16_t SkToU16(uintmax_t);
243 SK_API int32_t SkToS32(intmax_t);
244 SK_API uint32_t SkToU32(uintmax_t);
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000245 SK_API int SkToInt(intmax_t);
reed@google.com7fa2a652014-01-27 13:42:58 +0000246 SK_API unsigned SkToUInt(uintmax_t);
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000247 SK_API size_t SkToSizeT(uintmax_t);
bsalomonef3fcd82014-12-12 08:51:38 -0800248 SK_API off_t SkToOffT(intmax_t x);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249#else
250 #define SkToS8(x) ((int8_t)(x))
251 #define SkToU8(x) ((uint8_t)(x))
252 #define SkToS16(x) ((int16_t)(x))
253 #define SkToU16(x) ((uint16_t)(x))
254 #define SkToS32(x) ((int32_t)(x))
255 #define SkToU32(x) ((uint32_t)(x))
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000256 #define SkToInt(x) ((int)(x))
reed@google.com7fa2a652014-01-27 13:42:58 +0000257 #define SkToUInt(x) ((unsigned)(x))
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000258 #define SkToSizeT(x) ((size_t)(x))
bsalomonef3fcd82014-12-12 08:51:38 -0800259 #define SkToOffT(x) ((off_t)(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260#endif
261
262/** Returns 0 or 1 based on the condition
263*/
264#define SkToBool(cond) ((cond) != 0)
265
266#define SK_MaxS16 32767
267#define SK_MinS16 -32767
268#define SK_MaxU16 0xFFFF
269#define SK_MinU16 0
270#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000271#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272#define SK_MaxU32 0xFFFFFFFF
273#define SK_MinU32 0
humper@google.com0e515772013-01-07 19:54:40 +0000274#define SK_NaN32 (1 << 31)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275
reed@android.comd4577752009-11-21 02:48:11 +0000276/** Returns true if the value can be represented with signed 16bits
277 */
reed@android.com90209ca2009-11-21 19:58:04 +0000278static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000279 return (int16_t)x == x;
280}
281
282/** Returns true if the value can be represented with unsigned 16bits
283 */
reed@android.com90209ca2009-11-21 19:58:04 +0000284static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000285 return (uint16_t)x == x;
286}
287
288//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289#ifndef SK_OFFSETOF
reed@google.com56d3a232012-03-21 12:25:48 +0000290 #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291#endif
292
293/** Returns the number of entries in an array (not a pointer)
294*/
295#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
296
reed@android.com8a1c16f2008-12-17 15:59:43 +0000297#define SkAlign2(x) (((x) + 1) >> 1 << 1)
reed@google.comc6faa5a2012-06-27 15:07:11 +0000298#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299
reed@google.comc6faa5a2012-06-27 15:07:11 +0000300#define SkAlign4(x) (((x) + 3) >> 2 << 2)
301#define SkIsAlign4(x) (0 == ((x) & 3))
302
303#define SkAlign8(x) (((x) + 7) >> 3 << 3)
304#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000305
mtklein0209e952014-08-28 14:10:05 -0700306#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x))
307#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
308
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309typedef uint32_t SkFourByteTag;
310#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
311
312/** 32 bit integer to hold a unicode value
313*/
314typedef int32_t SkUnichar;
315/** 32 bit value to hold a millisecond count
316*/
317typedef uint32_t SkMSec;
318/** 1 second measured in milliseconds
319*/
320#define SK_MSec1 1000
321/** maximum representable milliseconds
322*/
323#define SK_MSecMax 0x7FFFFFFF
324/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
325*/
326#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
327/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
328*/
329#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
330
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000331/** The generation IDs in Skia reserve 0 has an invalid marker.
332 */
333#define SK_InvalidGenID 0
bsalomon1c63bf62014-07-22 13:09:46 -0700334/** The unique IDs in Skia reserve 0 has an invalid marker.
335 */
336#define SK_InvalidUniqueID 0
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000337
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338/****************************************************************************
339 The rest of these only build with C++
340*/
341#ifdef __cplusplus
342
343/** Faster than SkToBool for integral conditions. Returns 0 or 1
344*/
reed@android.comd4577752009-11-21 02:48:11 +0000345static inline int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 return (n | (0-n)) >> 31;
347}
348
bsalomon@google.comff436612013-02-27 19:07:32 +0000349/** Generic swap function. Classes with efficient swaps should specialize this function to take
350 their fast path. This function is used by SkTSort. */
reed@android.comd4577752009-11-21 02:48:11 +0000351template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352 T c(a);
353 a = b;
354 b = c;
355}
356
reed@android.comd4577752009-11-21 02:48:11 +0000357static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800358 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000359 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000361 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363}
364
reed@google.com2b57dc62013-01-08 13:23:32 +0000365template <typename T> inline T SkTAbs(T value) {
366 if (value < 0) {
367 value = -value;
368 }
369 return value;
370}
371
reed@android.comd4577752009-11-21 02:48:11 +0000372static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 if (a < b)
374 a = b;
375 return a;
376}
377
reed@android.comd4577752009-11-21 02:48:11 +0000378static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379 if (a > b)
380 a = b;
381 return a;
382}
383
caryclark@google.com3b97af52013-04-23 11:56:44 +0000384template <typename T> const T& SkTMin(const T& a, const T& b) {
385 return (a < b) ? a : b;
386}
387
388template <typename T> const T& SkTMax(const T& a, const T& b) {
389 return (b < a) ? a : b;
390}
391
reed@android.comd4577752009-11-21 02:48:11 +0000392static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 return (a >> 31) | ((unsigned) -a >> 31);
394}
395
reed@android.comd4577752009-11-21 02:48:11 +0000396static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000397 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000399 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401}
402
403/** Returns signed 32 bit value pinned between min and max, inclusively
404*/
reed@android.comd4577752009-11-21 02:48:11 +0000405static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000406 if (value < min) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 value = min;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000408 }
409 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000411 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 return value;
413}
414
reed@android.comd4577752009-11-21 02:48:11 +0000415static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
416 unsigned shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417 SkASSERT((int)cond == 0 || (int)cond == 1);
418 return (bits & ~(1 << shift)) | ((int)cond << shift);
419}
420
reed@android.comd4577752009-11-21 02:48:11 +0000421static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
422 uint32_t mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000423 return cond ? bits | mask : bits & ~mask;
424}
425
reed@google.com1fcd51e2011-01-05 15:50:27 +0000426///////////////////////////////////////////////////////////////////////////////
427
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000428/** Use to combine multiple bits in a bitmask in a type safe way.
429 */
430template <typename T>
431T SkTBitOr(T a, T b) {
432 return (T)(a | b);
433}
434
reed@google.com1fcd51e2011-01-05 15:50:27 +0000435/**
436 * Use to cast a pointer to a different type, and maintaining strict-aliasing
437 */
438template <typename Dst> Dst SkTCast(const void* ptr) {
439 union {
440 const void* src;
441 Dst dst;
442 } data;
443 data.src = ptr;
444 return data.dst;
445}
446
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447//////////////////////////////////////////////////////////////////////////////
448
449/** \class SkNoncopyable
450
fmalita055f6b52015-04-09 08:49:32 -0700451SkNoncopyable is the base class for objects that do not want to
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452be copied. It hides its copy-constructor and its assignment-operator.
453*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000454class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455public:
456 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000457
reed@android.com8a1c16f2008-12-17 15:59:43 +0000458private:
459 SkNoncopyable(const SkNoncopyable&);
460 SkNoncopyable& operator=(const SkNoncopyable&);
461};
462
463class SkAutoFree : SkNoncopyable {
464public:
465 SkAutoFree() : fPtr(NULL) {}
466 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
467 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000468
reed@android.com8a1c16f2008-12-17 15:59:43 +0000469 /** Return the currently allocate buffer, or null
470 */
471 void* get() const { return fPtr; }
472
473 /** Assign a new ptr allocated with sk_malloc (or null), and return the
474 previous ptr. Note it is the caller's responsibility to sk_free the
475 returned ptr.
476 */
477 void* set(void* ptr) {
478 void* prev = fPtr;
479 fPtr = ptr;
480 return prev;
481 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000482
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483 /** Transfer ownership of the current ptr to the caller, setting the
484 internal reference to null. Note the caller is reponsible for calling
485 sk_free on the returned address.
486 */
487 void* detach() { return this->set(NULL); }
488
489 /** Free the current buffer, and set the internal reference to NULL. Same
490 as calling sk_free(detach())
491 */
492 void free() {
493 sk_free(fPtr);
494 fPtr = NULL;
495 }
496
497private:
498 void* fPtr;
499 // illegal
500 SkAutoFree(const SkAutoFree&);
501 SkAutoFree& operator=(const SkAutoFree&);
502};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000503#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000505/**
506 * Manage an allocated block of heap memory. This object is the sole manager of
507 * the lifetime of the block, so the caller must not call sk_free() or delete
reed@google.com1c401d82011-10-18 18:52:03 +0000508 * on the block, unless detach() was called.
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000509 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +0000510class SkAutoMalloc : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000511public:
reed@google.com3ab41952011-10-18 18:32:46 +0000512 explicit SkAutoMalloc(size_t size = 0) {
513 fPtr = size ? sk_malloc_throw(size) : NULL;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000514 fSize = size;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000516
517 ~SkAutoMalloc() {
518 sk_free(fPtr);
519 }
520
521 /**
reed@google.com1c401d82011-10-18 18:52:03 +0000522 * Passed to reset to specify what happens if the requested size is smaller
523 * than the current size (and the current block was dynamically allocated).
524 */
525 enum OnShrink {
526 /**
527 * If the requested size is smaller than the current size, and the
528 * current block is dynamically allocated, free the old block and
529 * malloc a new block of the smaller size.
530 */
531 kAlloc_OnShrink,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000532
reed@google.com1c401d82011-10-18 18:52:03 +0000533 /**
534 * If the requested size is smaller than the current size, and the
535 * current block is dynamically allocated, just return the old
536 * block.
537 */
tomhudson@google.com1f902872012-06-01 13:15:47 +0000538 kReuse_OnShrink
reed@google.com1c401d82011-10-18 18:52:03 +0000539 };
540
541 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000542 * Reallocates the block to a new size. The ptr may or may not change.
543 */
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000544 void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) {
reed@google.com1c401d82011-10-18 18:52:03 +0000545 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
bsalomon49f085d2014-09-05 13:34:00 -0700546 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000547 *didChangeAlloc = false;
548 }
reed@google.com1c401d82011-10-18 18:52:03 +0000549 return fPtr;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000550 }
reed@google.com1c401d82011-10-18 18:52:03 +0000551
552 sk_free(fPtr);
553 fPtr = size ? sk_malloc_throw(size) : NULL;
554 fSize = size;
bsalomon49f085d2014-09-05 13:34:00 -0700555 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000556 *didChangeAlloc = true;
557 }
reed@google.com1c401d82011-10-18 18:52:03 +0000558
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000559 return fPtr;
560 }
561
562 /**
563 * Releases the block back to the heap
564 */
565 void free() {
566 this->reset(0);
567 }
568
569 /**
570 * Return the allocated block.
571 */
572 void* get() { return fPtr; }
573 const void* get() const { return fPtr; }
574
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000575 /** Transfer ownership of the current ptr to the caller, setting the
576 internal reference to null. Note the caller is reponsible for calling
577 sk_free on the returned address.
578 */
579 void* detach() {
580 void* ptr = fPtr;
581 fPtr = NULL;
582 fSize = 0;
583 return ptr;
584 }
585
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000586private:
587 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000588 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000589};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000590#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000591
reed@google.com63a60602011-03-10 13:07:35 +0000592/**
593 * Manage an allocated block of memory. If the requested size is <= kSize, then
594 * the allocation will come from the stack rather than the heap. This object
595 * is the sole manager of the lifetime of the block, so the caller must not
596 * call sk_free() or delete on the block.
597 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000598template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
599public:
reed@google.com63a60602011-03-10 13:07:35 +0000600 /**
601 * Creates initially empty storage. get() returns a ptr, but it is to
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000602 * a zero-byte allocation. Must call reset(size) to return an allocated
reed@google.com63a60602011-03-10 13:07:35 +0000603 * block.
604 */
605 SkAutoSMalloc() {
606 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000607 fSize = kSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000608 }
reed@google.com63a60602011-03-10 13:07:35 +0000609
610 /**
611 * Allocate a block of the specified size. If size <= kSize, then the
612 * allocation will come from the stack, otherwise it will be dynamically
613 * allocated.
614 */
615 explicit SkAutoSMalloc(size_t size) {
616 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000617 fSize = kSize;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000618 this->reset(size);
reed@google.com63a60602011-03-10 13:07:35 +0000619 }
620
621 /**
622 * Free the allocated block (if any). If the block was small enought to
623 * have been allocated on the stack (size <= kSize) then this does nothing.
624 */
625 ~SkAutoSMalloc() {
626 if (fPtr != (void*)fStorage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627 sk_free(fPtr);
reed@google.com63a60602011-03-10 13:07:35 +0000628 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000629 }
reed@google.com63a60602011-03-10 13:07:35 +0000630
631 /**
632 * Return the allocated block. May return non-null even if the block is
633 * of zero size. Since this may be on the stack or dynamically allocated,
634 * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
635 * to manage it.
636 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637 void* get() const { return fPtr; }
reed@google.com63a60602011-03-10 13:07:35 +0000638
639 /**
640 * Return a new block of the requested size, freeing (as necessary) any
641 * previously allocated block. As with the constructor, if size <= kSize
642 * then the return block may be allocated locally, rather than from the
643 * heap.
644 */
reed@google.com1c401d82011-10-18 18:52:03 +0000645 void* reset(size_t size,
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000646 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
647 bool* didChangeAlloc = NULL) {
648 size = (size < kSize) ? kSize : size;
robertphillips@google.com0f2b1952013-05-23 14:59:40 +0000649 bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
bsalomon49f085d2014-09-05 13:34:00 -0700650 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000651 *didChangeAlloc = alloc;
reed@google.com1c401d82011-10-18 18:52:03 +0000652 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000653 if (alloc) {
654 if (fPtr != (void*)fStorage) {
655 sk_free(fPtr);
656 }
reed@google.com1c401d82011-10-18 18:52:03 +0000657
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000658 if (size == kSize) {
659 SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
660 fPtr = fStorage;
661 } else {
662 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
663 }
reed@google.com63a60602011-03-10 13:07:35 +0000664
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000665 fSize = size;
reed@google.com63a60602011-03-10 13:07:35 +0000666 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000667 SkASSERT(fSize >= size && fSize >= kSize);
668 SkASSERT((fPtr == fStorage) || fSize > kSize);
reed@google.com63a60602011-03-10 13:07:35 +0000669 return fPtr;
670 }
671
reed@android.com8a1c16f2008-12-17 15:59:43 +0000672private:
673 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000674 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675 uint32_t fStorage[(kSize + 3) >> 2];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000676};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000677// Can't guard the constructor because it's a template class.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678
679#endif /* C++ */
680
681#endif