Updating tree for new function work in libhashkit.
[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/io.h"
61 #include "libmemcached/internal.h"
62 #include "libmemcached/libmemcached_probes.h"
63 #include "libmemcached/memcached/protocol_binary.h"
64 #include "libmemcached/byteorder.h"
65 #include "libmemcached/response.h"
66
67 /* string value */
68 struct memcached_continuum_item_st
69 {
70 uint32_t index;
71 uint32_t value;
72 };
73
74 /* Yum, Fortran.... can you make the reference? */
75 typedef enum {
76 MEM_NOT= -1,
77 MEM_FALSE= false,
78 MEM_TRUE= true,
79 } memcached_ternary_t;
80
81
82 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
83
84 #define likely(x) if((x))
85 #define unlikely(x) if((x))
86
87 #else
88
89 #define likely(x) if(__builtin_expect((x) != 0, 1))
90 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
91 #endif
92
93 #define MEMCACHED_BLOCK_SIZE 1024
94 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
95 #define SMALL_STRING_LEN 1024
96 #define HUGE_STRING_LEN 8196
97
98 /* Hashing algo */
99
100 LIBMEMCACHED_LOCAL
101 void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result);
102 LIBMEMCACHED_LOCAL
103 uint32_t hash_crc32(const char *data,
104 size_t data_len);
105 #ifdef HAVE_HSIEH_HASH
106 LIBMEMCACHED_LOCAL
107 uint32_t hsieh_hash(const char *key, size_t key_length);
108 #endif
109 LIBMEMCACHED_LOCAL
110 uint32_t murmur_hash(const char *key, size_t key_length);
111 LIBMEMCACHED_LOCAL
112 uint32_t jenkins_hash(const void *key, size_t length, uint32_t initval);
113
114 LIBMEMCACHED_LOCAL
115 memcached_return_t memcached_connect(memcached_server_instance_st *ptr);
116 LIBMEMCACHED_LOCAL
117 void memcached_quit_server(memcached_server_instance_st *ptr, uint8_t io_death);
118
119 LIBMEMCACHED_LOCAL
120 memcached_return_t run_distribution(memcached_st *ptr);
121
122 #define memcached_server_response_increment(A) (A)->cursor_active++
123 #define memcached_server_response_decrement(A) (A)->cursor_active--
124 #define memcached_server_response_reset(A) (A)->cursor_active=0
125
126 // These are private
127 #define memcached_is_allocated(__object) ((__object)->options.is_allocated)
128 #define memcached_is_initialized(__object) ((__object)->options.is_initialized)
129 #define memcached_is_purging(__object) ((__object)->state.is_purging)
130 #define memcached_is_processing_input(__object) ((__object)->state.is_processing_input)
131 #define memcached_set_purging(__object, __value) ((__object)->state.is_purging= (__value))
132 #define memcached_set_processing_input(__object, __value) ((__object)->state.is_processing_input= (__value))
133 #define memcached_set_initialized(__object, __value) ((__object)->options.is_initialized(= (__value))
134 #define memcached_set_allocated(__object, __value) ((__object)->options.is_allocated(= (__value))
135
136
137 LIBMEMCACHED_LOCAL
138 memcached_return_t memcached_do(memcached_server_instance_st *ptr, const void *commmand,
139 size_t command_length, uint8_t with_flush);
140 LIBMEMCACHED_LOCAL
141 memcached_return_t value_fetch(memcached_server_instance_st *ptr,
142 char *buffer,
143 memcached_result_st *result);
144 LIBMEMCACHED_LOCAL
145 void server_list_free(memcached_st *ptr, memcached_server_instance_st *servers);
146
147 LIBMEMCACHED_LOCAL
148 memcached_return_t memcached_key_test(const char * const *keys,
149 const size_t *key_length,
150 size_t number_of_keys);
151
152
153 LIBMEMCACHED_LOCAL
154 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length);
155
156 LIBMEMCACHED_LOCAL
157 memcached_return_t memcached_purge(memcached_server_instance_st *ptr);
158
159 static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
160 {
161 unlikely (key_length == 0)
162 return MEMCACHED_BAD_KEY_PROVIDED;
163
164 if (binary)
165 {
166 unlikely (key_length > 0xffff)
167 return MEMCACHED_BAD_KEY_PROVIDED;
168 }
169 else
170 {
171 unlikely (key_length >= MEMCACHED_MAX_KEY)
172 return MEMCACHED_BAD_KEY_PROVIDED;
173 }
174
175 return MEMCACHED_SUCCESS;
176 }
177
178 #ifdef TCP_CORK
179 #define CORK TCP_CORK
180 #elif defined TCP_NOPUSH
181 #define CORK TCP_NOPUSH
182 #endif
183
184 /*
185 test_cork() tries to enable TCP_CORK. IF TCP_CORK is not an option
186 on the system it returns false but sets errno to 0. Otherwise on
187 failure errno is set.
188 */
189 static inline memcached_ternary_t test_cork(memcached_server_st *ptr, int enable)
190 {
191 #ifdef CORK
192 if (ptr->type != MEMCACHED_CONNECTION_TCP)
193 return MEM_FALSE;
194
195 int err= setsockopt(ptr->fd, IPPROTO_TCP, CORK,
196 &enable, (socklen_t)sizeof(int));
197 if (! err)
198 {
199 return MEM_TRUE;
200 }
201
202 perror(strerror(errno));
203 ptr->cached_errno= errno;
204
205 return MEM_FALSE;
206 #else
207 (void)ptr;
208 (void)enable;
209
210 ptr->cached_errno= 0;
211
212 return MEM_NOT;
213 #endif
214 }
215
216 static inline void libmemcached_free(const memcached_st *ptr, void *mem)
217 {
218 ptr->allocators.free(ptr, mem, ptr->allocators.context);
219 }
220
221 static inline void *libmemcached_malloc(const memcached_st *ptr, const size_t size)
222 {
223 return ptr->allocators.malloc(ptr, size, ptr->allocators.context);
224 }
225
226 static inline void *libmemcached_realloc(const memcached_st *ptr, void *mem, const size_t size)
227 {
228 return ptr->allocators.realloc(ptr, mem, size, ptr->allocators.context);
229 }
230
231 static inline void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size)
232 {
233 return ptr->allocators.calloc(ptr, nelem, size, ptr->allocators.context);
234 }
235
236
237 #endif /* LIBMEMCACHED_COMMON_H */