4 static void host_reset(memcached_server_st
*host
, char *new_hostname
, unsigned int port
)
6 host
->stack_responses
= 0;
7 host
->cursor_active
= 0;
8 host
->hostname
= new_hostname
;
13 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
17 memcached_server_st
*new_host_list
;
20 return MEMCACHED_SUCCESS
;
22 for (count
= 0; list
[count
].hostname
; count
++);
25 (memcached_server_st
*)realloc(ptr
->hosts
,
26 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
+ 1));
29 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
31 ptr
->hosts
= new_host_list
;
33 for (x
= 0; list
[x
].hostname
; x
++)
35 ptr
->hosts
[ptr
->number_of_hosts
].hostname
= strdup(list
[x
].hostname
);
36 ptr
->hosts
[ptr
->number_of_hosts
].port
= list
[x
].port
;
37 ptr
->hosts
[ptr
->number_of_hosts
].fd
= list
[x
].fd
;
38 ptr
->hosts
[ptr
->number_of_hosts
].stack_responses
= list
[x
].stack_responses
;
39 ptr
->hosts
[ptr
->number_of_hosts
].cursor_active
= list
[x
].cursor_active
;
40 ptr
->number_of_hosts
++;
42 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], NULL
, 0);
44 return MEMCACHED_SUCCESS
;
47 memcached_return
memcached_server_add(memcached_st
*ptr
, char *hostname
, unsigned int port
)
49 memcached_server_st
*new_host_list
;
51 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
54 port
= MEMCACHED_DEFAULT_PORT
;
57 hostname
= "localhost";
60 if (ptr
->number_of_hosts
)
62 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
63 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
65 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
66 host_reset(&new_host_list
[ptr
->number_of_hosts
], NULL
, 0);
71 (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * 2);
73 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
74 host_reset(&new_host_list
[0], NULL
, 0);
75 host_reset(&new_host_list
[1], NULL
, 0);
78 ptr
->hosts
= new_host_list
;
81 (char *)malloc(sizeof(char) * (strlen(hostname
)+1));
84 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
86 memset(new_hostname
, 0, strlen(hostname
)+1);
87 memcpy(new_hostname
, hostname
, strlen(hostname
));
88 host_reset(&ptr
->hosts
[ptr
->number_of_hosts
], new_hostname
, port
);
89 ptr
->number_of_hosts
++;
91 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
93 return MEMCACHED_SUCCESS
;
96 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
97 char *hostname
, unsigned int port
,
98 memcached_return
*error
)
101 memcached_server_st
*new_host_list
;
108 port
= MEMCACHED_DEFAULT_PORT
;
110 /* Always count so that we keep a free host at the end */
113 for (count
= 0; ptr
[count
].hostname
; count
++);
115 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
118 host_reset(&new_host_list
[count
-1], NULL
, 0);
123 new_host_list
= (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * count
);
126 host_reset(&new_host_list
[0], NULL
, 0);
127 host_reset(&new_host_list
[1], NULL
, 0);
130 new_hostname
= strdup(hostname
);
135 host_reset(&new_host_list
[count
-2], new_hostname
, port
);
137 *error
= MEMCACHED_SUCCESS
;
138 return new_host_list
;
140 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
145 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
149 for (x
= 0; ptr
[x
].hostname
; x
++);
154 void memcached_server_list_free(memcached_server_st
*ptr
)
158 for (x
= 0; ptr
[x
].hostname
; x
++)
159 free(ptr
[x
].hostname
);