Store up errno in a couple of spots.
[awesomized/libmemcached] / libmemcached / server_list.c
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 "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 (! port)
28 port= MEMCACHED_DEFAULT_PORT;
29
30 /* Increment count for hosts */
31 count= 1;
32 if (ptr != NULL)
33 {
34 count+= memcached_server_list_count(ptr);
35 }
36
37 new_host_list= (memcached_server_write_instance_st)realloc(ptr, sizeof(memcached_server_st) * count);
38 if (!new_host_list)
39 {
40 ptr->cached_errno= errno;
41 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
42 return NULL;
43 }
44
45 /* TODO: Check return type */
46 memcached_server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, MEMCACHED_CONNECTION_TCP);
47
48 /* Backwards compatibility hack */
49 memcached_servers_set_count(new_host_list, count);
50
51 *error= MEMCACHED_SUCCESS;
52 return new_host_list;
53 }
54
55 memcached_server_list_st
56 memcached_server_list_append(memcached_server_list_st ptr,
57 const char *hostname, in_port_t port,
58 memcached_return_t *error)
59 {
60 return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error);
61 }
62
63 uint32_t memcached_server_list_count(const memcached_server_list_st self)
64 {
65 return (self == NULL)
66 ? 0
67 : self->number_of_hosts;
68 }
69
70 memcached_server_st *memcached_server_list(const memcached_st *self)
71 {
72 return self->servers;
73 }
74
75 void memcached_server_list_set(memcached_st *self, memcached_server_st *list)
76 {
77 self->servers= list;
78 }