c413b1fe62ec80153aabcca5447d15d095be1801
[awesomized/libmemcached] / libmemcached / common.h
1 /*
2 Common include file for libmemached
3 */
4
5 #ifndef LIBMEMCACHED_COMMON_H
6 #define LIBMEMCACHED_COMMON_H
7
8 #include "config.h"
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <strings.h>
14 #include <ctype.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <netdb.h>
20 #include <unistd.h>
21 #include <limits.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/un.h>
25 #include <netinet/tcp.h>
26 #ifdef TIME_WITH_SYS_TIME
27 # include <sys/time.h>
28 # include <time.h>
29 #else
30 # ifdef HAVE_SYS_TIME_H
31 # include <sys/time.h>
32 # else
33 # include <time.h>
34 # endif
35 #endif
36
37
38 #include "libmemcached/memcached.h"
39 #include "libmemcached/memcached_watchpoint.h"
40
41 #if defined(BUILDING_LIBMEMCACHED)
42 /* These are private not to be installed headers */
43 #include "libmemcached/memcached_io.h"
44 #include "libmemcached/memcached_internal.h"
45 #include "libmemcached/libmemcached_probes.h"
46 #include "libmemcached/memcached/protocol_binary.h"
47
48 /* string value */
49 struct memcached_continuum_item_st {
50 uint32_t index;
51 uint32_t value;
52 };
53
54 #endif
55
56
57
58
59 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
60
61 #define likely(x) if((x))
62 #define unlikely(x) if((x))
63
64 #else
65
66 #define likely(x) if(__builtin_expect(!!(x), 1))
67 #define unlikely(x) if(__builtin_expect((x), 0))
68 #endif
69
70
71 #define MEMCACHED_BLOCK_SIZE 1024
72 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
73 #define SMALL_STRING_LEN 1024
74 #define HUGE_STRING_LEN 8196
75
76
77 typedef enum {
78 MEM_NO_BLOCK= (1 << 0),
79 MEM_TCP_NODELAY= (1 << 1),
80 MEM_REUSE_MEMORY= (1 << 2),
81 MEM_USE_MD5= (1 << 3),
82 /* 4 was once Ketama */
83 MEM_USE_CRC= (1 << 5),
84 MEM_USE_CACHE_LOOKUPS= (1 << 6),
85 MEM_SUPPORT_CAS= (1 << 7),
86 MEM_BUFFER_REQUESTS= (1 << 8),
87 MEM_USE_SORT_HOSTS= (1 << 9),
88 MEM_VERIFY_KEY= (1 << 10),
89 /* 11 used for weighted ketama */
90 MEM_KETAMA_WEIGHTED= (1 << 11),
91 MEM_BINARY_PROTOCOL= (1 << 12),
92 MEM_HASH_WITH_PREFIX_KEY= (1 << 13),
93 MEM_NOREPLY= (1 << 14),
94 MEM_USE_UDP= (1 << 15),
95 MEM_AUTO_EJECT_HOSTS= (1 << 16)
96 } memcached_flags;
97
98 #if defined(BUILDING_LIBMEMCACHED)
99 /* Hashing algo */
100 void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result);
101 uint32_t hash_crc32(const char *data,
102 size_t data_len);
103 #ifdef HAVE_HSIEH_HASH
104 uint32_t hsieh_hash(const char *key, size_t key_length);
105 #endif
106 uint32_t murmur_hash(const char *key, size_t key_length);
107 uint32_t jenkins_hash(const void *key, size_t length, uint32_t initval);
108
109 memcached_return memcached_connect(memcached_server_st *ptr);
110 memcached_return memcached_response(memcached_server_st *ptr,
111 char *buffer, size_t buffer_length,
112 memcached_result_st *result);
113 void memcached_quit_server(memcached_server_st *ptr, uint8_t io_death);
114
115 #define memcached_server_response_increment(A) (A)->cursor_active++
116 #define memcached_server_response_decrement(A) (A)->cursor_active--
117 #define memcached_server_response_reset(A) (A)->cursor_active=0
118
119 memcached_return memcached_do(memcached_server_st *ptr, const void *commmand,
120 size_t command_length, uint8_t with_flush);
121 memcached_return memcached_version(memcached_st *ptr);
122 memcached_return value_fetch(memcached_server_st *ptr,
123 char *buffer,
124 memcached_result_st *result);
125 void server_list_free(memcached_st *ptr, memcached_server_st *servers);
126
127 memcached_return memcached_key_test(char **keys, size_t *key_length,
128 unsigned int number_of_keys);
129
130 memcached_return run_distribution(memcached_st *ptr);
131
132 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length);
133 memcached_return memcached_server_remove(memcached_server_st *st_ptr);
134
135 #ifndef HAVE_HTONLL
136 extern uint64_t ntohll(uint64_t);
137 extern uint64_t htonll(uint64_t);
138 #endif
139
140 memcached_return memcached_purge(memcached_server_st *ptr);
141
142 static inline memcached_return memcached_validate_key_length(size_t key_length,
143 bool binary) {
144 unlikely (key_length == 0)
145 return MEMCACHED_BAD_KEY_PROVIDED;
146
147 if (binary)
148 {
149 unlikely (key_length > 0xffff)
150 return MEMCACHED_BAD_KEY_PROVIDED;
151 }
152 else
153 {
154 unlikely (key_length >= MEMCACHED_MAX_KEY)
155 return MEMCACHED_BAD_KEY_PROVIDED;
156 }
157
158 return MEMCACHED_SUCCESS;
159 }
160 #endif /* BUILDING_LIBMEMCACHED */
161
162 #endif /* !defined(LIBMEMCACHED_COMMON_H) && defined(BUILDING_LIBMEMCACHED) */