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 /*
34 memory wrappers to be implemented by the porting layer (platform)
35 */
36
37 /** Called internally if we run out of memory. The platform implementation must
38 not return, but should either throw an exception or otherwise exit.
39 */
40 extern void sk_out_of_memory(void);
41 /** Called internally if we hit an unrecoverable error.
42 The platform implementation must not return, but should either throw
43 an exception or otherwise exit.
44 */
45 extern void sk_throw(void);
46
47 enum {
48 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
49 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
50 };
51 /** Return a block of memory (at least 4-byte aligned) of at least the
52 specified size. If the requested memory cannot be returned, either
53 return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw()
54 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
55 */
56 extern void* sk_malloc_flags(size_t size, unsigned flags);
57 /** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
58 */
59 extern void* sk_malloc_throw(size_t size);
60 /** Same as standard realloc(), but this one never returns null on failure. It will throw
61 an exception if it fails.
62 */
63 extern void* sk_realloc_throw(void* buffer, size_t size);
64 /** Free memory returned by sk_malloc(). It is safe to pass null.
65 */
66 extern void sk_free(void*);
67
68 // bzero is safer than memset, but we can't rely on it, so... sk_bzero()
sk_bzero(void * buffer,size_t size)69 static inline void sk_bzero(void* buffer, size_t size) {
70 memset(buffer, 0, size);
71 }
72
73 ///////////////////////////////////////////////////////////////////////
74
75 #define SK_INIT_TO_AVOID_WARNING = 0
76
77 #ifndef SkDebugf
78 void SkDebugf(const char format[], ...);
79 #endif
80
81 #ifdef SK_DEBUG
82 #define SkASSERT(cond) SK_DEBUGBREAK(cond)
83 #define SkDEBUGCODE(code) code
84 #define SkDECLAREPARAM(type, var) , type var
85 #define SkPARAM(var) , var
86 // #define SkDEBUGF(args ) SkDebugf##args
87 #define SkDEBUGF(args ) SkDebugf args
88 #define SkAssertResult(cond) SkASSERT(cond)
89 #else
90 #define SkASSERT(cond)
91 #define SkDEBUGCODE(code)
92 #define SkDEBUGF(args)
93 #define SkDECLAREPARAM(type, var)
94 #define SkPARAM(var)
95
96 // unlike SkASSERT, this guy executes its condition in the non-debug build
97 #define SkAssertResult(cond) cond
98 #endif
99
100 ///////////////////////////////////////////////////////////////////////
101
102 /** Fast type for signed 8 bits. Use for parameter passing and local variables, not for storage
103 */
104 typedef int S8CPU;
105 /** Fast type for unsigned 8 bits. Use for parameter passing and local variables, not for storage
106 */
107 typedef int S16CPU;
108 /** Fast type for signed 16 bits. Use for parameter passing and local variables, not for storage
109 */
110 typedef unsigned U8CPU;
111 /** Fast type for unsigned 16 bits. Use for parameter passing and local variables, not for storage
112 */
113 typedef unsigned U16CPU;
114
115 /** Meant to be faster than bool (doesn't promise to be 0 or 1, just 0 or non-zero
116 */
117 typedef int SkBool;
118 /** Meant to be a small version of bool, for storage purposes. Will be 0 or 1
119 */
120 typedef uint8_t SkBool8;
121
122 #ifdef SK_DEBUG
123 int8_t SkToS8(long);
124 uint8_t SkToU8(size_t);
125 int16_t SkToS16(long);
126 uint16_t SkToU16(size_t);
127 int32_t SkToS32(long);
128 uint32_t SkToU32(size_t);
129 #else
130 #define SkToS8(x) ((int8_t)(x))
131 #define SkToU8(x) ((uint8_t)(x))
132 #define SkToS16(x) ((int16_t)(x))
133 #define SkToU16(x) ((uint16_t)(x))
134 #define SkToS32(x) ((int32_t)(x))
135 #define SkToU32(x) ((uint32_t)(x))
136 #endif
137
138 /** Returns 0 or 1 based on the condition
139 */
140 #define SkToBool(cond) ((cond) != 0)
141
142 #define SK_MaxS16 32767
143 #define SK_MinS16 -32767
144 #define SK_MaxU16 0xFFFF
145 #define SK_MinU16 0
146 #define SK_MaxS32 0x7FFFFFFF
147 #define SK_MinS32 0x80000001
148 #define SK_MaxU32 0xFFFFFFFF
149 #define SK_MinU32 0
150 #define SK_NaN32 0x80000000
151
152 #ifndef SK_OFFSETOF
153 #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1)
154 #endif
155
156 /** Returns the number of entries in an array (not a pointer)
157 */
158 #define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
159
160 /** Returns x rounded up to a multiple of 2
161 */
162 #define SkAlign2(x) (((x) + 1) >> 1 << 1)
163 /** Returns x rounded up to a multiple of 4
164 */
165 #define SkAlign4(x) (((x) + 3) >> 2 << 2)
166
167 typedef uint32_t SkFourByteTag;
168 #define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
169
170 /** 32 bit integer to hold a unicode value
171 */
172 typedef int32_t SkUnichar;
173 /** 32 bit value to hold a millisecond count
174 */
175 typedef uint32_t SkMSec;
176 /** 1 second measured in milliseconds
177 */
178 #define SK_MSec1 1000
179 /** maximum representable milliseconds
180 */
181 #define SK_MSecMax 0x7FFFFFFF
182 /** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
183 */
184 #define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
185 /** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
186 */
187 #define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
188
189
190 /****************************************************************************
191 The rest of these only build with C++
192 */
193 #ifdef __cplusplus
194
195 /** Faster than SkToBool for integral conditions. Returns 0 or 1
196 */
Sk32ToBool(uint32_t n)197 inline int Sk32ToBool(uint32_t n)
198 {
199 return (n | (0-n)) >> 31;
200 }
201
SkTSwap(T & a,T & b)202 template <typename T> inline void SkTSwap(T& a, T& b)
203 {
204 T c(a);
205 a = b;
206 b = c;
207 }
208
SkAbs32(int32_t value)209 inline int32_t SkAbs32(int32_t value)
210 {
211 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
212 if (value < 0)
213 value = -value;
214 return value;
215 #else
216 int32_t mask = value >> 31;
217 return (value ^ mask) - mask;
218 #endif
219 }
220
SkMax32(int32_t a,int32_t b)221 inline int32_t SkMax32(int32_t a, int32_t b)
222 {
223 if (a < b)
224 a = b;
225 return a;
226 }
227
SkMin32(int32_t a,int32_t b)228 inline int32_t SkMin32(int32_t a, int32_t b)
229 {
230 if (a > b)
231 a = b;
232 return a;
233 }
234
SkSign32(int32_t a)235 inline int32_t SkSign32(int32_t a)
236 {
237 return (a >> 31) | ((unsigned) -a >> 31);
238 }
239
SkFastMin32(int32_t value,int32_t max)240 inline int32_t SkFastMin32(int32_t value, int32_t max)
241 {
242 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
243 if (value > max)
244 value = max;
245 return value;
246 #else
247 int diff = max - value;
248 // clear diff if it is negative (clear if value > max)
249 diff &= (diff >> 31);
250 return value + diff;
251 #endif
252 }
253
254 /** Returns signed 32 bit value pinned between min and max, inclusively
255 */
SkPin32(int32_t value,int32_t min,int32_t max)256 inline int32_t SkPin32(int32_t value, int32_t min, int32_t max)
257 {
258 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
259 if (value < min)
260 value = min;
261 if (value > max)
262 value = max;
263 #else
264 if (value < min)
265 value = min;
266 else if (value > max)
267 value = max;
268 #endif
269 return value;
270 }
271
SkSetClearShift(uint32_t bits,bool cond,unsigned shift)272 inline uint32_t SkSetClearShift(uint32_t bits, bool cond, unsigned shift)
273 {
274 SkASSERT((int)cond == 0 || (int)cond == 1);
275 return (bits & ~(1 << shift)) | ((int)cond << shift);
276 }
277
SkSetClearMask(uint32_t bits,bool cond,uint32_t mask)278 inline uint32_t SkSetClearMask(uint32_t bits, bool cond, uint32_t mask)
279 {
280 return cond ? bits | mask : bits & ~mask;
281 }
282
283 //////////////////////////////////////////////////////////////////////////////
284
285 /** \class SkNoncopyable
286
287 SkNoncopyable is the base class for objects that may do not want to
288 be copied. It hides its copy-constructor and its assignment-operator.
289 */
290 class SkNoncopyable {
291 public:
SkNoncopyable()292 SkNoncopyable() {}
293
294 private:
295 SkNoncopyable(const SkNoncopyable&);
296 SkNoncopyable& operator=(const SkNoncopyable&);
297 };
298
299 class SkAutoFree : SkNoncopyable {
300 public:
SkAutoFree()301 SkAutoFree() : fPtr(NULL) {}
SkAutoFree(void * ptr)302 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
~SkAutoFree()303 ~SkAutoFree() { sk_free(fPtr); }
304
305 /** Return the currently allocate buffer, or null
306 */
get()307 void* get() const { return fPtr; }
308
309 /** Assign a new ptr allocated with sk_malloc (or null), and return the
310 previous ptr. Note it is the caller's responsibility to sk_free the
311 returned ptr.
312 */
set(void * ptr)313 void* set(void* ptr) {
314 void* prev = fPtr;
315 fPtr = ptr;
316 return prev;
317 }
318
319 /** Transfer ownership of the current ptr to the caller, setting the
320 internal reference to null. Note the caller is reponsible for calling
321 sk_free on the returned address.
322 */
detach()323 void* detach() { return this->set(NULL); }
324
325 /** Free the current buffer, and set the internal reference to NULL. Same
326 as calling sk_free(detach())
327 */
free()328 void free() {
329 sk_free(fPtr);
330 fPtr = NULL;
331 }
332
333 private:
334 void* fPtr;
335 // illegal
336 SkAutoFree(const SkAutoFree&);
337 SkAutoFree& operator=(const SkAutoFree&);
338 };
339
340 class SkAutoMalloc : public SkAutoFree {
341 public:
SkAutoMalloc(size_t size)342 explicit SkAutoMalloc(size_t size)
343 : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {}
344
SkAutoMalloc(size_t size,unsigned flags)345 SkAutoMalloc(size_t size, unsigned flags)
346 : SkAutoFree(sk_malloc_flags(size, flags)) {}
SkAutoMalloc()347 SkAutoMalloc() {}
348
349 void* alloc(size_t size,
350 unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) {
351 sk_free(set(sk_malloc_flags(size, flags)));
352 return get();
353 }
354 };
355
356 template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
357 public:
SkAutoSMalloc(size_t size)358 explicit SkAutoSMalloc(size_t size)
359 {
360 if (size <= kSize)
361 fPtr = fStorage;
362 else
363 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
364 }
~SkAutoSMalloc()365 ~SkAutoSMalloc()
366 {
367 if (fPtr != (void*)fStorage)
368 sk_free(fPtr);
369 }
get()370 void* get() const { return fPtr; }
371 private:
372 void* fPtr;
373 uint32_t fStorage[(kSize + 3) >> 2];
374 // illegal
375 SkAutoSMalloc(const SkAutoSMalloc&);
376 SkAutoSMalloc& operator=(const SkAutoSMalloc&);
377 };
378
379 #endif /* C++ */
380
381 #endif
382
383