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_instance_st
;
59 /* These are private not to be installed headers */
60 #include "libmemcached/io.h"
61 #include "libmemcached/internal.h"
62 #include "libmemcached/libmemcached_probes.h"
63 #include "libmemcached/memcached/protocol_binary.h"
64 #include "libmemcached/byteorder.h"
65 #include "libmemcached/response.h"
68 struct memcached_continuum_item_st
74 /* Yum, Fortran.... can you make the reference? */
79 } memcached_ternary_t
;
82 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
84 #define likely(x) if((x))
85 #define unlikely(x) if((x))
89 #define likely(x) if(__builtin_expect((x) != 0, 1))
90 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
93 #define MEMCACHED_BLOCK_SIZE 1024
94 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
95 #define SMALL_STRING_LEN 1024
96 #define HUGE_STRING_LEN 8196
101 void md5_signature(const unsigned char *key
, unsigned int length
, unsigned char *result
);
103 uint32_t hash_crc32(const char *data
,
105 #ifdef HAVE_HSIEH_HASH
107 uint32_t hsieh_hash(const char *key
, size_t key_length
);
110 uint32_t murmur_hash(const char *key
, size_t key_length
);
112 uint32_t jenkins_hash(const void *key
, size_t length
, uint32_t initval
);
115 memcached_return_t
memcached_connect(memcached_server_instance_st
*ptr
);
117 void memcached_quit_server(memcached_server_instance_st
*ptr
, uint8_t io_death
);
119 #define memcached_server_response_increment(A) (A)->cursor_active++
120 #define memcached_server_response_decrement(A) (A)->cursor_active--
121 #define memcached_server_response_reset(A) (A)->cursor_active=0
124 memcached_return_t
memcached_do(memcached_server_instance_st
*ptr
, const void *commmand
,
125 size_t command_length
, uint8_t with_flush
);
127 memcached_return_t
value_fetch(memcached_server_instance_st
*ptr
,
129 memcached_result_st
*result
);
131 void server_list_free(memcached_st
*ptr
, memcached_server_instance_st
*servers
);
134 memcached_return_t
memcached_key_test(const char * const *keys
,
135 const size_t *key_length
,
136 size_t number_of_keys
);
140 uint32_t generate_hash(memcached_st
*ptr
, const char *key
, size_t key_length
);
143 memcached_return_t
memcached_purge(memcached_server_instance_st
*ptr
);
145 static inline memcached_return_t
memcached_validate_key_length(size_t key_length
, bool binary
)
147 unlikely (key_length
== 0)
148 return MEMCACHED_BAD_KEY_PROVIDED
;
152 unlikely (key_length
> 0xffff)
153 return MEMCACHED_BAD_KEY_PROVIDED
;
157 unlikely (key_length
>= MEMCACHED_MAX_KEY
)
158 return MEMCACHED_BAD_KEY_PROVIDED
;
161 return MEMCACHED_SUCCESS
;
165 #define CORK TCP_CORK
166 #elif defined TCP_NOPUSH
167 #define CORK TCP_NOPUSH
171 test_cork() tries to enable TCP_CORK. IF TCP_CORK is not an option
172 on the system it returns false but sets errno to 0. Otherwise on
173 failure errno is set.
175 static inline memcached_ternary_t
test_cork(memcached_server_st
*ptr
, bool enable
)
178 if (ptr
->type
!= MEMCACHED_CONNECTION_TCP
)
181 int err
= setsockopt(ptr
->fd
, IPPROTO_TCP
, CORK
,
182 &enable
, (socklen_t
)sizeof(int));
188 perror(strerror(errno
));
189 ptr
->cached_errno
= errno
;
196 ptr
->cached_errno
= 0;
202 #endif /* LIBMEMCACHED_COMMON_H */