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_server_st
*host
, char *hostname
, unsigned int port
,
35 memcached_connection type
)
37 memset(host
, 0, sizeof(memcached_server_st
));
38 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
42 host
->read_ptr
= host
->read_buffer
;
43 host
->write_ptr
= host
->write_buffer
;
44 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
47 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
54 for (x
= 0; x
< servers
->count
; x
++)
55 if (servers
[x
].address_info
)
56 freeaddrinfo(servers
[x
].address_info
);
58 if (ptr
&& ptr
->call_free
)
59 ptr
->call_free(ptr
, servers
);
64 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
68 memcached_server_st
*new_host_list
;
71 return MEMCACHED_SUCCESS
;
75 if (ptr
->call_realloc
)
77 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
78 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
81 (memcached_server_st
*)realloc(ptr
->hosts
,
82 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
85 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
87 ptr
->hosts
= new_host_list
;
89 for (x
= 0; x
< count
; x
++)
91 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
92 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
93 list
[x
].port
, list
[x
].type
);
94 ptr
->number_of_hosts
++;
96 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
100 return MEMCACHED_SUCCESS
;
103 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
106 return MEMCACHED_FAILURE
;
108 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
111 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
116 port
= MEMCACHED_DEFAULT_PORT
;
119 hostname
= "localhost";
121 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
124 memcached_return
memcached_server_add(memcached_st
*ptr
,
129 port
= MEMCACHED_DEFAULT_PORT
;
132 hostname
= "localhost";
134 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
137 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
139 memcached_connection type
)
141 memcached_server_st
*new_host_list
;
142 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
145 if (ptr
->call_realloc
)
146 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
147 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
149 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
150 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
151 if (new_host_list
== NULL
)
152 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
154 ptr
->hosts
= new_host_list
;
156 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
157 ptr
->number_of_hosts
++;
158 ptr
->hosts
[0].count
++;
160 rebalance_wheel(ptr
);
162 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
164 return MEMCACHED_SUCCESS
;
167 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
168 char *hostname
, unsigned int port
,
169 memcached_return
*error
)
172 memcached_server_st
*new_host_list
;
174 if (hostname
== NULL
|| error
== NULL
)
178 port
= MEMCACHED_DEFAULT_PORT
;
180 /* Increment count for hosts */
184 count
+= ptr
[0].count
;
187 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
190 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
194 host_reset(&new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
195 new_host_list
[0].count
++;
198 *error
= MEMCACHED_SUCCESS
;
199 return new_host_list
;
202 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
210 void memcached_server_list_free(memcached_server_st
*ptr
)
212 server_list_free(NULL
, ptr
);