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
;
64 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
67 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
74 for (x
= 0; x
< servers
->count
; x
++)
75 if (servers
[x
].address_info
)
76 freeaddrinfo(servers
[x
].address_info
);
78 if (ptr
&& ptr
->call_free
)
79 ptr
->call_free(ptr
, servers
);
84 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
88 memcached_server_st
*new_host_list
;
91 return MEMCACHED_SUCCESS
;
95 if (ptr
->call_realloc
)
97 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
98 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
101 (memcached_server_st
*)realloc(ptr
->hosts
,
102 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
105 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
107 ptr
->hosts
= new_host_list
;
109 for (x
= 0; x
< count
; x
++)
111 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
112 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
113 list
[x
].port
, list
[x
].type
);
114 ptr
->number_of_hosts
++;
116 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
118 if (ptr
->number_of_hosts
> 1)
119 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
121 rebalance_wheel(ptr
);
123 return MEMCACHED_SUCCESS
;
126 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
129 return MEMCACHED_FAILURE
;
131 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
134 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
139 port
= MEMCACHED_DEFAULT_PORT
;
142 hostname
= "localhost";
144 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
147 memcached_return
memcached_server_add(memcached_st
*ptr
,
152 port
= MEMCACHED_DEFAULT_PORT
;
155 hostname
= "localhost";
157 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
160 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
162 memcached_connection type
)
164 memcached_server_st
*new_host_list
;
165 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
168 if (ptr
->call_realloc
)
169 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
170 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
172 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
173 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
174 if (new_host_list
== NULL
)
175 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
177 ptr
->hosts
= new_host_list
;
179 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
180 ptr
->number_of_hosts
++;
181 ptr
->hosts
[0].count
++;
183 if (ptr
->number_of_hosts
> 1)
184 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
186 rebalance_wheel(ptr
);
188 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
190 return MEMCACHED_SUCCESS
;
193 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
194 char *hostname
, unsigned int port
,
195 memcached_return
*error
)
198 memcached_server_st
*new_host_list
;
200 if (hostname
== NULL
|| error
== NULL
)
204 port
= MEMCACHED_DEFAULT_PORT
;
206 /* Increment count for hosts */
210 count
+= ptr
[0].count
;
213 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
216 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
220 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
222 /* Backwards compatibility hack */
223 new_host_list
[0].count
++;
225 count
= new_host_list
[0].count
;
227 if (new_host_list
[0].count
> 1)
228 qsort(new_host_list
, count
, sizeof(memcached_server_st
), compare_servers
);
230 new_host_list
[0].count
= count
;
233 *error
= MEMCACHED_SUCCESS
;
234 return new_host_list
;
237 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
245 void memcached_server_list_free(memcached_server_st
*ptr
)
247 server_list_free(NULL
, ptr
);