Fix for sorting when no servers have been added.
[m6w6/libmemcached] / libmemcached / memcached_hosts.c
1 #include "common.h"
2
3 /* Protoypes (static) */
4 static memcached_return server_add(memcached_st *ptr, char *hostname,
5 unsigned int port,
6 memcached_connection type);
7
8 #define MEMCACHED_WHEEL_SIZE 1024
9 #define MEMCACHED_STRIDE 4
10 static void rebalance_wheel(memcached_st *ptr)
11 {
12 unsigned int x;
13 unsigned int y;
14 unsigned int latch;
15
16 /* Seed the Wheel */
17 memset(ptr->wheel, 0, sizeof(unsigned int) * MEMCACHED_WHEEL_SIZE);
18
19 for (latch= y= x= 0; x < MEMCACHED_WHEEL_SIZE; x++, latch++)
20 {
21 if (latch == MEMCACHED_STRIDE)
22 {
23 y++;
24 if (y == ptr->number_of_hosts)
25 y= 0;
26 latch= 0;
27 }
28
29 ptr->wheel[x]= y;
30 }
31 }
32
33 static int compare_servers(const void *p1, const void *p2)
34 {
35 int return_value;
36 memcached_server_st *a= (memcached_server_st *)p1;
37 memcached_server_st *b= (memcached_server_st *)p2;
38
39 return_value= strcmp(a->hostname, b->hostname);
40
41 if (return_value == 0)
42 {
43 if (a->port > b->port)
44 return_value++;
45 else
46 return_value--;
47 }
48
49 return return_value;
50 }
51
52 void sort_hosts(memcached_st *ptr)
53 {
54 if (ptr->number_of_hosts)
55 {
56 qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers);
57 ptr->hosts[0].count= ptr->number_of_hosts;
58 }
59 }
60
61 static void host_reset(memcached_st *ptr, memcached_server_st *host,
62 char *hostname, unsigned int port,
63 memcached_connection type)
64 {
65 memset(host, 0, sizeof(memcached_server_st));
66 strncpy(host->hostname, hostname, MEMCACHED_MAX_HOST_LENGTH - 1);
67 host->root= ptr ? ptr : NULL;
68 host->port= port;
69 host->fd= -1;
70 host->type= type;
71 host->read_ptr= host->read_buffer;
72 if (ptr)
73 host->next_retry= ptr->retry_timeout;
74 host->sockaddr_inited= MEMCACHED_NOT_ALLOCATED;
75 }
76
77 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
78 {
79 unsigned int x;
80
81 if (servers == NULL)
82 return;
83
84 for (x= 0; x < servers->count; x++)
85 if (servers[x].address_info)
86 freeaddrinfo(servers[x].address_info);
87
88 if (ptr && ptr->call_free)
89 ptr->call_free(ptr, servers);
90 else
91 free(servers);
92 }
93
94 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
95 {
96 unsigned int x;
97 uint16_t count;
98 memcached_server_st *new_host_list;
99
100 if (!list)
101 return MEMCACHED_SUCCESS;
102
103 count= list[0].count;
104
105 if (ptr->call_realloc)
106 new_host_list=
107 (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
108 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
109 else
110 new_host_list=
111 (memcached_server_st *)realloc(ptr->hosts,
112 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
113
114 if (!new_host_list)
115 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
116
117 ptr->hosts= new_host_list;
118
119 for (x= 0; x < count; x++)
120 {
121 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
122 host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
123 list[x].port, list[x].type);
124 ptr->number_of_hosts++;
125 }
126 ptr->hosts[0].count= ptr->number_of_hosts;
127
128 if (ptr->flags & MEM_USE_SORT_HOSTS)
129 sort_hosts(ptr);
130
131 rebalance_wheel(ptr);
132
133 return MEMCACHED_SUCCESS;
134 }
135
136 memcached_return memcached_server_add_unix_socket(memcached_st *ptr, char *filename)
137 {
138 if (!filename)
139 return MEMCACHED_FAILURE;
140
141 return server_add(ptr, filename, 0, MEMCACHED_CONNECTION_UNIX_SOCKET);
142 }
143
144 memcached_return memcached_server_add_udp(memcached_st *ptr,
145 char *hostname,
146 unsigned int port)
147 {
148 if (!port)
149 port= MEMCACHED_DEFAULT_PORT;
150
151 if (!hostname)
152 hostname= "localhost";
153
154 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_UDP);
155 }
156
157 memcached_return memcached_server_add(memcached_st *ptr,
158 char *hostname,
159 unsigned int port)
160 {
161 if (!port)
162 port= MEMCACHED_DEFAULT_PORT;
163
164 if (!hostname)
165 hostname= "localhost";
166
167 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_TCP);
168 }
169
170 static memcached_return server_add(memcached_st *ptr, char *hostname,
171 unsigned int port,
172 memcached_connection type)
173 {
174 memcached_server_st *new_host_list;
175 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
176
177
178 if (ptr->call_realloc)
179 new_host_list= (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
180 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
181 else
182 new_host_list= (memcached_server_st *)realloc(ptr->hosts,
183 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
184 if (new_host_list == NULL)
185 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
186
187 ptr->hosts= new_host_list;
188
189 host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, type);
190 ptr->number_of_hosts++;
191
192 if (ptr->flags & MEM_USE_SORT_HOSTS)
193 sort_hosts(ptr);
194
195 ptr->hosts[0].count= ptr->number_of_hosts;
196
197 rebalance_wheel(ptr);
198
199 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
200
201 return MEMCACHED_SUCCESS;
202 }
203
204 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
205 char *hostname, unsigned int port,
206 memcached_return *error)
207 {
208 unsigned int count;
209 memcached_server_st *new_host_list;
210
211 if (hostname == NULL || error == NULL)
212 return NULL;
213
214 if (!port)
215 port= MEMCACHED_DEFAULT_PORT;
216
217 /* Increment count for hosts */
218 count= 1;
219 if (ptr != NULL)
220 {
221 count+= ptr[0].count;
222 }
223
224 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
225 if (!new_host_list)
226 {
227 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
228 return NULL;
229 }
230
231 host_reset(NULL, &new_host_list[count-1], hostname, port, MEMCACHED_CONNECTION_TCP);
232
233 /* Backwards compatibility hack */
234 new_host_list[0].count= count;
235
236 *error= MEMCACHED_SUCCESS;
237 return new_host_list;
238 }
239
240 unsigned int memcached_server_list_count(memcached_server_st *ptr)
241 {
242 if (ptr == NULL)
243 return 0;
244
245 return ptr[0].count;
246 }
247
248 void memcached_server_list_free(memcached_server_st *ptr)
249 {
250 server_list_free(NULL, ptr);
251 }