common.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/byteorder.h>
  45. #include "libgearman/backtrace.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 GEARMAN_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. /**
  103. * Delete an object from a list.
  104. * @ingroup gearman_constants
  105. */
  106. #define GEARMAN_LIST_DEL(__list, __obj, __prefix) { \
  107. if (__list ## _list == __obj) \
  108. __list ## _list= __obj->__prefix ## next; \
  109. if (__obj->__prefix ## prev != NULL) \
  110. __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
  111. if (__obj->__prefix ## next != NULL) \
  112. __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
  113. __list ## _count--; \
  114. }
  115. /**
  116. * Add an object to a fifo list.
  117. * @ingroup gearman_constants
  118. */
  119. #define GEARMAN_FIFO_ADD(__list, __obj, __prefix) { \
  120. if (__list ## _end == NULL) \
  121. __list ## _list= __obj; \
  122. else \
  123. __list ## _end->__prefix ## next= __obj; \
  124. __list ## _end= __obj; \
  125. __list ## _count++; \
  126. }
  127. /**
  128. * Delete an object from a fifo list.
  129. * @ingroup gearman_constants
  130. */
  131. #define GEARMAN_FIFO_DEL(__list, __obj, __prefix) { \
  132. __list ## _list= __obj->__prefix ## next; \
  133. if (__list ## _list == NULL) \
  134. __list ## _end= NULL; \
  135. __list ## _count--; \
  136. }
  137. /**
  138. * Add an object to a hash.
  139. * @ingroup gearman_constants
  140. */
  141. #define GEARMAN_HASH_ADD(__hash, __key, __obj, __prefix) { \
  142. if (__hash ## _hash[__key] != NULL) \
  143. __hash ## _hash[__key]->__prefix ## prev= __obj; \
  144. __obj->__prefix ## next= __hash ## _hash[__key]; \
  145. __obj->__prefix ## prev= NULL; \
  146. __hash ## _hash[__key]= __obj; \
  147. __hash ## _count++; \
  148. }
  149. /**
  150. * Delete an object from a hash.
  151. * @ingroup gearman_constants
  152. */
  153. #define GEARMAN_HASH_DEL(__hash, __key, __obj, __prefix) { \
  154. if (__hash ## _hash[__key] == __obj) \
  155. __hash ## _hash[__key]= __obj->__prefix ## next; \
  156. if (__obj->__prefix ## prev != NULL) \
  157. __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
  158. if (__obj->__prefix ## next != NULL) \
  159. __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
  160. __hash ## _count--; \
  161. }
  162. #define gearmand_array_size(__object) (sizeof((__object)) / sizeof(*(__object)))
  163. #ifdef __cplusplus
  164. }
  165. #endif
  166. #ifdef NI_MAXHOST
  167. #define GEARMAND_NI_MAXHOST NI_MAXHOST
  168. #else
  169. #define GEARMAND_NI_MAXHOST 1025
  170. #endif
  171. #ifdef NI_MAXSERV
  172. #define GEARMAND_NI_MAXSERV NI_MAXSERV
  173. #else
  174. #define GEARMAND_NI_MAXSERV 32
  175. #endif