common.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Gearman server and library
  2. * Copyright (C) 2008 Brian Aker, Eric Day
  3. * All rights reserved.
  4. *
  5. * Use and distribution licensed under the BSD license. See
  6. * the COPYING file in the parent directory for full text.
  7. */
  8. /**
  9. * @file
  10. * @brief System Include Files
  11. */
  12. #pragma once
  13. #include <libgearman-server/gearmand.h>
  14. #include <libgearman-server/byteorder.h>
  15. #ifdef HAVE_FCNTL_H
  16. #include <fcntl.h>
  17. #endif
  18. #ifdef HAVE_PTHREAD
  19. #include <pthread.h>
  20. #endif
  21. #ifdef HAVE_STDARG_H
  22. #include <stdarg.h>
  23. #endif
  24. #ifdef HAVE_STDDEF_H
  25. #include <stddef.h>
  26. #endif
  27. #ifdef HAVE_STRINGS_H
  28. #include <strings.h>
  29. #endif
  30. #ifdef HAVE_SYS_UTSNAME_H
  31. #include <sys/utsname.h>
  32. #endif
  33. #ifdef HAVE_NETINET_TCP_H
  34. #include <netinet/tcp.h>
  35. #endif
  36. #ifdef HAVE_UNISTD_H
  37. #include <unistd.h>
  38. #endif
  39. #ifdef TIME_WITH_SYS_TIME
  40. # include <sys/time.h>
  41. # include <time.h>
  42. #else
  43. # ifdef HAVE_SYS_TIME_H
  44. # include <sys/time.h>
  45. # else
  46. # include <time.h>
  47. # endif
  48. #endif
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
  53. #define likely(__x) if((__x))
  54. #define unlikely(__x) if((__x))
  55. #else
  56. #define likely(__x) if(__builtin_expect((__x), 1))
  57. #define unlikely(__x) if(__builtin_expect((__x), 0))
  58. #endif
  59. /**
  60. * Add an object to a list.
  61. * @ingroup gearman_constants
  62. */
  63. #define GEARMAN_LIST_ADD(__list, __obj, __prefix) { \
  64. if (__list ## _list != NULL) \
  65. __list ## _list->__prefix ## prev= __obj; \
  66. __obj->__prefix ## next= __list ## _list; \
  67. __obj->__prefix ## prev= NULL; \
  68. __list ## _list= __obj; \
  69. __list ## _count++; \
  70. }
  71. /**
  72. * Delete an object from a list.
  73. * @ingroup gearman_constants
  74. */
  75. #define GEARMAN_LIST_DEL(__list, __obj, __prefix) { \
  76. if (__list ## _list == __obj) \
  77. __list ## _list= __obj->__prefix ## next; \
  78. if (__obj->__prefix ## prev != NULL) \
  79. __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
  80. if (__obj->__prefix ## next != NULL) \
  81. __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
  82. __list ## _count--; \
  83. }
  84. /**
  85. * Add an object to a fifo list.
  86. * @ingroup gearman_constants
  87. */
  88. #define GEARMAN_FIFO_ADD(__list, __obj, __prefix) { \
  89. if (__list ## _end == NULL) \
  90. __list ## _list= __obj; \
  91. else \
  92. __list ## _end->__prefix ## next= __obj; \
  93. __list ## _end= __obj; \
  94. __list ## _count++; \
  95. }
  96. /**
  97. * Delete an object from a fifo list.
  98. * @ingroup gearman_constants
  99. */
  100. #define GEARMAN_FIFO_DEL(__list, __obj, __prefix) { \
  101. __list ## _list= __obj->__prefix ## next; \
  102. if (__list ## _list == NULL) \
  103. __list ## _end= NULL; \
  104. __list ## _count--; \
  105. }
  106. /**
  107. * Add an object to a hash.
  108. * @ingroup gearman_constants
  109. */
  110. #define GEARMAN_HASH_ADD(__hash, __key, __obj, __prefix) { \
  111. if (__hash ## _hash[__key] != NULL) \
  112. __hash ## _hash[__key]->__prefix ## prev= __obj; \
  113. __obj->__prefix ## next= __hash ## _hash[__key]; \
  114. __obj->__prefix ## prev= NULL; \
  115. __hash ## _hash[__key]= __obj; \
  116. __hash ## _count++; \
  117. }
  118. /**
  119. * Delete an object from a hash.
  120. * @ingroup gearman_constants
  121. */
  122. #define GEARMAN_HASH_DEL(__hash, __key, __obj, __prefix) { \
  123. if (__hash ## _hash[__key] == __obj) \
  124. __hash ## _hash[__key]= __obj->__prefix ## next; \
  125. if (__obj->__prefix ## prev != NULL) \
  126. __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
  127. if (__obj->__prefix ## next != NULL) \
  128. __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
  129. __hash ## _count--; \
  130. }
  131. #define gearmand_array_size(__object) (sizeof((__object)) / sizeof(*(__object)))
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. #ifdef NI_MAXHOST
  136. #define GEARMAND_NI_MAXHOST NI_MAXHOST
  137. #else
  138. #define GEARMAND_NI_MAXHOST 1025
  139. #endif
  140. #ifdef NI_MAXSERV
  141. #define GEARMAND_NI_MAXSERV NI_MAXSERV
  142. #else
  143. #define GEARMAND_NI_MAXSERV 32
  144. #endif