Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkTypes_DEFINED
     18 #define SkTypes_DEFINED
     19 
     20 #include "SkPreConfig.h"
     21 #include "SkUserConfig.h"
     22 #include "SkPostConfig.h"
     23 
     24 #ifndef SK_IGNORE_STDINT_DOT_H
     25     #include <stdint.h>
     26 #endif
     27 
     28 #include <stdio.h>
     29 
     30 /** \file SkTypes.h
     31 */
     32 
     33 /** See SkGraphics::GetVersion() to retrieve these at runtime
     34  */
     35 #define SKIA_VERSION_MAJOR  1
     36 #define SKIA_VERSION_MINOR  0
     37 #define SKIA_VERSION_PATCH  0
     38 
     39 /*
     40     memory wrappers to be implemented by the porting layer (platform)
     41 */
     42 
     43 /** Called internally if we run out of memory. The platform implementation must
     44     not return, but should either throw an exception or otherwise exit.
     45 */
     46 extern void  sk_out_of_memory(void);
     47 /** Called internally if we hit an unrecoverable error.
     48     The platform implementation must not return, but should either throw
     49     an exception or otherwise exit.
     50 */
     51 extern void  sk_throw(void);
     52 
     53 enum {
     54     SK_MALLOC_TEMP  = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
     55     SK_MALLOC_THROW = 0x02  //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
     56 };
     57 /** Return a block of memory (at least 4-byte aligned) of at least the
     58     specified size. If the requested memory cannot be returned, either
     59     return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw()
     60     (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
     61 */
     62 SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
     63 /** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
     64 */
     65 extern void* sk_malloc_throw(size_t size);
     66 /** Same as standard realloc(), but this one never returns null on failure. It will throw
     67     an exception if it fails.
     68 */
     69 extern void* sk_realloc_throw(void* buffer, size_t size);
     70 /** Free memory returned by sk_malloc(). It is safe to pass null.
     71 */
     72 SK_API extern void  sk_free(void*);
     73 
     74 // bzero is safer than memset, but we can't rely on it, so... sk_bzero()
     75 static inline void sk_bzero(void* buffer, size_t size) {
     76     memset(buffer, 0, size);
     77 }
     78 
     79 ///////////////////////////////////////////////////////////////////////
     80 
     81 #define SK_INIT_TO_AVOID_WARNING    = 0
     82 
     83 #ifndef SkDebugf
     84     void SkDebugf(const char format[], ...);
     85 #endif
     86 
     87 #ifdef SK_DEBUG
     88     #define SkASSERT(cond)              SK_DEBUGBREAK(cond)
     89     #define SkDEBUGCODE(code)           code
     90     #define SkDECLAREPARAM(type, var)   , type var
     91     #define SkPARAM(var)                , var
     92 //  #define SkDEBUGF(args       )       SkDebugf##args
     93     #define SkDEBUGF(args       )       SkDebugf args
     94     #define SkAssertResult(cond)        SkASSERT(cond)
     95 #else
     96     #define SkASSERT(cond)
     97     #define SkDEBUGCODE(code)
     98     #define SkDEBUGF(args)
     99     #define SkDECLAREPARAM(type, var)
    100     #define SkPARAM(var)
    101 
    102     // unlike SkASSERT, this guy executes its condition in the non-debug build
    103     #define SkAssertResult(cond)        cond
    104 #endif
    105 
    106 namespace {
    107 
    108 template <bool>
    109 struct SkCompileAssert {
    110 };
    111 
    112 }  // namespace
    113 
    114 #define SK_COMPILE_ASSERT(expr, msg) \
    115     typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
    116 
    117 ///////////////////////////////////////////////////////////////////////
    118 
    119 /**
    120  *  Fast type for signed 8 bits. Use for parameter passing and local variables,
    121  *  not for storage.
    122  */
    123 typedef int S8CPU;
    124 
    125 /**
    126  *  Fast type for unsigned 8 bits. Use for parameter passing and local
    127  *  variables, not for storage
    128  */
    129 typedef unsigned U8CPU;
    130 
    131 /**
    132  *  Fast type for signed 16 bits. Use for parameter passing and local variables,
    133  *  not for storage
    134  */
    135 typedef int S16CPU;
    136 
    137 /**
    138  *  Fast type for unsigned 16 bits. Use for parameter passing and local
    139  *  variables, not for storage
    140  */
    141 typedef unsigned U16CPU;
    142 
    143 /**
    144  *  Meant to be faster than bool (doesn't promise to be 0 or 1,
    145  *  just 0 or non-zero
    146  */
    147 typedef int SkBool;
    148 
    149 /**
    150  *  Meant to be a small version of bool, for storage purposes. Will be 0 or 1
    151  */
    152 typedef uint8_t SkBool8;
    153 
    154 #ifdef SK_DEBUG
    155     SK_API int8_t      SkToS8(long);
    156     SK_API uint8_t     SkToU8(size_t);
    157     SK_API int16_t     SkToS16(long);
    158     SK_API uint16_t    SkToU16(size_t);
    159     SK_API int32_t     SkToS32(long);
    160     SK_API uint32_t    SkToU32(size_t);
    161 #else
    162     #define SkToS8(x)   ((int8_t)(x))
    163     #define SkToU8(x)   ((uint8_t)(x))
    164     #define SkToS16(x)  ((int16_t)(x))
    165     #define SkToU16(x)  ((uint16_t)(x))
    166     #define SkToS32(x)  ((int32_t)(x))
    167     #define SkToU32(x)  ((uint32_t)(x))
    168 #endif
    169 
    170 /** Returns 0 or 1 based on the condition
    171 */
    172 #define SkToBool(cond)  ((cond) != 0)
    173 
    174 #define SK_MaxS16   32767
    175 #define SK_MinS16   -32767
    176 #define SK_MaxU16   0xFFFF
    177 #define SK_MinU16   0
    178 #define SK_MaxS32   0x7FFFFFFF
    179 #define SK_MinS32   0x80000001
    180 #define SK_MaxU32   0xFFFFFFFF
    181 #define SK_MinU32   0
    182 #define SK_NaN32    0x80000000
    183 
    184 /** Returns true if the value can be represented with signed 16bits
    185  */
    186 static inline bool SkIsS16(long x) {
    187     return (int16_t)x == x;
    188 }
    189 
    190 /** Returns true if the value can be represented with unsigned 16bits
    191  */
    192 static inline bool SkIsU16(long x) {
    193     return (uint16_t)x == x;
    194 }
    195 
    196 //////////////////////////////////////////////////////////////////////////////
    197 #ifndef SK_OFFSETOF
    198     #define SK_OFFSETOF(type, field)    ((char*)&(((type*)1)->field) - (char*)1)
    199 #endif
    200 
    201 /** Returns the number of entries in an array (not a pointer)
    202 */
    203 #define SK_ARRAY_COUNT(array)       (sizeof(array) / sizeof(array[0]))
    204 
    205 /** Returns x rounded up to a multiple of 2
    206 */
    207 #define SkAlign2(x)     (((x) + 1) >> 1 << 1)
    208 /** Returns x rounded up to a multiple of 4
    209 */
    210 #define SkAlign4(x)     (((x) + 3) >> 2 << 2)
    211 
    212 typedef uint32_t SkFourByteTag;
    213 #define SkSetFourByteTag(a, b, c, d)    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
    214 
    215 /** 32 bit integer to hold a unicode value
    216 */
    217 typedef int32_t SkUnichar;
    218 /** 32 bit value to hold a millisecond count
    219 */
    220 typedef uint32_t SkMSec;
    221 /** 1 second measured in milliseconds
    222 */
    223 #define SK_MSec1 1000
    224 /** maximum representable milliseconds
    225 */
    226 #define SK_MSecMax 0x7FFFFFFF
    227 /** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
    228 */
    229 #define SkMSec_LT(a, b)     ((int32_t)(a) - (int32_t)(b) < 0)
    230 /** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
    231 */
    232 #define SkMSec_LE(a, b)     ((int32_t)(a) - (int32_t)(b) <= 0)
    233 
    234 /****************************************************************************
    235     The rest of these only build with C++
    236 */
    237 #ifdef __cplusplus
    238 
    239 /** Faster than SkToBool for integral conditions. Returns 0 or 1
    240 */
    241 static inline int Sk32ToBool(uint32_t n) {
    242     return (n | (0-n)) >> 31;
    243 }
    244 
    245 template <typename T> inline void SkTSwap(T& a, T& b) {
    246     T c(a);
    247     a = b;
    248     b = c;
    249 }
    250 
    251 static inline int32_t SkAbs32(int32_t value) {
    252 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
    253     if (value < 0)
    254         value = -value;
    255     return value;
    256 #else
    257     int32_t mask = value >> 31;
    258     return (value ^ mask) - mask;
    259 #endif
    260 }
    261 
    262 static inline int32_t SkMax32(int32_t a, int32_t b) {
    263     if (a < b)
    264         a = b;
    265     return a;
    266 }
    267 
    268 static inline int32_t SkMin32(int32_t a, int32_t b) {
    269     if (a > b)
    270         a = b;
    271     return a;
    272 }
    273 
    274 static inline int32_t SkSign32(int32_t a) {
    275     return (a >> 31) | ((unsigned) -a >> 31);
    276 }
    277 
    278 static inline int32_t SkFastMin32(int32_t value, int32_t max) {
    279 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
    280     if (value > max)
    281         value = max;
    282     return value;
    283 #else
    284     int diff = max - value;
    285     // clear diff if it is negative (clear if value > max)
    286     diff &= (diff >> 31);
    287     return value + diff;
    288 #endif
    289 }
    290 
    291 /** Returns signed 32 bit value pinned between min and max, inclusively
    292 */
    293 static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
    294 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
    295     if (value < min)
    296         value = min;
    297     if (value > max)
    298         value = max;
    299 #else
    300     if (value < min)
    301         value = min;
    302     else if (value > max)
    303         value = max;
    304 #endif
    305     return value;
    306 }
    307 
    308 static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
    309                                        unsigned shift) {
    310     SkASSERT((int)cond == 0 || (int)cond == 1);
    311     return (bits & ~(1 << shift)) | ((int)cond << shift);
    312 }
    313 
    314 static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
    315                                       uint32_t mask) {
    316     return cond ? bits | mask : bits & ~mask;
    317 }
    318 
    319 ///////////////////////////////////////////////////////////////////////////////
    320 
    321 /** Use to combine multiple bits in a bitmask in a type safe way.
    322  */
    323 template <typename T>
    324 T SkTBitOr(T a, T b) {
    325     return (T)(a | b);
    326 }
    327 
    328 /**
    329  *  Use to cast a pointer to a different type, and maintaining strict-aliasing
    330  */
    331 template <typename Dst> Dst SkTCast(const void* ptr) {
    332     union {
    333         const void* src;
    334         Dst dst;
    335     } data;
    336     data.src = ptr;
    337     return data.dst;
    338 }
    339 
    340 //////////////////////////////////////////////////////////////////////////////
    341 
    342 /** \class SkNoncopyable
    343 
    344 SkNoncopyable is the base class for objects that may do not want to
    345 be copied. It hides its copy-constructor and its assignment-operator.
    346 */
    347 class SK_API SkNoncopyable {
    348 public:
    349     SkNoncopyable() {}
    350 
    351 private:
    352     SkNoncopyable(const SkNoncopyable&);
    353     SkNoncopyable& operator=(const SkNoncopyable&);
    354 };
    355 
    356 class SkAutoFree : SkNoncopyable {
    357 public:
    358     SkAutoFree() : fPtr(NULL) {}
    359     explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
    360     ~SkAutoFree() { sk_free(fPtr); }
    361 
    362     /** Return the currently allocate buffer, or null
    363     */
    364     void* get() const { return fPtr; }
    365 
    366     /** Assign a new ptr allocated with sk_malloc (or null), and return the
    367         previous ptr. Note it is the caller's responsibility to sk_free the
    368         returned ptr.
    369     */
    370     void* set(void* ptr) {
    371         void* prev = fPtr;
    372         fPtr = ptr;
    373         return prev;
    374     }
    375 
    376     /** Transfer ownership of the current ptr to the caller, setting the
    377         internal reference to null. Note the caller is reponsible for calling
    378         sk_free on the returned address.
    379     */
    380     void* detach() { return this->set(NULL); }
    381 
    382     /** Free the current buffer, and set the internal reference to NULL. Same
    383         as calling sk_free(detach())
    384     */
    385     void free() {
    386         sk_free(fPtr);
    387         fPtr = NULL;
    388     }
    389 
    390 private:
    391     void* fPtr;
    392     // illegal
    393     SkAutoFree(const SkAutoFree&);
    394     SkAutoFree& operator=(const SkAutoFree&);
    395 };
    396 
    397 class SkAutoMalloc : public SkAutoFree {
    398 public:
    399     explicit SkAutoMalloc(size_t size)
    400         : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {}
    401 
    402     SkAutoMalloc(size_t size, unsigned flags)
    403         : SkAutoFree(sk_malloc_flags(size, flags)) {}
    404     SkAutoMalloc() {}
    405 
    406     void* alloc(size_t size,
    407                 unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) {
    408         sk_free(set(sk_malloc_flags(size, flags)));
    409         return get();
    410     }
    411 };
    412 
    413 /**
    414  *  Manage an allocated block of memory. If the requested size is <= kSize, then
    415  *  the allocation will come from the stack rather than the heap. This object
    416  *  is the sole manager of the lifetime of the block, so the caller must not
    417  *  call sk_free() or delete on the block.
    418  */
    419 template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
    420 public:
    421     /**
    422      *  Creates initially empty storage. get() returns a ptr, but it is to
    423      *  a zero-byte allocation. Must call realloc(size) to return an allocated
    424      *  block.
    425      */
    426     SkAutoSMalloc() {
    427         fPtr = fStorage;
    428     }
    429 
    430     /**
    431      *  Allocate a block of the specified size. If size <= kSize, then the
    432      *  allocation will come from the stack, otherwise it will be dynamically
    433      *  allocated.
    434      */
    435     explicit SkAutoSMalloc(size_t size) {
    436         fPtr = fStorage;
    437         this->realloc(size);
    438     }
    439 
    440     /**
    441      *  Free the allocated block (if any). If the block was small enought to
    442      *  have been allocated on the stack (size <= kSize) then this does nothing.
    443      */
    444     ~SkAutoSMalloc() {
    445         if (fPtr != (void*)fStorage) {
    446             sk_free(fPtr);
    447         }
    448     }
    449 
    450     /**
    451      *  Return the allocated block. May return non-null even if the block is
    452      *  of zero size. Since this may be on the stack or dynamically allocated,
    453      *  the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
    454      *  to manage it.
    455      */
    456     void* get() const { return fPtr; }
    457 
    458     /**
    459      *  Return a new block of the requested size, freeing (as necessary) any
    460      *  previously allocated block. As with the constructor, if size <= kSize
    461      *  then the return block may be allocated locally, rather than from the
    462      *  heap.
    463      */
    464     void* realloc(size_t size) {
    465         if (fPtr != (void*)fStorage) {
    466             sk_free(fPtr);
    467         }
    468 
    469         if (size <= kSize) {
    470             fPtr = fStorage;
    471         } else {
    472             fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
    473         }
    474         return fPtr;
    475     }
    476 
    477 private:
    478     void*       fPtr;
    479     uint32_t    fStorage[(kSize + 3) >> 2];
    480     // illegal
    481     SkAutoSMalloc(const SkAutoSMalloc&);
    482     SkAutoSMalloc& operator=(const SkAutoSMalloc&);
    483 };
    484 
    485 #endif /* C++ */
    486 
    487 #endif
    488 
    489