4 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
8 memcached_server_st
*new_host_list
;
11 return MEMCACHED_SUCCESS
;
13 for (count
= 0; list
[count
].hostname
; count
++);
16 (memcached_server_st
*)realloc(ptr
->hosts
,
17 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
+ 1));
20 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
22 ptr
->hosts
= new_host_list
;
24 for (x
= 0; list
[x
].hostname
; x
++)
26 ptr
->hosts
[ptr
->number_of_hosts
].hostname
= strdup(list
[x
].hostname
);
27 ptr
->hosts
[ptr
->number_of_hosts
].port
= list
[x
].port
;
28 ptr
->hosts
[ptr
->number_of_hosts
].fd
= list
[x
].fd
;
29 ptr
->number_of_hosts
++;
31 memset(&ptr
->hosts
[ptr
->number_of_hosts
], 0, sizeof(memcached_server_st
));
33 return MEMCACHED_SUCCESS
;
36 memcached_return
memcached_server_add(memcached_st
*ptr
, char *hostname
, unsigned int port
)
38 memcached_server_st
*new_host_list
;
40 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
43 port
= MEMCACHED_DEFAULT_PORT
;
46 hostname
= "localhost";
49 if (ptr
->number_of_hosts
)
51 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
52 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
54 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
55 memset(&new_host_list
[ptr
->number_of_hosts
], 0, sizeof(memcached_server_st
));
60 (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * 2);
62 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
63 memset(new_host_list
, 0, sizeof(memcached_server_st
) * 2);
66 ptr
->hosts
= new_host_list
;
69 (char *)malloc(sizeof(char) * (strlen(hostname
)+1));
72 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
74 memset(new_hostname
, 0, strlen(hostname
)+1);
75 memcpy(new_hostname
, hostname
, strlen(hostname
));
76 ptr
->hosts
[ptr
->number_of_hosts
].hostname
= new_hostname
;
77 ptr
->hosts
[ptr
->number_of_hosts
].port
= port
;
78 ptr
->hosts
[ptr
->number_of_hosts
].fd
= -1;
79 ptr
->number_of_hosts
++;
81 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
83 return MEMCACHED_SUCCESS
;
86 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
87 char *hostname
, unsigned int port
,
88 memcached_return
*error
)
91 memcached_server_st
*new_host_list
;
98 port
= MEMCACHED_DEFAULT_PORT
;
100 /* Always count so that we keep a free host at the end */
103 for (count
= 0; ptr
[count
].hostname
; count
++);
105 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
108 memset(&new_host_list
[count
-1], 0, sizeof(memcached_server_st
));
113 new_host_list
= (memcached_server_st
*)malloc(sizeof(memcached_server_st
) * count
);
116 memset(new_host_list
, 0, sizeof(memcached_server_st
) * 2);
119 new_hostname
= strdup(hostname
);
124 new_host_list
[count
-2].hostname
= new_hostname
;
125 new_host_list
[count
-2].port
= port
;
126 new_host_list
[count
-2].fd
= -1;
128 *error
= MEMCACHED_SUCCESS
;
129 return new_host_list
;
131 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
136 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
140 for (x
= 0; ptr
[x
].hostname
; x
++);
145 void memcached_server_list_free(memcached_server_st
*ptr
)
149 for (x
= 0; ptr
[x
].hostname
; x
++)
150 free(ptr
[x
].hostname
);