common.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <config.h>
  14. #include <libgearman-server/gearmand.h>
  15. #include <libgearman-server/byteorder.h>
  16. #ifdef HAVE_FCNTL_H
  17. #include <fcntl.h>
  18. #endif
  19. #ifdef HAVE_PTHREAD
  20. #include <pthread.h>
  21. #endif
  22. #ifdef HAVE_STDARG_H
  23. #include <stdarg.h>
  24. #endif
  25. #ifdef HAVE_STDDEF_H
  26. #include <stddef.h>
  27. #endif
  28. #ifdef HAVE_STRINGS_H
  29. #include <strings.h>
  30. #endif
  31. #ifdef HAVE_SYS_UTSNAME_H
  32. #include <sys/utsname.h>
  33. #endif
  34. #ifdef HAVE_NETINET_TCP_H
  35. #include <netinet/tcp.h>
  36. #endif
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #ifdef TIME_WITH_SYS_TIME
  41. # include <sys/time.h>
  42. # include <time.h>
  43. #else
  44. # ifdef HAVE_SYS_TIME_H
  45. # include <sys/time.h>
  46. # else
  47. # include <time.h>
  48. # endif
  49. #endif
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
  54. #define likely(__x) if((__x))
  55. #define unlikely(__x) if((__x))
  56. #else
  57. #define likely(__x) if(__builtin_expect((__x), 1))
  58. #define unlikely(__x) if(__builtin_expect((__x), 0))
  59. #endif
  60. /**
  61. * Add an object to a list.
  62. * @ingroup gearman_constants
  63. */
  64. #define GEARMAN_LIST_ADD(__list, __obj, __prefix) { \
  65. if (__list ## _list != NULL) \
  66. __list ## _list->__prefix ## prev= __obj; \
  67. __obj->__prefix ## next= __list ## _list; \
  68. __obj->__prefix ## prev= NULL; \
  69. __list ## _list= __obj; \
  70. __list ## _count++; \
  71. }
  72. /**
  73. * Delete an object from a list.
  74. * @ingroup gearman_constants
  75. */
  76. #define GEARMAN_LIST_DEL(__list, __obj, __prefix) { \
  77. if (__list ## _list == __obj) \
  78. __list ## _list= __obj->__prefix ## next; \
  79. if (__obj->__prefix ## prev != NULL) \
  80. __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
  81. if (__obj->__prefix ## next != NULL) \
  82. __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
  83. __list ## _count--; \
  84. }
  85. /**
  86. * Add an object to a fifo list.
  87. * @ingroup gearman_constants
  88. */
  89. #define GEARMAN_FIFO_ADD(__list, __obj, __prefix) { \
  90. if (__list ## _end == NULL) \
  91. __list ## _list= __obj; \
  92. else \
  93. __list ## _end->__prefix ## next= __obj; \
  94. __list ## _end= __obj; \
  95. __list ## _count++; \
  96. }
  97. /**
  98. * Delete an object from a fifo list.
  99. * @ingroup gearman_constants
  100. */
  101. #define GEARMAN_FIFO_DEL(__list, __obj, __prefix) { \
  102. __list ## _list= __obj->__prefix ## next; \
  103. if (__list ## _list == NULL) \
  104. __list ## _end= NULL; \
  105. __list ## _count--; \
  106. }
  107. /**
  108. * Add an object to a hash.
  109. * @ingroup gearman_constants
  110. */
  111. #define GEARMAN_HASH_ADD(__hash, __key, __obj, __prefix) { \
  112. if (__hash ## _hash[__key] != NULL) \
  113. __hash ## _hash[__key]->__prefix ## prev= __obj; \
  114. __obj->__prefix ## next= __hash ## _hash[__key]; \
  115. __obj->__prefix ## prev= NULL; \
  116. __hash ## _hash[__key]= __obj; \
  117. __hash ## _count++; \
  118. }
  119. /**
  120. * Delete an object from a hash.
  121. * @ingroup gearman_constants
  122. */
  123. #define GEARMAN_HASH_DEL(__hash, __key, __obj, __prefix) { \
  124. if (__hash ## _hash[__key] == __obj) \
  125. __hash ## _hash[__key]= __obj->__prefix ## next; \
  126. if (__obj->__prefix ## prev != NULL) \
  127. __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
  128. if (__obj->__prefix ## next != NULL) \
  129. __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
  130. __hash ## _count--; \
  131. }
  132. #define gearmand_array_size(__object) (sizeof((__object)) / sizeof(*(__object)))
  133. #ifdef __cplusplus
  134. }
  135. #endif