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 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
51 memcached_server_st
*new_host_list
;
54 return MEMCACHED_SUCCESS
;
59 (memcached_server_st
*)realloc(ptr
->hosts
,
60 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
63 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
65 ptr
->hosts
= new_host_list
;
67 for (x
= 0; x
< count
; x
++)
69 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
70 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
71 list
[x
].port
, list
[x
].type
);
72 ptr
->number_of_hosts
++;
74 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
78 return MEMCACHED_SUCCESS
;
81 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
84 return MEMCACHED_FAILURE
;
86 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
89 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
94 port
= MEMCACHED_DEFAULT_PORT
;
97 hostname
= "localhost";
99 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
102 memcached_return
memcached_server_add(memcached_st
*ptr
,
107 port
= MEMCACHED_DEFAULT_PORT
;
110 hostname
= "localhost";
112 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
115 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
117 memcached_connection type
)
119 memcached_server_st
*new_host_list
;
120 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
123 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
124 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
126 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
128 ptr
->hosts
= new_host_list
;
130 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
131 ptr
->number_of_hosts
++;
132 ptr
->hosts
[0].count
++;
134 rebalance_wheel(ptr
);
136 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
138 return MEMCACHED_SUCCESS
;
141 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
142 char *hostname
, unsigned int port
,
143 memcached_return
*error
)
146 memcached_server_st
*new_host_list
;
148 if (hostname
== NULL
|| error
== NULL
)
152 port
= MEMCACHED_DEFAULT_PORT
;
154 /* Increment count for hosts */
158 count
+= ptr
[0].count
;
161 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
164 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
168 host_reset(&new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
169 new_host_list
[0].count
++;
172 *error
= MEMCACHED_SUCCESS
;
173 return new_host_list
;
176 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
184 void memcached_server_list_free(memcached_server_st
*ptr
)
191 for (x
= 0; x
< ptr
->count
; x
++)
192 if (ptr
[x
].address_info
)
193 freeaddrinfo(ptr
[x
].address_info
);