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