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