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
);
73 self
->on_cleanup
= NULL
;
74 self
->get_key_failure
= NULL
;
75 self
->delete_trigger
= NULL
;
76 self
->callbacks
= NULL
;
79 memcached_st
*memcached_create(memcached_st
*ptr
)
83 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
87 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
90 ptr
->options
.is_allocated
= true;
94 ptr
->options
.is_allocated
= false;
98 memcached_set_purging(ptr
, false);
99 memcached_set_processing_input(ptr
, false);
102 _memcached_init(ptr
);
104 if (! memcached_result_create(ptr
, &ptr
->result
))
110 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
115 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
122 for (x
= 0; x
< memcached_servers_count(servers
); x
++)
124 if (servers
[x
].address_info
)
126 freeaddrinfo(servers
[x
].address_info
);
127 servers
[x
].address_info
= NULL
;
133 ptr
->call_free(ptr
, servers
);
141 void memcached_servers_reset(memcached_st
*ptr
)
143 server_list_free(ptr
, memcached_server_list(ptr
));
145 memcached_server_list_set(ptr
, NULL
);
146 ptr
->number_of_hosts
= 0;
147 ptr
->last_disconnected_server
= NULL
;
148 ptr
->server_failure_limit
= 0;
151 void memcached_free(memcached_st
*ptr
)
153 /* If we have anything open, lets close it now */
155 server_list_free(ptr
, memcached_server_list(ptr
));
156 memcached_result_free(&ptr
->result
);
159 ptr
->on_cleanup(ptr
);
162 ptr
->call_free(ptr
, ptr
->continuum
);
164 if (memcached_is_allocated(ptr
))
166 ptr
->call_free(ptr
, ptr
);
171 clone is the destination, while source is the structure to clone.
172 If source is NULL the call is the same as if a memcached_create() was
175 memcached_st
*memcached_clone(memcached_st
*clone
, memcached_st
*source
)
177 memcached_return_t rc
= MEMCACHED_SUCCESS
;
178 memcached_st
*new_clone
;
181 return memcached_create(clone
);
183 if (clone
&& memcached_is_allocated(clone
))
188 new_clone
= memcached_create(clone
);
190 if (new_clone
== NULL
)
193 new_clone
->flags
= source
->flags
;
194 new_clone
->send_size
= source
->send_size
;
195 new_clone
->recv_size
= source
->recv_size
;
196 new_clone
->poll_timeout
= source
->poll_timeout
;
197 new_clone
->connect_timeout
= source
->connect_timeout
;
198 new_clone
->retry_timeout
= source
->retry_timeout
;
199 new_clone
->distribution
= source
->distribution
;
200 new_clone
->hash
= source
->hash
;
201 new_clone
->distribution_hash
= source
->distribution_hash
;
202 new_clone
->user_data
= source
->user_data
;
204 new_clone
->snd_timeout
= source
->snd_timeout
;
205 new_clone
->rcv_timeout
= source
->rcv_timeout
;
207 new_clone
->on_clone
= source
->on_clone
;
208 new_clone
->on_cleanup
= source
->on_cleanup
;
209 new_clone
->call_free
= source
->call_free
;
210 new_clone
->call_malloc
= source
->call_malloc
;
211 new_clone
->call_realloc
= source
->call_realloc
;
212 new_clone
->call_calloc
= source
->call_calloc
;
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
;