blob: 74ddf0273d1b305062098fa13a3abb250ebba94a [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>
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
16/** \file SkTypes.h
17*/
18
reed@android.com9aa8b322010-04-13 13:22:54 +000019/** See SkGraphics::GetVersion() to retrieve these at runtime
20 */
21#define SKIA_VERSION_MAJOR 1
22#define SKIA_VERSION_MINOR 0
23#define SKIA_VERSION_PATCH 0
24
reed@android.com8a1c16f2008-12-17 15:59:43 +000025/*
26 memory wrappers to be implemented by the porting layer (platform)
27*/
28
29/** Called internally if we run out of memory. The platform implementation must
30 not return, but should either throw an exception or otherwise exit.
31*/
reed@google.comde916c82011-10-19 19:50:48 +000032SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000033/** Called internally if we hit an unrecoverable error.
34 The platform implementation must not return, but should either throw
35 an exception or otherwise exit.
36*/
reed@google.comde916c82011-10-19 19:50:48 +000037SK_API extern void sk_throw(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
39enum {
40 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
41 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
42};
43/** Return a block of memory (at least 4-byte aligned) of at least the
44 specified size. If the requested memory cannot be returned, either
mtklein@google.com519f9672013-09-20 14:31:45 +000045 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
47*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000048SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
50*/
reed@google.comde916c82011-10-19 19:50:48 +000051SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000052/** Same as standard realloc(), but this one never returns null on failure. It will throw
53 an exception if it fails.
54*/
reed@google.comde916c82011-10-19 19:50:48 +000055SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000056/** Free memory returned by sk_malloc(). It is safe to pass null.
57*/
reed@google.comde916c82011-10-19 19:50:48 +000058SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000059
mtklein@google.com519f9672013-09-20 14:31:45 +000060/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
61 */
62SK_API extern void* sk_calloc(size_t size);
63
64/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
65 */
66SK_API extern void* sk_calloc_throw(size_t size);
67
reed@android.com4516f472009-06-29 16:25:36 +000068// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
69static inline void sk_bzero(void* buffer, size_t size) {
70 memset(buffer, 0, size);
71}
72
reed@google.combdf73612011-09-06 14:56:20 +000073///////////////////////////////////////////////////////////////////////////////
74
75#ifdef SK_OVERRIDE_GLOBAL_NEW
76#include <new>
77
78inline void* operator new(size_t size) {
79 return sk_malloc_throw(size);
80}
81
82inline void operator delete(void* p) {
83 sk_free(p);
84}
85#endif
86
87///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
89#define SK_INIT_TO_AVOID_WARNING = 0
90
91#ifndef SkDebugf
92 void SkDebugf(const char format[], ...);
93#endif
94
95#ifdef SK_DEBUG
96 #define SkASSERT(cond) SK_DEBUGBREAK(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +000097 #define SkDEBUGFAIL(message) SkASSERT(false && message)
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 #define SkDEBUGCODE(code) code
99 #define SkDECLAREPARAM(type, var) , type var
100 #define SkPARAM(var) , var
101// #define SkDEBUGF(args ) SkDebugf##args
102 #define SkDEBUGF(args ) SkDebugf args
103 #define SkAssertResult(cond) SkASSERT(cond)
104#else
105 #define SkASSERT(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000106 #define SkDEBUGFAIL(message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 #define SkDEBUGCODE(code)
108 #define SkDEBUGF(args)
109 #define SkDECLAREPARAM(type, var)
110 #define SkPARAM(var)
111
112 // unlike SkASSERT, this guy executes its condition in the non-debug build
113 #define SkAssertResult(cond) cond
114#endif
115
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000116#ifdef SK_DEVELOPER
117 #define SkDEVCODE(code) code
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000118#else
119 #define SkDEVCODE(code)
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000120#endif
121
122#ifdef SK_IGNORE_TO_STRING
skia.committer@gmail.combc3d92a2014-03-14 03:02:26 +0000123 #define SK_TO_STRING_NONVIRT()
124 #define SK_TO_STRING_VIRT()
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000125 #define SK_TO_STRING_PUREVIRT()
126 #define SK_TO_STRING_OVERRIDE()
127#else
128 // the 'toString' helper functions convert Sk* objects to human-readable
129 // form in developer mode
130 #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
131 #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
132 #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
133 #define SK_TO_STRING_OVERRIDE() virtual void toString(SkString* str) const SK_OVERRIDE;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000134#endif
135
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000136template <bool>
137struct SkCompileAssert {
138};
139
bungeman@google.com562b2e62014-03-12 21:41:06 +0000140// Uses static_cast<bool>(expr) instead of bool(expr) due to
141// https://connect.microsoft.com/VisualStudio/feedback/details/832915
bungeman@google.com9c28fa52014-03-13 17:51:05 +0000142
143// The extra parentheses in SkCompileAssert<(...)> are a work around for
144// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57771
145// which was fixed in gcc 4.8.2.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000146#define SK_COMPILE_ASSERT(expr, msg) \
bungeman@google.com2f582092014-03-12 21:56:23 +0000147 typedef SkCompileAssert<(static_cast<bool>(expr))> \
bungeman@google.com562b2e62014-03-12 21:41:06 +0000148 msg[static_cast<bool>(expr) ? 1 : -1] SK_UNUSED
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000149
reed@google.com49a5b192012-10-25 17:31:39 +0000150/*
151 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
152 *
153 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
154 *
155 */
156#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
157#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
158
159/*
160 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
161 * line number. Easy way to construct
162 * unique names for local functions or
163 * variables.
164 */
165#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
166
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000167/**
168 * For some classes, it's almost always an error to instantiate one without a name, e.g.
169 * {
170 * SkAutoMutexAcquire(&mutex);
171 * <some code>
172 * }
173 * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
174 * but instead the mutex is acquired and then immediately released. The correct usage is
175 * {
176 * SkAutoMutexAcquire lock(&mutex);
177 * <some code>
178 * }
179 *
180 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
181 * like this:
182 * class classname {
183 * <your class>
184 * };
185 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
186 *
187 * This won't work with templates, and you must inline the class' constructors and destructors.
188 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
189 */
190#define SK_REQUIRE_LOCAL_VAR(classname) \
191 SK_COMPILE_ASSERT(false, missing_name_for_##classname)
192
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193///////////////////////////////////////////////////////////////////////
194
reed@google.com37a31332011-01-25 14:55:42 +0000195/**
196 * Fast type for signed 8 bits. Use for parameter passing and local variables,
197 * not for storage.
198 */
199typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200
reed@google.com37a31332011-01-25 14:55:42 +0000201/**
202 * Fast type for unsigned 8 bits. Use for parameter passing and local
203 * variables, not for storage
204 */
205typedef unsigned U8CPU;
206
207/**
208 * Fast type for signed 16 bits. Use for parameter passing and local variables,
209 * not for storage
210 */
211typedef int S16CPU;
212
213/**
214 * Fast type for unsigned 16 bits. Use for parameter passing and local
215 * variables, not for storage
216 */
217typedef unsigned U16CPU;
218
219/**
220 * Meant to be faster than bool (doesn't promise to be 0 or 1,
221 * just 0 or non-zero
222 */
223typedef int SkBool;
224
225/**
226 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
227 */
228typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229
230#ifdef SK_DEBUG
bungeman@google.com777ded02013-07-19 22:30:11 +0000231 SK_API int8_t SkToS8(intmax_t);
232 SK_API uint8_t SkToU8(uintmax_t);
233 SK_API int16_t SkToS16(intmax_t);
234 SK_API uint16_t SkToU16(uintmax_t);
235 SK_API int32_t SkToS32(intmax_t);
236 SK_API uint32_t SkToU32(uintmax_t);
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000237 SK_API int SkToInt(intmax_t);
reed@google.com7fa2a652014-01-27 13:42:58 +0000238 SK_API unsigned SkToUInt(uintmax_t);
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000239 SK_API size_t SkToSizeT(uintmax_t);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240#else
241 #define SkToS8(x) ((int8_t)(x))
242 #define SkToU8(x) ((uint8_t)(x))
243 #define SkToS16(x) ((int16_t)(x))
244 #define SkToU16(x) ((uint16_t)(x))
245 #define SkToS32(x) ((int32_t)(x))
246 #define SkToU32(x) ((uint32_t)(x))
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000247 #define SkToInt(x) ((int)(x))
reed@google.com7fa2a652014-01-27 13:42:58 +0000248 #define SkToUInt(x) ((unsigned)(x))
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000249 #define SkToSizeT(x) ((size_t)(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250#endif
251
252/** Returns 0 or 1 based on the condition
253*/
254#define SkToBool(cond) ((cond) != 0)
255
256#define SK_MaxS16 32767
257#define SK_MinS16 -32767
258#define SK_MaxU16 0xFFFF
259#define SK_MinU16 0
260#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000261#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262#define SK_MaxU32 0xFFFFFFFF
263#define SK_MinU32 0
humper@google.com0e515772013-01-07 19:54:40 +0000264#define SK_NaN32 (1 << 31)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265
reed@android.comd4577752009-11-21 02:48:11 +0000266/** Returns true if the value can be represented with signed 16bits
267 */
reed@android.com90209ca2009-11-21 19:58:04 +0000268static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000269 return (int16_t)x == x;
270}
271
272/** Returns true if the value can be represented with unsigned 16bits
273 */
reed@android.com90209ca2009-11-21 19:58:04 +0000274static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000275 return (uint16_t)x == x;
276}
277
278//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279#ifndef SK_OFFSETOF
reed@google.com56d3a232012-03-21 12:25:48 +0000280 #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281#endif
282
283/** Returns the number of entries in an array (not a pointer)
284*/
285#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
286
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287#define SkAlign2(x) (((x) + 1) >> 1 << 1)
reed@google.comc6faa5a2012-06-27 15:07:11 +0000288#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289
reed@google.comc6faa5a2012-06-27 15:07:11 +0000290#define SkAlign4(x) (((x) + 3) >> 2 << 2)
291#define SkIsAlign4(x) (0 == ((x) & 3))
292
293#define SkAlign8(x) (((x) + 7) >> 3 << 3)
294#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000295
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296typedef uint32_t SkFourByteTag;
297#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
298
299/** 32 bit integer to hold a unicode value
300*/
301typedef int32_t SkUnichar;
302/** 32 bit value to hold a millisecond count
303*/
304typedef uint32_t SkMSec;
305/** 1 second measured in milliseconds
306*/
307#define SK_MSec1 1000
308/** maximum representable milliseconds
309*/
310#define SK_MSecMax 0x7FFFFFFF
311/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
312*/
313#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
314/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
315*/
316#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
317
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000318/** The generation IDs in Skia reserve 0 has an invalid marker.
319 */
320#define SK_InvalidGenID 0
321
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322/****************************************************************************
323 The rest of these only build with C++
324*/
325#ifdef __cplusplus
326
327/** Faster than SkToBool for integral conditions. Returns 0 or 1
328*/
reed@android.comd4577752009-11-21 02:48:11 +0000329static inline int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 return (n | (0-n)) >> 31;
331}
332
bsalomon@google.comff436612013-02-27 19:07:32 +0000333/** Generic swap function. Classes with efficient swaps should specialize this function to take
334 their fast path. This function is used by SkTSort. */
reed@android.comd4577752009-11-21 02:48:11 +0000335template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336 T c(a);
337 a = b;
338 b = c;
339}
340
reed@android.comd4577752009-11-21 02:48:11 +0000341static inline int32_t SkAbs32(int32_t value) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000342 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000344 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346}
347
reed@google.com2b57dc62013-01-08 13:23:32 +0000348template <typename T> inline T SkTAbs(T value) {
349 if (value < 0) {
350 value = -value;
351 }
352 return value;
353}
354
reed@android.comd4577752009-11-21 02:48:11 +0000355static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356 if (a < b)
357 a = b;
358 return a;
359}
360
reed@android.comd4577752009-11-21 02:48:11 +0000361static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 if (a > b)
363 a = b;
364 return a;
365}
366
caryclark@google.com3b97af52013-04-23 11:56:44 +0000367template <typename T> const T& SkTMin(const T& a, const T& b) {
368 return (a < b) ? a : b;
369}
370
371template <typename T> const T& SkTMax(const T& a, const T& b) {
372 return (b < a) ? a : b;
373}
374
reed@android.comd4577752009-11-21 02:48:11 +0000375static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 return (a >> 31) | ((unsigned) -a >> 31);
377}
378
reed@android.comd4577752009-11-21 02:48:11 +0000379static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000380 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000381 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000382 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000383 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000384}
385
386/** Returns signed 32 bit value pinned between min and max, inclusively
387*/
reed@android.comd4577752009-11-21 02:48:11 +0000388static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000389 if (value < min) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390 value = min;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000391 }
392 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000394 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000395 return value;
396}
397
reed@android.comd4577752009-11-21 02:48:11 +0000398static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
399 unsigned shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400 SkASSERT((int)cond == 0 || (int)cond == 1);
401 return (bits & ~(1 << shift)) | ((int)cond << shift);
402}
403
reed@android.comd4577752009-11-21 02:48:11 +0000404static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
405 uint32_t mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406 return cond ? bits | mask : bits & ~mask;
407}
408
reed@google.com1fcd51e2011-01-05 15:50:27 +0000409///////////////////////////////////////////////////////////////////////////////
410
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000411/** Use to combine multiple bits in a bitmask in a type safe way.
412 */
413template <typename T>
414T SkTBitOr(T a, T b) {
415 return (T)(a | b);
416}
417
reed@google.com1fcd51e2011-01-05 15:50:27 +0000418/**
419 * Use to cast a pointer to a different type, and maintaining strict-aliasing
420 */
421template <typename Dst> Dst SkTCast(const void* ptr) {
422 union {
423 const void* src;
424 Dst dst;
425 } data;
426 data.src = ptr;
427 return data.dst;
428}
429
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430//////////////////////////////////////////////////////////////////////////////
431
432/** \class SkNoncopyable
433
434SkNoncopyable is the base class for objects that may do not want to
435be copied. It hides its copy-constructor and its assignment-operator.
436*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000437class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438public:
439 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000440
reed@android.com8a1c16f2008-12-17 15:59:43 +0000441private:
442 SkNoncopyable(const SkNoncopyable&);
443 SkNoncopyable& operator=(const SkNoncopyable&);
444};
445
446class SkAutoFree : SkNoncopyable {
447public:
448 SkAutoFree() : fPtr(NULL) {}
449 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
450 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000451
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452 /** Return the currently allocate buffer, or null
453 */
454 void* get() const { return fPtr; }
455
456 /** Assign a new ptr allocated with sk_malloc (or null), and return the
457 previous ptr. Note it is the caller's responsibility to sk_free the
458 returned ptr.
459 */
460 void* set(void* ptr) {
461 void* prev = fPtr;
462 fPtr = ptr;
463 return prev;
464 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000465
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466 /** Transfer ownership of the current ptr to the caller, setting the
467 internal reference to null. Note the caller is reponsible for calling
468 sk_free on the returned address.
469 */
470 void* detach() { return this->set(NULL); }
471
472 /** Free the current buffer, and set the internal reference to NULL. Same
473 as calling sk_free(detach())
474 */
475 void free() {
476 sk_free(fPtr);
477 fPtr = NULL;
478 }
479
480private:
481 void* fPtr;
482 // illegal
483 SkAutoFree(const SkAutoFree&);
484 SkAutoFree& operator=(const SkAutoFree&);
485};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000486#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000487
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000488/**
489 * Manage an allocated block of heap memory. This object is the sole manager of
490 * the lifetime of the block, so the caller must not call sk_free() or delete
reed@google.com1c401d82011-10-18 18:52:03 +0000491 * on the block, unless detach() was called.
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000492 */
493class SkAutoMalloc : public SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000494public:
reed@google.com3ab41952011-10-18 18:32:46 +0000495 explicit SkAutoMalloc(size_t size = 0) {
496 fPtr = size ? sk_malloc_throw(size) : NULL;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000497 fSize = size;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000498 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000499
500 ~SkAutoMalloc() {
501 sk_free(fPtr);
502 }
503
504 /**
reed@google.com1c401d82011-10-18 18:52:03 +0000505 * Passed to reset to specify what happens if the requested size is smaller
506 * than the current size (and the current block was dynamically allocated).
507 */
508 enum OnShrink {
509 /**
510 * If the requested size is smaller than the current size, and the
511 * current block is dynamically allocated, free the old block and
512 * malloc a new block of the smaller size.
513 */
514 kAlloc_OnShrink,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000515
reed@google.com1c401d82011-10-18 18:52:03 +0000516 /**
517 * If the requested size is smaller than the current size, and the
518 * current block is dynamically allocated, just return the old
519 * block.
520 */
tomhudson@google.com1f902872012-06-01 13:15:47 +0000521 kReuse_OnShrink
reed@google.com1c401d82011-10-18 18:52:03 +0000522 };
523
524 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000525 * Reallocates the block to a new size. The ptr may or may not change.
526 */
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000527 void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) {
reed@google.com1c401d82011-10-18 18:52:03 +0000528 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000529 if (NULL != didChangeAlloc) {
530 *didChangeAlloc = false;
531 }
reed@google.com1c401d82011-10-18 18:52:03 +0000532 return fPtr;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000533 }
reed@google.com1c401d82011-10-18 18:52:03 +0000534
535 sk_free(fPtr);
536 fPtr = size ? sk_malloc_throw(size) : NULL;
537 fSize = size;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000538 if (NULL != didChangeAlloc) {
539 *didChangeAlloc = true;
540 }
reed@google.com1c401d82011-10-18 18:52:03 +0000541
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000542 return fPtr;
543 }
544
545 /**
546 * Releases the block back to the heap
547 */
548 void free() {
549 this->reset(0);
550 }
551
552 /**
553 * Return the allocated block.
554 */
555 void* get() { return fPtr; }
556 const void* get() const { return fPtr; }
557
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000558 /** Transfer ownership of the current ptr to the caller, setting the
559 internal reference to null. Note the caller is reponsible for calling
560 sk_free on the returned address.
561 */
562 void* detach() {
563 void* ptr = fPtr;
564 fPtr = NULL;
565 fSize = 0;
566 return ptr;
567 }
568
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000569private:
570 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000571 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000572};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000573#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574
reed@google.com63a60602011-03-10 13:07:35 +0000575/**
576 * Manage an allocated block of memory. If the requested size is <= kSize, then
577 * the allocation will come from the stack rather than the heap. This object
578 * is the sole manager of the lifetime of the block, so the caller must not
579 * call sk_free() or delete on the block.
580 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000581template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
582public:
reed@google.com63a60602011-03-10 13:07:35 +0000583 /**
584 * Creates initially empty storage. get() returns a ptr, but it is to
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000585 * a zero-byte allocation. Must call reset(size) to return an allocated
reed@google.com63a60602011-03-10 13:07:35 +0000586 * block.
587 */
588 SkAutoSMalloc() {
589 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000590 fSize = kSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000591 }
reed@google.com63a60602011-03-10 13:07:35 +0000592
593 /**
594 * Allocate a block of the specified size. If size <= kSize, then the
595 * allocation will come from the stack, otherwise it will be dynamically
596 * allocated.
597 */
598 explicit SkAutoSMalloc(size_t size) {
599 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000600 fSize = kSize;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000601 this->reset(size);
reed@google.com63a60602011-03-10 13:07:35 +0000602 }
603
604 /**
605 * Free the allocated block (if any). If the block was small enought to
606 * have been allocated on the stack (size <= kSize) then this does nothing.
607 */
608 ~SkAutoSMalloc() {
609 if (fPtr != (void*)fStorage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000610 sk_free(fPtr);
reed@google.com63a60602011-03-10 13:07:35 +0000611 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000612 }
reed@google.com63a60602011-03-10 13:07:35 +0000613
614 /**
615 * Return the allocated block. May return non-null even if the block is
616 * of zero size. Since this may be on the stack or dynamically allocated,
617 * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
618 * to manage it.
619 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000620 void* get() const { return fPtr; }
reed@google.com63a60602011-03-10 13:07:35 +0000621
622 /**
623 * Return a new block of the requested size, freeing (as necessary) any
624 * previously allocated block. As with the constructor, if size <= kSize
625 * then the return block may be allocated locally, rather than from the
626 * heap.
627 */
reed@google.com1c401d82011-10-18 18:52:03 +0000628 void* reset(size_t size,
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000629 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
630 bool* didChangeAlloc = NULL) {
631 size = (size < kSize) ? kSize : size;
robertphillips@google.com0f2b1952013-05-23 14:59:40 +0000632 bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000633 if (NULL != didChangeAlloc) {
634 *didChangeAlloc = alloc;
reed@google.com1c401d82011-10-18 18:52:03 +0000635 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000636 if (alloc) {
637 if (fPtr != (void*)fStorage) {
638 sk_free(fPtr);
639 }
reed@google.com1c401d82011-10-18 18:52:03 +0000640
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000641 if (size == kSize) {
642 SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
643 fPtr = fStorage;
644 } else {
645 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
646 }
reed@google.com63a60602011-03-10 13:07:35 +0000647
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000648 fSize = size;
reed@google.com63a60602011-03-10 13:07:35 +0000649 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000650 SkASSERT(fSize >= size && fSize >= kSize);
651 SkASSERT((fPtr == fStorage) || fSize > kSize);
reed@google.com63a60602011-03-10 13:07:35 +0000652 return fPtr;
653 }
654
reed@android.com8a1c16f2008-12-17 15:59:43 +0000655private:
656 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000657 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658 uint32_t fStorage[(kSize + 3) >> 2];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000659};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000660// Can't guard the constructor because it's a template class.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000661
662#endif /* C++ */
663
664#endif