Fix error condition (which,... would not happen under current API).
[m6w6/libmemcached] / libmemcached / server_list.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12
13 #include <libmemcached/common.h>
14
15 memcached_server_list_st
16 memcached_server_list_append_with_weight(memcached_server_list_st ptr,
17 const char *hostname, in_port_t port,
18 uint32_t weight,
19 memcached_return_t *error)
20 {
21 uint32_t count;
22 memcached_server_list_st new_host_list;
23
24 if (hostname == NULL || error == NULL)
25 return NULL;
26
27 if (hostname[0] == '/')
28 port = 0;
29 else if (! port)
30 port= MEMCACHED_DEFAULT_PORT;
31
32 /* Increment count for hosts */
33 count= 1;
34 if (ptr != NULL)
35 {
36 count+= memcached_server_list_count(ptr);
37 }
38
39 new_host_list= (memcached_server_write_instance_st)realloc(ptr, sizeof(memcached_server_st) * count);
40 if (!new_host_list)
41 {
42 ptr->cached_errno= errno;
43 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
44 return NULL;
45 }
46
47 /* @todo Check return type */
48 if (not memcached_server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, port ? MEMCACHED_CONNECTION_TCP : MEMCACHED_CONNECTION_UNIX_SOCKET))
49 {
50 ptr->cached_errno= errno;
51 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
52 return NULL;
53 }
54
55 // Handset allocated since
56 new_host_list->options.is_allocated= true;
57
58 /* Backwards compatibility hack */
59 memcached_servers_set_count(new_host_list, count);
60
61 *error= MEMCACHED_SUCCESS;
62 return new_host_list;
63 }
64
65 memcached_server_list_st
66 memcached_server_list_append(memcached_server_list_st ptr,
67 const char *hostname, in_port_t port,
68 memcached_return_t *error)
69 {
70 return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error);
71 }
72
73 uint32_t memcached_server_list_count(const memcached_server_list_st self)
74 {
75 return (self == NULL)
76 ? 0
77 : self->number_of_hosts;
78 }
79
80 memcached_server_st *memcached_server_list(const memcached_st *self)
81 {
82 return self->servers;
83 }
84
85 void memcached_server_list_set(memcached_st *self, memcached_server_st *list)
86 {
87 self->servers= list;
88 }