Removing dead code.
[awesomized/libmemcached] / lib / memcached_hosts.c
1 #include <memcached.h>
2 #include "common.h"
3
4 /* Protoypes (static) */
5 static memcached_return server_add(memcached_st *ptr, char *hostname,
6 unsigned int port,
7 memcached_connection type);
8
9 #define MEMCACHED_WHEEL_SIZE 1024
10 #define MEMCACHED_STRIDE 4
11 static void rebalance_wheel(memcached_st *ptr)
12 {
13 unsigned int x;
14 unsigned int y;
15 unsigned int latch;
16
17 /* Seed the Wheel */
18 memset(ptr->wheel, 0, sizeof(unsigned int) * MEMCACHED_WHEEL_SIZE);
19
20 for (latch= y= x= 0; x < MEMCACHED_WHEEL_SIZE; x++, latch++)
21 {
22 if (latch == MEMCACHED_STRIDE)
23 {
24 y++;
25 if (y == ptr->number_of_hosts)
26 y= 0;
27 latch= 0;
28 }
29
30 ptr->wheel[x]= y;
31 }
32 }
33
34 static void host_reset(memcached_server_st *host, char *hostname, unsigned int port,
35 memcached_connection type)
36 {
37 memset(host, 0, sizeof(memcached_server_st));
38 strncpy(host->hostname, hostname, MEMCACHED_MAX_HOST_LENGTH - 1);
39 host->port= port;
40 host->fd= -1;
41 host->type= type;
42 host->read_ptr= host->read_buffer;
43 host->write_ptr= host->write_buffer;
44 host->sockaddr_inited= MEMCACHED_NOT_ALLOCATED;
45 }
46
47 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
48 {
49 unsigned int x;
50 uint16_t count;
51 memcached_server_st *new_host_list;
52
53 if (!list)
54 return MEMCACHED_SUCCESS;
55
56 count= list[0].count;
57
58 new_host_list=
59 (memcached_server_st *)realloc(ptr->hosts,
60 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
61
62 if (!new_host_list)
63 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
64
65 ptr->hosts= new_host_list;
66
67 for (x= 0; x < count; x++)
68 {
69 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
70 host_reset(&ptr->hosts[ptr->number_of_hosts], list[x].hostname,
71 list[x].port, list[x].type);
72 ptr->number_of_hosts++;
73 }
74 ptr->hosts[0].count= ptr->number_of_hosts;
75
76 rebalance_wheel(ptr);
77
78 return MEMCACHED_SUCCESS;
79 }
80
81 memcached_return memcached_server_add_unix_socket(memcached_st *ptr, char *filename)
82 {
83 if (!filename)
84 return MEMCACHED_FAILURE;
85
86 return server_add(ptr, filename, 0, MEMCACHED_CONNECTION_UNIX_SOCKET);
87 }
88
89 memcached_return memcached_server_add_udp(memcached_st *ptr,
90 char *hostname,
91 unsigned int port)
92 {
93 if (!port)
94 port= MEMCACHED_DEFAULT_PORT;
95
96 if (!hostname)
97 hostname= "localhost";
98
99 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_UDP);
100 }
101
102 memcached_return memcached_server_add(memcached_st *ptr,
103 char *hostname,
104 unsigned int port)
105 {
106 if (!port)
107 port= MEMCACHED_DEFAULT_PORT;
108
109 if (!hostname)
110 hostname= "localhost";
111
112 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_TCP);
113 }
114
115 static memcached_return server_add(memcached_st *ptr, char *hostname,
116 unsigned int port,
117 memcached_connection type)
118 {
119 memcached_server_st *new_host_list;
120 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
121
122
123 new_host_list= (memcached_server_st *)realloc(ptr->hosts,
124 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
125 if (!new_host_list)
126 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
127
128 ptr->hosts= new_host_list;
129
130 host_reset(&ptr->hosts[ptr->number_of_hosts], hostname, port, type);
131 ptr->number_of_hosts++;
132 ptr->hosts[0].count++;
133
134 rebalance_wheel(ptr);
135
136 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
137
138 return MEMCACHED_SUCCESS;
139 }
140
141 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
142 char *hostname, unsigned int port,
143 memcached_return *error)
144 {
145 unsigned int count;
146 memcached_server_st *new_host_list;
147
148 if (hostname == NULL || error == NULL)
149 return NULL;
150
151 if (!port)
152 port= MEMCACHED_DEFAULT_PORT;
153
154 /* Increment count for hosts */
155 count= 1;
156 if (ptr != NULL)
157 {
158 count+= ptr[0].count;
159 }
160
161 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
162 if (!new_host_list)
163 {
164 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
165 return NULL;
166 }
167
168 host_reset(&new_host_list[count-1], hostname, port, MEMCACHED_CONNECTION_TCP);
169 new_host_list[0].count++;
170
171
172 *error= MEMCACHED_SUCCESS;
173 return new_host_list;
174 }
175
176 unsigned int memcached_server_list_count(memcached_server_st *ptr)
177 {
178 if (ptr == NULL)
179 return 0;
180
181 return ptr[0].count;
182 }
183
184 void memcached_server_list_free(memcached_server_st *ptr)
185 {
186 unsigned int x;
187
188 if (ptr == NULL)
189 return;
190
191 for (x= 0; x < ptr->count; x++)
192 if (ptr[x].address_info)
193 freeaddrinfo(ptr[x].address_info);
194
195 free(ptr);
196 }