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