common.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /**
  39. * @file
  40. * @brief System Include Files
  41. */
  42. #pragma once
  43. #include <libgearman-server/gearmand.h>
  44. #include "libgearman-server/config.hpp"
  45. #include "libgearman/assert.hpp"
  46. #ifdef HAVE_FCNTL_H
  47. # include <fcntl.h>
  48. #endif
  49. #ifdef HAVE_PTHREAD
  50. # include <pthread.h>
  51. #endif
  52. #ifdef HAVE_STDARG_H
  53. # include <stdarg.h>
  54. #endif
  55. #ifdef HAVE_STDDEF_H
  56. # include <stddef.h>
  57. #endif
  58. #ifdef HAVE_STRINGS_H
  59. # include <strings.h>
  60. #endif
  61. #ifdef HAVE_SYS_UTSNAME_H
  62. # include <sys/utsname.h>
  63. #endif
  64. #ifdef HAVE_NETINET_TCP_H
  65. # include <netinet/tcp.h>
  66. #endif
  67. #ifdef HAVE_UNISTD_H
  68. # include <unistd.h>
  69. #endif
  70. #ifdef TIME_WITH_SYS_TIME
  71. # include <sys/time.h>
  72. # include <time.h>
  73. #else
  74. # ifdef HAVE_SYS_TIME_H
  75. # include <sys/time.h>
  76. # else
  77. # include <time.h>
  78. # endif
  79. #endif
  80. #ifdef __cplusplus
  81. extern "C" {
  82. #endif
  83. #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
  84. #define likely(__x) if((__x))
  85. #define unlikely(__x) if((__x))
  86. #else
  87. #define likely(__x) if(__builtin_expect((__x), 1))
  88. #define unlikely(__x) if(__builtin_expect((__x), 0))
  89. #endif
  90. /**
  91. * Add an object to a list.
  92. * @ingroup gearman_constants
  93. */
  94. #define GEARMAND_LIST_ADD(__list, __obj, __prefix) { \
  95. if (__list ## _list != NULL) \
  96. __list ## _list->__prefix ## prev= __obj; \
  97. __obj->__prefix ## next= __list ## _list; \
  98. __obj->__prefix ## prev= NULL; \
  99. __list ## _list= __obj; \
  100. __list ## _count++; \
  101. }
  102. #define GEARMAND_LIST__ADD(__list, __obj) { \
  103. if (__list ## _list != NULL) \
  104. __list ## _list->prev= __obj; \
  105. __obj->next= __list ## _list; \
  106. __obj->prev= NULL; \
  107. __list ## _list= __obj; \
  108. __list ## _count++; \
  109. }
  110. /**
  111. * Delete an object from a list.
  112. * @ingroup gearman_constants
  113. */
  114. #define GEARMAND_LIST_DEL(__list, __obj, __prefix) { \
  115. if (__list ## _list == __obj) \
  116. __list ## _list= __obj->__prefix ## next; \
  117. if (__obj->__prefix ## prev != NULL) \
  118. __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
  119. if (__obj->__prefix ## next != NULL) \
  120. __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
  121. __list ## _count--; \
  122. }
  123. #define GEARMAND_LIST__DEL(__list, __obj) { \
  124. if (__list ## _list == __obj) \
  125. __list ## _list= __obj->next; \
  126. if (__obj->prev != NULL) \
  127. __obj->prev->next= __obj->next; \
  128. if (__obj->next != NULL) \
  129. __obj->next->prev= __obj->prev; \
  130. __list ## _count--; \
  131. }
  132. /**
  133. * Add an object to a fifo list.
  134. * @ingroup gearman_constants
  135. */
  136. #define GEARMAND_FIFO_ADD(__list, __obj, __prefix) { \
  137. if (__list ## _end == NULL) \
  138. __list ## _list= __obj; \
  139. else \
  140. __list ## _end->__prefix ## next= __obj; \
  141. __list ## _end= __obj; \
  142. __list ## _count++; \
  143. }
  144. #define GEARMAND_FIFO__ADD(__list, __obj) { \
  145. if (__list ## _end == NULL) \
  146. __list ## _list= __obj; \
  147. else \
  148. __list ## _end->next= __obj; \
  149. __list ## _end= __obj; \
  150. __list ## _count++; \
  151. }
  152. /**
  153. * Delete an object from a fifo list.
  154. * @ingroup gearman_constants
  155. */
  156. #define GEARMAND_FIFO_DEL(__list, __obj, __prefix) { \
  157. __list ## _list= __obj->__prefix ## next; \
  158. if (__list ## _list == NULL) \
  159. __list ## _end= NULL; \
  160. __list ## _count--; \
  161. }
  162. #define GEARMAND_FIFO__DEL(__list, __obj) { \
  163. __list ## _list= __obj->next; \
  164. if (__list ## _list == NULL) \
  165. __list ## _end= NULL; \
  166. __list ## _count--; \
  167. }
  168. /**
  169. * Add an object to a hash.
  170. * @ingroup gearman_constants
  171. */
  172. #define GEARMAND_HASH_ADD(__hash, __key, __obj, __prefix) { \
  173. if (__hash ## _hash[__key] != NULL) \
  174. __hash ## _hash[__key]->__prefix ## prev= __obj; \
  175. __obj->__prefix ## next= __hash ## _hash[__key]; \
  176. __obj->__prefix ## prev= NULL; \
  177. __hash ## _hash[__key]= __obj; \
  178. __hash ## _count++; \
  179. }
  180. #define GEARMAND_HASH__ADD(__hash, __key, __obj) { \
  181. if (__hash ## _hash[__key] != NULL) \
  182. __hash ## _hash[__key]->prev= __obj; \
  183. __obj->next= __hash ## _hash[__key]; \
  184. __obj->prev= NULL; \
  185. __hash ## _hash[__key]= __obj; \
  186. __hash ## _count++; \
  187. }
  188. /**
  189. * Delete an object from a hash.
  190. * @ingroup gearman_constants
  191. */
  192. #define GEARMAND_HASH_DEL(__hash, __key, __obj, __prefix) { \
  193. if (__hash ## _hash[__key] == __obj) \
  194. __hash ## _hash[__key]= __obj->__prefix ## next; \
  195. if (__obj->__prefix ## prev != NULL) \
  196. __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
  197. if (__obj->__prefix ## next != NULL) \
  198. __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
  199. __hash ## _count--; \
  200. }
  201. #define GEARMAND_HASH__DEL(__hash, __key, __obj) { \
  202. if (__hash ## _hash[__key] == __obj) \
  203. __hash ## _hash[__key]= __obj->next; \
  204. if (__obj->prev != NULL) \
  205. __obj->prev->next= __obj->next; \
  206. if (__obj->next != NULL) \
  207. __obj->next->prev= __obj->prev; \
  208. __hash ## _count--; \
  209. }
  210. #define gearmand_array_size(__object) (sizeof((__object)) / sizeof(*(__object)))
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214. #ifdef NI_MAXHOST
  215. #define GEARMAND_NI_MAXHOST NI_MAXHOST
  216. #else
  217. #define GEARMAND_NI_MAXHOST 1025
  218. #endif
  219. #ifdef NI_MAXSERV
  220. #define GEARMAND_NI_MAXSERV NI_MAXSERV
  221. #else
  222. #define GEARMAND_NI_MAXSERV 32
  223. #endif