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)
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
++;
182 ptr
->hosts
[0].count
++;
184 if (ptr
->number_of_hosts
> 1)
185 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
187 rebalance_wheel(ptr
);
189 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
191 return MEMCACHED_SUCCESS
;
194 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
195 char *hostname
, unsigned int port
,
196 memcached_return
*error
)
199 memcached_server_st
*new_host_list
;
201 if (hostname
== NULL
|| error
== NULL
)
205 port
= MEMCACHED_DEFAULT_PORT
;
207 /* Increment count for hosts */
211 count
+= ptr
[0].count
;
214 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
217 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
221 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
223 /* Backwards compatibility hack */
224 new_host_list
[0].count
++;
226 count
= new_host_list
[0].count
;
228 if (new_host_list
[0].count
> 1)
229 qsort(new_host_list
, count
, sizeof(memcached_server_st
), compare_servers
);
231 new_host_list
[0].count
= count
;
234 *error
= MEMCACHED_SUCCESS
;
235 return new_host_list
;
238 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
246 void memcached_server_list_free(memcached_server_st
*ptr
)
248 server_list_free(NULL
, ptr
);