ad7ed06b668585b96df7d67d68aad999eb6a43f9
[awesomized/libmemcached] / lib / memcached_hosts.c
1 #include <memcached.h>
2 #include "common.h"
3
4 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
5 {
6 unsigned int x;
7 unsigned int count;
8 memcached_server_st *new_host_list;
9
10 if (!list)
11 return MEMCACHED_SUCCESS;
12
13 for (count= 0; list[count].hostname; count++);
14
15 new_host_list=
16 (memcached_server_st *)realloc(ptr->hosts,
17 sizeof(memcached_server_st) * (count + ptr->number_of_hosts + 1));
18
19 if (!new_host_list)
20 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
21
22 ptr->hosts= new_host_list;
23
24 for (x= 0; list[x].hostname; x++)
25 {
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++;
30 }
31 memset(&ptr->hosts[ptr->number_of_hosts], 0, sizeof(memcached_server_st));
32
33 return MEMCACHED_SUCCESS;
34 }
35
36 memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigned int port)
37 {
38 memcached_server_st *new_host_list;
39 char *new_hostname;
40 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
41
42 if (!port)
43 port= MEMCACHED_DEFAULT_PORT;
44
45 if (!hostname)
46 hostname= "localhost";
47
48
49 if (ptr->number_of_hosts)
50 {
51 new_host_list= (memcached_server_st *)realloc(ptr->hosts,
52 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
53 if (!new_host_list)
54 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
55 memset(&new_host_list[ptr->number_of_hosts], 0, sizeof(memcached_server_st));
56 }
57 else
58 {
59 new_host_list=
60 (memcached_server_st *)malloc(sizeof(memcached_server_st) * 2);
61 if (!new_host_list)
62 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
63 memset(new_host_list, 0, sizeof(memcached_server_st) * 2);
64 }
65
66 ptr->hosts= new_host_list;
67
68 new_hostname=
69 (char *)malloc(sizeof(char) * (strlen(hostname)+1));
70
71 if (!new_hostname)
72 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
73
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++;
80
81 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
82
83 return MEMCACHED_SUCCESS;
84 }
85
86 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
87 char *hostname, unsigned int port,
88 memcached_return *error)
89 {
90 unsigned int count;
91 memcached_server_st *new_host_list;
92 char *new_hostname;
93
94 if (!hostname)
95 return ptr;
96
97 if (!port)
98 port= MEMCACHED_DEFAULT_PORT;
99
100 /* Always count so that we keep a free host at the end */
101 if (ptr)
102 {
103 for (count= 0; ptr[count].hostname; count++);
104 count+= 2;
105 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
106 if (!new_host_list)
107 goto error;
108 memset(&new_host_list[count-1], 0, sizeof(memcached_server_st));
109 }
110 else
111 {
112 count= 2;
113 new_host_list= (memcached_server_st *)malloc(sizeof(memcached_server_st) * count);
114 if (!new_host_list)
115 goto error;
116 memset(new_host_list, 0, sizeof(memcached_server_st) * 2);
117 }
118
119 new_hostname= strdup(hostname);
120
121 if (!new_hostname)
122 goto error;
123
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;
127
128 *error= MEMCACHED_SUCCESS;
129 return new_host_list;
130 error:
131 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
132
133 return NULL;
134 }
135
136 unsigned int memcached_server_list_count(memcached_server_st *ptr)
137 {
138 unsigned int x;
139
140 for (x= 0; ptr[x].hostname; x++);
141
142 return x;
143 }
144
145 void memcached_server_list_free(memcached_server_st *ptr)
146 {
147 unsigned int x;
148
149 for (x= 0; ptr[x].hostname; x++)
150 {
151 if (ptr[x].fd > 0)
152 close(ptr[x].fd);
153
154 free(ptr[x].hostname);
155 }
156
157 free(ptr);
158 }