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 void sort_hosts(memcached_st
*ptr
)
54 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
57 static void host_reset(memcached_st
*ptr
, memcached_server_st
*host
,
58 char *hostname
, unsigned int port
,
59 memcached_connection type
)
61 memset(host
, 0, sizeof(memcached_server_st
));
62 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
63 host
->root
= ptr
? ptr
: NULL
;
67 host
->read_ptr
= host
->read_buffer
;
69 host
->next_retry
= ptr
->retry_timeout
;
70 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
73 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
80 for (x
= 0; x
< servers
->count
; x
++)
81 if (servers
[x
].address_info
)
82 freeaddrinfo(servers
[x
].address_info
);
84 if (ptr
&& ptr
->call_free
)
85 ptr
->call_free(ptr
, servers
);
90 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
94 memcached_server_st
*new_host_list
;
97 return MEMCACHED_SUCCESS
;
101 if (ptr
->call_realloc
)
103 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
104 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
107 (memcached_server_st
*)realloc(ptr
->hosts
,
108 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
111 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
113 ptr
->hosts
= new_host_list
;
115 for (x
= 0; x
< count
; x
++)
117 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
118 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
119 list
[x
].port
, list
[x
].type
);
120 ptr
->number_of_hosts
++;
122 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
124 if (ptr
->number_of_hosts
> 1 && ptr
->flags
& MEM_USE_SORT_HOSTS
)
127 rebalance_wheel(ptr
);
129 return MEMCACHED_SUCCESS
;
132 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
135 return MEMCACHED_FAILURE
;
137 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
140 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
145 port
= MEMCACHED_DEFAULT_PORT
;
148 hostname
= "localhost";
150 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
153 memcached_return
memcached_server_add(memcached_st
*ptr
,
158 port
= MEMCACHED_DEFAULT_PORT
;
161 hostname
= "localhost";
163 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
166 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
168 memcached_connection type
)
170 memcached_server_st
*new_host_list
;
171 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
174 if (ptr
->call_realloc
)
175 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
176 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
178 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
179 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
180 if (new_host_list
== NULL
)
181 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
183 ptr
->hosts
= new_host_list
;
185 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
186 ptr
->number_of_hosts
++;
188 if (ptr
->number_of_hosts
> 1 && ptr
->flags
& MEM_USE_SORT_HOSTS
)
191 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
193 rebalance_wheel(ptr
);
195 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
197 return MEMCACHED_SUCCESS
;
200 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
201 char *hostname
, unsigned int port
,
202 memcached_return
*error
)
205 memcached_server_st
*new_host_list
;
207 if (hostname
== NULL
|| error
== NULL
)
211 port
= MEMCACHED_DEFAULT_PORT
;
213 /* Increment count for hosts */
217 count
+= ptr
[0].count
;
220 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
223 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
227 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
229 /* Backwards compatibility hack */
230 new_host_list
[0].count
= count
;
232 *error
= MEMCACHED_SUCCESS
;
233 return new_host_list
;
236 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
244 void memcached_server_list_free(memcached_server_st
*ptr
)
246 server_list_free(NULL
, ptr
);