Import parser/etc
[awesomized/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 <unistd.h>
28 #include <limits.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #ifdef TIME_WITH_SYS_TIME
32 # include <sys/time.h>
33 # include <time.h>
34 #else
35 # ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
37 # else
38 # include <time.h>
39 # endif
40 #endif
41
42 /* Define this here, which will turn on the visibilty controls while we're
43 * building libmemcached.
44 */
45 #define BUILDING_LIBMEMCACHED 1
46
47
48 #include "libmemcached/memcached.h"
49 #include "libmemcached/watchpoint.h"
50 #include "libmemcached/is.h"
51
52 typedef struct memcached_server_st * memcached_server_write_instance_st;
53
54 typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, memcached_server_write_instance_st server, void *context);
55
56 LIBMEMCACHED_LOCAL
57 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key);
58
59 LIBMEMCACHED_LOCAL
60 memcached_return_t memcached_server_execute(memcached_st *ptr,
61 memcached_server_execute_fn callback,
62 void *context);
63
64
65 /* These are private not to be installed headers */
66 #include "libmemcached/io.h"
67 #include "libmemcached/do.h"
68 #include "libmemcached/internal.h"
69 #include "libmemcached/libmemcached_probes.h"
70 #include "libmemcached/memcached/protocol_binary.h"
71 #include "libmemcached/byteorder.h"
72 #include "libmemcached/response.h"
73
74 /* string value */
75 struct memcached_continuum_item_st
76 {
77 uint32_t index;
78 uint32_t value;
79 };
80
81 /* Yum, Fortran.... can you make the reference? */
82 typedef enum {
83 MEM_NOT= -1,
84 MEM_FALSE= false,
85 MEM_TRUE= true
86 } memcached_ternary_t;
87
88
89 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
90
91 #define likely(x) if((x))
92 #define unlikely(x) if((x))
93
94 #else
95
96 #define likely(x) if(__builtin_expect((x) != 0, 1))
97 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
98 #endif
99
100 #define MEMCACHED_BLOCK_SIZE 1024
101 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
102 #define SMALL_STRING_LEN 1024
103 #define HUGE_STRING_LEN 8196
104
105 #ifdef __cplusplus
106 extern "C" {
107 #endif
108
109 LIBMEMCACHED_LOCAL
110 memcached_return_t memcached_connect(memcached_server_write_instance_st ptr);
111
112 LIBMEMCACHED_LOCAL
113 memcached_return_t run_distribution(memcached_st *ptr);
114
115 #define memcached_server_response_increment(A) (A)->cursor_active++
116 #define memcached_server_response_decrement(A) (A)->cursor_active--
117 #define memcached_server_response_reset(A) (A)->cursor_active=0
118
119 LIBMEMCACHED_LOCAL
120 void set_last_disconnected_host(memcached_server_write_instance_st ptr);
121
122 LIBMEMCACHED_LOCAL
123 memcached_return_t memcached_key_test(const char * const *keys,
124 const size_t *key_length,
125 size_t number_of_keys);
126
127 LIBMEMCACHED_LOCAL
128 memcached_return_t memcached_purge(memcached_server_write_instance_st ptr);
129
130 LIBMEMCACHED_LOCAL
131 memcached_server_st *memcached_server_create_with(const memcached_st *memc,
132 memcached_server_write_instance_st host,
133 const char *hostname,
134 in_port_t port,
135 uint32_t weight,
136 memcached_connection_t type);
137
138
139 static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
140 {
141 unlikely (key_length == 0)
142 return MEMCACHED_BAD_KEY_PROVIDED;
143
144 if (binary)
145 {
146 unlikely (key_length > 0xffff)
147 return MEMCACHED_BAD_KEY_PROVIDED;
148 }
149 else
150 {
151 unlikely (key_length >= MEMCACHED_MAX_KEY)
152 return MEMCACHED_BAD_KEY_PROVIDED;
153 }
154
155 return MEMCACHED_SUCCESS;
156 }
157
158 #ifdef TCP_CORK
159 #define CORK TCP_CORK
160 #elif defined TCP_NOPUSH
161 #define CORK TCP_NOPUSH
162 #endif
163
164 /*
165 test_cork() tries to enable TCP_CORK. IF TCP_CORK is not an option
166 on the system it returns false but sets errno to 0. Otherwise on
167 failure errno is set.
168 */
169 static inline memcached_ternary_t test_cork(memcached_server_st *ptr, int enable)
170 {
171 #ifdef CORK
172 if (ptr->type != MEMCACHED_CONNECTION_TCP)
173 return MEM_FALSE;
174
175 int err= setsockopt(ptr->fd, IPPROTO_TCP, CORK,
176 &enable, (socklen_t)sizeof(int));
177 if (! err)
178 {
179 return MEM_TRUE;
180 }
181
182 perror(strerror(errno));
183 ptr->cached_errno= errno;
184
185 return MEM_FALSE;
186 #else
187 (void)ptr;
188 (void)enable;
189
190 ptr->cached_errno= 0;
191
192 return MEM_NOT;
193 #endif
194 }
195
196 static inline void libmemcached_free(const memcached_st *ptr, void *mem)
197 {
198 ptr->allocators.free(ptr, mem, ptr->allocators.context);
199 }
200
201 static inline void *libmemcached_malloc(const memcached_st *ptr, const size_t size)
202 {
203 return ptr->allocators.malloc(ptr, size, ptr->allocators.context);
204 }
205
206 static inline void *libmemcached_realloc(const memcached_st *ptr, void *mem, const size_t size)
207 {
208 return ptr->allocators.realloc(ptr, mem, size, ptr->allocators.context);
209 }
210
211 static inline void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size)
212 {
213 return ptr->allocators.calloc(ptr, nelem, size, ptr->allocators.context);
214 }
215
216 #ifdef __cplusplus
217 }
218 #endif
219
220 #endif /* __LIBMEMCACHED_COMMON_H__ */