Merge in simple extrace for strings, style issues in io
[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 #ifdef __cplusplus
48 #include <cstdio>
49 #include <cstdlib>
50 #include <cstring>
51 #include <ctime>
52 #include <ctype.h>
53 #else
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #endif
59
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <limits.h>
63 #include <sys/types.h>
64 #include <unistd.h>
65
66 #include <libmemcached/memcached.h>
67 #include <libmemcached/watchpoint.h>
68 #include <libmemcached/is.h>
69 #include <libmemcached/namespace.h>
70
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74
75 typedef struct memcached_server_st * memcached_server_write_instance_st;
76
77 typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, memcached_server_write_instance_st server, void *context);
78
79 LIBMEMCACHED_LOCAL
80 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key);
81
82 LIBMEMCACHED_LOCAL
83 memcached_return_t memcached_server_execute(memcached_st *ptr,
84 memcached_server_execute_fn callback,
85 void *context);
86 #ifdef __cplusplus
87 } // extern "C"
88 #endif
89
90
91 /* These are private not to be installed headers */
92 #include <libmemcached/error.hpp>
93 #include <libmemcached/memory.h>
94 #include <libmemcached/io.h>
95 #ifdef __cplusplus
96 #include <libmemcached/string.hpp>
97 #include <libmemcached/io.hpp>
98 #include <libmemcached/do.hpp>
99 #endif
100 #include <libmemcached/internal.h>
101 #include <libmemcached/array.h>
102 #include <libmemcached/libmemcached_probes.h>
103 #include <libmemcached/memcached/protocol_binary.h>
104 #include <libmemcached/byteorder.h>
105 #include <libmemcached/initialize_query.h>
106 #include <libmemcached/response.h>
107 #include <libmemcached/namespace.h>
108
109 #ifdef __cplusplus
110 #include <libmemcached/backtrace.hpp>
111 #include <libmemcached/assert.hpp>
112 #endif
113
114 /* string value */
115 struct memcached_continuum_item_st
116 {
117 uint32_t index;
118 uint32_t value;
119 };
120
121 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
122
123 #define likely(x) if((x))
124 #define unlikely(x) if((x))
125
126 #else
127
128 #define likely(x) if(__builtin_expect((x) != 0, 1))
129 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
130 #endif
131
132 #define MEMCACHED_BLOCK_SIZE 1024
133 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
134 #define SMALL_STRING_LEN 1024
135 #define HUGE_STRING_LEN 8196
136
137 #ifdef __cplusplus
138 extern "C" {
139 #endif
140
141 LIBMEMCACHED_LOCAL
142 memcached_return_t memcached_connect(memcached_server_write_instance_st ptr);
143
144 LIBMEMCACHED_LOCAL
145 memcached_return_t run_distribution(memcached_st *ptr);
146
147 #define memcached_server_response_increment(A) (A)->cursor_active++
148 #define memcached_server_response_decrement(A) (A)->cursor_active--
149 #define memcached_server_response_reset(A) (A)->cursor_active=0
150
151 LIBMEMCACHED_LOCAL
152 void set_last_disconnected_host(memcached_server_write_instance_st ptr);
153
154 #ifdef __cplusplus
155 LIBMEMCACHED_LOCAL
156 memcached_return_t memcached_key_test(const memcached_st& memc,
157 const char * const *keys,
158 const size_t *key_length,
159 size_t number_of_keys);
160 #endif
161
162 LIBMEMCACHED_LOCAL
163 memcached_return_t memcached_purge(memcached_server_write_instance_st ptr);
164
165 LIBMEMCACHED_LOCAL
166 memcached_server_st *__server_create_with(const memcached_st *memc,
167 memcached_server_write_instance_st host,
168 const char *hostname,
169 in_port_t port,
170 uint32_t weight,
171 memcached_connection_t type);
172
173
174 static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
175 {
176 if (key_length == 0)
177 {
178 return MEMCACHED_BAD_KEY_PROVIDED;
179 }
180
181 if (binary)
182 {
183 if (key_length > 0xffff)
184 {
185 return MEMCACHED_BAD_KEY_PROVIDED;
186 }
187 }
188 else
189 {
190 if (key_length >= MEMCACHED_MAX_KEY)
191 {
192 return MEMCACHED_BAD_KEY_PROVIDED;
193 }
194 }
195
196 return MEMCACHED_SUCCESS;
197 }
198
199 #ifdef __cplusplus
200 }
201 #endif