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 range
= (MEMCACHED_WHEEL_SIZE
/ ptr
->number_of_hosts
);
21 memset(ptr
->wheel
, 0, sizeof(unsigned int) * MEMCACHED_WHEEL_SIZE
);
23 for (latch
= y
= x
= 0; x
< MEMCACHED_WHEEL_SIZE
; x
++, latch
++)
25 if (latch
== MEMCACHED_STRIDE
)
28 if (y
== ptr
->number_of_hosts
)
37 static void host_reset(memcached_server_st
*host
, char *hostname
, unsigned int port
,
38 memcached_connection type
)
40 memset(host
, 0, sizeof(memcached_server_st
));
41 memcpy(host
->hostname
, hostname
, strlen(hostname
));
45 host
->read_ptr
= host
->read_buffer
;
46 host
->write_ptr
= host
->write_buffer
;
47 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
50 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
54 memcached_server_st
*new_host_list
;
57 return MEMCACHED_SUCCESS
;
62 (memcached_server_st
*)realloc(ptr
->hosts
,
63 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
66 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
68 ptr
->hosts
= new_host_list
;
70 for (x
= 0; x
< count
; x
++)
72 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
73 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
74 list
[x
].port
, list
[x
].type
);
75 ptr
->number_of_hosts
++;
77 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
81 return MEMCACHED_SUCCESS
;
84 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
87 return MEMCACHED_FAILURE
;
89 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
92 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
97 port
= MEMCACHED_DEFAULT_PORT
;
100 hostname
= "localhost";
102 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
105 memcached_return
memcached_server_add(memcached_st
*ptr
,
110 port
= MEMCACHED_DEFAULT_PORT
;
113 hostname
= "localhost";
115 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
118 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
120 memcached_connection type
)
122 memcached_server_st
*new_host_list
;
123 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
126 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
127 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
129 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
131 ptr
->hosts
= new_host_list
;
133 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
134 ptr
->number_of_hosts
++;
135 ptr
->hosts
[0].count
++;
137 rebalance_wheel(ptr
);
139 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
141 return MEMCACHED_SUCCESS
;
144 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
145 char *hostname
, unsigned int port
,
146 memcached_return
*error
)
149 memcached_server_st
*new_host_list
;
155 port
= MEMCACHED_DEFAULT_PORT
;
157 /* Increment count for hosts */
161 count
+= ptr
[0].count
;
164 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
167 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
171 host_reset(&new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
172 new_host_list
[0].count
++;
175 *error
= MEMCACHED_SUCCESS
;
176 return new_host_list
;
179 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
187 void memcached_server_list_free(memcached_server_st
*ptr
)
194 for (x
= 0; x
< ptr
->count
; x
++)
195 if (ptr
[x
].address_info
)
196 freeaddrinfo(ptr
[x
].address_info
);