Set C linkage for the function pointers
[m6w6/libmemcached] / libmemcached / memcached.c
1 /* LibMemcached
2 * Copyright (C) 2006-2010 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 #include "common.h"
13
14 static const memcached_st global_copy= {
15 .state= {
16 .is_purging= false,
17 .is_processing_input= false,
18 },
19 .flags= {
20 .auto_eject_hosts= false,
21 .binary_protocol= false,
22 .buffer_requests= false,
23 .cork= false,
24 .hash_with_prefix_key= false,
25 .ketama_weighted= false,
26 .no_block= false,
27 .no_reply= false,
28 .randomize_replica_read= false,
29 .reuse_memory= false,
30 .support_cas= false,
31 .tcp_nodelay= false,
32 .use_cache_lookups= false,
33 .use_sort_hosts= false,
34 .use_udp= false,
35 .verify_key= false
36 }
37 };
38
39 static inline bool _memcached_init(memcached_st *self)
40 {
41 self->state= global_copy.state;
42 self->flags= global_copy.flags;
43
44 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
45
46 hashkit_st *hash_ptr;
47 hash_ptr= hashkit_create(&self->hashkit);
48 if (! hash_ptr)
49 return false;
50
51 self->continuum_points_counter= 0;
52
53 self->number_of_hosts= 0;
54 self->servers= NULL;
55 self->last_disconnected_server= NULL;
56
57 self->snd_timeout= 0;
58 self->rcv_timeout= 0;
59 self->server_failure_limit= 0;
60
61 /* TODO, Document why we picked these defaults */
62 self->io_msg_watermark= 500;
63 self->io_bytes_watermark= 65 * 1024;
64
65 self->io_key_prefetch= 0;
66 self->cached_errno= 0;
67 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
68 self->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
69 self->retry_timeout= 0;
70 self->continuum_count= 0;
71
72 self->send_size= -1;
73 self->recv_size= -1;
74
75 self->user_data= NULL;
76 self->next_distribution_rebuild= 0;
77 self->prefix_key_length= 0;
78 self->number_of_replicas= 0;
79 hash_ptr= hashkit_create(&self->distribution_hashkit);
80 if (! hash_ptr)
81 return false;
82 self->continuum= NULL;
83
84 self->allocators= memcached_allocators_return_default();
85
86 self->on_clone= NULL;
87 self->on_cleanup= NULL;
88 self->get_key_failure= NULL;
89 self->delete_trigger= NULL;
90 self->callbacks= NULL;
91
92 return true;
93 }
94
95 memcached_st *memcached_create(memcached_st *ptr)
96 {
97 if (ptr == NULL)
98 {
99 ptr= (memcached_st *)malloc(sizeof(memcached_st));
100
101 if (! ptr)
102 {
103 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
104 }
105
106 ptr->options.is_allocated= true;
107 }
108 else
109 {
110 ptr->options.is_allocated= false;
111 }
112
113 #if 0
114 memcached_set_purging(ptr, false);
115 memcached_set_processing_input(ptr, false);
116 #endif
117
118 if (! _memcached_init(ptr))
119 {
120 memcached_free(ptr);
121 return NULL;
122 }
123
124 if (! memcached_result_create(ptr, &ptr->result))
125 {
126 memcached_free(ptr);
127 return NULL;
128 }
129
130 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
131
132 return ptr;
133 }
134
135 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
136 {
137 uint32_t x;
138
139 if (servers == NULL)
140 return;
141
142 for (x= 0; x < memcached_servers_count(servers); x++)
143 {
144 if (servers[x].address_info)
145 {
146 freeaddrinfo(servers[x].address_info);
147 servers[x].address_info= NULL;
148 }
149 }
150
151 if (ptr)
152 {
153 libmemcached_free(ptr, servers);
154 }
155 else
156 {
157 free(servers);
158 }
159 }
160
161 void memcached_servers_reset(memcached_st *ptr)
162 {
163 server_list_free(ptr, memcached_server_list(ptr));
164
165 memcached_server_list_set(ptr, NULL);
166 ptr->number_of_hosts= 0;
167 ptr->last_disconnected_server= NULL;
168 ptr->server_failure_limit= 0;
169 }
170
171 void memcached_free(memcached_st *ptr)
172 {
173 /* If we have anything open, lets close it now */
174 memcached_quit(ptr);
175 server_list_free(ptr, memcached_server_list(ptr));
176 memcached_result_free(&ptr->result);
177
178 if (ptr->on_cleanup)
179 ptr->on_cleanup(ptr);
180
181 if (ptr->continuum)
182 libmemcached_free(ptr, ptr->continuum);
183
184 if (memcached_is_allocated(ptr))
185 {
186 libmemcached_free(ptr, ptr);
187 }
188 }
189
190 /*
191 clone is the destination, while source is the structure to clone.
192 If source is NULL the call is the same as if a memcached_create() was
193 called.
194 */
195 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
196 {
197 memcached_return_t rc= MEMCACHED_SUCCESS;
198 memcached_st *new_clone;
199
200 if (source == NULL)
201 return memcached_create(clone);
202
203 if (clone && memcached_is_allocated(clone))
204 {
205 return NULL;
206 }
207
208 new_clone= memcached_create(clone);
209
210 if (new_clone == NULL)
211 return NULL;
212
213 new_clone->flags= source->flags;
214 new_clone->send_size= source->send_size;
215 new_clone->recv_size= source->recv_size;
216 new_clone->poll_timeout= source->poll_timeout;
217 new_clone->connect_timeout= source->connect_timeout;
218 new_clone->retry_timeout= source->retry_timeout;
219 new_clone->distribution= source->distribution;
220
221 hashkit_st *hash_ptr;
222
223 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
224 if (! hash_ptr)
225 {
226 memcached_free(new_clone);
227 return NULL;
228 }
229
230 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
231 if (! hash_ptr)
232 {
233 memcached_free(new_clone);
234 return NULL;
235 }
236
237 new_clone->user_data= source->user_data;
238
239 new_clone->snd_timeout= source->snd_timeout;
240 new_clone->rcv_timeout= source->rcv_timeout;
241
242 new_clone->on_clone= source->on_clone;
243 new_clone->on_cleanup= source->on_cleanup;
244
245 new_clone->allocators= source->allocators;
246
247 new_clone->get_key_failure= source->get_key_failure;
248 new_clone->delete_trigger= source->delete_trigger;
249 new_clone->server_failure_limit= source->server_failure_limit;
250 new_clone->io_msg_watermark= source->io_msg_watermark;
251 new_clone->io_bytes_watermark= source->io_bytes_watermark;
252 new_clone->io_key_prefetch= source->io_key_prefetch;
253 new_clone->number_of_replicas= source->number_of_replicas;
254
255 if (memcached_server_list(source))
256 rc= memcached_server_push(new_clone, memcached_server_list(source));
257
258 if (rc != MEMCACHED_SUCCESS)
259 {
260 memcached_free(new_clone);
261
262 return NULL;
263 }
264
265
266 if (source->prefix_key_length)
267 {
268 strcpy(new_clone->prefix_key, source->prefix_key);
269 new_clone->prefix_key_length= source->prefix_key_length;
270 }
271
272 rc= run_distribution(new_clone);
273
274 if (rc != MEMCACHED_SUCCESS)
275 {
276 memcached_free(new_clone);
277
278 return NULL;
279 }
280
281 if (source->on_clone)
282 source->on_clone(new_clone, source);
283
284 return new_clone;
285 }
286
287 void *memcached_get_user_data(const memcached_st *ptr)
288 {
289 return ptr->user_data;
290 }
291
292 void *memcached_set_user_data(memcached_st *ptr, void *data)
293 {
294 void *ret= ptr->user_data;
295 ptr->user_data= data;
296
297 return ret;
298 }