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 +--------------------------------------------------------------------+
16 #include "libmemcached/common.h"
18 memcached_server_list_st
memcached_server_list_append_with_weight(memcached_server_list_st ptr
,
20 in_port_t port
, uint32_t weight
,
21 memcached_return_t
*error
) {
22 memcached_return_t unused
;
27 if (hostname
== NULL
) {
28 hostname
= "localhost";
31 if (hostname
[0] == '/') {
33 } else if (port
== 0) {
34 port
= MEMCACHED_DEFAULT_PORT
;
37 /* Increment count for hosts */
40 count
+= memcached_server_list_count(ptr
);
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
) {
47 *error
= memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
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
)
59 *error
= memcached_set_errno(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
66 // Handset allocated since
67 new_host_list
->options
.is_allocated
= true;
70 /* Backwards compatibility hack */
71 memcached_servers_set_count(new_host_list
, count
);
73 *error
= MEMCACHED_SUCCESS
;
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
);
83 uint32_t memcached_server_list_count(const memcached_server_list_st self
) {
84 return (self
== NULL
) ? 0 : self
->number_of_hosts
;
87 uint32_t memcached_instance_list_count(const memcached_st
*self
) {
88 return (self
== NULL
) ? 0 : self
->number_of_hosts
;
91 void memcached_instance_set(memcached_st
*memc
, memcached_instance_st
*list
,
92 const uint32_t host_list_size
) {
95 memc
->number_of_hosts
= host_list_size
;
98 void memcached_server_list_free(memcached_server_list_st 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
]);
107 libmemcached_free(self
->root
, self
);
111 void memcached_instance_list_free(memcached_instance_st
*self
, uint32_t instance_count
) {
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
]);
120 libmemcached_free(self
->root
, self
);