6 static const memcached_st global_copy
= {
9 .is_processing_input
= false,
12 .auto_eject_hosts
= false,
13 .binary_protocol
= false,
14 .buffer_requests
= false,
16 .hash_with_prefix_key
= false,
17 .ketama_weighted
= false,
20 .randomize_replica_read
= false,
24 .use_cache_lookups
= false,
25 .use_sort_hosts
= false,
31 static inline bool _memcached_init(memcached_st
*self
)
33 self
->state
= global_copy
.state
;
34 self
->flags
= global_copy
.flags
;
36 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
39 hash_ptr
= hashkit_create(&self
->hashkit
);
43 self
->continuum_points_counter
= 0;
45 self
->number_of_hosts
= 0;
47 self
->last_disconnected_server
= NULL
;
51 self
->server_failure_limit
= 0;
53 /* TODO, Document why we picked these defaults */
54 self
->io_msg_watermark
= 500;
55 self
->io_bytes_watermark
= 65 * 1024;
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;
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
);
74 self
->continuum
= NULL
;
76 self
->allocators
= memcached_allocators_return_default();
79 self
->on_cleanup
= NULL
;
80 self
->get_key_failure
= NULL
;
81 self
->delete_trigger
= NULL
;
82 self
->callbacks
= NULL
;
87 memcached_st
*memcached_create(memcached_st
*ptr
)
91 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
95 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
98 ptr
->options
.is_allocated
= true;
102 ptr
->options
.is_allocated
= false;
106 memcached_set_purging(ptr
, false);
107 memcached_set_processing_input(ptr
, false);
110 if (! _memcached_init(ptr
))
116 if (! memcached_result_create(ptr
, &ptr
->result
))
122 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
127 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
134 for (x
= 0; x
< memcached_servers_count(servers
); x
++)
136 if (servers
[x
].address_info
)
138 freeaddrinfo(servers
[x
].address_info
);
139 servers
[x
].address_info
= NULL
;
145 libmemcached_free(ptr
, servers
);
153 void memcached_servers_reset(memcached_st
*ptr
)
155 server_list_free(ptr
, memcached_server_list(ptr
));
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;
163 void memcached_free(memcached_st
*ptr
)
165 /* If we have anything open, lets close it now */
167 server_list_free(ptr
, memcached_server_list(ptr
));
168 memcached_result_free(&ptr
->result
);
171 ptr
->on_cleanup(ptr
);
174 libmemcached_free(ptr
, ptr
->continuum
);
176 if (memcached_is_allocated(ptr
))
178 libmemcached_free(ptr
, ptr
);
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
187 memcached_st
*memcached_clone(memcached_st
*clone
, memcached_st
*source
)
189 memcached_return_t rc
= MEMCACHED_SUCCESS
;
190 memcached_st
*new_clone
;
193 return memcached_create(clone
);
195 if (clone
&& memcached_is_allocated(clone
))
200 new_clone
= memcached_create(clone
);
202 if (new_clone
== NULL
)
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
;
213 hashkit_st
*hash_ptr
;
215 hash_ptr
= hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
);
218 memcached_free(new_clone
);
222 hash_ptr
= hashkit_clone(&new_clone
->distribution_hashkit
, &source
->distribution_hashkit
);
225 memcached_free(new_clone
);
229 new_clone
->user_data
= source
->user_data
;
231 new_clone
->snd_timeout
= source
->snd_timeout
;
232 new_clone
->rcv_timeout
= source
->rcv_timeout
;
234 new_clone
->on_clone
= source
->on_clone
;
235 new_clone
->on_cleanup
= source
->on_cleanup
;
237 new_clone
->allocators
= source
->allocators
;
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
;
247 if (memcached_server_list(source
))
248 rc
= memcached_server_push(new_clone
, memcached_server_list(source
));
250 if (rc
!= MEMCACHED_SUCCESS
)
252 memcached_free(new_clone
);
258 if (source
->prefix_key_length
)
260 strcpy(new_clone
->prefix_key
, source
->prefix_key
);
261 new_clone
->prefix_key_length
= source
->prefix_key_length
;
264 rc
= run_distribution(new_clone
);
266 if (rc
!= MEMCACHED_SUCCESS
)
268 memcached_free(new_clone
);
273 if (source
->on_clone
)
274 source
->on_clone(source
, new_clone
);
279 void *memcached_get_user_data(const memcached_st
*ptr
)
281 return ptr
->user_data
;
284 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
286 void *ret
= ptr
->user_data
;
287 ptr
->user_data
= data
;