Updates for CORK.
[m6w6/libmemcached] / libmemcached / memcached.c
1 /*
2 Memcached library
3 */
4 #include "common.h"
5
6 memcached_st *memcached_create(memcached_st *ptr)
7 {
8 if (ptr == NULL)
9 {
10 ptr= (memcached_st *)calloc(1, sizeof(memcached_st));
11
12 if (! ptr)
13 {
14 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
15 }
16
17 ptr->options.is_allocated= true;
18 }
19 else
20 {
21 memset(ptr, 0, sizeof(memcached_st));
22 }
23
24 ptr->options.is_initialized= true;
25 ptr->options.is_purging= false;
26 ptr->options.is_processing_input= false;
27
28 memcached_set_memory_allocators(ptr, NULL, NULL, NULL, NULL);
29
30 if (! memcached_result_create(ptr, &ptr->result))
31 {
32 memcached_free(ptr);
33 return NULL;
34 }
35 ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
36 ptr->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
37 ptr->retry_timeout= 0;
38 ptr->distribution= MEMCACHED_DISTRIBUTION_MODULA;
39
40
41 ptr->send_size= -1;
42 ptr->recv_size= -1;
43
44 /* TODO, Document why we picked these defaults */
45 ptr->io_msg_watermark= 500;
46 ptr->io_bytes_watermark= 65 * 1024;
47
48 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
49
50 return ptr;
51 }
52
53 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
54 {
55 uint32_t x;
56
57 if (servers == NULL)
58 return;
59
60 for (x= 0; x < memcached_servers_count(servers); x++)
61 {
62 if (servers[x].address_info)
63 {
64 freeaddrinfo(servers[x].address_info);
65 servers[x].address_info= NULL;
66 }
67 }
68
69 if (ptr)
70 {
71 ptr->call_free(ptr, servers);
72 }
73 else
74 {
75 free(servers);
76 }
77 }
78
79 void memcached_servers_reset(memcached_st *ptr)
80 {
81 server_list_free(ptr, memcached_server_list(ptr));
82
83 memcached_server_list_set(ptr, NULL);
84 ptr->number_of_hosts= 0;
85 ptr->last_disconnected_server= NULL;
86 ptr->server_failure_limit= 0;
87 }
88
89 void memcached_free(memcached_st *ptr)
90 {
91 /* If we have anything open, lets close it now */
92 memcached_quit(ptr);
93 server_list_free(ptr, memcached_server_list(ptr));
94 memcached_result_free(&ptr->result);
95
96 if (ptr->on_cleanup)
97 ptr->on_cleanup(ptr);
98
99 if (ptr->continuum)
100 ptr->call_free(ptr, ptr->continuum);
101
102 if (memcached_is_allocated(ptr))
103 {
104 ptr->call_free(ptr, ptr);
105 }
106 else
107 {
108 ptr->options.is_initialized= false;
109 }
110 }
111
112 /*
113 clone is the destination, while source is the structure to clone.
114 If source is NULL the call is the same as if a memcached_create() was
115 called.
116 */
117 memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
118 {
119 memcached_return_t rc= MEMCACHED_SUCCESS;
120 memcached_st *new_clone;
121
122 if (source == NULL)
123 return memcached_create(clone);
124
125 if (clone && memcached_is_allocated(clone))
126 {
127 return NULL;
128 }
129
130 new_clone= memcached_create(clone);
131
132 if (new_clone == NULL)
133 return NULL;
134
135 new_clone->flags= source->flags;
136 new_clone->send_size= source->send_size;
137 new_clone->recv_size= source->recv_size;
138 new_clone->poll_timeout= source->poll_timeout;
139 new_clone->connect_timeout= source->connect_timeout;
140 new_clone->retry_timeout= source->retry_timeout;
141 new_clone->distribution= source->distribution;
142 new_clone->hash= source->hash;
143 new_clone->distribution_hash= source->distribution_hash;
144 new_clone->user_data= source->user_data;
145
146 new_clone->snd_timeout= source->snd_timeout;
147 new_clone->rcv_timeout= source->rcv_timeout;
148
149 new_clone->on_clone= source->on_clone;
150 new_clone->on_cleanup= source->on_cleanup;
151 new_clone->call_free= source->call_free;
152 new_clone->call_malloc= source->call_malloc;
153 new_clone->call_realloc= source->call_realloc;
154 new_clone->call_calloc= source->call_calloc;
155 new_clone->get_key_failure= source->get_key_failure;
156 new_clone->delete_trigger= source->delete_trigger;
157 new_clone->server_failure_limit= source->server_failure_limit;
158 new_clone->io_msg_watermark= source->io_msg_watermark;
159 new_clone->io_bytes_watermark= source->io_bytes_watermark;
160 new_clone->io_key_prefetch= source->io_key_prefetch;
161 new_clone->number_of_replicas= source->number_of_replicas;
162
163 if (memcached_server_list(source))
164 rc= memcached_server_push(new_clone, memcached_server_list(source));
165
166 if (rc != MEMCACHED_SUCCESS)
167 {
168 memcached_free(new_clone);
169
170 return NULL;
171 }
172
173
174 if (source->prefix_key[0] != 0)
175 {
176 strcpy(new_clone->prefix_key, source->prefix_key);
177 new_clone->prefix_key_length= source->prefix_key_length;
178 }
179
180 rc= run_distribution(new_clone);
181
182 if (rc != MEMCACHED_SUCCESS)
183 {
184 memcached_free(new_clone);
185
186 return NULL;
187 }
188
189 if (source->on_clone)
190 source->on_clone(source, new_clone);
191
192 return new_clone;
193 }
194
195 void *memcached_get_user_data(memcached_st *ptr)
196 {
197 return ptr->user_data;
198 }
199
200 void *memcached_set_user_data(memcached_st *ptr, void *data)
201 {
202 void *ret= ptr->user_data;
203 ptr->user_data= data;
204
205 return ret;
206 }