blob: 219e51fbe929a1e56190a618e2725e3e05393147 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkTypes_DEFINED
11#define SkTypes_DEFINED
12
13#include "SkPreConfig.h"
14#include "SkUserConfig.h"
15#include "SkPostConfig.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +000016#include <stdint.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
18/** \file SkTypes.h
19*/
20
reed@android.com9aa8b322010-04-13 13:22:54 +000021/** See SkGraphics::GetVersion() to retrieve these at runtime
22 */
23#define SKIA_VERSION_MAJOR 1
24#define SKIA_VERSION_MINOR 0
25#define SKIA_VERSION_PATCH 0
26
reed@android.com8a1c16f2008-12-17 15:59:43 +000027/*
28 memory wrappers to be implemented by the porting layer (platform)
29*/
30
31/** Called internally if we run out of memory. The platform implementation must
32 not return, but should either throw an exception or otherwise exit.
33*/
reed@google.comde916c82011-10-19 19:50:48 +000034SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000035/** Called internally if we hit an unrecoverable error.
36 The platform implementation must not return, but should either throw
37 an exception or otherwise exit.
38*/
reed@google.comde916c82011-10-19 19:50:48 +000039SK_API extern void sk_throw(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
41enum {
42 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
43 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
44};
45/** Return a block of memory (at least 4-byte aligned) of at least the
46 specified size. If the requested memory cannot be returned, either
mtklein@google.com519f9672013-09-20 14:31:45 +000047 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
49*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000050SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
52*/
reed@google.comde916c82011-10-19 19:50:48 +000053SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000054/** Same as standard realloc(), but this one never returns null on failure. It will throw
55 an exception if it fails.
56*/
reed@google.comde916c82011-10-19 19:50:48 +000057SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000058/** Free memory returned by sk_malloc(). It is safe to pass null.
59*/
reed@google.comde916c82011-10-19 19:50:48 +000060SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000061
mtklein@google.com519f9672013-09-20 14:31:45 +000062/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
63 */
64SK_API extern void* sk_calloc(size_t size);
65
66/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
67 */
68SK_API extern void* sk_calloc_throw(size_t size);
69
reed@android.com4516f472009-06-29 16:25:36 +000070// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
71static inline void sk_bzero(void* buffer, size_t size) {
72 memset(buffer, 0, size);
73}
74
reed@google.combdf73612011-09-06 14:56:20 +000075///////////////////////////////////////////////////////////////////////////////
76
77#ifdef SK_OVERRIDE_GLOBAL_NEW
78#include <new>
79
80inline void* operator new(size_t size) {
81 return sk_malloc_throw(size);
82}
83
84inline void operator delete(void* p) {
85 sk_free(p);
86}
87#endif
88
89///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
91#define SK_INIT_TO_AVOID_WARNING = 0
92
93#ifndef SkDebugf
94 void SkDebugf(const char format[], ...);
95#endif
96
97#ifdef SK_DEBUG
98 #define SkASSERT(cond) SK_DEBUGBREAK(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +000099 #define SkDEBUGFAIL(message) SkASSERT(false && message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 #define SkDEBUGCODE(code) code
101 #define SkDECLAREPARAM(type, var) , type var
102 #define SkPARAM(var) , var
103// #define SkDEBUGF(args ) SkDebugf##args
104 #define SkDEBUGF(args ) SkDebugf args
105 #define SkAssertResult(cond) SkASSERT(cond)
106#else
107 #define SkASSERT(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000108 #define SkDEBUGFAIL(message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 #define SkDEBUGCODE(code)
110 #define SkDEBUGF(args)
111 #define SkDECLAREPARAM(type, var)
112 #define SkPARAM(var)
113
114 // unlike SkASSERT, this guy executes its condition in the non-debug build
115 #define SkAssertResult(cond) cond
116#endif
117
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000118#ifdef SK_DEVELOPER
119 #define SkDEVCODE(code) code
120 // the 'toString' helper functions convert Sk* objects to human-readable
121 // form in developer mode
122 #define SK_DEVELOPER_TO_STRING() virtual void toString(SkString* str) const SK_OVERRIDE;
123#else
124 #define SkDEVCODE(code)
125 #define SK_DEVELOPER_TO_STRING()
126#endif
127
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000128template <bool>
129struct SkCompileAssert {
130};
131
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000132#define SK_COMPILE_ASSERT(expr, msg) \
bungeman@google.com5ebbbe52013-08-06 18:32:29 +0000133 typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] SK_UNUSED
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000134
reed@google.com49a5b192012-10-25 17:31:39 +0000135/*
136 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
137 *
138 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
139 *
140 */
141#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
142#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
143
144/*
145 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
146 * line number. Easy way to construct
147 * unique names for local functions or
148 * variables.
149 */
150#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152///////////////////////////////////////////////////////////////////////
153
reed@google.com37a31332011-01-25 14:55:42 +0000154/**
155 * Fast type for signed 8 bits. Use for parameter passing and local variables,
156 * not for storage.
157 */
158typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159
reed@google.com37a31332011-01-25 14:55:42 +0000160/**
161 * Fast type for unsigned 8 bits. Use for parameter passing and local
162 * variables, not for storage
163 */
164typedef unsigned U8CPU;
165
166/**
167 * Fast type for signed 16 bits. Use for parameter passing and local variables,
168 * not for storage
169 */
170typedef int S16CPU;
171
172/**
173 * Fast type for unsigned 16 bits. Use for parameter passing and local
174 * variables, not for storage
175 */
176typedef unsigned U16CPU;
177
178/**
179 * Meant to be faster than bool (doesn't promise to be 0 or 1,
180 * just 0 or non-zero
181 */
182typedef int SkBool;
183
184/**
185 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
186 */
187typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188
189#ifdef SK_DEBUG
bungeman@google.com777ded02013-07-19 22:30:11 +0000190 SK_API int8_t SkToS8(intmax_t);
191 SK_API uint8_t SkToU8(uintmax_t);
192 SK_API int16_t SkToS16(intmax_t);
193 SK_API uint16_t SkToU16(uintmax_t);
194 SK_API int32_t SkToS32(intmax_t);
195 SK_API uint32_t SkToU32(uintmax_t);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196#else
197 #define SkToS8(x) ((int8_t)(x))
198 #define SkToU8(x) ((uint8_t)(x))
199 #define SkToS16(x) ((int16_t)(x))
200 #define SkToU16(x) ((uint16_t)(x))
201 #define SkToS32(x) ((int32_t)(x))
202 #define SkToU32(x) ((uint32_t)(x))
203#endif
204
205/** Returns 0 or 1 based on the condition
206*/
207#define SkToBool(cond) ((cond) != 0)
208
209#define SK_MaxS16 32767
210#define SK_MinS16 -32767
211#define SK_MaxU16 0xFFFF
212#define SK_MinU16 0
213#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000214#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215#define SK_MaxU32 0xFFFFFFFF
216#define SK_MinU32 0
humper@google.com0e515772013-01-07 19:54:40 +0000217#define SK_NaN32 (1 << 31)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218
reed@android.comd4577752009-11-21 02:48:11 +0000219/** Returns true if the value can be represented with signed 16bits
220 */
reed@android.com90209ca2009-11-21 19:58:04 +0000221static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000222 return (int16_t)x == x;
223}
224
225/** Returns true if the value can be represented with unsigned 16bits
226 */
reed@android.com90209ca2009-11-21 19:58:04 +0000227static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000228 return (uint16_t)x == x;
229}
230
231//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232#ifndef SK_OFFSETOF
reed@google.com56d3a232012-03-21 12:25:48 +0000233 #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234#endif
235
236/** Returns the number of entries in an array (not a pointer)
237*/
238#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240#define SkAlign2(x) (((x) + 1) >> 1 << 1)
reed@google.comc6faa5a2012-06-27 15:07:11 +0000241#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242
reed@google.comc6faa5a2012-06-27 15:07:11 +0000243#define SkAlign4(x) (((x) + 3) >> 2 << 2)
244#define SkIsAlign4(x) (0 == ((x) & 3))
245
246#define SkAlign8(x) (((x) + 7) >> 3 << 3)
247#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000248
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249typedef uint32_t SkFourByteTag;
250#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
251
252/** 32 bit integer to hold a unicode value
253*/
254typedef int32_t SkUnichar;
255/** 32 bit value to hold a millisecond count
256*/
257typedef uint32_t SkMSec;
258/** 1 second measured in milliseconds
259*/
260#define SK_MSec1 1000
261/** maximum representable milliseconds
262*/
263#define SK_MSecMax 0x7FFFFFFF
264/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
265*/
266#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
267/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
268*/
269#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
270
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271/****************************************************************************
272 The rest of these only build with C++
273*/
274#ifdef __cplusplus
275
276/** Faster than SkToBool for integral conditions. Returns 0 or 1
277*/
reed@android.comd4577752009-11-21 02:48:11 +0000278static inline int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 return (n | (0-n)) >> 31;
280}
281
bsalomon@google.comff436612013-02-27 19:07:32 +0000282/** Generic swap function. Classes with efficient swaps should specialize this function to take
283 their fast path. This function is used by SkTSort. */
reed@android.comd4577752009-11-21 02:48:11 +0000284template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 T c(a);
286 a = b;
287 b = c;
288}
289
reed@android.comd4577752009-11-21 02:48:11 +0000290static inline int32_t SkAbs32(int32_t value) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000291 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000293 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295}
296
reed@google.com2b57dc62013-01-08 13:23:32 +0000297template <typename T> inline T SkTAbs(T value) {
298 if (value < 0) {
299 value = -value;
300 }
301 return value;
302}
303
reed@android.comd4577752009-11-21 02:48:11 +0000304static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305 if (a < b)
306 a = b;
307 return a;
308}
309
reed@android.comd4577752009-11-21 02:48:11 +0000310static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311 if (a > b)
312 a = b;
313 return a;
314}
315
caryclark@google.com3b97af52013-04-23 11:56:44 +0000316template <typename T> const T& SkTMin(const T& a, const T& b) {
317 return (a < b) ? a : b;
318}
319
320template <typename T> const T& SkTMax(const T& a, const T& b) {
321 return (b < a) ? a : b;
322}
323
reed@android.comd4577752009-11-21 02:48:11 +0000324static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 return (a >> 31) | ((unsigned) -a >> 31);
326}
327
reed@android.comd4577752009-11-21 02:48:11 +0000328static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000329 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000331 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333}
334
335/** Returns signed 32 bit value pinned between min and max, inclusively
336*/
reed@android.comd4577752009-11-21 02:48:11 +0000337static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000338 if (value < min) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339 value = min;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000340 }
341 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000343 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 return value;
345}
346
reed@android.comd4577752009-11-21 02:48:11 +0000347static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
348 unsigned shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 SkASSERT((int)cond == 0 || (int)cond == 1);
350 return (bits & ~(1 << shift)) | ((int)cond << shift);
351}
352
reed@android.comd4577752009-11-21 02:48:11 +0000353static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
354 uint32_t mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355 return cond ? bits | mask : bits & ~mask;
356}
357
reed@google.com1fcd51e2011-01-05 15:50:27 +0000358///////////////////////////////////////////////////////////////////////////////
359
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000360/** Use to combine multiple bits in a bitmask in a type safe way.
361 */
362template <typename T>
363T SkTBitOr(T a, T b) {
364 return (T)(a | b);
365}
366
reed@google.com1fcd51e2011-01-05 15:50:27 +0000367/**
368 * Use to cast a pointer to a different type, and maintaining strict-aliasing
369 */
370template <typename Dst> Dst SkTCast(const void* ptr) {
371 union {
372 const void* src;
373 Dst dst;
374 } data;
375 data.src = ptr;
376 return data.dst;
377}
378
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379//////////////////////////////////////////////////////////////////////////////
380
381/** \class SkNoncopyable
382
383SkNoncopyable is the base class for objects that may do not want to
384be copied. It hides its copy-constructor and its assignment-operator.
385*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000386class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000387public:
388 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000389
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390private:
391 SkNoncopyable(const SkNoncopyable&);
392 SkNoncopyable& operator=(const SkNoncopyable&);
393};
394
395class SkAutoFree : SkNoncopyable {
396public:
397 SkAutoFree() : fPtr(NULL) {}
398 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
399 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000400
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 /** Return the currently allocate buffer, or null
402 */
403 void* get() const { return fPtr; }
404
405 /** Assign a new ptr allocated with sk_malloc (or null), and return the
406 previous ptr. Note it is the caller's responsibility to sk_free the
407 returned ptr.
408 */
409 void* set(void* ptr) {
410 void* prev = fPtr;
411 fPtr = ptr;
412 return prev;
413 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000414
reed@android.com8a1c16f2008-12-17 15:59:43 +0000415 /** Transfer ownership of the current ptr to the caller, setting the
416 internal reference to null. Note the caller is reponsible for calling
417 sk_free on the returned address.
418 */
419 void* detach() { return this->set(NULL); }
420
421 /** Free the current buffer, and set the internal reference to NULL. Same
422 as calling sk_free(detach())
423 */
424 void free() {
425 sk_free(fPtr);
426 fPtr = NULL;
427 }
428
429private:
430 void* fPtr;
431 // illegal
432 SkAutoFree(const SkAutoFree&);
433 SkAutoFree& operator=(const SkAutoFree&);
434};
435
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000436/**
437 * Manage an allocated block of heap memory. This object is the sole manager of
438 * the lifetime of the block, so the caller must not call sk_free() or delete
reed@google.com1c401d82011-10-18 18:52:03 +0000439 * on the block, unless detach() was called.
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000440 */
441class SkAutoMalloc : public SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000442public:
reed@google.com3ab41952011-10-18 18:32:46 +0000443 explicit SkAutoMalloc(size_t size = 0) {
444 fPtr = size ? sk_malloc_throw(size) : NULL;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000445 fSize = size;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000447
448 ~SkAutoMalloc() {
449 sk_free(fPtr);
450 }
451
452 /**
reed@google.com1c401d82011-10-18 18:52:03 +0000453 * Passed to reset to specify what happens if the requested size is smaller
454 * than the current size (and the current block was dynamically allocated).
455 */
456 enum OnShrink {
457 /**
458 * If the requested size is smaller than the current size, and the
459 * current block is dynamically allocated, free the old block and
460 * malloc a new block of the smaller size.
461 */
462 kAlloc_OnShrink,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000463
reed@google.com1c401d82011-10-18 18:52:03 +0000464 /**
465 * If the requested size is smaller than the current size, and the
466 * current block is dynamically allocated, just return the old
467 * block.
468 */
tomhudson@google.com1f902872012-06-01 13:15:47 +0000469 kReuse_OnShrink
reed@google.com1c401d82011-10-18 18:52:03 +0000470 };
471
472 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000473 * Reallocates the block to a new size. The ptr may or may not change.
474 */
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000475 void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) {
reed@google.com1c401d82011-10-18 18:52:03 +0000476 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000477 if (NULL != didChangeAlloc) {
478 *didChangeAlloc = false;
479 }
reed@google.com1c401d82011-10-18 18:52:03 +0000480 return fPtr;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000481 }
reed@google.com1c401d82011-10-18 18:52:03 +0000482
483 sk_free(fPtr);
484 fPtr = size ? sk_malloc_throw(size) : NULL;
485 fSize = size;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000486 if (NULL != didChangeAlloc) {
487 *didChangeAlloc = true;
488 }
reed@google.com1c401d82011-10-18 18:52:03 +0000489
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000490 return fPtr;
491 }
492
493 /**
494 * Releases the block back to the heap
495 */
496 void free() {
497 this->reset(0);
498 }
499
500 /**
501 * Return the allocated block.
502 */
503 void* get() { return fPtr; }
504 const void* get() const { return fPtr; }
505
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000506 /** Transfer ownership of the current ptr to the caller, setting the
507 internal reference to null. Note the caller is reponsible for calling
508 sk_free on the returned address.
509 */
510 void* detach() {
511 void* ptr = fPtr;
512 fPtr = NULL;
513 fSize = 0;
514 return ptr;
515 }
516
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000517private:
518 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000519 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520};
521
reed@google.com63a60602011-03-10 13:07:35 +0000522/**
523 * Manage an allocated block of memory. If the requested size is <= kSize, then
524 * the allocation will come from the stack rather than the heap. This object
525 * is the sole manager of the lifetime of the block, so the caller must not
526 * call sk_free() or delete on the block.
527 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
529public:
reed@google.com63a60602011-03-10 13:07:35 +0000530 /**
531 * Creates initially empty storage. get() returns a ptr, but it is to
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000532 * a zero-byte allocation. Must call reset(size) to return an allocated
reed@google.com63a60602011-03-10 13:07:35 +0000533 * block.
534 */
535 SkAutoSMalloc() {
536 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000537 fSize = kSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000538 }
reed@google.com63a60602011-03-10 13:07:35 +0000539
540 /**
541 * Allocate a block of the specified size. If size <= kSize, then the
542 * allocation will come from the stack, otherwise it will be dynamically
543 * allocated.
544 */
545 explicit SkAutoSMalloc(size_t size) {
546 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000547 fSize = kSize;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000548 this->reset(size);
reed@google.com63a60602011-03-10 13:07:35 +0000549 }
550
551 /**
552 * Free the allocated block (if any). If the block was small enought to
553 * have been allocated on the stack (size <= kSize) then this does nothing.
554 */
555 ~SkAutoSMalloc() {
556 if (fPtr != (void*)fStorage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000557 sk_free(fPtr);
reed@google.com63a60602011-03-10 13:07:35 +0000558 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000559 }
reed@google.com63a60602011-03-10 13:07:35 +0000560
561 /**
562 * Return the allocated block. May return non-null even if the block is
563 * of zero size. Since this may be on the stack or dynamically allocated,
564 * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
565 * to manage it.
566 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000567 void* get() const { return fPtr; }
reed@google.com63a60602011-03-10 13:07:35 +0000568
569 /**
570 * Return a new block of the requested size, freeing (as necessary) any
571 * previously allocated block. As with the constructor, if size <= kSize
572 * then the return block may be allocated locally, rather than from the
573 * heap.
574 */
reed@google.com1c401d82011-10-18 18:52:03 +0000575 void* reset(size_t size,
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000576 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
577 bool* didChangeAlloc = NULL) {
578 size = (size < kSize) ? kSize : size;
robertphillips@google.com0f2b1952013-05-23 14:59:40 +0000579 bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000580 if (NULL != didChangeAlloc) {
581 *didChangeAlloc = alloc;
reed@google.com1c401d82011-10-18 18:52:03 +0000582 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000583 if (alloc) {
584 if (fPtr != (void*)fStorage) {
585 sk_free(fPtr);
586 }
reed@google.com1c401d82011-10-18 18:52:03 +0000587
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000588 if (size == kSize) {
589 SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
590 fPtr = fStorage;
591 } else {
592 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
593 }
reed@google.com63a60602011-03-10 13:07:35 +0000594
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000595 fSize = size;
reed@google.com63a60602011-03-10 13:07:35 +0000596 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000597 SkASSERT(fSize >= size && fSize >= kSize);
598 SkASSERT((fPtr == fStorage) || fSize > kSize);
reed@google.com63a60602011-03-10 13:07:35 +0000599 return fPtr;
600 }
601
reed@android.com8a1c16f2008-12-17 15:59:43 +0000602private:
603 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000604 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000605 uint32_t fStorage[(kSize + 3) >> 2];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000606};
607
608#endif /* C++ */
609
610#endif