blob: a5468f3090af865984fd3e627b22ad23fe61fc5d [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000027/** \file SkTypes.h
28*/
29
reed@android.com9aa8b322010-04-13 13:22:54 +000030/** See SkGraphics::GetVersion() to retrieve these at runtime
31 */
32#define SKIA_VERSION_MAJOR 1
33#define SKIA_VERSION_MINOR 0
34#define SKIA_VERSION_PATCH 0
35
reed@android.com8a1c16f2008-12-17 15:59:43 +000036/*
37 memory wrappers to be implemented by the porting layer (platform)
38*/
39
40/** Called internally if we run out of memory. The platform implementation must
41 not return, but should either throw an exception or otherwise exit.
42*/
reed@google.comde916c82011-10-19 19:50:48 +000043SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000044/** Called internally if we hit an unrecoverable error.
45 The platform implementation must not return, but should either throw
46 an exception or otherwise exit.
47*/
reed@google.comde916c82011-10-19 19:50:48 +000048SK_API extern void sk_throw(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049
50enum {
51 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
52 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
53};
54/** Return a block of memory (at least 4-byte aligned) of at least the
55 specified size. If the requested memory cannot be returned, either
mtklein@google.com519f9672013-09-20 14:31:45 +000056 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
58*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000059SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000060/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
61*/
reed@google.comde916c82011-10-19 19:50:48 +000062SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000063/** Same as standard realloc(), but this one never returns null on failure. It will throw
64 an exception if it fails.
65*/
reed@google.comde916c82011-10-19 19:50:48 +000066SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000067/** Free memory returned by sk_malloc(). It is safe to pass null.
68*/
reed@google.comde916c82011-10-19 19:50:48 +000069SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
mtklein@google.com519f9672013-09-20 14:31:45 +000071/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
72 */
73SK_API extern void* sk_calloc(size_t size);
74
75/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
76 */
77SK_API extern void* sk_calloc_throw(size_t size);
78
reed@android.com4516f472009-06-29 16:25:36 +000079// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
80static inline void sk_bzero(void* buffer, size_t size) {
81 memset(buffer, 0, size);
82}
83
reed@google.combdf73612011-09-06 14:56:20 +000084///////////////////////////////////////////////////////////////////////////////
85
mtklein36352bf2015-03-25 18:17:31 -070086#ifdef override_GLOBAL_NEW
reed@google.combdf73612011-09-06 14:56:20 +000087#include <new>
88
89inline void* operator new(size_t size) {
90 return sk_malloc_throw(size);
91}
92
93inline void operator delete(void* p) {
94 sk_free(p);
95}
96#endif
97
98///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000099
100#define SK_INIT_TO_AVOID_WARNING = 0
101
102#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -0700103 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104#endif
105
106#ifdef SK_DEBUG
commit-bot@chromium.org45d1d1d2014-04-30 18:24:16 +0000107 #define SkASSERT(cond) SK_ALWAYSBREAK(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000108 #define SkDEBUGFAIL(message) SkASSERT(false && message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 #define SkDEBUGCODE(code) code
110 #define SkDECLAREPARAM(type, var) , type var
111 #define SkPARAM(var) , var
112// #define SkDEBUGF(args ) SkDebugf##args
113 #define SkDEBUGF(args ) SkDebugf args
114 #define SkAssertResult(cond) SkASSERT(cond)
115#else
116 #define SkASSERT(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000117 #define SkDEBUGFAIL(message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 #define SkDEBUGCODE(code)
119 #define SkDEBUGF(args)
120 #define SkDECLAREPARAM(type, var)
121 #define SkPARAM(var)
122
123 // unlike SkASSERT, this guy executes its condition in the non-debug build
124 #define SkAssertResult(cond) cond
125#endif
126
commit-bot@chromium.org45d1d1d2014-04-30 18:24:16 +0000127#define SkFAIL(message) SK_ALWAYSBREAK(false && message)
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000128
mtkleinb59161f2014-06-18 07:54:47 -0700129// We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form.
130// So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond,
131// and if it's true the assert passes; if it's false, we'll print the message and the assert fails.
132#define SkASSERTF(cond, fmt, ...) SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false))
133
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000134#ifdef SK_DEVELOPER
135 #define SkDEVCODE(code) code
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000136#else
137 #define SkDEVCODE(code)
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000138#endif
139
140#ifdef SK_IGNORE_TO_STRING
skia.committer@gmail.combc3d92a2014-03-14 03:02:26 +0000141 #define SK_TO_STRING_NONVIRT()
142 #define SK_TO_STRING_VIRT()
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000143 #define SK_TO_STRING_PUREVIRT()
144 #define SK_TO_STRING_OVERRIDE()
145#else
bungemand3ebb482015-08-05 13:57:49 -0700146 class SkString;
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000147 // the 'toString' helper functions convert Sk* objects to human-readable
148 // form in developer mode
149 #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
150 #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
151 #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
mtklein36352bf2015-03-25 18:17:31 -0700152 #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000153#endif
154
reed@google.com49a5b192012-10-25 17:31:39 +0000155/*
156 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
157 *
158 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
159 *
160 */
161#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
162#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
163
164/*
165 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
166 * line number. Easy way to construct
167 * unique names for local functions or
168 * variables.
169 */
170#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
171
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000172/**
173 * For some classes, it's almost always an error to instantiate one without a name, e.g.
174 * {
175 * SkAutoMutexAcquire(&mutex);
176 * <some code>
177 * }
178 * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
179 * but instead the mutex is acquired and then immediately released. The correct usage is
180 * {
181 * SkAutoMutexAcquire lock(&mutex);
182 * <some code>
183 * }
184 *
185 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
186 * like this:
187 * class classname {
188 * <your class>
189 * };
190 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
191 *
192 * This won't work with templates, and you must inline the class' constructors and destructors.
193 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
194 */
195#define SK_REQUIRE_LOCAL_VAR(classname) \
bungeman99fe8222015-08-20 07:57:51 -0700196 static_assert(false, "missing name for " #classname)
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000197
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198///////////////////////////////////////////////////////////////////////
199
reed@google.com37a31332011-01-25 14:55:42 +0000200/**
201 * Fast type for signed 8 bits. Use for parameter passing and local variables,
202 * not for storage.
203 */
204typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205
reed@google.com37a31332011-01-25 14:55:42 +0000206/**
207 * Fast type for unsigned 8 bits. Use for parameter passing and local
208 * variables, not for storage
209 */
210typedef unsigned U8CPU;
211
212/**
213 * Fast type for signed 16 bits. Use for parameter passing and local variables,
214 * not for storage
215 */
216typedef int S16CPU;
217
218/**
219 * Fast type for unsigned 16 bits. Use for parameter passing and local
220 * variables, not for storage
221 */
222typedef unsigned U16CPU;
223
224/**
225 * Meant to be faster than bool (doesn't promise to be 0 or 1,
226 * just 0 or non-zero
227 */
228typedef int SkBool;
229
230/**
231 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
232 */
233typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234
235#ifdef SK_DEBUG
bungeman@google.com777ded02013-07-19 22:30:11 +0000236 SK_API int8_t SkToS8(intmax_t);
237 SK_API uint8_t SkToU8(uintmax_t);
238 SK_API int16_t SkToS16(intmax_t);
239 SK_API uint16_t SkToU16(uintmax_t);
240 SK_API int32_t SkToS32(intmax_t);
241 SK_API uint32_t SkToU32(uintmax_t);
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000242 SK_API int SkToInt(intmax_t);
reed@google.com7fa2a652014-01-27 13:42:58 +0000243 SK_API unsigned SkToUInt(uintmax_t);
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000244 SK_API size_t SkToSizeT(uintmax_t);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245#else
246 #define SkToS8(x) ((int8_t)(x))
247 #define SkToU8(x) ((uint8_t)(x))
248 #define SkToS16(x) ((int16_t)(x))
249 #define SkToU16(x) ((uint16_t)(x))
250 #define SkToS32(x) ((int32_t)(x))
251 #define SkToU32(x) ((uint32_t)(x))
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000252 #define SkToInt(x) ((int)(x))
reed@google.com7fa2a652014-01-27 13:42:58 +0000253 #define SkToUInt(x) ((unsigned)(x))
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000254 #define SkToSizeT(x) ((size_t)(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255#endif
256
257/** Returns 0 or 1 based on the condition
258*/
259#define SkToBool(cond) ((cond) != 0)
260
261#define SK_MaxS16 32767
262#define SK_MinS16 -32767
263#define SK_MaxU16 0xFFFF
264#define SK_MinU16 0
265#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000266#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267#define SK_MaxU32 0xFFFFFFFF
268#define SK_MinU32 0
humper@google.com0e515772013-01-07 19:54:40 +0000269#define SK_NaN32 (1 << 31)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270
reed@android.comd4577752009-11-21 02:48:11 +0000271/** Returns true if the value can be represented with signed 16bits
272 */
reed@android.com90209ca2009-11-21 19:58:04 +0000273static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000274 return (int16_t)x == x;
275}
276
277/** Returns true if the value can be represented with unsigned 16bits
278 */
reed@android.com90209ca2009-11-21 19:58:04 +0000279static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000280 return (uint16_t)x == x;
281}
282
283//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284
mtkleinfc00a7c2015-05-07 10:58:44 -0700285/** Returns the number of entries in an array (not a pointer) */
286template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
caryclark95b96d62015-08-19 10:12:59 -0700287#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289#define SkAlign2(x) (((x) + 1) >> 1 << 1)
reed@google.comc6faa5a2012-06-27 15:07:11 +0000290#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291
reed@google.comc6faa5a2012-06-27 15:07:11 +0000292#define SkAlign4(x) (((x) + 3) >> 2 << 2)
293#define SkIsAlign4(x) (0 == ((x) & 3))
294
295#define SkAlign8(x) (((x) + 7) >> 3 << 3)
296#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000297
mtklein0209e952014-08-28 14:10:05 -0700298#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x))
299#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
300
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301typedef uint32_t SkFourByteTag;
302#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
303
304/** 32 bit integer to hold a unicode value
305*/
306typedef int32_t SkUnichar;
307/** 32 bit value to hold a millisecond count
308*/
309typedef uint32_t SkMSec;
310/** 1 second measured in milliseconds
311*/
312#define SK_MSec1 1000
313/** maximum representable milliseconds
314*/
315#define SK_MSecMax 0x7FFFFFFF
316/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
317*/
318#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
319/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
320*/
321#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
322
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000323/** The generation IDs in Skia reserve 0 has an invalid marker.
324 */
325#define SK_InvalidGenID 0
bsalomon1c63bf62014-07-22 13:09:46 -0700326/** The unique IDs in Skia reserve 0 has an invalid marker.
327 */
328#define SK_InvalidUniqueID 0
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000329
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330/****************************************************************************
331 The rest of these only build with C++
332*/
333#ifdef __cplusplus
334
335/** Faster than SkToBool for integral conditions. Returns 0 or 1
336*/
reed@android.comd4577752009-11-21 02:48:11 +0000337static inline int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338 return (n | (0-n)) >> 31;
339}
340
bsalomon@google.comff436612013-02-27 19:07:32 +0000341/** Generic swap function. Classes with efficient swaps should specialize this function to take
342 their fast path. This function is used by SkTSort. */
reed@android.comd4577752009-11-21 02:48:11 +0000343template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 T c(a);
345 a = b;
346 b = c;
347}
348
reed@android.comd4577752009-11-21 02:48:11 +0000349static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800350 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000351 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000353 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000354 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355}
356
reed@google.com2b57dc62013-01-08 13:23:32 +0000357template <typename T> inline T SkTAbs(T value) {
358 if (value < 0) {
359 value = -value;
360 }
361 return value;
362}
363
reed@android.comd4577752009-11-21 02:48:11 +0000364static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 if (a < b)
366 a = b;
367 return a;
368}
369
reed@android.comd4577752009-11-21 02:48:11 +0000370static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371 if (a > b)
372 a = b;
373 return a;
374}
375
caryclark@google.com3b97af52013-04-23 11:56:44 +0000376template <typename T> const T& SkTMin(const T& a, const T& b) {
377 return (a < b) ? a : b;
378}
379
380template <typename T> const T& SkTMax(const T& a, const T& b) {
381 return (b < a) ? a : b;
382}
383
reed@android.comd4577752009-11-21 02:48:11 +0000384static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 return (a >> 31) | ((unsigned) -a >> 31);
386}
387
reed@android.comd4577752009-11-21 02:48:11 +0000388static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000389 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000391 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393}
394
bungeman62ce0302015-08-28 09:09:32 -0700395/** Returns value pinned between min and max, inclusively. */
396template <typename T> static inline const T& SkTPin(const T& value, const T& min, const T& max) {
397 return SkTMax(SkTMin(value, max), min);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398}
399
reed@android.comd4577752009-11-21 02:48:11 +0000400static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
401 unsigned shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402 SkASSERT((int)cond == 0 || (int)cond == 1);
403 return (bits & ~(1 << shift)) | ((int)cond << shift);
404}
405
reed@android.comd4577752009-11-21 02:48:11 +0000406static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
407 uint32_t mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000408 return cond ? bits | mask : bits & ~mask;
409}
410
reed@google.com1fcd51e2011-01-05 15:50:27 +0000411///////////////////////////////////////////////////////////////////////////////
412
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000413/** Use to combine multiple bits in a bitmask in a type safe way.
414 */
415template <typename T>
416T SkTBitOr(T a, T b) {
417 return (T)(a | b);
418}
419
reed@google.com1fcd51e2011-01-05 15:50:27 +0000420/**
421 * Use to cast a pointer to a different type, and maintaining strict-aliasing
422 */
423template <typename Dst> Dst SkTCast(const void* ptr) {
424 union {
425 const void* src;
426 Dst dst;
427 } data;
428 data.src = ptr;
429 return data.dst;
430}
431
reed@android.com8a1c16f2008-12-17 15:59:43 +0000432//////////////////////////////////////////////////////////////////////////////
433
434/** \class SkNoncopyable
435
fmalita055f6b52015-04-09 08:49:32 -0700436SkNoncopyable is the base class for objects that do not want to
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437be copied. It hides its copy-constructor and its assignment-operator.
438*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000439class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440public:
441 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000442
reed@android.com8a1c16f2008-12-17 15:59:43 +0000443private:
444 SkNoncopyable(const SkNoncopyable&);
445 SkNoncopyable& operator=(const SkNoncopyable&);
446};
447
448class SkAutoFree : SkNoncopyable {
449public:
450 SkAutoFree() : fPtr(NULL) {}
451 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
452 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000453
reed@android.com8a1c16f2008-12-17 15:59:43 +0000454 /** Return the currently allocate buffer, or null
455 */
456 void* get() const { return fPtr; }
457
458 /** Assign a new ptr allocated with sk_malloc (or null), and return the
459 previous ptr. Note it is the caller's responsibility to sk_free the
460 returned ptr.
461 */
462 void* set(void* ptr) {
463 void* prev = fPtr;
464 fPtr = ptr;
465 return prev;
466 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000467
reed@android.com8a1c16f2008-12-17 15:59:43 +0000468 /** Transfer ownership of the current ptr to the caller, setting the
469 internal reference to null. Note the caller is reponsible for calling
470 sk_free on the returned address.
471 */
472 void* detach() { return this->set(NULL); }
473
474 /** Free the current buffer, and set the internal reference to NULL. Same
475 as calling sk_free(detach())
476 */
477 void free() {
478 sk_free(fPtr);
479 fPtr = NULL;
480 }
481
482private:
483 void* fPtr;
484 // illegal
485 SkAutoFree(const SkAutoFree&);
486 SkAutoFree& operator=(const SkAutoFree&);
487};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000488#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000490/**
491 * Manage an allocated block of heap memory. This object is the sole manager of
492 * the lifetime of the block, so the caller must not call sk_free() or delete
reed@google.com1c401d82011-10-18 18:52:03 +0000493 * on the block, unless detach() was called.
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000494 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +0000495class SkAutoMalloc : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496public:
reed@google.com3ab41952011-10-18 18:32:46 +0000497 explicit SkAutoMalloc(size_t size = 0) {
498 fPtr = size ? sk_malloc_throw(size) : NULL;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000499 fSize = size;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000500 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000501
502 ~SkAutoMalloc() {
503 sk_free(fPtr);
504 }
505
506 /**
reed@google.com1c401d82011-10-18 18:52:03 +0000507 * Passed to reset to specify what happens if the requested size is smaller
508 * than the current size (and the current block was dynamically allocated).
509 */
510 enum OnShrink {
511 /**
512 * If the requested size is smaller than the current size, and the
513 * current block is dynamically allocated, free the old block and
514 * malloc a new block of the smaller size.
515 */
516 kAlloc_OnShrink,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000517
reed@google.com1c401d82011-10-18 18:52:03 +0000518 /**
519 * If the requested size is smaller than the current size, and the
520 * current block is dynamically allocated, just return the old
521 * block.
522 */
tomhudson@google.com1f902872012-06-01 13:15:47 +0000523 kReuse_OnShrink
reed@google.com1c401d82011-10-18 18:52:03 +0000524 };
525
526 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000527 * Reallocates the block to a new size. The ptr may or may not change.
528 */
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000529 void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) {
reed@google.com1c401d82011-10-18 18:52:03 +0000530 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
bsalomon49f085d2014-09-05 13:34:00 -0700531 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000532 *didChangeAlloc = false;
533 }
reed@google.com1c401d82011-10-18 18:52:03 +0000534 return fPtr;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000535 }
reed@google.com1c401d82011-10-18 18:52:03 +0000536
537 sk_free(fPtr);
538 fPtr = size ? sk_malloc_throw(size) : NULL;
539 fSize = size;
bsalomon49f085d2014-09-05 13:34:00 -0700540 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000541 *didChangeAlloc = true;
542 }
reed@google.com1c401d82011-10-18 18:52:03 +0000543
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000544 return fPtr;
545 }
546
547 /**
548 * Releases the block back to the heap
549 */
550 void free() {
551 this->reset(0);
552 }
553
554 /**
555 * Return the allocated block.
556 */
557 void* get() { return fPtr; }
558 const void* get() const { return fPtr; }
559
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000560 /** Transfer ownership of the current ptr to the caller, setting the
561 internal reference to null. Note the caller is reponsible for calling
562 sk_free on the returned address.
563 */
564 void* detach() {
565 void* ptr = fPtr;
566 fPtr = NULL;
567 fSize = 0;
568 return ptr;
569 }
570
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000571private:
572 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000573 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000575#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576
reed@google.com63a60602011-03-10 13:07:35 +0000577/**
578 * Manage an allocated block of memory. If the requested size is <= kSize, then
579 * the allocation will come from the stack rather than the heap. This object
580 * is the sole manager of the lifetime of the block, so the caller must not
581 * call sk_free() or delete on the block.
582 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
584public:
reed@google.com63a60602011-03-10 13:07:35 +0000585 /**
586 * Creates initially empty storage. get() returns a ptr, but it is to
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000587 * a zero-byte allocation. Must call reset(size) to return an allocated
reed@google.com63a60602011-03-10 13:07:35 +0000588 * block.
589 */
590 SkAutoSMalloc() {
591 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000592 fSize = kSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000593 }
reed@google.com63a60602011-03-10 13:07:35 +0000594
595 /**
596 * Allocate a block of the specified size. If size <= kSize, then the
597 * allocation will come from the stack, otherwise it will be dynamically
598 * allocated.
599 */
600 explicit SkAutoSMalloc(size_t size) {
601 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000602 fSize = kSize;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000603 this->reset(size);
reed@google.com63a60602011-03-10 13:07:35 +0000604 }
605
606 /**
607 * Free the allocated block (if any). If the block was small enought to
608 * have been allocated on the stack (size <= kSize) then this does nothing.
609 */
610 ~SkAutoSMalloc() {
611 if (fPtr != (void*)fStorage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000612 sk_free(fPtr);
reed@google.com63a60602011-03-10 13:07:35 +0000613 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000614 }
reed@google.com63a60602011-03-10 13:07:35 +0000615
616 /**
617 * Return the allocated block. May return non-null even if the block is
618 * of zero size. Since this may be on the stack or dynamically allocated,
619 * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
620 * to manage it.
621 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000622 void* get() const { return fPtr; }
reed@google.com63a60602011-03-10 13:07:35 +0000623
624 /**
625 * Return a new block of the requested size, freeing (as necessary) any
626 * previously allocated block. As with the constructor, if size <= kSize
627 * then the return block may be allocated locally, rather than from the
628 * heap.
629 */
reed@google.com1c401d82011-10-18 18:52:03 +0000630 void* reset(size_t size,
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000631 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
632 bool* didChangeAlloc = NULL) {
633 size = (size < kSize) ? kSize : size;
robertphillips@google.com0f2b1952013-05-23 14:59:40 +0000634 bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
bsalomon49f085d2014-09-05 13:34:00 -0700635 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000636 *didChangeAlloc = alloc;
reed@google.com1c401d82011-10-18 18:52:03 +0000637 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000638 if (alloc) {
639 if (fPtr != (void*)fStorage) {
640 sk_free(fPtr);
641 }
reed@google.com1c401d82011-10-18 18:52:03 +0000642
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000643 if (size == kSize) {
644 SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
645 fPtr = fStorage;
646 } else {
647 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
648 }
reed@google.com63a60602011-03-10 13:07:35 +0000649
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000650 fSize = size;
reed@google.com63a60602011-03-10 13:07:35 +0000651 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000652 SkASSERT(fSize >= size && fSize >= kSize);
653 SkASSERT((fPtr == fStorage) || fSize > kSize);
reed@google.com63a60602011-03-10 13:07:35 +0000654 return fPtr;
655 }
656
reed@android.com8a1c16f2008-12-17 15:59:43 +0000657private:
658 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000659 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000660 uint32_t fStorage[(kSize + 3) >> 2];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000661};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000662// Can't guard the constructor because it's a template class.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663
664#endif /* C++ */
665
666#endif