Merge Monty
[m6w6/libmemcached] / libmemcached / common.h
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 /*
13 Common include file for libmemached
14 */
15
16 #ifndef LIBMEMCACHED_COMMON_H
17 #define LIBMEMCACHED_COMMON_H
18
19 #include "config.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <strings.h>
25 #include <ctype.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <netdb.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <sys/un.h>
36 #include <netinet/tcp.h>
37 #ifdef TIME_WITH_SYS_TIME
38 # include <sys/time.h>
39 # include <time.h>
40 #else
41 # ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
43 # else
44 # include <time.h>
45 # endif
46 #endif
47
48 /* Define this here, which will turn on the visibilty controls while we're
49 * building libmemcached.
50 */
51 #define BUILDING_LIBMEMCACHED 1
52
53
54 #include "libmemcached/memcached.h"
55 #include "libmemcached/watchpoint.h"
56
57 typedef struct memcached_server_st memcached_server_instance_st;
58
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"
66
67 /* string value */
68 struct memcached_continuum_item_st
69 {
70 uint32_t index;
71 uint32_t value;
72 };
73
74
75 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
76
77 #define likely(x) if((x))
78 #define unlikely(x) if((x))
79
80 #else
81
82 #define likely(x) if(__builtin_expect((x) != 0, 1))
83 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
84 #endif
85
86
87 #define MEMCACHED_BLOCK_SIZE 1024
88 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
89 #define SMALL_STRING_LEN 1024
90 #define HUGE_STRING_LEN 8196
91
92 /* Hashing algo */
93
94 LIBMEMCACHED_LOCAL
95 void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result);
96 LIBMEMCACHED_LOCAL
97 uint32_t hash_crc32(const char *data,
98 size_t data_len);
99 #ifdef HAVE_HSIEH_HASH
100 LIBMEMCACHED_LOCAL
101 uint32_t hsieh_hash(const char *key, size_t key_length);
102 #endif
103 LIBMEMCACHED_LOCAL
104 uint32_t murmur_hash(const char *key, size_t key_length);
105 LIBMEMCACHED_LOCAL
106 uint32_t jenkins_hash(const void *key, size_t length, uint32_t initval);
107
108 LIBMEMCACHED_LOCAL
109 memcached_return_t memcached_connect(memcached_server_instance_st *ptr);
110 LIBMEMCACHED_LOCAL
111 void memcached_quit_server(memcached_server_instance_st *ptr, uint8_t io_death);
112
113 #define memcached_server_response_increment(A) (A)->cursor_active++
114 #define memcached_server_response_decrement(A) (A)->cursor_active--
115 #define memcached_server_response_reset(A) (A)->cursor_active=0
116
117 LIBMEMCACHED_LOCAL
118 memcached_return_t memcached_do(memcached_server_instance_st *ptr, const void *commmand,
119 size_t command_length, uint8_t with_flush);
120 LIBMEMCACHED_LOCAL
121 memcached_return_t value_fetch(memcached_server_instance_st *ptr,
122 char *buffer,
123 memcached_result_st *result);
124 LIBMEMCACHED_LOCAL
125 void server_list_free(memcached_st *ptr, memcached_server_instance_st *servers);
126
127 LIBMEMCACHED_LOCAL
128 memcached_return_t memcached_key_test(const char * const *keys,
129 const size_t *key_length,
130 size_t number_of_keys);
131
132
133 LIBMEMCACHED_LOCAL
134 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length);
135
136 LIBMEMCACHED_LOCAL
137 memcached_return_t memcached_purge(memcached_server_instance_st *ptr);
138
139 static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
140 {
141 unlikely (key_length == 0)
142 return MEMCACHED_BAD_KEY_PROVIDED;
143
144 if (binary)
145 {
146 unlikely (key_length > 0xffff)
147 return MEMCACHED_BAD_KEY_PROVIDED;
148 }
149 else
150 {
151 unlikely (key_length >= MEMCACHED_MAX_KEY)
152 return MEMCACHED_BAD_KEY_PROVIDED;
153 }
154
155 return MEMCACHED_SUCCESS;
156 }
157
158 #endif /* LIBMEMCACHED_COMMON_H */