665b75dc0dc5754a16aed23e401cd7f46cb4b693
[m6w6/libmemcached] / src / libmemcached / server_list.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcached/common.h"
17
18 memcached_server_list_st memcached_server_list_append_with_weight(memcached_server_list_st ptr,
19 const char *hostname,
20 in_port_t port, uint32_t weight,
21 memcached_return_t *error) {
22 memcached_return_t unused;
23 if (error == NULL) {
24 error = &unused;
25 }
26
27 if (hostname == NULL) {
28 hostname = "localhost";
29 }
30
31 if (hostname[0] == '/') {
32 port = 0;
33 } else if (port == 0) {
34 port = MEMCACHED_DEFAULT_PORT;
35 }
36
37 /* Increment count for hosts */
38 uint32_t count = 1;
39 if (ptr) {
40 count += memcached_server_list_count(ptr);
41 }
42
43 memcached_server_list_st new_host_list =
44 (memcached_server_st *) realloc(ptr, sizeof(memcached_server_st) * count);
45 if (new_host_list == NULL) {
46 #if 0
47 *error= memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
48 #endif
49 return NULL;
50 }
51
52 memcached_string_t _hostname = {memcached_string_make_from_cstr(hostname)};
53 /* @todo Check return type */
54 if (__server_create_with(NULL, &new_host_list[count - 1], _hostname, port, weight,
55 port ? MEMCACHED_CONNECTION_TCP : MEMCACHED_CONNECTION_UNIX_SOCKET)
56 == NULL)
57 {
58 #if 0
59 *error= memcached_set_errno(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
60 #endif
61 free(new_host_list);
62 return NULL;
63 }
64
65 #if 0
66 // Handset allocated since
67 new_host_list->options.is_allocated= true;
68 #endif
69
70 /* Backwards compatibility hack */
71 memcached_servers_set_count(new_host_list, count);
72
73 *error = MEMCACHED_SUCCESS;
74 return new_host_list;
75 }
76
77 memcached_server_list_st memcached_server_list_append(memcached_server_list_st ptr,
78 const char *hostname, in_port_t port,
79 memcached_return_t *error) {
80 return memcached_server_list_append_with_weight(ptr, hostname, port, 0, error);
81 }
82
83 uint32_t memcached_server_list_count(const memcached_server_list_st self) {
84 return (self == NULL) ? 0 : self->number_of_hosts;
85 }
86
87 uint32_t memcached_instance_list_count(const memcached_st *self) {
88 return (self == NULL) ? 0 : self->number_of_hosts;
89 }
90
91 void memcached_instance_set(memcached_st *memc, memcached_instance_st *list,
92 const uint32_t host_list_size) {
93 assert(memc);
94 memc->servers = list;
95 memc->number_of_hosts = host_list_size;
96 }
97
98 void memcached_server_list_free(memcached_server_list_st self) {
99 if (self) {
100 for (uint32_t x = 0; x < memcached_server_list_count(self); x++) {
101 assert_msg(not memcached_is_allocated(&self[x]),
102 "You have called memcached_server_list_free(), but you did not pass it a valid "
103 "memcached_server_list_st");
104 __server_free(&self[x]);
105 }
106
107 libmemcached_free(self->root, self);
108 }
109 }
110
111 void memcached_instance_list_free(memcached_instance_st *self, uint32_t instance_count) {
112 if (self) {
113 for (uint32_t x = 0; x < instance_count; x++) {
114 assert_msg(memcached_is_allocated(&self[x]) == false,
115 "You have called memcached_server_list_free(), but you did not pass it a valid "
116 "memcached_server_list_st");
117 __instance_free(&self[x]);
118 }
119
120 libmemcached_free(self->root, self);
121 }
122 }