4 /* Protoypes (static) */
5 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
7 memcached_connection type
);
9 #define MEMCACHED_WHEEL_SIZE 1024
10 #define MEMCACHED_STRIDE 4
11 static void rebalance_wheel(memcached_st
*ptr
)
18 memset(ptr
->wheel
, 0, sizeof(unsigned int) * MEMCACHED_WHEEL_SIZE
);
20 for (latch
= y
= x
= 0; x
< MEMCACHED_WHEEL_SIZE
; x
++, latch
++)
22 if (latch
== MEMCACHED_STRIDE
)
25 if (y
== ptr
->number_of_hosts
)
34 static int compare_servers(const void *p1
, const void *p2
)
37 memcached_server_st
*a
= (memcached_server_st
*)p1
;
38 memcached_server_st
*b
= (memcached_server_st
*)p2
;
40 return_value
= strcmp(a
->hostname
, b
->hostname
);
42 if (return_value
== 0)
44 if (a
->port
> b
->port
)
53 static void host_reset(memcached_st
*ptr
, memcached_server_st
*host
,
54 char *hostname
, unsigned int port
,
55 memcached_connection type
)
57 memset(host
, 0, sizeof(memcached_server_st
));
58 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
59 host
->root
= ptr
? ptr
: NULL
;
63 host
->read_ptr
= host
->read_buffer
;
65 host
->next_retry
= ptr
->retry_timeout
;
66 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
69 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
76 for (x
= 0; x
< servers
->count
; x
++)
77 if (servers
[x
].address_info
)
78 freeaddrinfo(servers
[x
].address_info
);
80 if (ptr
&& ptr
->call_free
)
81 ptr
->call_free(ptr
, servers
);
86 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
90 memcached_server_st
*new_host_list
;
93 return MEMCACHED_SUCCESS
;
97 if (ptr
->call_realloc
)
99 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
100 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
103 (memcached_server_st
*)realloc(ptr
->hosts
,
104 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
107 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
109 ptr
->hosts
= new_host_list
;
111 for (x
= 0; x
< count
; x
++)
113 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
114 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
115 list
[x
].port
, list
[x
].type
);
116 ptr
->number_of_hosts
++;
118 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
120 if (ptr
->number_of_hosts
> 1)
121 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
123 rebalance_wheel(ptr
);
125 return MEMCACHED_SUCCESS
;
128 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
131 return MEMCACHED_FAILURE
;
133 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
136 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
141 port
= MEMCACHED_DEFAULT_PORT
;
144 hostname
= "localhost";
146 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
149 memcached_return
memcached_server_add(memcached_st
*ptr
,
154 port
= MEMCACHED_DEFAULT_PORT
;
157 hostname
= "localhost";
159 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
162 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
164 memcached_connection type
)
166 memcached_server_st
*new_host_list
;
167 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
170 if (ptr
->call_realloc
)
171 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
172 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
174 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
175 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
176 if (new_host_list
== NULL
)
177 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
179 ptr
->hosts
= new_host_list
;
181 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
182 ptr
->number_of_hosts
++;
183 ptr
->hosts
[0].count
++;
185 if (ptr
->number_of_hosts
> 1)
186 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
188 rebalance_wheel(ptr
);
190 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
192 return MEMCACHED_SUCCESS
;
195 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
196 char *hostname
, unsigned int port
,
197 memcached_return
*error
)
200 memcached_server_st
*new_host_list
;
202 if (hostname
== NULL
|| error
== NULL
)
206 port
= MEMCACHED_DEFAULT_PORT
;
208 /* Increment count for hosts */
212 count
+= ptr
[0].count
;
215 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
218 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
222 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
224 /* Backwards compatibility hack */
225 new_host_list
[0].count
++;
227 count
= new_host_list
[0].count
;
229 if (new_host_list
[0].count
> 1)
230 qsort(new_host_list
, count
, sizeof(memcached_server_st
), compare_servers
);
232 new_host_list
[0].count
= count
;
235 *error
= MEMCACHED_SUCCESS
;
236 return new_host_list
;
239 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
247 void memcached_server_list_free(memcached_server_st
*ptr
)
249 server_list_free(NULL
, ptr
);