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 void _memcached_init(memcached_st
*self
)
33 self
->state
= global_copy
.state
;
34 self
->flags
= global_copy
.flags
;
36 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
37 self
->hash
= MEMCACHED_HASH_DEFAULT
;
38 self
->continuum_points_counter
= 0;
40 self
->number_of_hosts
= 0;
42 self
->last_disconnected_server
= NULL
;
46 self
->server_failure_limit
= 0;
48 /* TODO, Document why we picked these defaults */
49 self
->io_msg_watermark
= 500;
50 self
->io_bytes_watermark
= 65 * 1024;
52 self
->io_key_prefetch
= 0;
53 self
->cached_errno
= 0;
54 self
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
55 self
->connect_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
56 self
->retry_timeout
= 0;
57 self
->continuum_count
= 0;
62 self
->user_data
= NULL
;
63 self
->next_distribution_rebuild
= 0;
64 self
->prefix_key_length
= 0;
65 self
->number_of_replicas
= 0;
66 self
->distribution_hash
= MEMCACHED_HASH_DEFAULT
;
67 self
->continuum
= NULL
;
70 memcached_set_memory_allocators(self
, NULL
, NULL
, NULL
, NULL
, NULL
);
71 self
->allocators
= memcached_allocators_return_default();
74 self
->on_cleanup
= NULL
;
75 self
->get_key_failure
= NULL
;
76 self
->delete_trigger
= NULL
;
77 self
->callbacks
= NULL
;
80 memcached_st
*memcached_create(memcached_st
*ptr
)
84 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
88 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
91 ptr
->options
.is_allocated
= true;
95 ptr
->options
.is_allocated
= false;
99 memcached_set_purging(ptr
, false);
100 memcached_set_processing_input(ptr
, false);
103 _memcached_init(ptr
);
105 if (! memcached_result_create(ptr
, &ptr
->result
))
111 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
116 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
123 for (x
= 0; x
< memcached_servers_count(servers
); x
++)
125 if (servers
[x
].address_info
)
127 freeaddrinfo(servers
[x
].address_info
);
128 servers
[x
].address_info
= NULL
;
134 libmemcached_free(ptr
, servers
);
142 void memcached_servers_reset(memcached_st
*ptr
)
144 server_list_free(ptr
, memcached_server_list(ptr
));
146 memcached_server_list_set(ptr
, NULL
);
147 ptr
->number_of_hosts
= 0;
148 ptr
->last_disconnected_server
= NULL
;
149 ptr
->server_failure_limit
= 0;
152 void memcached_free(memcached_st
*ptr
)
154 /* If we have anything open, lets close it now */
156 server_list_free(ptr
, memcached_server_list(ptr
));
157 memcached_result_free(&ptr
->result
);
160 ptr
->on_cleanup(ptr
);
163 libmemcached_free(ptr
, ptr
->continuum
);
165 if (memcached_is_allocated(ptr
))
167 libmemcached_free(ptr
, ptr
);
172 clone is the destination, while source is the structure to clone.
173 If source is NULL the call is the same as if a memcached_create() was
176 memcached_st
*memcached_clone(memcached_st
*clone
, memcached_st
*source
)
178 memcached_return_t rc
= MEMCACHED_SUCCESS
;
179 memcached_st
*new_clone
;
182 return memcached_create(clone
);
184 if (clone
&& memcached_is_allocated(clone
))
189 new_clone
= memcached_create(clone
);
191 if (new_clone
== NULL
)
194 new_clone
->flags
= source
->flags
;
195 new_clone
->send_size
= source
->send_size
;
196 new_clone
->recv_size
= source
->recv_size
;
197 new_clone
->poll_timeout
= source
->poll_timeout
;
198 new_clone
->connect_timeout
= source
->connect_timeout
;
199 new_clone
->retry_timeout
= source
->retry_timeout
;
200 new_clone
->distribution
= source
->distribution
;
201 new_clone
->hash
= source
->hash
;
202 new_clone
->distribution_hash
= source
->distribution_hash
;
203 new_clone
->user_data
= source
->user_data
;
205 new_clone
->snd_timeout
= source
->snd_timeout
;
206 new_clone
->rcv_timeout
= source
->rcv_timeout
;
208 new_clone
->on_clone
= source
->on_clone
;
209 new_clone
->on_cleanup
= source
->on_cleanup
;
211 new_clone
->allocators
= source
->allocators
;
213 new_clone
->get_key_failure
= source
->get_key_failure
;
214 new_clone
->delete_trigger
= source
->delete_trigger
;
215 new_clone
->server_failure_limit
= source
->server_failure_limit
;
216 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
217 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
218 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
219 new_clone
->number_of_replicas
= source
->number_of_replicas
;
221 if (memcached_server_list(source
))
222 rc
= memcached_server_push(new_clone
, memcached_server_list(source
));
224 if (rc
!= MEMCACHED_SUCCESS
)
226 memcached_free(new_clone
);
232 if (source
->prefix_key_length
)
234 strcpy(new_clone
->prefix_key
, source
->prefix_key
);
235 new_clone
->prefix_key_length
= source
->prefix_key_length
;
238 rc
= run_distribution(new_clone
);
240 if (rc
!= MEMCACHED_SUCCESS
)
242 memcached_free(new_clone
);
247 if (source
->on_clone
)
248 source
->on_clone(source
, new_clone
);
253 void *memcached_get_user_data(const memcached_st
*ptr
)
255 return ptr
->user_data
;
258 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
260 void *ret
= ptr
->user_data
;
261 ptr
->user_data
= data
;