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 *new_hostname
, unsigned int port
,
10 memcached_connection type
)
12 host
->stack_responses
= 0;
13 host
->cursor_active
= 0;
14 host
->hostname
= new_hostname
;
20 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
24 memcached_server_st
*new_host_list
;
27 return MEMCACHED_SUCCESS
;
29 for (count
= 0; list
[count
].hostname
; count
++);
32 (memcached_server_st
*)realloc(ptr
->hosts
,
33 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
+ 1));
36 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
38 ptr
->hosts
= new_host_list
;
40 for (x
= 0; list
[x
].hostname
; x
++)
42 ptr
->hosts
[ptr
->number_of_hosts
].hostname
= strdup(list
[x
].hostname
);
43 ptr
->hosts
[ptr
->number_of_hosts
].port
= list
[x
].port
;
44 ptr
->hosts
[ptr
->number_of_hosts
].fd
= list
[x
].fd
;
45 ptr
->hosts
[ptr
->number_of_hosts
].stack_responses
= list
[x
].stack_responses
;
46 ptr
->hosts
[ptr
->number_of_hosts
].cursor_active
= list
[x
].cursor_active
;
47 ptr
->hosts
[ptr
->number_of_hosts
].type
= list
[x
].type
;
48 ptr
->number_of_hosts
++;
50 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], NULL
, 0,
51 MEMCACHED_CONNECTION_UNKNOWN
);
53 return MEMCACHED_SUCCESS
;
56 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
59 return MEMCACHED_FAILURE
;
61 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
64 memcached_return
memcached_server_add(memcached_st
*ptr
, char *hostname
, unsigned int port
)
67 port
= MEMCACHED_DEFAULT_PORT
;
70 hostname
= "localhost";
72 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
75 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
77 memcached_connection type
)
79 memcached_server_st
*new_host_list
;
81 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
84 if (ptr
->number_of_hosts
)
86 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
87 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
89 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
90 host_reset(&new_host_list
[ptr
->number_of_hosts
], NULL
, 0,
91 MEMCACHED_CONNECTION_UNKNOWN
);
96 (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * 2);
98 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
99 host_reset(&new_host_list
[0], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
100 host_reset(&new_host_list
[1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
103 ptr
->hosts
= new_host_list
;
106 (char *)malloc(sizeof(char) * (strlen(hostname
)+1));
109 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
111 memset(new_hostname
, 0, strlen(hostname
)+1);
112 memcpy(new_hostname
, hostname
, strlen(hostname
));
113 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], new_hostname
, port
, type
);
114 ptr
->number_of_hosts
++;
116 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
118 return MEMCACHED_SUCCESS
;
121 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
122 char *hostname
, unsigned int port
,
123 memcached_return
*error
)
126 memcached_server_st
*new_host_list
;
133 port
= MEMCACHED_DEFAULT_PORT
;
135 /* Always count so that we keep a free host at the end */
138 for (count
= 0; ptr
[count
].hostname
; count
++);
140 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
143 host_reset(&new_host_list
[count
-1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
148 new_host_list
= (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * count
);
151 host_reset(&new_host_list
[0], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
152 host_reset(&new_host_list
[1], NULL
, 0, MEMCACHED_CONNECTION_UNKNOWN
);
155 new_hostname
= strdup(hostname
);
160 host_reset(&new_host_list
[count
-2], new_hostname
, port
, MEMCACHED_CONNECTION_TCP
);
162 *error
= MEMCACHED_SUCCESS
;
163 return new_host_list
;
165 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
170 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
174 for (x
= 0; ptr
[x
].hostname
; x
++);
179 void memcached_server_list_free(memcached_server_st
*ptr
)
183 for (x
= 0; ptr
[x
].hostname
; x
++)
184 free(ptr
[x
].hostname
);