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