foo
[cdmenu] / menu / src / util.h
1 #ifndef UTIL_H_
2 #define UTIL_H_
3
4 #include <stdlib.h>
5 #include "sizeint.h"
6
7 #if defined(__WATCOMC__) || defined(_WIN32) || defined(__DJGPP__)
8 #include <malloc.h>
9 #else
10 #include <alloca.h>
11 #endif
12
13 #ifdef __GNUC__
14 #define PACKED __attribute__((packed))
15 #else
16 #define PACKED
17 #endif
18
19
20 /* Non-failing versions of malloc/calloc/realloc. They never return 0, they call
21  * demo_abort on failure. Use the macros, don't call the *_impl functions.
22  */
23 #define malloc_nf(sz)   malloc_nf_impl(sz, __FILE__, __LINE__)
24 void *malloc_nf_impl(size_t sz, const char *file, int line);
25 #define calloc_nf(n, sz)        calloc_nf_impl(n, sz, __FILE__, __LINE__)
26 void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line);
27 #define realloc_nf(p, sz)       realloc_nf_impl(p, sz, __FILE__, __LINE__)
28 void *realloc_nf_impl(void *p, size_t sz, const char *file, int line);
29 #define strdup_nf(s)    strdup_nf_impl(s, __FILE__, __LINE__)
30 char *strdup_nf_impl(const char *s, const char *file, int line);
31
32 int match_prefix(const char *str, const char *prefix);
33
34 #ifndef INLINE
35 #if (__STDC_VERSION__ >= 199901) || defined(__GNUC__)
36 #define INLINE inline
37 #else
38 #define INLINE __inline
39 #endif
40 #endif
41
42 #if defined(__i386__) || defined(__386__) || defined(MSDOS)
43
44 /* fast conversion of double -> 32bit int
45  * for details see:
46  *  - http://chrishecker.com/images/f/fb/Gdmfp.pdf
47  *  - http://stereopsis.com/FPU.html#convert
48  */
49 static INLINE int32_t cround64(double val)
50 {
51         val += 6755399441055744.0;
52         return *(int32_t*)&val;
53 }
54 #else
55 #define cround64(x)     ((int32_t)(x))
56 #endif
57
58 #endif  /* UTIL_H_ */