More Cleanup
[m6w6/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_st *memcached_server_list_append_with_weight(memcached_server_st *ptr,
16 const char *hostname, in_port_t port,
17 uint32_t weight,
18 memcached_return_t *error)
19 {
20 unsigned int count;
21 memcached_server_instance_st *new_host_list;
22
23 if (hostname == NULL || error == NULL)
24 return NULL;
25
26 if (! port)
27 port= MEMCACHED_DEFAULT_PORT;
28
29 /* Increment count for hosts */
30 count= 1;
31 if (ptr != NULL)
32 {
33 count+= memcached_servers_count(ptr);
34 }
35
36 new_host_list= (memcached_server_instance_st *)realloc(ptr, sizeof(memcached_server_instance_st) * count);
37 if (!new_host_list)
38 {
39 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
40 return NULL;
41 }
42
43 /* TODO: Check return type */
44 memcached_server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, MEMCACHED_CONNECTION_TCP);
45
46 /* Backwards compatibility hack */
47 memcached_servers_set_count(new_host_list, count);
48
49 *error= MEMCACHED_SUCCESS;
50 return new_host_list;
51 }
52
53 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
54 const char *hostname, in_port_t port,
55 memcached_return_t *error)
56 {
57 return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error);
58 }