2 * Copyright (C) 2006-2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
14 static const memcached_st global_copy
= {
17 .is_processing_input
= false,
20 .auto_eject_hosts
= false,
21 .binary_protocol
= false,
22 .buffer_requests
= false,
24 .hash_with_prefix_key
= false,
25 .ketama_weighted
= false,
28 .randomize_replica_read
= false,
32 .use_cache_lookups
= false,
33 .use_sort_hosts
= false,
39 static inline bool _memcached_init(memcached_st
*self
)
41 self
->state
= global_copy
.state
;
42 self
->flags
= global_copy
.flags
;
44 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
47 hash_ptr
= hashkit_create(&self
->hashkit
);
51 self
->continuum_points_counter
= 0;
53 self
->number_of_hosts
= 0;
55 self
->last_disconnected_server
= NULL
;
59 self
->server_failure_limit
= 0;
61 /* TODO, Document why we picked these defaults */
62 self
->io_msg_watermark
= 500;
63 self
->io_bytes_watermark
= 65 * 1024;
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;
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
);
82 self
->continuum
= NULL
;
84 self
->allocators
= memcached_allocators_return_default();
87 self
->on_cleanup
= NULL
;
88 self
->get_key_failure
= NULL
;
89 self
->delete_trigger
= NULL
;
90 self
->callbacks
= NULL
;
95 memcached_st
*memcached_create(memcached_st
*ptr
)
99 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
103 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
106 ptr
->options
.is_allocated
= true;
110 ptr
->options
.is_allocated
= false;
114 memcached_set_purging(ptr
, false);
115 memcached_set_processing_input(ptr
, false);
118 if (! _memcached_init(ptr
))
124 if (! memcached_result_create(ptr
, &ptr
->result
))
130 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
135 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
142 for (x
= 0; x
< memcached_servers_count(servers
); x
++)
144 if (servers
[x
].address_info
)
146 freeaddrinfo(servers
[x
].address_info
);
147 servers
[x
].address_info
= NULL
;
153 libmemcached_free(ptr
, servers
);
161 void memcached_servers_reset(memcached_st
*ptr
)
163 server_list_free(ptr
, memcached_server_list(ptr
));
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;
171 void memcached_free(memcached_st
*ptr
)
173 /* If we have anything open, lets close it now */
175 server_list_free(ptr
, memcached_server_list(ptr
));
176 memcached_result_free(&ptr
->result
);
179 ptr
->on_cleanup(ptr
);
182 libmemcached_free(ptr
, ptr
->continuum
);
184 if (memcached_is_allocated(ptr
))
186 libmemcached_free(ptr
, ptr
);
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
195 memcached_st
*memcached_clone(memcached_st
*clone
, const memcached_st
*source
)
197 memcached_return_t rc
= MEMCACHED_SUCCESS
;
198 memcached_st
*new_clone
;
201 return memcached_create(clone
);
203 if (clone
&& memcached_is_allocated(clone
))
208 new_clone
= memcached_create(clone
);
210 if (new_clone
== NULL
)
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
;
221 hashkit_st
*hash_ptr
;
223 hash_ptr
= hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
);
226 memcached_free(new_clone
);
230 hash_ptr
= hashkit_clone(&new_clone
->distribution_hashkit
, &source
->distribution_hashkit
);
233 memcached_free(new_clone
);
237 new_clone
->user_data
= source
->user_data
;
239 new_clone
->snd_timeout
= source
->snd_timeout
;
240 new_clone
->rcv_timeout
= source
->rcv_timeout
;
242 new_clone
->on_clone
= source
->on_clone
;
243 new_clone
->on_cleanup
= source
->on_cleanup
;
245 new_clone
->allocators
= source
->allocators
;
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
;
255 if (memcached_server_list(source
))
256 rc
= memcached_server_push(new_clone
, memcached_server_list(source
));
258 if (rc
!= MEMCACHED_SUCCESS
)
260 memcached_free(new_clone
);
266 if (source
->prefix_key_length
)
268 strcpy(new_clone
->prefix_key
, source
->prefix_key
);
269 new_clone
->prefix_key_length
= source
->prefix_key_length
;
272 rc
= run_distribution(new_clone
);
274 if (rc
!= MEMCACHED_SUCCESS
)
276 memcached_free(new_clone
);
281 if (source
->on_clone
)
282 source
->on_clone(new_clone
, source
);
287 void *memcached_get_user_data(const memcached_st
*ptr
)
289 return ptr
->user_data
;
292 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
294 void *ret
= ptr
->user_data
;
295 ptr
->user_data
= data
;