00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef SkTypes_DEFINED
00018 #define SkTypes_DEFINED
00019
00020 #include "SkPreConfig.h"
00021 #include "SkUserConfig.h"
00022 #include "SkPostConfig.h"
00023
00024 #ifndef SK_IGNORE_STDINT_DOT_H
00025 #include <stdint.h>
00026 #endif
00027
00028 #include <stdio.h>
00029
00033
00034
00035
00036
00040 extern void sk_out_of_memory(void);
00045 extern void sk_throw(void);
00046
00047 enum {
00048 SK_MALLOC_TEMP = 0x01,
00049 SK_MALLOC_THROW = 0x02
00050 };
00056 extern void* sk_malloc_flags(size_t size, unsigned flags);
00059 extern void* sk_malloc_throw(size_t size);
00063 extern void* sk_realloc_throw(void* buffer, size_t size);
00066 extern void sk_free(void*);
00067
00068
00069 static inline void sk_bzero(void* buffer, size_t size) {
00070 memset(buffer, 0, size);
00071 }
00072
00074
00075 #define SK_INIT_TO_AVOID_WARNING = 0
00076
00077 #ifndef SkDebugf
00078 void SkDebugf(const char format[], ...);
00079 #endif
00080
00081 #ifdef SK_DEBUG
00082 #define SkASSERT(cond) SK_DEBUGBREAK(cond)
00083 #define SkDEBUGCODE(code) code
00084 #define SkDECLAREPARAM(type, var) , type var
00085 #define SkPARAM(var) , var
00086
00087 #define SkDEBUGF(args ) SkDebugf args
00088 #define SkAssertResult(cond) SkASSERT(cond)
00089 #else
00090 #define SkASSERT(cond)
00091 #define SkDEBUGCODE(code)
00092 #define SkDEBUGF(args)
00093 #define SkDECLAREPARAM(type, var)
00094 #define SkPARAM(var)
00095
00096
00097 #define SkAssertResult(cond) cond
00098 #endif
00099
00101
00104 typedef int S8CPU;
00107 typedef int S16CPU;
00110 typedef unsigned U8CPU;
00113 typedef unsigned U16CPU;
00114
00117 typedef int SkBool;
00120 typedef uint8_t SkBool8;
00121
00122 #ifdef SK_DEBUG
00123 int8_t SkToS8(long);
00124 uint8_t SkToU8(size_t);
00125 int16_t SkToS16(long);
00126 uint16_t SkToU16(size_t);
00127 int32_t SkToS32(long);
00128 uint32_t SkToU32(size_t);
00129 #else
00130 #define SkToS8(x) ((int8_t)(x))
00131 #define SkToU8(x) ((uint8_t)(x))
00132 #define SkToS16(x) ((int16_t)(x))
00133 #define SkToU16(x) ((uint16_t)(x))
00134 #define SkToS32(x) ((int32_t)(x))
00135 #define SkToU32(x) ((uint32_t)(x))
00136 #endif
00137
00140 #define SkToBool(cond) ((cond) != 0)
00141
00142 #define SK_MaxS16 32767
00143 #define SK_MinS16 -32767
00144 #define SK_MaxU16 0xFFFF
00145 #define SK_MinU16 0
00146 #define SK_MaxS32 0x7FFFFFFF
00147 #define SK_MinS32 0x80000001
00148 #define SK_MaxU32 0xFFFFFFFF
00149 #define SK_MinU32 0
00150 #define SK_NaN32 0x80000000
00151
00152 #ifndef SK_OFFSETOF
00153 #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1)
00154 #endif
00155
00158 #define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
00159
00162 #define SkAlign2(x) (((x) + 1) >> 1 << 1)
00163
00165 #define SkAlign4(x) (((x) + 3) >> 2 << 2)
00166
00167 typedef uint32_t SkFourByteTag;
00168 #define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
00169
00172 typedef int32_t SkUnichar;
00175 typedef uint32_t SkMSec;
00178 #define SK_MSec1 1000
00179
00181 #define SK_MSecMax 0x7FFFFFFF
00182
00184 #define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
00185
00187 #define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
00188
00189
00190
00191
00192
00193 #ifdef __cplusplus
00194
00197 inline int Sk32ToBool(uint32_t n)
00198 {
00199 return (n | (0-n)) >> 31;
00200 }
00201
00202 template <typename T> inline void SkTSwap(T& a, T& b)
00203 {
00204 T c(a);
00205 a = b;
00206 b = c;
00207 }
00208
00209 inline int32_t SkAbs32(int32_t value)
00210 {
00211 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
00212 if (value < 0)
00213 value = -value;
00214 return value;
00215 #else
00216 int32_t mask = value >> 31;
00217 return (value ^ mask) - mask;
00218 #endif
00219 }
00220
00221 inline int32_t SkMax32(int32_t a, int32_t b)
00222 {
00223 if (a < b)
00224 a = b;
00225 return a;
00226 }
00227
00228 inline int32_t SkMin32(int32_t a, int32_t b)
00229 {
00230 if (a > b)
00231 a = b;
00232 return a;
00233 }
00234
00235 inline int32_t SkSign32(int32_t a)
00236 {
00237 return (a >> 31) | ((unsigned) -a >> 31);
00238 }
00239
00240 inline int32_t SkFastMin32(int32_t value, int32_t max)
00241 {
00242 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
00243 if (value > max)
00244 value = max;
00245 return value;
00246 #else
00247 int diff = max - value;
00248
00249 diff &= (diff >> 31);
00250 return value + diff;
00251 #endif
00252 }
00253
00256 inline int32_t SkPin32(int32_t value, int32_t min, int32_t max)
00257 {
00258 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
00259 if (value < min)
00260 value = min;
00261 if (value > max)
00262 value = max;
00263 #else
00264 if (value < min)
00265 value = min;
00266 else if (value > max)
00267 value = max;
00268 #endif
00269 return value;
00270 }
00271
00272 inline uint32_t SkSetClearShift(uint32_t bits, bool cond, unsigned shift)
00273 {
00274 SkASSERT((int)cond == 0 || (int)cond == 1);
00275 return (bits & ~(1 << shift)) | ((int)cond << shift);
00276 }
00277
00278 inline uint32_t SkSetClearMask(uint32_t bits, bool cond, uint32_t mask)
00279 {
00280 return cond ? bits | mask : bits & ~mask;
00281 }
00282
00284
00290 class SkNoncopyable {
00291 public:
00292 SkNoncopyable() {}
00293
00294 private:
00295 SkNoncopyable(const SkNoncopyable&);
00296 SkNoncopyable& operator=(const SkNoncopyable&);
00297 };
00298
00299 class SkAutoFree : SkNoncopyable {
00300 public:
00301 SkAutoFree() : fPtr(NULL) {}
00302 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
00303 ~SkAutoFree() { sk_free(fPtr); }
00304
00307 void* get() const { return fPtr; }
00308
00313 void* set(void* ptr) {
00314 void* prev = fPtr;
00315 fPtr = ptr;
00316 return prev;
00317 }
00318
00323 void* detach() { return this->set(NULL); }
00324
00328 void free() {
00329 sk_free(fPtr);
00330 fPtr = NULL;
00331 }
00332
00333 private:
00334 void* fPtr;
00335
00336 SkAutoFree(const SkAutoFree&);
00337 SkAutoFree& operator=(const SkAutoFree&);
00338 };
00339
00340 class SkAutoMalloc : public SkAutoFree {
00341 public:
00342 explicit SkAutoMalloc(size_t size)
00343 : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {}
00344
00345 SkAutoMalloc(size_t size, unsigned flags)
00346 : SkAutoFree(sk_malloc_flags(size, flags)) {}
00347 SkAutoMalloc() {}
00348
00349 void* alloc(size_t size,
00350 unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) {
00351 sk_free(set(sk_malloc_flags(size, flags)));
00352 return get();
00353 }
00354 };
00355
00356 template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
00357 public:
00358 explicit SkAutoSMalloc(size_t size)
00359 {
00360 if (size <= kSize)
00361 fPtr = fStorage;
00362 else
00363 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
00364 }
00365 ~SkAutoSMalloc()
00366 {
00367 if (fPtr != (void*)fStorage)
00368 sk_free(fPtr);
00369 }
00370 void* get() const { return fPtr; }
00371 private:
00372 void* fPtr;
00373 uint32_t fStorage[(kSize + 3) >> 2];
00374
00375 SkAutoSMalloc(const SkAutoSMalloc&);
00376 SkAutoSMalloc& operator=(const SkAutoSMalloc&);
00377 };
00378
00379 #endif
00380
00381 #endif
00382