Remove mass calloc() on memcached_st creation.
[awesomized/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);
71
72 self->on_clone= NULL;
73 self->on_cleanup= NULL;
74 self->get_key_failure= NULL;
75 self->delete_trigger= NULL;
76 self->callbacks= NULL;
77 }
78
79 memcached_st *memcached_create(memcached_st *ptr)
80 {
81 if (ptr == NULL)
82 {
83 ptr= (memcached_st *)malloc(sizeof(memcached_st));
84
85 if (! ptr)
86 {
87 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
88 }
89
90 ptr->options.is_allocated= true;
91 }
92 else
93 {
94 ptr->options.is_allocated= false;
95 }
96
97 #if 0
98 memcached_set_purging(ptr, false);
99 memcached_set_processing_input(ptr, false);
100 #endif
101
102 _memcached_init(ptr);
103
104 if (! memcached_result_create(ptr, &ptr->result))
105 {
106 memcached_free(ptr);
107 return NULL;
108 }
109
110 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
111
112 return ptr;
113 }
114
115 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
116 {
117 uint32_t x;
118
119 if (servers == NULL)
120 return;
121
122 for (x= 0; x < memcached_servers_count(servers); x++)
123 {
124 if (servers[x].address_info)
125 {
126 freeaddrinfo(servers[x].address_info);
127 servers[x].address_info= NULL;
128 }
129 }
130
131 if (ptr)
132 {
133 ptr->call_free(ptr, servers);
134 }
135 else
136 {
137 free(servers);
138 }
139 }
140
141 void memcached_servers_reset(memcached_st *ptr)
142 {
143 server_list_free(ptr, memcached_server_list(ptr));
144
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;
149 }
150
151 void memcached_free(memcached_st *ptr)
152 {
153 /* If we have anything open, lets close it now */
154 memcached_quit(ptr);
155 server_list_free(ptr, memcached_server_list(ptr));
156 memcached_result_free(&ptr->result);
157
158 if (ptr->on_cleanup)
159 ptr->on_cleanup(ptr);
160
161 if (ptr->continuum)
162 ptr->call_free(ptr, ptr->continuum);
163
164 if (memcached_is_allocated(ptr))
165 {
166 ptr->call_free(ptr, ptr);
167 }
168 }
169
170 /*
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
173 called.
174 */
175 memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
176 {
177 memcached_return_t rc= MEMCACHED_SUCCESS;
178 memcached_st *new_clone;
179
180 if (source == NULL)
181 return memcached_create(clone);
182
183 if (clone && memcached_is_allocated(clone))
184 {
185 return NULL;
186 }
187
188 new_clone= memcached_create(clone);
189
190 if (new_clone == NULL)
191 return NULL;
192
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;
203
204 new_clone->snd_timeout= source->snd_timeout;
205 new_clone->rcv_timeout= source->rcv_timeout;
206
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;
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 }