blob: 113fd00e6b45db0e6e226381c24f17f931f96676 [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"
16
17#ifndef SK_IGNORE_STDINT_DOT_H
18 #include <stdint.h>
19#endif
20
21#include <stdio.h>
22
23/** \file SkTypes.h
24*/
25
reed@android.com9aa8b322010-04-13 13:22:54 +000026/** See SkGraphics::GetVersion() to retrieve these at runtime
27 */
28#define SKIA_VERSION_MAJOR 1
29#define SKIA_VERSION_MINOR 0
30#define SKIA_VERSION_PATCH 0
31
reed@android.com8a1c16f2008-12-17 15:59:43 +000032/*
33 memory wrappers to be implemented by the porting layer (platform)
34*/
35
36/** Called internally if we run out of memory. The platform implementation must
37 not return, but should either throw an exception or otherwise exit.
38*/
reed@google.comde916c82011-10-19 19:50:48 +000039SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040/** Called internally if we hit an unrecoverable error.
41 The platform implementation must not return, but should either throw
42 an exception or otherwise exit.
43*/
reed@google.comde916c82011-10-19 19:50:48 +000044SK_API extern void sk_throw(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000045
46enum {
47 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
48 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
49};
50/** Return a block of memory (at least 4-byte aligned) of at least the
51 specified size. If the requested memory cannot be returned, either
52 return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw()
53 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
54*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000055SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000056/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
57*/
reed@google.comde916c82011-10-19 19:50:48 +000058SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000059/** Same as standard realloc(), but this one never returns null on failure. It will throw
60 an exception if it fails.
61*/
reed@google.comde916c82011-10-19 19:50:48 +000062SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000063/** Free memory returned by sk_malloc(). It is safe to pass null.
64*/
reed@google.comde916c82011-10-19 19:50:48 +000065SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000066
reed@android.com4516f472009-06-29 16:25:36 +000067// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
68static inline void sk_bzero(void* buffer, size_t size) {
69 memset(buffer, 0, size);
70}
71
reed@google.combdf73612011-09-06 14:56:20 +000072///////////////////////////////////////////////////////////////////////////////
73
74#ifdef SK_OVERRIDE_GLOBAL_NEW
75#include <new>
76
77inline void* operator new(size_t size) {
78 return sk_malloc_throw(size);
79}
80
81inline void operator delete(void* p) {
82 sk_free(p);
83}
84#endif
85
86///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000087
88#define SK_INIT_TO_AVOID_WARNING = 0
89
90#ifndef SkDebugf
91 void SkDebugf(const char format[], ...);
92#endif
93
94#ifdef SK_DEBUG
95 #define SkASSERT(cond) SK_DEBUGBREAK(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +000096 #define SkDEBUGFAIL(message) SkASSERT(false && message)
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 #define SkDEBUGCODE(code) code
98 #define SkDECLAREPARAM(type, var) , type var
99 #define SkPARAM(var) , var
100// #define SkDEBUGF(args ) SkDebugf##args
101 #define SkDEBUGF(args ) SkDebugf args
102 #define SkAssertResult(cond) SkASSERT(cond)
103#else
104 #define SkASSERT(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000105 #define SkDEBUGFAIL(message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 #define SkDEBUGCODE(code)
107 #define SkDEBUGF(args)
108 #define SkDECLAREPARAM(type, var)
109 #define SkPARAM(var)
110
111 // unlike SkASSERT, this guy executes its condition in the non-debug build
112 #define SkAssertResult(cond) cond
113#endif
114
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000115template <bool>
116struct SkCompileAssert {
117};
118
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000119#define SK_COMPILE_ASSERT(expr, msg) \
120 typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
121
reed@google.com49a5b192012-10-25 17:31:39 +0000122/*
123 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
124 *
125 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
126 *
127 */
128#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
129#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
130
131/*
132 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
133 * line number. Easy way to construct
134 * unique names for local functions or
135 * variables.
136 */
137#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139///////////////////////////////////////////////////////////////////////
140
reed@google.com37a31332011-01-25 14:55:42 +0000141/**
142 * Fast type for signed 8 bits. Use for parameter passing and local variables,
143 * not for storage.
144 */
145typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146
reed@google.com37a31332011-01-25 14:55:42 +0000147/**
148 * Fast type for unsigned 8 bits. Use for parameter passing and local
149 * variables, not for storage
150 */
151typedef unsigned U8CPU;
152
153/**
154 * Fast type for signed 16 bits. Use for parameter passing and local variables,
155 * not for storage
156 */
157typedef int S16CPU;
158
159/**
160 * Fast type for unsigned 16 bits. Use for parameter passing and local
161 * variables, not for storage
162 */
163typedef unsigned U16CPU;
164
165/**
166 * Meant to be faster than bool (doesn't promise to be 0 or 1,
167 * just 0 or non-zero
168 */
169typedef int SkBool;
170
171/**
172 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
173 */
174typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175
176#ifdef SK_DEBUG
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000177 SK_API int8_t SkToS8(long);
178 SK_API uint8_t SkToU8(size_t);
179 SK_API int16_t SkToS16(long);
180 SK_API uint16_t SkToU16(size_t);
181 SK_API int32_t SkToS32(long);
182 SK_API uint32_t SkToU32(size_t);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183#else
184 #define SkToS8(x) ((int8_t)(x))
185 #define SkToU8(x) ((uint8_t)(x))
186 #define SkToS16(x) ((int16_t)(x))
187 #define SkToU16(x) ((uint16_t)(x))
188 #define SkToS32(x) ((int32_t)(x))
189 #define SkToU32(x) ((uint32_t)(x))
190#endif
191
192/** Returns 0 or 1 based on the condition
193*/
194#define SkToBool(cond) ((cond) != 0)
195
196#define SK_MaxS16 32767
197#define SK_MinS16 -32767
198#define SK_MaxU16 0xFFFF
199#define SK_MinU16 0
200#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000201#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202#define SK_MaxU32 0xFFFFFFFF
203#define SK_MinU32 0
humper@google.com0e515772013-01-07 19:54:40 +0000204#define SK_NaN32 (1 << 31)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205
reed@android.comd4577752009-11-21 02:48:11 +0000206/** Returns true if the value can be represented with signed 16bits
207 */
reed@android.com90209ca2009-11-21 19:58:04 +0000208static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000209 return (int16_t)x == x;
210}
211
212/** Returns true if the value can be represented with unsigned 16bits
213 */
reed@android.com90209ca2009-11-21 19:58:04 +0000214static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000215 return (uint16_t)x == x;
216}
217
218//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219#ifndef SK_OFFSETOF
reed@google.com56d3a232012-03-21 12:25:48 +0000220 #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221#endif
222
223/** Returns the number of entries in an array (not a pointer)
224*/
225#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
226
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227#define SkAlign2(x) (((x) + 1) >> 1 << 1)
reed@google.comc6faa5a2012-06-27 15:07:11 +0000228#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229
reed@google.comc6faa5a2012-06-27 15:07:11 +0000230#define SkAlign4(x) (((x) + 3) >> 2 << 2)
231#define SkIsAlign4(x) (0 == ((x) & 3))
232
233#define SkAlign8(x) (((x) + 7) >> 3 << 3)
234#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000235
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236typedef uint32_t SkFourByteTag;
237#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
238
239/** 32 bit integer to hold a unicode value
240*/
241typedef int32_t SkUnichar;
242/** 32 bit value to hold a millisecond count
243*/
244typedef uint32_t SkMSec;
245/** 1 second measured in milliseconds
246*/
247#define SK_MSec1 1000
248/** maximum representable milliseconds
249*/
250#define SK_MSecMax 0x7FFFFFFF
251/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
252*/
253#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
254/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
255*/
256#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
257
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258/****************************************************************************
259 The rest of these only build with C++
260*/
261#ifdef __cplusplus
262
263/** Faster than SkToBool for integral conditions. Returns 0 or 1
264*/
reed@android.comd4577752009-11-21 02:48:11 +0000265static inline int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266 return (n | (0-n)) >> 31;
267}
268
reed@android.comd4577752009-11-21 02:48:11 +0000269template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270 T c(a);
271 a = b;
272 b = c;
273}
274
reed@android.comd4577752009-11-21 02:48:11 +0000275static inline int32_t SkAbs32(int32_t value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
277 if (value < 0)
278 value = -value;
279 return value;
280#else
281 int32_t mask = value >> 31;
282 return (value ^ mask) - mask;
283#endif
284}
285
reed@google.com2b57dc62013-01-08 13:23:32 +0000286template <typename T> inline T SkTAbs(T value) {
287 if (value < 0) {
288 value = -value;
289 }
290 return value;
291}
292
reed@android.comd4577752009-11-21 02:48:11 +0000293static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294 if (a < b)
295 a = b;
296 return a;
297}
298
reed@android.comd4577752009-11-21 02:48:11 +0000299static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 if (a > b)
301 a = b;
302 return a;
303}
304
reed@android.comd4577752009-11-21 02:48:11 +0000305static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 return (a >> 31) | ((unsigned) -a >> 31);
307}
308
reed@android.comd4577752009-11-21 02:48:11 +0000309static inline int32_t SkFastMin32(int32_t value, int32_t max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
311 if (value > max)
312 value = max;
313 return value;
314#else
315 int diff = max - value;
316 // clear diff if it is negative (clear if value > max)
317 diff &= (diff >> 31);
318 return value + diff;
319#endif
320}
321
322/** Returns signed 32 bit value pinned between min and max, inclusively
323*/
reed@android.comd4577752009-11-21 02:48:11 +0000324static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
326 if (value < min)
327 value = min;
328 if (value > max)
329 value = max;
330#else
331 if (value < min)
332 value = min;
333 else if (value > max)
334 value = max;
335#endif
336 return value;
337}
338
reed@android.comd4577752009-11-21 02:48:11 +0000339static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
340 unsigned shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 SkASSERT((int)cond == 0 || (int)cond == 1);
342 return (bits & ~(1 << shift)) | ((int)cond << shift);
343}
344
reed@android.comd4577752009-11-21 02:48:11 +0000345static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
346 uint32_t mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 return cond ? bits | mask : bits & ~mask;
348}
349
reed@google.com1fcd51e2011-01-05 15:50:27 +0000350///////////////////////////////////////////////////////////////////////////////
351
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000352/** Use to combine multiple bits in a bitmask in a type safe way.
353 */
354template <typename T>
355T SkTBitOr(T a, T b) {
356 return (T)(a | b);
357}
358
reed@google.com1fcd51e2011-01-05 15:50:27 +0000359/**
360 * Use to cast a pointer to a different type, and maintaining strict-aliasing
361 */
362template <typename Dst> Dst SkTCast(const void* ptr) {
363 union {
364 const void* src;
365 Dst dst;
366 } data;
367 data.src = ptr;
368 return data.dst;
369}
370
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371//////////////////////////////////////////////////////////////////////////////
372
373/** \class SkNoncopyable
374
375SkNoncopyable is the base class for objects that may do not want to
376be copied. It hides its copy-constructor and its assignment-operator.
377*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000378class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379public:
380 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000381
reed@android.com8a1c16f2008-12-17 15:59:43 +0000382private:
383 SkNoncopyable(const SkNoncopyable&);
384 SkNoncopyable& operator=(const SkNoncopyable&);
385};
386
387class SkAutoFree : SkNoncopyable {
388public:
389 SkAutoFree() : fPtr(NULL) {}
390 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
391 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000392
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 /** Return the currently allocate buffer, or null
394 */
395 void* get() const { return fPtr; }
396
397 /** Assign a new ptr allocated with sk_malloc (or null), and return the
398 previous ptr. Note it is the caller's responsibility to sk_free the
399 returned ptr.
400 */
401 void* set(void* ptr) {
402 void* prev = fPtr;
403 fPtr = ptr;
404 return prev;
405 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000406
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 /** Transfer ownership of the current ptr to the caller, setting the
408 internal reference to null. Note the caller is reponsible for calling
409 sk_free on the returned address.
410 */
411 void* detach() { return this->set(NULL); }
412
413 /** Free the current buffer, and set the internal reference to NULL. Same
414 as calling sk_free(detach())
415 */
416 void free() {
417 sk_free(fPtr);
418 fPtr = NULL;
419 }
420
421private:
422 void* fPtr;
423 // illegal
424 SkAutoFree(const SkAutoFree&);
425 SkAutoFree& operator=(const SkAutoFree&);
426};
427
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000428/**
429 * Manage an allocated block of heap memory. This object is the sole manager of
430 * the lifetime of the block, so the caller must not call sk_free() or delete
reed@google.com1c401d82011-10-18 18:52:03 +0000431 * on the block, unless detach() was called.
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000432 */
433class SkAutoMalloc : public SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434public:
reed@google.com3ab41952011-10-18 18:32:46 +0000435 explicit SkAutoMalloc(size_t size = 0) {
436 fPtr = size ? sk_malloc_throw(size) : NULL;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000437 fSize = size;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000439
440 ~SkAutoMalloc() {
441 sk_free(fPtr);
442 }
443
444 /**
reed@google.com1c401d82011-10-18 18:52:03 +0000445 * Passed to reset to specify what happens if the requested size is smaller
446 * than the current size (and the current block was dynamically allocated).
447 */
448 enum OnShrink {
449 /**
450 * If the requested size is smaller than the current size, and the
451 * current block is dynamically allocated, free the old block and
452 * malloc a new block of the smaller size.
453 */
454 kAlloc_OnShrink,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000455
reed@google.com1c401d82011-10-18 18:52:03 +0000456 /**
457 * If the requested size is smaller than the current size, and the
458 * current block is dynamically allocated, just return the old
459 * block.
460 */
tomhudson@google.com1f902872012-06-01 13:15:47 +0000461 kReuse_OnShrink
reed@google.com1c401d82011-10-18 18:52:03 +0000462 };
463
464 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000465 * Reallocates the block to a new size. The ptr may or may not change.
466 */
reed@google.com1c401d82011-10-18 18:52:03 +0000467 void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink) {
468 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
469 return fPtr;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000470 }
reed@google.com1c401d82011-10-18 18:52:03 +0000471
472 sk_free(fPtr);
473 fPtr = size ? sk_malloc_throw(size) : NULL;
474 fSize = size;
475
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000476 return fPtr;
477 }
478
479 /**
480 * Releases the block back to the heap
481 */
482 void free() {
483 this->reset(0);
484 }
485
486 /**
487 * Return the allocated block.
488 */
489 void* get() { return fPtr; }
490 const void* get() const { return fPtr; }
491
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000492 /** Transfer ownership of the current ptr to the caller, setting the
493 internal reference to null. Note the caller is reponsible for calling
494 sk_free on the returned address.
495 */
496 void* detach() {
497 void* ptr = fPtr;
498 fPtr = NULL;
499 fSize = 0;
500 return ptr;
501 }
502
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000503private:
504 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000505 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000506};
507
reed@google.com63a60602011-03-10 13:07:35 +0000508/**
509 * Manage an allocated block of memory. If the requested size is <= kSize, then
510 * the allocation will come from the stack rather than the heap. This object
511 * is the sole manager of the lifetime of the block, so the caller must not
512 * call sk_free() or delete on the block.
513 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
515public:
reed@google.com63a60602011-03-10 13:07:35 +0000516 /**
517 * Creates initially empty storage. get() returns a ptr, but it is to
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000518 * a zero-byte allocation. Must call reset(size) to return an allocated
reed@google.com63a60602011-03-10 13:07:35 +0000519 * block.
520 */
521 SkAutoSMalloc() {
522 fPtr = fStorage;
reed@google.com1c401d82011-10-18 18:52:03 +0000523 fSize = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524 }
reed@google.com63a60602011-03-10 13:07:35 +0000525
526 /**
527 * Allocate a block of the specified size. If size <= kSize, then the
528 * allocation will come from the stack, otherwise it will be dynamically
529 * allocated.
530 */
531 explicit SkAutoSMalloc(size_t size) {
532 fPtr = fStorage;
reed@google.com7a17e3a2011-10-18 18:58:06 +0000533 fSize = 0;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000534 this->reset(size);
reed@google.com63a60602011-03-10 13:07:35 +0000535 }
536
537 /**
538 * Free the allocated block (if any). If the block was small enought to
539 * have been allocated on the stack (size <= kSize) then this does nothing.
540 */
541 ~SkAutoSMalloc() {
542 if (fPtr != (void*)fStorage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000543 sk_free(fPtr);
reed@google.com63a60602011-03-10 13:07:35 +0000544 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000545 }
reed@google.com63a60602011-03-10 13:07:35 +0000546
547 /**
548 * Return the allocated block. May return non-null even if the block is
549 * of zero size. Since this may be on the stack or dynamically allocated,
550 * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
551 * to manage it.
552 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000553 void* get() const { return fPtr; }
reed@google.com63a60602011-03-10 13:07:35 +0000554
555 /**
556 * Return a new block of the requested size, freeing (as necessary) any
557 * previously allocated block. As with the constructor, if size <= kSize
558 * then the return block may be allocated locally, rather than from the
559 * heap.
560 */
reed@google.com1c401d82011-10-18 18:52:03 +0000561 void* reset(size_t size,
562 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink) {
563 if (size == fSize || (SkAutoMalloc::kReuse_OnShrink == shrink &&
564 size < fSize)) {
565 return fPtr;
566 }
567
reed@google.com63a60602011-03-10 13:07:35 +0000568 if (fPtr != (void*)fStorage) {
569 sk_free(fPtr);
570 }
571
572 if (size <= kSize) {
573 fPtr = fStorage;
574 } else {
575 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
576 }
577 return fPtr;
578 }
579
reed@android.com8a1c16f2008-12-17 15:59:43 +0000580private:
581 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000582 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583 uint32_t fStorage[(kSize + 3) >> 2];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000584};
585
586#endif /* C++ */
587
588#endif