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 /**
40  * @file
41  * @brief System Include Files
42  */
43 
44 #pragma once
45 
46 #include <libgearman-server/gearmand.h>
47 #include <libgearman-server/byteorder.h>
48 
49 #include "libgearman/assert.hpp"
50 
51 #ifdef HAVE_FCNTL_H
52 # include <fcntl.h>
53 #endif
54 #ifdef HAVE_PTHREAD
55 # include <pthread.h>
56 #endif
57 #ifdef HAVE_STDARG_H
58 # include <stdarg.h>
59 #endif
60 #ifdef HAVE_STDDEF_H
61 # include <stddef.h>
62 #endif
63 #ifdef HAVE_STRINGS_H
64 # include <strings.h>
65 #endif
66 #ifdef HAVE_SYS_UTSNAME_H
67 # include <sys/utsname.h>
68 #endif
69 #ifdef HAVE_NETINET_TCP_H
70 # include <netinet/tcp.h>
71 #endif
72 #ifdef HAVE_UNISTD_H
73 # include <unistd.h>
74 #endif
75 
76 #ifdef TIME_WITH_SYS_TIME
77 # include <sys/time.h>
78 # include <time.h>
79 #else
80 # ifdef HAVE_SYS_TIME_H
81 #  include <sys/time.h>
82 # else
83 #  include <time.h>
84 # endif
85 #endif
86 
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90 
91 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
92 #define likely(__x) if((__x))
93 #define unlikely(__x) if((__x))
94 #else
95 #define likely(__x) if(__builtin_expect((__x), 1))
96 #define unlikely(__x) if(__builtin_expect((__x), 0))
97 #endif
98 
99 /**
100  * Add an object to a list.
101  * @ingroup gearman_constants
102  */
103 #define GEARMAN_LIST_ADD(__list, __obj, __prefix) { \
104   if (__list ## _list != NULL) \
105     __list ## _list->__prefix ## prev= __obj; \
106   __obj->__prefix ## next= __list ## _list; \
107   __obj->__prefix ## prev= NULL; \
108   __list ## _list= __obj; \
109   __list ## _count++; \
110 }
111 
112 #define GEARMAN_LIST__ADD(__list, __obj) { \
113   if (__list ## _list != NULL) \
114     __list ## _list->prev= __obj; \
115   __obj->next= __list ## _list; \
116   __obj->prev= NULL; \
117   __list ## _list= __obj; \
118   __list ## _count++; \
119 }
120 
121 /**
122  * Delete an object from a list.
123  * @ingroup gearman_constants
124  */
125 #define GEARMAN_LIST_DEL(__list, __obj, __prefix) { \
126   if (__list ## _list == __obj) \
127     __list ## _list= __obj->__prefix ## next; \
128   if (__obj->__prefix ## prev != NULL) \
129     __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
130   if (__obj->__prefix ## next != NULL) \
131     __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
132   __list ## _count--; \
133 }
134 
135 #define GEARMAN_LIST__DEL(__list, __obj) { \
136   if (__list ## _list == __obj) \
137     __list ## _list= __obj->next; \
138   if (__obj->prev != NULL) \
139     __obj->prev->next= __obj->next; \
140   if (__obj->next != NULL) \
141     __obj->next->prev= __obj->prev; \
142   __list ## _count--; \
143 }
144 
145 /**
146  * Add an object to a fifo list.
147  * @ingroup gearman_constants
148  */
149 #define GEARMAN_FIFO_ADD(__list, __obj, __prefix) { \
150   if (__list ## _end == NULL) \
151     __list ## _list= __obj; \
152   else \
153     __list ## _end->__prefix ## next= __obj; \
154   __list ## _end= __obj; \
155   __list ## _count++; \
156 }
157 
158 #define GEARMAN_FIFO__ADD(__list, __obj) { \
159   if (__list ## _end == NULL) \
160     __list ## _list= __obj; \
161   else \
162     __list ## _end->next= __obj; \
163   __list ## _end= __obj; \
164   __list ## _count++; \
165 }
166 
167 /**
168  * Delete an object from a fifo list.
169  * @ingroup gearman_constants
170  */
171 #define GEARMAN_FIFO_DEL(__list, __obj, __prefix) { \
172   __list ## _list= __obj->__prefix ## next; \
173   if (__list ## _list == NULL) \
174     __list ## _end= NULL; \
175   __list ## _count--; \
176 }
177 
178 #define GEARMAN_FIFO__DEL(__list, __obj) { \
179   __list ## _list= __obj->next; \
180   if (__list ## _list == NULL) \
181     __list ## _end= NULL; \
182   __list ## _count--; \
183 }
184 
185 /**
186  * Add an object to a hash.
187  * @ingroup gearman_constants
188  */
189 #define GEARMAN_HASH_ADD(__hash, __key, __obj, __prefix) { \
190   if (__hash ## _hash[__key] != NULL) \
191     __hash ## _hash[__key]->__prefix ## prev= __obj; \
192   __obj->__prefix ## next= __hash ## _hash[__key]; \
193   __obj->__prefix ## prev= NULL; \
194   __hash ## _hash[__key]= __obj; \
195   __hash ## _count++; \
196 }
197 
198 #define GEARMAN_HASH__ADD(__hash, __key, __obj) { \
199   if (__hash ## _hash[__key] != NULL) \
200     __hash ## _hash[__key]->prev= __obj; \
201   __obj->next= __hash ## _hash[__key]; \
202   __obj->prev= NULL; \
203   __hash ## _hash[__key]= __obj; \
204   __hash ## _count++; \
205 }
206 
207 /**
208  * Delete an object from a hash.
209  * @ingroup gearman_constants
210  */
211 #define GEARMAN_HASH_DEL(__hash, __key, __obj, __prefix) { \
212   if (__hash ## _hash[__key] == __obj) \
213     __hash ## _hash[__key]= __obj->__prefix ## next; \
214   if (__obj->__prefix ## prev != NULL) \
215     __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
216   if (__obj->__prefix ## next != NULL) \
217     __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
218   __hash ## _count--; \
219 }
220 
221 #define GEARMAN_HASH__DEL(__hash, __key, __obj) { \
222   if (__hash ## _hash[__key] == __obj) \
223     __hash ## _hash[__key]= __obj->next; \
224   if (__obj->prev != NULL) \
225     __obj->prev->next= __obj->next; \
226   if (__obj->next != NULL) \
227     __obj->next->prev= __obj->prev; \
228   __hash ## _count--; \
229 }
230 
231 #define gearmand_array_size(__object) (sizeof((__object)) / sizeof(*(__object)))
232 
233 #ifdef __cplusplus
234 }
235 #endif
236 
237 #ifdef NI_MAXHOST
238 #define GEARMAND_NI_MAXHOST NI_MAXHOST
239 #else
240 #define GEARMAND_NI_MAXHOST 1025
241 #endif
242 
243 #ifdef NI_MAXSERV
244 #define GEARMAND_NI_MAXSERV NI_MAXSERV
245 #else
246 #define GEARMAND_NI_MAXSERV 32
247 #endif
248