3 /* Protoypes (static) */
4 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
6 memcached_connection type
);
8 #define MEMCACHED_WHEEL_SIZE 1024
9 #define MEMCACHED_STRIDE 4
10 static void rebalance_wheel(memcached_st
*ptr
)
17 memset(ptr
->wheel
, 0, sizeof(unsigned int) * MEMCACHED_WHEEL_SIZE
);
19 for (latch
= y
= x
= 0; x
< MEMCACHED_WHEEL_SIZE
; x
++, latch
++)
21 if (latch
== MEMCACHED_STRIDE
)
24 if (y
== ptr
->number_of_hosts
)
33 static int compare_servers(const void *p1
, const void *p2
)
36 memcached_server_st
*a
= (memcached_server_st
*)p1
;
37 memcached_server_st
*b
= (memcached_server_st
*)p2
;
39 return_value
= strcmp(a
->hostname
, b
->hostname
);
41 if (return_value
== 0)
43 if (a
->port
> b
->port
)
52 static void host_reset(memcached_st
*ptr
, memcached_server_st
*host
,
53 char *hostname
, unsigned int port
,
54 memcached_connection type
)
56 memset(host
, 0, sizeof(memcached_server_st
));
57 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
58 host
->root
= ptr
? ptr
: NULL
;
62 host
->read_ptr
= host
->read_buffer
;
64 host
->next_retry
= ptr
->retry_timeout
;
65 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
68 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
75 for (x
= 0; x
< servers
->count
; x
++)
76 if (servers
[x
].address_info
)
77 freeaddrinfo(servers
[x
].address_info
);
79 if (ptr
&& ptr
->call_free
)
80 ptr
->call_free(ptr
, servers
);
85 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
89 memcached_server_st
*new_host_list
;
92 return MEMCACHED_SUCCESS
;
96 if (ptr
->call_realloc
)
98 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
99 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
102 (memcached_server_st
*)realloc(ptr
->hosts
,
103 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
106 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
108 ptr
->hosts
= new_host_list
;
110 for (x
= 0; x
< count
; x
++)
112 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
113 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
114 list
[x
].port
, list
[x
].type
);
115 ptr
->number_of_hosts
++;
117 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
119 if (ptr
->number_of_hosts
> 1 && ptr
->flags
& MEM_USE_SORT_HOSTS
)
120 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
122 rebalance_wheel(ptr
);
124 return MEMCACHED_SUCCESS
;
127 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
130 return MEMCACHED_FAILURE
;
132 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
135 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
140 port
= MEMCACHED_DEFAULT_PORT
;
143 hostname
= "localhost";
145 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
148 memcached_return
memcached_server_add(memcached_st
*ptr
,
153 port
= MEMCACHED_DEFAULT_PORT
;
156 hostname
= "localhost";
158 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
161 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
163 memcached_connection type
)
165 memcached_server_st
*new_host_list
;
166 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
169 if (ptr
->call_realloc
)
170 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
171 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
173 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
174 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
175 if (new_host_list
== NULL
)
176 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
178 ptr
->hosts
= new_host_list
;
180 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
181 ptr
->number_of_hosts
++;
183 if (ptr
->number_of_hosts
> 1 && ptr
->flags
& MEM_USE_SORT_HOSTS
)
184 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
186 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
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
= count
;
227 *error
= MEMCACHED_SUCCESS
;
228 return new_host_list
;
231 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
239 void memcached_server_list_free(memcached_server_st
*ptr
)
241 server_list_free(NULL
, ptr
);