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