Add packaging files for Tizen
[profile/ivi/yasm.git] / util.h
1 /*
2  * YASM utility functions.
3  *
4  * Includes standard headers and defines prototypes for replacement functions
5  * if needed.
6  *
7  *  Copyright (C) 2001-2007  Peter Johnson
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef YASM_UTIL_H
31 #define YASM_UTIL_H
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #if defined(HAVE_GNU_C_LIBRARY) || defined(__MINGW32__) || defined(__DJGPP__)
38
39 /* Work around glibc's non-defining of certain things when using gcc -ansi */
40 # ifdef __STRICT_ANSI__
41 #  undef __STRICT_ANSI__
42 # endif
43
44 /* Work around glibc's string inlines (in bits/string2.h) if needed */
45 # ifdef NO_STRING_INLINES
46 #  define __NO_STRING_INLINES
47 # endif
48
49 #endif
50
51 #if !defined(lint) && !defined(NDEBUG)
52 # define NDEBUG
53 #endif
54
55 #include <stdio.h>
56 #include <stdarg.h>
57
58 #include <stddef.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <assert.h>
62
63 #ifdef HAVE_STRINGS_H
64 #include <strings.h>
65 #endif
66
67 #include <libyasm-stdint.h>
68 #include <libyasm/coretype.h>
69
70 #ifdef lint
71 # define _(String)      String
72 #else
73 # ifdef HAVE_LOCALE_H
74 #  include <locale.h>
75 # endif
76
77 # ifdef ENABLE_NLS
78 #  include <libintl.h>
79 #  define _(String)     gettext(String)
80 # else
81 #  define gettext(Msgid)                            (Msgid)
82 #  define dgettext(Domainname, Msgid)               (Msgid)
83 #  define dcgettext(Domainname, Msgid, Category)    (Msgid)
84 #  define textdomain(Domainname)                    while (0) /* nothing */
85 #  define bindtextdomain(Domainname, Dirname)       while (0) /* nothing */
86 #  define _(String)     (String)
87 # endif
88 #endif
89
90 #ifdef gettext_noop
91 # define N_(String)     gettext_noop(String)
92 #else
93 # define N_(String)     (String)
94 #endif
95
96 #ifdef HAVE_MERGESORT
97 #define yasm__mergesort(a, b, c, d)     mergesort(a, b, c, d)
98 #endif
99
100 #ifdef HAVE_STRSEP
101 #define yasm__strsep(a, b)              strsep(a, b)
102 #endif
103
104 #ifdef HAVE_STRCASECMP
105 # define yasm__strcasecmp(x, y)         strcasecmp(x, y)
106 # define yasm__strncasecmp(x, y, n)     strncasecmp(x, y, n)
107 #elif HAVE_STRICMP
108 # define yasm__strcasecmp(x, y)         stricmp(x, y)
109 # define yasm__strncasecmp(x, y, n)     strnicmp(x, y, n)
110 #elif HAVE__STRICMP
111 # define yasm__strcasecmp(x, y)         _stricmp(x, y)
112 # define yasm__strncasecmp(x, y, n)     _strnicmp(x, y, n)
113 #elif HAVE_STRCMPI
114 # define yasm__strcasecmp(x, y)         strcmpi(x, y)
115 # define yasm__strncasecmp(x, y, n)     strncmpi(x, y, n)
116 #else
117 # define USE_OUR_OWN_STRCASECMP
118 #endif
119
120 #include <libyasm/compat-queue.h>
121
122 #ifdef WITH_DMALLOC
123 # include <dmalloc.h>
124 # define yasm__xstrdup(str)             xstrdup(str)
125 # define yasm_xmalloc(size)             xmalloc(size)
126 # define yasm_xcalloc(count, size)      xcalloc(count, size)
127 # define yasm_xrealloc(ptr, size)       xrealloc(ptr, size)
128 # define yasm_xfree(ptr)                xfree(ptr)
129 #endif
130
131 /* Bit-counting: used primarily by HAMT but also in a few other places. */
132 #define BC_TWO(c)       (0x1ul << (c))
133 #define BC_MSK(c)       (((unsigned long)(-1)) / (BC_TWO(BC_TWO(c)) + 1ul))
134 #define BC_COUNT(x,c)   ((x) & BC_MSK(c)) + (((x) >> (BC_TWO(c))) & BC_MSK(c))
135 #define BitCount(d, s)          do {            \
136         d = BC_COUNT(s, 0);                     \
137         d = BC_COUNT(d, 1);                     \
138         d = BC_COUNT(d, 2);                     \
139         d = BC_COUNT(d, 3);                     \
140         d = BC_COUNT(d, 4);                     \
141     } while (0)
142
143 /** Determine if a value is exactly a power of 2.  Zero is treated as a power
144  * of two.
145  * \param x     value
146  * \return Nonzero if x is a power of 2.
147  */
148 #define is_exp2(x)  ((x & (x - 1)) == 0)
149
150 #ifndef NELEMS
151 /** Get the number of elements in an array.
152  * \internal
153  * \param array     array
154  * \return Number of elements.
155  */
156 #define NELEMS(array)   (sizeof(array) / sizeof(array[0]))
157 #endif
158
159 #endif