Fix for issue where stats may not be initialized.
[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 void _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 self->hash= MEMCACHED_HASH_DEFAULT;
38 self->continuum_points_counter= 0;
39
40 self->number_of_hosts= 0;
41 self->servers= NULL;
42 self->last_disconnected_server= NULL;
43
44 self->snd_timeout= 0;
45 self->rcv_timeout= 0;
46 self->server_failure_limit= 0;
47
48 /* TODO, Document why we picked these defaults */
49 self->io_msg_watermark= 500;
50 self->io_bytes_watermark= 65 * 1024;
51
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;
58
59 self->send_size= -1;
60 self->recv_size= -1;
61
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;
68
69
70 memcached_set_memory_allocators(self, NULL, NULL, NULL, NULL, NULL);
71 self->allocators= memcached_allocators_return_default();
72
73 self->on_clone= NULL;
74 self->on_cleanup= NULL;
75 self->get_key_failure= NULL;
76 self->delete_trigger= NULL;
77 self->callbacks= NULL;
78 }
79
80 memcached_st *memcached_create(memcached_st *ptr)
81 {
82 if (ptr == NULL)
83 {
84 ptr= (memcached_st *)malloc(sizeof(memcached_st));
85
86 if (! ptr)
87 {
88 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
89 }
90
91 ptr->options.is_allocated= true;
92 }
93 else
94 {
95 ptr->options.is_allocated= false;
96 }
97
98 #if 0
99 memcached_set_purging(ptr, false);
100 memcached_set_processing_input(ptr, false);
101 #endif
102
103 _memcached_init(ptr);
104
105 if (! memcached_result_create(ptr, &ptr->result))
106 {
107 memcached_free(ptr);
108 return NULL;
109 }
110
111 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
112
113 return ptr;
114 }
115
116 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
117 {
118 uint32_t x;
119
120 if (servers == NULL)
121 return;
122
123 for (x= 0; x < memcached_servers_count(servers); x++)
124 {
125 if (servers[x].address_info)
126 {
127 freeaddrinfo(servers[x].address_info);
128 servers[x].address_info= NULL;
129 }
130 }
131
132 if (ptr)
133 {
134 libmemcached_free(ptr, servers);
135 }
136 else
137 {
138 free(servers);
139 }
140 }
141
142 void memcached_servers_reset(memcached_st *ptr)
143 {
144 server_list_free(ptr, memcached_server_list(ptr));
145
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;
150 }
151
152 void memcached_free(memcached_st *ptr)
153 {
154 /* If we have anything open, lets close it now */
155 memcached_quit(ptr);
156 server_list_free(ptr, memcached_server_list(ptr));
157 memcached_result_free(&ptr->result);
158
159 if (ptr->on_cleanup)
160 ptr->on_cleanup(ptr);
161
162 if (ptr->continuum)
163 libmemcached_free(ptr, ptr->continuum);
164
165 if (memcached_is_allocated(ptr))
166 {
167 libmemcached_free(ptr, ptr);
168 }
169 }
170
171 /*
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
174 called.
175 */
176 memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
177 {
178 memcached_return_t rc= MEMCACHED_SUCCESS;
179 memcached_st *new_clone;
180
181 if (source == NULL)
182 return memcached_create(clone);
183
184 if (clone && memcached_is_allocated(clone))
185 {
186 return NULL;
187 }
188
189 new_clone= memcached_create(clone);
190
191 if (new_clone == NULL)
192 return NULL;
193
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;
204
205 new_clone->snd_timeout= source->snd_timeout;
206 new_clone->rcv_timeout= source->rcv_timeout;
207
208 new_clone->on_clone= source->on_clone;
209 new_clone->on_cleanup= source->on_cleanup;
210
211 new_clone->allocators= source->allocators;
212
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;
220
221 if (memcached_server_list(source))
222 rc= memcached_server_push(new_clone, memcached_server_list(source));
223
224 if (rc != MEMCACHED_SUCCESS)
225 {
226 memcached_free(new_clone);
227
228 return NULL;
229 }
230
231
232 if (source->prefix_key_length)
233 {
234 strcpy(new_clone->prefix_key, source->prefix_key);
235 new_clone->prefix_key_length= source->prefix_key_length;
236 }
237
238 rc= run_distribution(new_clone);
239
240 if (rc != MEMCACHED_SUCCESS)
241 {
242 memcached_free(new_clone);
243
244 return NULL;
245 }
246
247 if (source->on_clone)
248 source->on_clone(source, new_clone);
249
250 return new_clone;
251 }
252
253 void *memcached_get_user_data(const memcached_st *ptr)
254 {
255 return ptr->user_data;
256 }
257
258 void *memcached_set_user_data(memcached_st *ptr, void *data)
259 {
260 void *ret= ptr->user_data;
261 ptr->user_data= data;
262
263 return ret;
264 }