Fix prefix key so that it is not statically built.
[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 <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/array.h"
70 #include "libmemcached/libmemcached_probes.h"
71 #include "libmemcached/memcached/protocol_binary.h"
72 #include "libmemcached/byteorder.h"
73 #include "libmemcached/response.h"
74
75 /* string value */
76 struct memcached_continuum_item_st
77 {
78 uint32_t index;
79 uint32_t value;
80 };
81
82 /* Yum, Fortran.... can you make the reference? */
83 typedef enum {
84 MEM_NOT= -1,
85 MEM_FALSE= false,
86 MEM_TRUE= true
87 } memcached_ternary_t;
88
89
90 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
91
92 #define likely(x) if((x))
93 #define unlikely(x) if((x))
94
95 #else
96
97 #define likely(x) if(__builtin_expect((x) != 0, 1))
98 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
99 #endif
100
101 #define MEMCACHED_BLOCK_SIZE 1024
102 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
103 #define SMALL_STRING_LEN 1024
104 #define HUGE_STRING_LEN 8196
105
106 #ifdef __cplusplus
107 extern "C" {
108 #endif
109
110 LIBMEMCACHED_LOCAL
111 memcached_return_t memcached_connect(memcached_server_write_instance_st ptr);
112
113 LIBMEMCACHED_LOCAL
114 memcached_return_t run_distribution(memcached_st *ptr);
115
116 #define memcached_server_response_increment(A) (A)->cursor_active++
117 #define memcached_server_response_decrement(A) (A)->cursor_active--
118 #define memcached_server_response_reset(A) (A)->cursor_active=0
119
120 LIBMEMCACHED_LOCAL
121 void set_last_disconnected_host(memcached_server_write_instance_st ptr);
122
123 LIBMEMCACHED_LOCAL
124 memcached_return_t memcached_key_test(const char * const *keys,
125 const size_t *key_length,
126 size_t number_of_keys);
127
128 LIBMEMCACHED_LOCAL
129 memcached_return_t memcached_purge(memcached_server_write_instance_st ptr);
130
131 LIBMEMCACHED_LOCAL
132 memcached_server_st *memcached_server_create_with(const memcached_st *memc,
133 memcached_server_write_instance_st host,
134 const char *hostname,
135 in_port_t port,
136 uint32_t weight,
137 memcached_connection_t type);
138
139
140 static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
141 {
142 unlikely (key_length == 0)
143 return MEMCACHED_BAD_KEY_PROVIDED;
144
145 if (binary)
146 {
147 unlikely (key_length > 0xffff)
148 return MEMCACHED_BAD_KEY_PROVIDED;
149 }
150 else
151 {
152 unlikely (key_length >= MEMCACHED_MAX_KEY)
153 return MEMCACHED_BAD_KEY_PROVIDED;
154 }
155
156 return MEMCACHED_SUCCESS;
157 }
158
159 #ifdef TCP_CORK
160 #define CORK TCP_CORK
161 #elif defined TCP_NOPUSH
162 #define CORK TCP_NOPUSH
163 #endif
164
165 /*
166 test_cork() tries to enable TCP_CORK. IF TCP_CORK is not an option
167 on the system it returns false but sets errno to 0. Otherwise on
168 failure errno is set.
169 */
170 static inline memcached_ternary_t test_cork(memcached_server_st *ptr, int enable)
171 {
172 #ifdef CORK
173 if (ptr->type != MEMCACHED_CONNECTION_TCP)
174 return MEM_FALSE;
175
176 int err= setsockopt(ptr->fd, IPPROTO_TCP, CORK,
177 &enable, (socklen_t)sizeof(int));
178 if (! err)
179 {
180 return MEM_TRUE;
181 }
182
183 perror(strerror(errno));
184 ptr->cached_errno= errno;
185
186 return MEM_FALSE;
187 #else
188 (void)ptr;
189 (void)enable;
190
191 ptr->cached_errno= 0;
192
193 return MEM_NOT;
194 #endif
195 }
196
197 static inline void libmemcached_free(const memcached_st *ptr, void *mem)
198 {
199 ptr->allocators.free(ptr, mem, ptr->allocators.context);
200 }
201
202 static inline void *libmemcached_malloc(const memcached_st *ptr, const size_t size)
203 {
204 return ptr->allocators.malloc(ptr, size, ptr->allocators.context);
205 }
206
207 static inline void *libmemcached_realloc(const memcached_st *ptr, void *mem, const size_t size)
208 {
209 return ptr->allocators.realloc(ptr, mem, size, ptr->allocators.context);
210 }
211
212 static inline void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size)
213 {
214 return ptr->allocators.calloc(ptr, nelem, size, ptr->allocators.context);
215 }
216
217 #ifdef __cplusplus
218 }
219 #endif
220
221 #endif /* __LIBMEMCACHED_COMMON_H__ */