Patches from Piotr Sikora < piotr.sikora@frickle.com > for OpenBSD.
[m6w6/libmemcached] / libmemcached / common.h
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
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 * Summary:
9 *
10 */
11
12 /*
13 Common include file for libmemached
14 */
15
16 #ifndef __LIBMEMCACHED_COMMON_H__
17 #define __LIBMEMCACHED_COMMON_H__
18
19 #include "config.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <strings.h>
25 #include <ctype.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <netdb.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <sys/un.h>
36 #include <netinet/tcp.h>
37 #ifdef TIME_WITH_SYS_TIME
38 # include <sys/time.h>
39 # include <time.h>
40 #else
41 # ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
43 # else
44 # include <time.h>
45 # endif
46 #endif
47
48 /* Define this here, which will turn on the visibilty controls while we're
49 * building libmemcached.
50 */
51 #define BUILDING_LIBMEMCACHED 1
52
53
54 #include "libmemcached/memcached.h"
55 #include "libmemcached/watchpoint.h"
56
57 typedef struct memcached_server_st memcached_server_instance_st;
58
59 /* These are private not to be installed headers */
60 #include "libmemcached/do.h"
61 #include "libmemcached/io.h"
62 #include "libmemcached/internal.h"
63 #include "libmemcached/libmemcached_probes.h"
64 #include "libmemcached/memcached/protocol_binary.h"
65 #include "libmemcached/byteorder.h"
66 #include "libmemcached/response.h"
67
68 /* string value */
69 struct memcached_continuum_item_st
70 {
71 uint32_t index;
72 uint32_t value;
73 };
74
75 /* Yum, Fortran.... can you make the reference? */
76 typedef enum {
77 MEM_NOT= -1,
78 MEM_FALSE= false,
79 MEM_TRUE= true,
80 } memcached_ternary_t;
81
82
83 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
84
85 #define likely(x) if((x))
86 #define unlikely(x) if((x))
87
88 #else
89
90 #define likely(x) if(__builtin_expect((x) != 0, 1))
91 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
92 #endif
93
94 #define MEMCACHED_BLOCK_SIZE 1024
95 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
96 #define SMALL_STRING_LEN 1024
97 #define HUGE_STRING_LEN 8196
98
99 #ifdef __cplusplus
100 extern "C" {
101 #endif
102
103 LIBMEMCACHED_LOCAL
104 memcached_return_t memcached_connect(memcached_server_instance_st *ptr);
105 LIBMEMCACHED_LOCAL
106 void memcached_quit_server(memcached_server_instance_st *ptr, uint8_t io_death);
107
108 LIBMEMCACHED_LOCAL
109 memcached_return_t run_distribution(memcached_st *ptr);
110
111 #define memcached_server_response_increment(A) (A)->cursor_active++
112 #define memcached_server_response_decrement(A) (A)->cursor_active--
113 #define memcached_server_response_reset(A) (A)->cursor_active=0
114
115 // These are private
116 #define memcached_is_allocated(__object) ((__object)->options.is_allocated)
117 #define memcached_is_initialized(__object) ((__object)->options.is_initialized)
118 #define memcached_is_purging(__object) ((__object)->state.is_purging)
119 #define memcached_is_processing_input(__object) ((__object)->state.is_processing_input)
120 #define memcached_set_purging(__object, __value) ((__object)->state.is_purging= (__value))
121 #define memcached_set_processing_input(__object, __value) ((__object)->state.is_processing_input= (__value))
122 #define memcached_set_initialized(__object, __value) ((__object)->options.is_initialized(= (__value))
123 #define memcached_set_allocated(__object, __value) ((__object)->options.is_allocated(= (__value))
124
125
126 LIBMEMCACHED_LOCAL
127 memcached_return_t value_fetch(memcached_server_instance_st *ptr,
128 char *buffer,
129 memcached_result_st *result);
130 LIBMEMCACHED_LOCAL
131 void server_list_free(memcached_st *ptr, memcached_server_instance_st *servers);
132
133 LIBMEMCACHED_LOCAL
134 memcached_return_t memcached_key_test(const char * const *keys,
135 const size_t *key_length,
136 size_t number_of_keys);
137
138
139 LIBMEMCACHED_LOCAL
140 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length);
141
142 LIBMEMCACHED_LOCAL
143 memcached_return_t memcached_purge(memcached_server_instance_st *ptr);
144
145 static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
146 {
147 unlikely (key_length == 0)
148 return MEMCACHED_BAD_KEY_PROVIDED;
149
150 if (binary)
151 {
152 unlikely (key_length > 0xffff)
153 return MEMCACHED_BAD_KEY_PROVIDED;
154 }
155 else
156 {
157 unlikely (key_length >= MEMCACHED_MAX_KEY)
158 return MEMCACHED_BAD_KEY_PROVIDED;
159 }
160
161 return MEMCACHED_SUCCESS;
162 }
163
164 #ifdef TCP_CORK
165 #define CORK TCP_CORK
166 #elif defined TCP_NOPUSH
167 #define CORK TCP_NOPUSH
168 #endif
169
170 /*
171 test_cork() tries to enable TCP_CORK. IF TCP_CORK is not an option
172 on the system it returns false but sets errno to 0. Otherwise on
173 failure errno is set.
174 */
175 static inline memcached_ternary_t test_cork(memcached_server_st *ptr, int enable)
176 {
177 #ifdef CORK
178 if (ptr->type != MEMCACHED_CONNECTION_TCP)
179 return MEM_FALSE;
180
181 int err= setsockopt(ptr->fd, IPPROTO_TCP, CORK,
182 &enable, (socklen_t)sizeof(int));
183 if (! err)
184 {
185 return MEM_TRUE;
186 }
187
188 perror(strerror(errno));
189 ptr->cached_errno= errno;
190
191 return MEM_FALSE;
192 #else
193 (void)ptr;
194 (void)enable;
195
196 ptr->cached_errno= 0;
197
198 return MEM_NOT;
199 #endif
200 }
201
202 static inline void libmemcached_free(const memcached_st *ptr, void *mem)
203 {
204 ptr->allocators.free(ptr, mem, ptr->allocators.context);
205 }
206
207 static inline void *libmemcached_malloc(const memcached_st *ptr, const size_t size)
208 {
209 return ptr->allocators.malloc(ptr, size, ptr->allocators.context);
210 }
211
212 static inline void *libmemcached_realloc(const memcached_st *ptr, void *mem, const size_t size)
213 {
214 return ptr->allocators.realloc(ptr, mem, size, ptr->allocators.context);
215 }
216
217 static inline void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size)
218 {
219 return ptr->allocators.calloc(ptr, nelem, size, ptr->allocators.context);
220 }
221
222 #ifdef __cplusplus
223 }
224 #endif
225
226 #endif /* __LIBMEMCACHED_COMMON_H__ */