common.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include <time.h>
  71. #ifdef HAVE_SYS_TIME_H
  72. # include <sys/time.h>
  73. #endif
  74. #ifdef __cplusplus
  75. extern "C" {
  76. #endif
  77. #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
  78. #define likely(__x) if((__x))
  79. #define unlikely(__x) if((__x))
  80. #else
  81. #define likely(__x) if(__builtin_expect((__x), 1))
  82. #define unlikely(__x) if(__builtin_expect((__x), 0))
  83. #endif
  84. /**
  85. * Add an object to a list.
  86. * @ingroup gearman_constants
  87. */
  88. #define GEARMAND_LIST_ADD(__list, __obj, __prefix) { \
  89. if (__list ## _list != NULL) \
  90. __list ## _list->__prefix ## prev= __obj; \
  91. __obj->__prefix ## next= __list ## _list; \
  92. __obj->__prefix ## prev= NULL; \
  93. __list ## _list= __obj; \
  94. __list ## _count++; \
  95. }
  96. #define GEARMAND_LIST__ADD(__list, __obj) { \
  97. if (__list ## _list != NULL) \
  98. __list ## _list->prev= __obj; \
  99. __obj->next= __list ## _list; \
  100. __obj->prev= NULL; \
  101. __list ## _list= __obj; \
  102. __list ## _count++; \
  103. }
  104. /**
  105. * Delete an object from a list.
  106. * @ingroup gearman_constants
  107. */
  108. #define GEARMAND_LIST_DEL(__list, __obj, __prefix) { \
  109. if (__list ## _list == __obj) \
  110. __list ## _list= __obj->__prefix ## next; \
  111. if (__obj->__prefix ## prev != NULL) \
  112. __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
  113. if (__obj->__prefix ## next != NULL) \
  114. __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
  115. __list ## _count--; \
  116. }
  117. #define GEARMAND_LIST__DEL(__list, __obj) { \
  118. if (__list ## _list == __obj) \
  119. __list ## _list= __obj->next; \
  120. if (__obj->prev != NULL) \
  121. __obj->prev->next= __obj->next; \
  122. if (__obj->next != NULL) \
  123. __obj->next->prev= __obj->prev; \
  124. __list ## _count--; \
  125. }
  126. /**
  127. * Add an object to a fifo list.
  128. * @ingroup gearman_constants
  129. */
  130. #define GEARMAND_FIFO_ADD(__list, __obj, __prefix) { \
  131. if (__list ## _end == NULL) \
  132. __list ## _list= __obj; \
  133. else \
  134. __list ## _end->__prefix ## next= __obj; \
  135. __list ## _end= __obj; \
  136. __list ## _count++; \
  137. }
  138. #define GEARMAND_FIFO__ADD(__list, __obj) { \
  139. if (__list ## _end == NULL) \
  140. __list ## _list= __obj; \
  141. else \
  142. __list ## _end->next= __obj; \
  143. __list ## _end= __obj; \
  144. __list ## _count++; \
  145. }
  146. /**
  147. * Delete an object from a fifo list.
  148. * @ingroup gearman_constants
  149. */
  150. #define GEARMAND_FIFO_DEL(__list, __obj, __prefix) { \
  151. __list ## _list= __obj->__prefix ## next; \
  152. if (__list ## _list == NULL) \
  153. __list ## _end= NULL; \
  154. __list ## _count--; \
  155. }
  156. #define GEARMAND_FIFO__DEL(__list, __obj) { \
  157. __list ## _list= __obj->next; \
  158. if (__list ## _list == NULL) \
  159. __list ## _end= NULL; \
  160. __list ## _count--; \
  161. }
  162. /**
  163. * Add an object to a hash.
  164. * @ingroup gearman_constants
  165. */
  166. #define GEARMAND_HASH_ADD(__hash, __key, __obj, __prefix) { \
  167. if (__hash ## _hash[__key] != NULL) \
  168. __hash ## _hash[__key]->__prefix ## prev= __obj; \
  169. __obj->__prefix ## next= __hash ## _hash[__key]; \
  170. __obj->__prefix ## prev= NULL; \
  171. __hash ## _hash[__key]= __obj; \
  172. __hash ## _count++; \
  173. }
  174. #define GEARMAND_HASH__ADD(__hash, __key, __obj) { \
  175. if (__hash ## _hash[__key] != NULL) \
  176. __hash ## _hash[__key]->prev= __obj; \
  177. __obj->next= __hash ## _hash[__key]; \
  178. __obj->prev= NULL; \
  179. __hash ## _hash[__key]= __obj; \
  180. __hash ## _count++; \
  181. }
  182. /**
  183. * Delete an object from a hash.
  184. * @ingroup gearman_constants
  185. */
  186. #define GEARMAND_HASH_DEL(__hash, __key, __obj, __prefix) { \
  187. if (__hash ## _hash[__key] == __obj) \
  188. __hash ## _hash[__key]= __obj->__prefix ## next; \
  189. if (__obj->__prefix ## prev != NULL) \
  190. __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
  191. if (__obj->__prefix ## next != NULL) \
  192. __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
  193. __hash ## _count--; \
  194. }
  195. #define GEARMAND_HASH__DEL(__hash, __key, __obj) { \
  196. if (__hash ## _hash[__key] == __obj) \
  197. __hash ## _hash[__key]= __obj->next; \
  198. if (__obj->prev != NULL) \
  199. __obj->prev->next= __obj->next; \
  200. if (__obj->next != NULL) \
  201. __obj->next->prev= __obj->prev; \
  202. __hash ## _count--; \
  203. }
  204. #define gearmand_array_size(__object) (sizeof((__object)) / sizeof(*(__object)))
  205. #ifdef __cplusplus
  206. }
  207. #endif
  208. #ifdef NI_MAXHOST
  209. #define GEARMAND_NI_MAXHOST NI_MAXHOST
  210. #else
  211. #define GEARMAND_NI_MAXHOST 1025
  212. #endif
  213. #ifdef NI_MAXSERV
  214. #define GEARMAND_NI_MAXSERV NI_MAXSERV
  215. #else
  216. #define GEARMAND_NI_MAXSERV 32
  217. #endif