util.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #ifdef HAVE_CONFIG_H
  33. #include <config.h>
  34. #endif
  35. #if defined(HAVE_GNU_C_LIBRARY) || defined(__MINGW32__) || defined(__DJGPP__)
  36. /* Work around glibc's non-defining of certain things when using gcc -ansi */
  37. # ifdef __STRICT_ANSI__
  38. # undef __STRICT_ANSI__
  39. # endif
  40. /* Work around glibc's string inlines (in bits/string2.h) if needed */
  41. # ifdef NO_STRING_INLINES
  42. # define __NO_STRING_INLINES
  43. # endif
  44. #endif
  45. #if !defined(lint) && !defined(NDEBUG)
  46. # define NDEBUG
  47. #endif
  48. #include <stdio.h>
  49. #include <stdarg.h>
  50. #include <stddef.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <assert.h>
  54. #ifdef HAVE_STRINGS_H
  55. #include <strings.h>
  56. #endif
  57. #if !defined(_musl_)
  58. #if __linux__ && __x86_64__
  59. __asm__(".symver memcpy,memcpy@GLIBC_2.2.5");
  60. #endif
  61. #endif
  62. #include <libyasm-stdint.h>
  63. #include <libyasm/coretype.h>
  64. #ifdef lint
  65. # define _(String) String
  66. #else
  67. # ifdef HAVE_LOCALE_H
  68. # include <locale.h>
  69. # endif
  70. # ifdef ENABLE_NLS
  71. # include <libintl.h>
  72. # define _(String) gettext(String)
  73. # else
  74. # define gettext(Msgid) (Msgid)
  75. # define dgettext(Domainname, Msgid) (Msgid)
  76. # define dcgettext(Domainname, Msgid, Category) (Msgid)
  77. # define textdomain(Domainname) while (0) /* nothing */
  78. # define bindtextdomain(Domainname, Dirname) while (0) /* nothing */
  79. # define _(String) (String)
  80. # endif
  81. #endif
  82. #ifdef gettext_noop
  83. # define N_(String) gettext_noop(String)
  84. #else
  85. # define N_(String) (String)
  86. #endif
  87. #ifdef HAVE_MERGESORT
  88. #define yasm__mergesort(a, b, c, d) mergesort(a, b, c, d)
  89. #endif
  90. #ifdef HAVE_STRSEP
  91. #define yasm__strsep(a, b) strsep(a, b)
  92. #endif
  93. #ifdef HAVE_STRCASECMP
  94. # define yasm__strcasecmp(x, y) strcasecmp(x, y)
  95. # define yasm__strncasecmp(x, y, n) strncasecmp(x, y, n)
  96. #elif HAVE_STRICMP
  97. # define yasm__strcasecmp(x, y) stricmp(x, y)
  98. # define yasm__strncasecmp(x, y, n) strnicmp(x, y, n)
  99. #elif HAVE__STRICMP
  100. # define yasm__strcasecmp(x, y) _stricmp(x, y)
  101. # define yasm__strncasecmp(x, y, n) _strnicmp(x, y, n)
  102. #elif HAVE_STRCMPI
  103. # define yasm__strcasecmp(x, y) strcmpi(x, y)
  104. # define yasm__strncasecmp(x, y, n) strncmpi(x, y, n)
  105. #else
  106. # define USE_OUR_OWN_STRCASECMP
  107. #endif
  108. #include <libyasm/compat-queue.h>
  109. #ifdef WITH_DMALLOC
  110. # include <dmalloc.h>
  111. # define yasm__xstrdup(str) xstrdup(str)
  112. # define yasm_xmalloc(size) xmalloc(size)
  113. # define yasm_xcalloc(count, size) xcalloc(count, size)
  114. # define yasm_xrealloc(ptr, size) xrealloc(ptr, size)
  115. # define yasm_xfree(ptr) xfree(ptr)
  116. #endif
  117. /* Bit-counting: used primarily by HAMT but also in a few other places. */
  118. #define BC_TWO(c) (0x1ul << (c))
  119. #define BC_MSK(c) (((unsigned long)(-1)) / (BC_TWO(BC_TWO(c)) + 1ul))
  120. #define BC_COUNT(x,c) ((x) & BC_MSK(c)) + (((x) >> (BC_TWO(c))) & BC_MSK(c))
  121. #define BitCount(d, s) do { \
  122. d = BC_COUNT(s, 0); \
  123. d = BC_COUNT(d, 1); \
  124. d = BC_COUNT(d, 2); \
  125. d = BC_COUNT(d, 3); \
  126. d = BC_COUNT(d, 4); \
  127. } while (0)
  128. /** Determine if a value is exactly a power of 2. Zero is treated as a power
  129. * of two.
  130. * \param x value
  131. * \return Nonzero if x is a power of 2.
  132. */
  133. #define is_exp2(x) ((x & (x - 1)) == 0)
  134. #ifndef NELEMS
  135. /** Get the number of elements in an array.
  136. * \internal
  137. * \param array array
  138. * \return Number of elements.
  139. */
  140. #define NELEMS(array) (sizeof(array) / sizeof(array[0]))
  141. #endif
  142. char * yasm_replace_path(const char* replace_map[], int size, const char* str, int pref_len);
  143. #endif