common.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* ===================================================================
  2. *
  3. * Copyright (c) 2018, Helder Eijs <helderijs@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. * ===================================================================
  30. */
  31. #ifndef COMMON_H
  32. #define COMMON_H
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "errors.h"
  37. /*
  38. * Define STATIC as an empty string to turn certain static functions public
  39. */
  40. #ifndef STATIC
  41. #define STATIC static inline
  42. #endif
  43. #define TRUE 1
  44. #define FALSE 0
  45. #ifndef MAX
  46. #define MAX(a,b) ((a)>(b)?(a):(b))
  47. #endif
  48. #ifndef MIN
  49. #define MIN(a,b) ((a)<(b)?(a):(b))
  50. #endif
  51. #define _PASTE(x,y) x##y
  52. #define _PASTE2(x,y) _PASTE(x,y)
  53. #ifdef HAVE_STDINT_H
  54. #include <stdint.h>
  55. #else
  56. typedef __int8 int8_t;
  57. typedef unsigned __int8 uint8_t;
  58. typedef __int16 int16_t;
  59. typedef unsigned __int16 uint16_t;
  60. typedef __int32 int32_t;
  61. typedef unsigned __int32 uint32_t;
  62. typedef __int64 int64_t;
  63. typedef unsigned __int64 uint64_t;
  64. #ifndef UINT32_MAX
  65. #define UINT32_MAX 0xFFFFFFFFUL
  66. #endif
  67. #endif /* HAVE_STDINT_H */
  68. #ifdef _MSC_VER
  69. #define inline _inline
  70. #define RESTRICT __restrict
  71. #include <malloc.h>
  72. #else /** Not MSC **/
  73. #if __STDC_VERSION__ >= 199901L
  74. #define RESTRICT restrict
  75. #else
  76. #ifdef __GNUC__
  77. #define RESTRICT __restrict
  78. #define inline __inline
  79. #else
  80. #define RESTRICT
  81. #define inline
  82. #endif
  83. #endif
  84. #endif
  85. /** Force checking of assertions **/
  86. #ifdef NDEBUG
  87. #undef NDEBUG
  88. #endif
  89. #include <assert.h>
  90. /*
  91. * On Windows, distutils expects that a CPython module always exports the symbol init${MODNAME}
  92. */
  93. #if defined(_MSC_VER) || defined(__MINGW32__)
  94. #include <Python.h>
  95. #if PY_MAJOR_VERSION >= 3
  96. #define FAKE_INIT(x) PyMODINIT_FUNC _PASTE2(PyInit__,x) (void) { return NULL; }
  97. #else
  98. #define FAKE_INIT(x) PyMODINIT_FUNC _PASTE2(init_,x) (void) { return; }
  99. #endif
  100. #else
  101. #define FAKE_INIT(x)
  102. #endif
  103. /*
  104. * On Windows, functions must be explicitly marked for export.
  105. */
  106. #if defined(_MSC_VER) || defined(__MINGW32__)
  107. #define EXPORT_SYM __declspec(dllexport)
  108. #else
  109. #define EXPORT_SYM
  110. #endif
  111. /*
  112. * Platform specific routine for aligned allocation
  113. */
  114. #if defined(_MSC_VER) || defined(__MINGW32__)
  115. static inline void* align_alloc(size_t size, unsigned boundary)
  116. {
  117. return _aligned_malloc(size, boundary);
  118. }
  119. static inline void align_free(void *mem)
  120. {
  121. if (mem) {
  122. _aligned_free(mem);
  123. }
  124. }
  125. #elif defined(HAVE_POSIX_MEMALIGN)
  126. static inline void* align_alloc(size_t size, unsigned boundary)
  127. {
  128. int result;
  129. void *new_mem;
  130. result = posix_memalign((void**)&new_mem, boundary, size);
  131. return result ? NULL : new_mem;
  132. }
  133. static inline void align_free(void *mem)
  134. {
  135. free(mem);
  136. }
  137. #elif defined(HAVE_MEMALIGN)
  138. #include <malloc.h>
  139. static inline void* align_alloc(size_t size, unsigned boundary)
  140. {
  141. return memalign(boundary, size);
  142. }
  143. static inline void align_free(void *mem)
  144. {
  145. free(mem);
  146. }
  147. #else
  148. #error No routines for aligned memory
  149. #endif
  150. /*
  151. * Find first character in a string which is not c.
  152. */
  153. static inline const uint8_t* memchr_not(const uint8_t* s, int c, size_t n)
  154. {
  155. size_t i;
  156. for (i=0; i<n; i++, s++)
  157. if (*s != c)
  158. return s;
  159. return NULL;
  160. }
  161. /*
  162. * On 32-bit x86 platforms, gcc assumes the stack to be aligned to 16
  163. * bytes, but the caller may actually only align it to 4 bytes, which
  164. * make functions crash if they use SSE2 intrinsics.
  165. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838
  166. */
  167. #if defined(GCC_REALIGN)
  168. #define FUNC_SSE2 __attribute__((force_align_arg_pointer))
  169. #else
  170. #define FUNC_SSE2
  171. #endif
  172. #endif