2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
13 Common include file for libmemached
16 #ifndef __LIBMEMCACHED_COMMON_H__
17 #define __LIBMEMCACHED_COMMON_H__
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
36 #include <netinet/tcp.h>
37 #ifdef TIME_WITH_SYS_TIME
38 # include <sys/time.h>
41 # ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
48 /* Define this here, which will turn on the visibilty controls while we're
49 * building libmemcached.
51 #define BUILDING_LIBMEMCACHED 1
54 #include "libmemcached/memcached.h"
55 #include "libmemcached/watchpoint.h"
57 typedef struct memcached_server_st
* memcached_server_write_instance_st
;
60 memcached_server_write_instance_st
memcached_server_instance_fetch(memcached_st
*ptr
, uint32_t server_key
);
62 /* These are private not to be installed headers */
63 #include "libmemcached/do.h"
64 #include "libmemcached/io.h"
65 #include "libmemcached/internal.h"
66 #include "libmemcached/libmemcached_probes.h"
67 #include "libmemcached/memcached/protocol_binary.h"
68 #include "libmemcached/byteorder.h"
69 #include "libmemcached/response.h"
72 struct memcached_continuum_item_st
78 /* Yum, Fortran.... can you make the reference? */
83 } memcached_ternary_t
;
86 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
88 #define likely(x) if((x))
89 #define unlikely(x) if((x))
93 #define likely(x) if(__builtin_expect((x) != 0, 1))
94 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
97 #define MEMCACHED_BLOCK_SIZE 1024
98 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
99 #define SMALL_STRING_LEN 1024
100 #define HUGE_STRING_LEN 8196
107 memcached_return_t
memcached_connect(memcached_server_write_instance_st ptr
);
110 memcached_return_t
run_distribution(memcached_st
*ptr
);
112 #define memcached_server_response_increment(A) (A)->cursor_active++
113 #define memcached_server_response_decrement(A) (A)->cursor_active--
114 #define memcached_server_response_reset(A) (A)->cursor_active=0
117 #define memcached_is_allocated(__object) ((__object)->options.is_allocated)
118 #define memcached_is_initialized(__object) ((__object)->options.is_initialized)
119 #define memcached_is_purging(__object) ((__object)->state.is_purging)
120 #define memcached_is_processing_input(__object) ((__object)->state.is_processing_input)
121 #define memcached_set_purging(__object, __value) ((__object)->state.is_purging= (__value))
122 #define memcached_set_processing_input(__object, __value) ((__object)->state.is_processing_input= (__value))
123 #define memcached_set_initialized(__object, __value) ((__object)->options.is_initialized(= (__value))
124 #define memcached_set_allocated(__object, __value) ((__object)->options.is_allocated(= (__value))
128 memcached_return_t
memcached_key_test(const char * const *keys
,
129 const size_t *key_length
,
130 size_t number_of_keys
);
133 memcached_return_t
memcached_purge(memcached_server_write_instance_st ptr
);
136 memcached_server_st
*memcached_server_create_with(const memcached_st
*memc
,
137 memcached_server_write_instance_st host
,
138 const char *hostname
,
141 memcached_connection_t type
);
144 static inline memcached_return_t
memcached_validate_key_length(size_t key_length
, bool binary
)
146 unlikely (key_length
== 0)
147 return MEMCACHED_BAD_KEY_PROVIDED
;
151 unlikely (key_length
> 0xffff)
152 return MEMCACHED_BAD_KEY_PROVIDED
;
156 unlikely (key_length
>= MEMCACHED_MAX_KEY
)
157 return MEMCACHED_BAD_KEY_PROVIDED
;
160 return MEMCACHED_SUCCESS
;
164 #define CORK TCP_CORK
165 #elif defined TCP_NOPUSH
166 #define CORK TCP_NOPUSH
170 test_cork() tries to enable TCP_CORK. IF TCP_CORK is not an option
171 on the system it returns false but sets errno to 0. Otherwise on
172 failure errno is set.
174 static inline memcached_ternary_t
test_cork(memcached_server_st
*ptr
, int enable
)
177 if (ptr
->type
!= MEMCACHED_CONNECTION_TCP
)
180 int err
= setsockopt(ptr
->fd
, IPPROTO_TCP
, CORK
,
181 &enable
, (socklen_t
)sizeof(int));
187 perror(strerror(errno
));
188 ptr
->cached_errno
= errno
;
195 ptr
->cached_errno
= 0;
201 static inline void libmemcached_free(const memcached_st
*ptr
, void *mem
)
203 ptr
->allocators
.free(ptr
, mem
, ptr
->allocators
.context
);
206 static inline void *libmemcached_malloc(const memcached_st
*ptr
, const size_t size
)
208 return ptr
->allocators
.malloc(ptr
, size
, ptr
->allocators
.context
);
211 static inline void *libmemcached_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
)
213 return ptr
->allocators
.realloc(ptr
, mem
, size
, ptr
->allocators
.context
);
216 static inline void *libmemcached_calloc(const memcached_st
*ptr
, size_t nelem
, size_t size
)
218 return ptr
->allocators
.calloc(ptr
, nelem
, size
, ptr
->allocators
.context
);
225 #endif /* __LIBMEMCACHED_COMMON_H__ */