SkTypes.h revision bdf736133b513bb13f7c66e01c8c37ac526ce8d4
1 2/* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10#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 26/** 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 32/* 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*/ 39extern void sk_out_of_memory(void); 40/** 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*/ 44extern void sk_throw(void); 45 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*/ 55SK_API extern void* sk_malloc_flags(size_t size, unsigned flags); 56/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag 57*/ 58extern void* sk_malloc_throw(size_t size); 59/** Same as standard realloc(), but this one never returns null on failure. It will throw 60 an exception if it fails. 61*/ 62extern void* sk_realloc_throw(void* buffer, size_t size); 63/** Free memory returned by sk_malloc(). It is safe to pass null. 64*/ 65SK_API extern void sk_free(void*); 66 67// 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 72/////////////////////////////////////////////////////////////////////////////// 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/////////////////////////////////////////////////////////////////////////////// 87 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) 96 #define SkDEBUGCODE(code) code 97 #define SkDECLAREPARAM(type, var) , type var 98 #define SkPARAM(var) , var 99// #define SkDEBUGF(args ) SkDebugf##args 100 #define SkDEBUGF(args ) SkDebugf args 101 #define SkAssertResult(cond) SkASSERT(cond) 102#else 103 #define SkASSERT(cond) 104 #define SkDEBUGCODE(code) 105 #define SkDEBUGF(args) 106 #define SkDECLAREPARAM(type, var) 107 #define SkPARAM(var) 108 109 // unlike SkASSERT, this guy executes its condition in the non-debug build 110 #define SkAssertResult(cond) cond 111#endif 112 113namespace { 114 115template <bool> 116struct SkCompileAssert { 117}; 118 119} // namespace 120 121#define SK_COMPILE_ASSERT(expr, msg) \ 122 typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] 123 124/////////////////////////////////////////////////////////////////////// 125 126/** 127 * Fast type for signed 8 bits. Use for parameter passing and local variables, 128 * not for storage. 129 */ 130typedef int S8CPU; 131 132/** 133 * Fast type for unsigned 8 bits. Use for parameter passing and local 134 * variables, not for storage 135 */ 136typedef unsigned U8CPU; 137 138/** 139 * Fast type for signed 16 bits. Use for parameter passing and local variables, 140 * not for storage 141 */ 142typedef int S16CPU; 143 144/** 145 * Fast type for unsigned 16 bits. Use for parameter passing and local 146 * variables, not for storage 147 */ 148typedef unsigned U16CPU; 149 150/** 151 * Meant to be faster than bool (doesn't promise to be 0 or 1, 152 * just 0 or non-zero 153 */ 154typedef int SkBool; 155 156/** 157 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1 158 */ 159typedef uint8_t SkBool8; 160 161#ifdef SK_DEBUG 162 SK_API int8_t SkToS8(long); 163 SK_API uint8_t SkToU8(size_t); 164 SK_API int16_t SkToS16(long); 165 SK_API uint16_t SkToU16(size_t); 166 SK_API int32_t SkToS32(long); 167 SK_API uint32_t SkToU32(size_t); 168#else 169 #define SkToS8(x) ((int8_t)(x)) 170 #define SkToU8(x) ((uint8_t)(x)) 171 #define SkToS16(x) ((int16_t)(x)) 172 #define SkToU16(x) ((uint16_t)(x)) 173 #define SkToS32(x) ((int32_t)(x)) 174 #define SkToU32(x) ((uint32_t)(x)) 175#endif 176 177/** Returns 0 or 1 based on the condition 178*/ 179#define SkToBool(cond) ((cond) != 0) 180 181#define SK_MaxS16 32767 182#define SK_MinS16 -32767 183#define SK_MaxU16 0xFFFF 184#define SK_MinU16 0 185#define SK_MaxS32 0x7FFFFFFF 186#define SK_MinS32 0x80000001 187#define SK_MaxU32 0xFFFFFFFF 188#define SK_MinU32 0 189#define SK_NaN32 0x80000000 190 191/** Returns true if the value can be represented with signed 16bits 192 */ 193static inline bool SkIsS16(long x) { 194 return (int16_t)x == x; 195} 196 197/** Returns true if the value can be represented with unsigned 16bits 198 */ 199static inline bool SkIsU16(long x) { 200 return (uint16_t)x == x; 201} 202 203////////////////////////////////////////////////////////////////////////////// 204#ifndef SK_OFFSETOF 205 #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1) 206#endif 207 208/** Returns the number of entries in an array (not a pointer) 209*/ 210#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) 211 212/** Returns x rounded up to a multiple of 2 213*/ 214#define SkAlign2(x) (((x) + 1) >> 1 << 1) 215/** Returns x rounded up to a multiple of 4 216*/ 217#define SkAlign4(x) (((x) + 3) >> 2 << 2) 218 219typedef uint32_t SkFourByteTag; 220#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) 221 222/** 32 bit integer to hold a unicode value 223*/ 224typedef int32_t SkUnichar; 225/** 32 bit value to hold a millisecond count 226*/ 227typedef uint32_t SkMSec; 228/** 1 second measured in milliseconds 229*/ 230#define SK_MSec1 1000 231/** maximum representable milliseconds 232*/ 233#define SK_MSecMax 0x7FFFFFFF 234/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 235*/ 236#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) 237/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 238*/ 239#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) 240 241/**************************************************************************** 242 The rest of these only build with C++ 243*/ 244#ifdef __cplusplus 245 246/** Faster than SkToBool for integral conditions. Returns 0 or 1 247*/ 248static inline int Sk32ToBool(uint32_t n) { 249 return (n | (0-n)) >> 31; 250} 251 252template <typename T> inline void SkTSwap(T& a, T& b) { 253 T c(a); 254 a = b; 255 b = c; 256} 257 258static inline int32_t SkAbs32(int32_t value) { 259#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 260 if (value < 0) 261 value = -value; 262 return value; 263#else 264 int32_t mask = value >> 31; 265 return (value ^ mask) - mask; 266#endif 267} 268 269static inline int32_t SkMax32(int32_t a, int32_t b) { 270 if (a < b) 271 a = b; 272 return a; 273} 274 275static inline int32_t SkMin32(int32_t a, int32_t b) { 276 if (a > b) 277 a = b; 278 return a; 279} 280 281static inline int32_t SkSign32(int32_t a) { 282 return (a >> 31) | ((unsigned) -a >> 31); 283} 284 285static inline int32_t SkFastMin32(int32_t value, int32_t max) { 286#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 287 if (value > max) 288 value = max; 289 return value; 290#else 291 int diff = max - value; 292 // clear diff if it is negative (clear if value > max) 293 diff &= (diff >> 31); 294 return value + diff; 295#endif 296} 297 298/** Returns signed 32 bit value pinned between min and max, inclusively 299*/ 300static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { 301#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 302 if (value < min) 303 value = min; 304 if (value > max) 305 value = max; 306#else 307 if (value < min) 308 value = min; 309 else if (value > max) 310 value = max; 311#endif 312 return value; 313} 314 315static inline uint32_t SkSetClearShift(uint32_t bits, bool cond, 316 unsigned shift) { 317 SkASSERT((int)cond == 0 || (int)cond == 1); 318 return (bits & ~(1 << shift)) | ((int)cond << shift); 319} 320 321static inline uint32_t SkSetClearMask(uint32_t bits, bool cond, 322 uint32_t mask) { 323 return cond ? bits | mask : bits & ~mask; 324} 325 326/////////////////////////////////////////////////////////////////////////////// 327 328/** Use to combine multiple bits in a bitmask in a type safe way. 329 */ 330template <typename T> 331T SkTBitOr(T a, T b) { 332 return (T)(a | b); 333} 334 335/** 336 * Use to cast a pointer to a different type, and maintaining strict-aliasing 337 */ 338template <typename Dst> Dst SkTCast(const void* ptr) { 339 union { 340 const void* src; 341 Dst dst; 342 } data; 343 data.src = ptr; 344 return data.dst; 345} 346 347////////////////////////////////////////////////////////////////////////////// 348 349/** \class SkNoncopyable 350 351SkNoncopyable is the base class for objects that may do not want to 352be copied. It hides its copy-constructor and its assignment-operator. 353*/ 354class SK_API SkNoncopyable { 355public: 356 SkNoncopyable() {} 357 358private: 359 SkNoncopyable(const SkNoncopyable&); 360 SkNoncopyable& operator=(const SkNoncopyable&); 361}; 362 363class SkAutoFree : SkNoncopyable { 364public: 365 SkAutoFree() : fPtr(NULL) {} 366 explicit SkAutoFree(void* ptr) : fPtr(ptr) {} 367 ~SkAutoFree() { sk_free(fPtr); } 368 369 /** Return the currently allocate buffer, or null 370 */ 371 void* get() const { return fPtr; } 372 373 /** Assign a new ptr allocated with sk_malloc (or null), and return the 374 previous ptr. Note it is the caller's responsibility to sk_free the 375 returned ptr. 376 */ 377 void* set(void* ptr) { 378 void* prev = fPtr; 379 fPtr = ptr; 380 return prev; 381 } 382 383 /** Transfer ownership of the current ptr to the caller, setting the 384 internal reference to null. Note the caller is reponsible for calling 385 sk_free on the returned address. 386 */ 387 void* detach() { return this->set(NULL); } 388 389 /** Free the current buffer, and set the internal reference to NULL. Same 390 as calling sk_free(detach()) 391 */ 392 void free() { 393 sk_free(fPtr); 394 fPtr = NULL; 395 } 396 397private: 398 void* fPtr; 399 // illegal 400 SkAutoFree(const SkAutoFree&); 401 SkAutoFree& operator=(const SkAutoFree&); 402}; 403 404/** 405 * Manage an allocated block of heap memory. This object is the sole manager of 406 * the lifetime of the block, so the caller must not call sk_free() or delete 407 * on the block. 408 */ 409class SkAutoMalloc : public SkNoncopyable { 410public: 411 explicit SkAutoMalloc(size_t size = 0, 412 unsigned flags = SK_MALLOC_THROW | SK_MALLOC_TEMP) { 413 fPtr = size ? sk_malloc_flags(size, flags) : NULL; 414 fSize = size; 415 } 416 417 ~SkAutoMalloc() { 418 sk_free(fPtr); 419 } 420 421 /** 422 * Reallocates the block to a new size. The ptr may or may not change. 423 */ 424 void* reset(size_t size, 425 unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) { 426 if (size != fSize) { 427 sk_free(fPtr); 428 fPtr = size ? sk_malloc_flags(size, flags) : NULL; 429 fSize = size; 430 } 431 return fPtr; 432 } 433 434 /** 435 * Releases the block back to the heap 436 */ 437 void free() { 438 this->reset(0); 439 } 440 441 /** 442 * Return the allocated block. 443 */ 444 void* get() { return fPtr; } 445 const void* get() const { return fPtr; } 446 447 /** 448 * Gets the size of the block in bytes 449 */ 450 size_t getSize() const { return fSize; } 451 452private: 453 void* fPtr; 454 size_t fSize; 455}; 456 457/** 458 * Manage an allocated block of memory. If the requested size is <= kSize, then 459 * the allocation will come from the stack rather than the heap. This object 460 * is the sole manager of the lifetime of the block, so the caller must not 461 * call sk_free() or delete on the block. 462 */ 463template <size_t kSize> class SkAutoSMalloc : SkNoncopyable { 464public: 465 /** 466 * Creates initially empty storage. get() returns a ptr, but it is to 467 * a zero-byte allocation. Must call reset(size) to return an allocated 468 * block. 469 */ 470 SkAutoSMalloc() { 471 fPtr = fStorage; 472 } 473 474 /** 475 * Allocate a block of the specified size. If size <= kSize, then the 476 * allocation will come from the stack, otherwise it will be dynamically 477 * allocated. 478 */ 479 explicit SkAutoSMalloc(size_t size) { 480 fPtr = fStorage; 481 this->reset(size); 482 } 483 484 /** 485 * Free the allocated block (if any). If the block was small enought to 486 * have been allocated on the stack (size <= kSize) then this does nothing. 487 */ 488 ~SkAutoSMalloc() { 489 if (fPtr != (void*)fStorage) { 490 sk_free(fPtr); 491 } 492 } 493 494 /** 495 * Return the allocated block. May return non-null even if the block is 496 * of zero size. Since this may be on the stack or dynamically allocated, 497 * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc 498 * to manage it. 499 */ 500 void* get() const { return fPtr; } 501 502 /** 503 * Return a new block of the requested size, freeing (as necessary) any 504 * previously allocated block. As with the constructor, if size <= kSize 505 * then the return block may be allocated locally, rather than from the 506 * heap. 507 */ 508 void* reset(size_t size) { 509 if (fPtr != (void*)fStorage) { 510 sk_free(fPtr); 511 } 512 513 if (size <= kSize) { 514 fPtr = fStorage; 515 } else { 516 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); 517 } 518 return fPtr; 519 } 520 521private: 522 void* fPtr; 523 uint32_t fStorage[(kSize + 3) >> 2]; 524 // illegal 525 SkAutoSMalloc(const SkAutoSMalloc&); 526 SkAutoSMalloc& operator=(const SkAutoSMalloc&); 527}; 528 529#endif /* C++ */ 530 531#endif 532