Add support for query_id, and fixes a few cases where programmer error can
[awesomized/libmemcached] / libmemcached / common.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * LibMemcached
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following disclaimer
18 * in the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * * The names of its contributors may not be used to endorse or
22 * promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
38
39 /*
40 Common include file for libmemached
41 */
42
43 #pragma once
44
45 #include <config.h>
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <strings.h>
51 #include <ctype.h>
52 #include <sys/types.h>
53 #include <unistd.h>
54 #include <limits.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #ifdef TIME_WITH_SYS_TIME
58 # include <sys/time.h>
59 # include <time.h>
60 #else
61 # ifdef HAVE_SYS_TIME_H
62 # include <sys/time.h>
63 # else
64 # include <time.h>
65 # endif
66 #endif
67
68 /* Define this here, which will turn on the visibilty controls while we're
69 * building libmemcached.
70 */
71 #define BUILDING_LIBMEMCACHED 1
72
73
74 #include <libmemcached/memcached.h>
75 #include <libmemcached/watchpoint.h>
76 #include <libmemcached/is.h>
77 #include <libmemcached/prefix_key.h>
78
79 typedef struct memcached_server_st * memcached_server_write_instance_st;
80
81 typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, memcached_server_write_instance_st server, void *context);
82
83 LIBMEMCACHED_LOCAL
84 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key);
85
86 LIBMEMCACHED_LOCAL
87 memcached_return_t memcached_server_execute(memcached_st *ptr,
88 memcached_server_execute_fn callback,
89 void *context);
90
91
92 /* These are private not to be installed headers */
93 #include <libmemcached/io.h>
94 #include <libmemcached/do.h>
95 #include <libmemcached/internal.h>
96 #include <libmemcached/array.h>
97 #include <libmemcached/libmemcached_probes.h>
98 #include <libmemcached/memcached/protocol_binary.h>
99 #include <libmemcached/byteorder.h>
100 #include <libmemcached/initialize_query.h>
101 #include <libmemcached/response.h>
102 #include <libmemcached/prefix_key.h>
103
104 /* string value */
105 struct memcached_continuum_item_st
106 {
107 uint32_t index;
108 uint32_t value;
109 };
110
111 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
112
113 #define likely(x) if((x))
114 #define unlikely(x) if((x))
115
116 #else
117
118 #define likely(x) if(__builtin_expect((x) != 0, 1))
119 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
120 #endif
121
122 #define MEMCACHED_BLOCK_SIZE 1024
123 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
124 #define SMALL_STRING_LEN 1024
125 #define HUGE_STRING_LEN 8196
126
127 #ifdef __cplusplus
128 extern "C" {
129 #endif
130
131 LIBMEMCACHED_LOCAL
132 memcached_return_t memcached_connect(memcached_server_write_instance_st ptr);
133
134 LIBMEMCACHED_LOCAL
135 memcached_return_t run_distribution(memcached_st *ptr);
136
137 #define memcached_server_response_increment(A) (A)->cursor_active++
138 #define memcached_server_response_decrement(A) (A)->cursor_active--
139 #define memcached_server_response_reset(A) (A)->cursor_active=0
140
141 LIBMEMCACHED_LOCAL
142 void set_last_disconnected_host(memcached_server_write_instance_st ptr);
143
144 LIBMEMCACHED_LOCAL
145 memcached_return_t memcached_key_test(const char * const *keys,
146 const size_t *key_length,
147 size_t number_of_keys);
148
149 LIBMEMCACHED_LOCAL
150 memcached_return_t memcached_purge(memcached_server_write_instance_st ptr);
151
152 LIBMEMCACHED_LOCAL
153 memcached_server_st *memcached_server_create_with(const memcached_st *memc,
154 memcached_server_write_instance_st host,
155 const char *hostname,
156 in_port_t port,
157 uint32_t weight,
158 memcached_connection_t type);
159
160
161 static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
162 {
163 unlikely (key_length == 0)
164 return MEMCACHED_BAD_KEY_PROVIDED;
165
166 if (binary)
167 {
168 unlikely (key_length > 0xffff)
169 return MEMCACHED_BAD_KEY_PROVIDED;
170 }
171 else
172 {
173 unlikely (key_length >= MEMCACHED_MAX_KEY)
174 return MEMCACHED_BAD_KEY_PROVIDED;
175 }
176
177 return MEMCACHED_SUCCESS;
178 }
179
180 static inline void libmemcached_free(const memcached_st *ptr, void *mem)
181 {
182 ptr->allocators.free(ptr, mem, ptr->allocators.context);
183 }
184
185 static inline void *libmemcached_malloc(const memcached_st *ptr, const size_t size)
186 {
187 return ptr->allocators.malloc(ptr, size, ptr->allocators.context);
188 }
189
190 static inline void *libmemcached_realloc(const memcached_st *ptr, void *mem, const size_t size)
191 {
192 return ptr->allocators.realloc(ptr, mem, size, ptr->allocators.context);
193 }
194
195 static inline void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size)
196 {
197 return ptr->allocators.calloc(ptr, nelem, size, ptr->allocators.context);
198 }
199
200 #ifdef __cplusplus
201 }
202 #endif