4 /* Protoypes (static) */
5 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
7 memcached_connection type
);
9 static void host_reset(memcached_server_st
*host
, char *hostname
, unsigned int port
,
10 memcached_connection type
)
12 memset(host
, 0, sizeof(memcached_server_st
));
13 host
->hostname
= strdup(hostname
);
17 host
->read_ptr
= host
->read_buffer
;
18 host
->write_ptr
= host
->write_buffer
;
19 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
22 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
26 memcached_server_st
*new_host_list
;
29 return MEMCACHED_SUCCESS
;
34 (memcached_server_st
*)realloc(ptr
->hosts
,
35 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
38 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
40 ptr
->hosts
= new_host_list
;
42 for (x
= 0; x
< count
; x
++)
44 WATCHPOINT_ASSERT(list
[x
].hostname
);
45 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
46 list
[x
].port
, list
[x
].type
);
47 ptr
->number_of_hosts
++;
49 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
51 return MEMCACHED_SUCCESS
;
54 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
57 return MEMCACHED_FAILURE
;
59 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
62 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
67 port
= MEMCACHED_DEFAULT_PORT
;
70 hostname
= "localhost";
72 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
75 memcached_return
memcached_server_add(memcached_st
*ptr
,
80 port
= MEMCACHED_DEFAULT_PORT
;
83 hostname
= "localhost";
85 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
88 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
90 memcached_connection type
)
92 memcached_server_st
*new_host_list
;
93 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
96 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
97 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
99 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
101 ptr
->hosts
= new_host_list
;
103 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
104 ptr
->number_of_hosts
++;
105 ptr
->hosts
[0].count
++;
107 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
109 return MEMCACHED_SUCCESS
;
112 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
113 char *hostname
, unsigned int port
,
114 memcached_return
*error
)
117 memcached_server_st
*new_host_list
;
123 port
= MEMCACHED_DEFAULT_PORT
;
125 /* Increment count for hosts */
129 count
+= ptr
[0].count
;
132 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
135 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
139 host_reset(&new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
140 new_host_list
[0].count
++;
143 *error
= MEMCACHED_SUCCESS
;
144 return new_host_list
;
147 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
156 void memcached_server_list_free(memcached_server_st
*ptr
)
163 for (x
= 0; x
< ptr
[0].count
; x
++)
164 free(ptr
[x
].hostname
);