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>
31 #ifdef TIME_WITH_SYS_TIME
32 # include <sys/time.h>
35 # ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
42 /* Define this here, which will turn on the visibilty controls while we're
43 * building libmemcached.
45 #define BUILDING_LIBMEMCACHED 1
48 #include "libmemcached/memcached.h"
49 #include "libmemcached/watchpoint.h"
51 typedef struct memcached_server_st
* memcached_server_write_instance_st
;
53 typedef memcached_return_t (*memcached_server_execute_fn
)(memcached_st
*ptr
, memcached_server_write_instance_st server
, void *context
);
56 memcached_server_write_instance_st
memcached_server_instance_fetch(memcached_st
*ptr
, uint32_t server_key
);
59 memcached_return_t
memcached_server_execute(memcached_st
*ptr
,
60 memcached_server_execute_fn callback
,
64 /* These are private not to be installed headers */
65 #include "libmemcached/io.h"
66 #include "libmemcached/do.h"
67 #include "libmemcached/internal.h"
68 #include "libmemcached/libmemcached_probes.h"
69 #include "libmemcached/memcached/protocol_binary.h"
70 #include "libmemcached/byteorder.h"
71 #include "libmemcached/response.h"
74 struct memcached_continuum_item_st
80 /* Yum, Fortran.... can you make the reference? */
85 } memcached_ternary_t
;
88 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
90 #define likely(x) if((x))
91 #define unlikely(x) if((x))
95 #define likely(x) if(__builtin_expect((x) != 0, 1))
96 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
99 #define MEMCACHED_BLOCK_SIZE 1024
100 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
101 #define SMALL_STRING_LEN 1024
102 #define HUGE_STRING_LEN 8196
109 memcached_return_t
memcached_connect(memcached_server_write_instance_st ptr
);
112 memcached_return_t
run_distribution(memcached_st
*ptr
);
114 #define memcached_server_response_increment(A) (A)->cursor_active++
115 #define memcached_server_response_decrement(A) (A)->cursor_active--
116 #define memcached_server_response_reset(A) (A)->cursor_active=0
119 #define memcached_is_allocated(__object) ((__object)->options.is_allocated)
120 #define memcached_is_initialized(__object) ((__object)->options.is_initialized)
121 #define memcached_is_purging(__object) ((__object)->state.is_purging)
122 #define memcached_is_processing_input(__object) ((__object)->state.is_processing_input)
123 #define memcached_set_purging(__object, __value) ((__object)->state.is_purging= (__value))
124 #define memcached_set_processing_input(__object, __value) ((__object)->state.is_processing_input= (__value))
125 #define memcached_set_initialized(__object, __value) ((__object)->options.is_initialized(= (__value))
126 #define memcached_set_allocated(__object, __value) ((__object)->options.is_allocated(= (__value))
129 void set_last_disconnected_host(memcached_server_write_instance_st ptr
);
132 memcached_return_t
memcached_key_test(const char * const *keys
,
133 const size_t *key_length
,
134 size_t number_of_keys
);
137 memcached_return_t
memcached_purge(memcached_server_write_instance_st ptr
);
140 memcached_server_st
*memcached_server_create_with(const memcached_st
*memc
,
141 memcached_server_write_instance_st host
,
142 const char *hostname
,
145 memcached_connection_t type
);
148 static inline memcached_return_t
memcached_validate_key_length(size_t key_length
, bool binary
)
150 unlikely (key_length
== 0)
151 return MEMCACHED_BAD_KEY_PROVIDED
;
155 unlikely (key_length
> 0xffff)
156 return MEMCACHED_BAD_KEY_PROVIDED
;
160 unlikely (key_length
>= MEMCACHED_MAX_KEY
)
161 return MEMCACHED_BAD_KEY_PROVIDED
;
164 return MEMCACHED_SUCCESS
;
168 #define CORK TCP_CORK
169 #elif defined TCP_NOPUSH
170 #define CORK TCP_NOPUSH
174 test_cork() tries to enable TCP_CORK. IF TCP_CORK is not an option
175 on the system it returns false but sets errno to 0. Otherwise on
176 failure errno is set.
178 static inline memcached_ternary_t
test_cork(memcached_server_st
*ptr
, int enable
)
181 if (ptr
->type
!= MEMCACHED_CONNECTION_TCP
)
184 int err
= setsockopt(ptr
->fd
, IPPROTO_TCP
, CORK
,
185 &enable
, (socklen_t
)sizeof(int));
191 perror(strerror(errno
));
192 ptr
->cached_errno
= errno
;
199 ptr
->cached_errno
= 0;
205 static inline void libmemcached_free(const memcached_st
*ptr
, void *mem
)
207 ptr
->allocators
.free(ptr
, mem
, ptr
->allocators
.context
);
210 static inline void *libmemcached_malloc(const memcached_st
*ptr
, const size_t size
)
212 return ptr
->allocators
.malloc(ptr
, size
, ptr
->allocators
.context
);
215 static inline void *libmemcached_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
)
217 return ptr
->allocators
.realloc(ptr
, mem
, size
, ptr
->allocators
.context
);
220 static inline void *libmemcached_calloc(const memcached_st
*ptr
, size_t nelem
, size_t size
)
222 return ptr
->allocators
.calloc(ptr
, nelem
, size
, ptr
->allocators
.context
);
229 #endif /* __LIBMEMCACHED_COMMON_H__ */