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 return_value
= (int) (a
->port
- b
->port
);
49 void sort_hosts(memcached_st
*ptr
)
51 if (ptr
->number_of_hosts
)
53 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
54 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
58 static void host_reset(memcached_st
*ptr
, memcached_server_st
*host
,
59 char *hostname
, unsigned int port
,
60 memcached_connection type
)
62 memset(host
, 0, sizeof(memcached_server_st
));
63 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
64 host
->root
= ptr
? ptr
: NULL
;
68 host
->read_ptr
= host
->read_buffer
;
70 host
->next_retry
= ptr
->retry_timeout
;
71 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
74 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
81 for (x
= 0; x
< servers
->count
; x
++)
82 if (servers
[x
].address_info
)
83 freeaddrinfo(servers
[x
].address_info
);
85 if (ptr
&& ptr
->call_free
)
86 ptr
->call_free(ptr
, servers
);
91 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
95 memcached_server_st
*new_host_list
;
98 return MEMCACHED_SUCCESS
;
100 count
= list
[0].count
;
102 if (ptr
->call_realloc
)
104 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
105 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
108 (memcached_server_st
*)realloc(ptr
->hosts
,
109 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
112 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
114 ptr
->hosts
= new_host_list
;
116 for (x
= 0; x
< count
; x
++)
118 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
119 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
120 list
[x
].port
, list
[x
].type
);
121 ptr
->number_of_hosts
++;
123 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
125 if (ptr
->flags
& MEM_USE_SORT_HOSTS
)
128 rebalance_wheel(ptr
);
130 return MEMCACHED_SUCCESS
;
133 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
136 return MEMCACHED_FAILURE
;
138 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
141 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
146 port
= MEMCACHED_DEFAULT_PORT
;
149 hostname
= "localhost";
151 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
154 memcached_return
memcached_server_add(memcached_st
*ptr
,
159 port
= MEMCACHED_DEFAULT_PORT
;
162 hostname
= "localhost";
164 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
167 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
169 memcached_connection type
)
171 memcached_server_st
*new_host_list
;
172 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
175 if (ptr
->call_realloc
)
176 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
177 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
179 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
180 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
181 if (new_host_list
== NULL
)
182 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
184 ptr
->hosts
= new_host_list
;
186 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
187 ptr
->number_of_hosts
++;
189 if (ptr
->flags
& MEM_USE_SORT_HOSTS
)
192 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
194 rebalance_wheel(ptr
);
196 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
198 return MEMCACHED_SUCCESS
;
201 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
202 char *hostname
, unsigned int port
,
203 memcached_return
*error
)
206 memcached_server_st
*new_host_list
;
208 if (hostname
== NULL
|| error
== NULL
)
212 port
= MEMCACHED_DEFAULT_PORT
;
214 /* Increment count for hosts */
218 count
+= ptr
[0].count
;
221 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
224 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
228 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
230 /* Backwards compatibility hack */
231 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
);