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 void host_reset(memcached_st
*ptr
, memcached_server_st
*host
,
35 char *hostname
, unsigned int port
,
36 memcached_connection type
)
38 memset(host
, 0, sizeof(memcached_server_st
));
39 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
40 host
->root
= ptr
? ptr
: NULL
;
44 host
->read_ptr
= host
->read_buffer
;
45 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
48 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
55 for (x
= 0; x
< servers
->count
; x
++)
56 if (servers
[x
].address_info
)
57 freeaddrinfo(servers
[x
].address_info
);
59 if (ptr
&& ptr
->call_free
)
60 ptr
->call_free(ptr
, servers
);
65 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
69 memcached_server_st
*new_host_list
;
72 return MEMCACHED_SUCCESS
;
76 if (ptr
->call_realloc
)
78 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
79 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
82 (memcached_server_st
*)realloc(ptr
->hosts
,
83 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
86 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
88 ptr
->hosts
= new_host_list
;
90 for (x
= 0; x
< count
; x
++)
92 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
93 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
94 list
[x
].port
, list
[x
].type
);
95 ptr
->number_of_hosts
++;
97 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
101 return MEMCACHED_SUCCESS
;
104 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
107 return MEMCACHED_FAILURE
;
109 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
112 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
117 port
= MEMCACHED_DEFAULT_PORT
;
120 hostname
= "localhost";
122 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
125 memcached_return
memcached_server_add(memcached_st
*ptr
,
130 port
= MEMCACHED_DEFAULT_PORT
;
133 hostname
= "localhost";
135 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
138 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
140 memcached_connection type
)
142 memcached_server_st
*new_host_list
;
143 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
146 if (ptr
->call_realloc
)
147 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
148 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
150 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
151 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
152 if (new_host_list
== NULL
)
153 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
155 ptr
->hosts
= new_host_list
;
157 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
158 ptr
->number_of_hosts
++;
159 ptr
->hosts
[0].count
++;
161 rebalance_wheel(ptr
);
163 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
165 return MEMCACHED_SUCCESS
;
168 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
169 char *hostname
, unsigned int port
,
170 memcached_return
*error
)
173 memcached_server_st
*new_host_list
;
175 if (hostname
== NULL
|| error
== NULL
)
179 port
= MEMCACHED_DEFAULT_PORT
;
181 /* Increment count for hosts */
185 count
+= ptr
[0].count
;
188 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
191 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
195 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
197 /* Backwards compatibility hack */
198 new_host_list
[0].count
++;
201 *error
= MEMCACHED_SUCCESS
;
202 return new_host_list
;
205 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
213 void memcached_server_list_free(memcached_server_st
*ptr
)
215 server_list_free(NULL
, ptr
);