6 memcached_st
*memcached_create(memcached_st
*ptr
)
10 ptr
= (memcached_st
*)calloc(1, sizeof(memcached_st
));
14 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
17 ptr
->options
.is_allocated
= true;
21 memset(ptr
, 0, sizeof(memcached_st
));
24 ptr
->options
.is_initialized
= true;
26 memcached_set_memory_allocators(ptr
, NULL
, NULL
, NULL
, NULL
);
28 if (! memcached_result_create(ptr
, &ptr
->result
))
33 ptr
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
34 ptr
->connect_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
35 ptr
->retry_timeout
= 0;
36 ptr
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
38 /* TODO, Document why we picked these defaults */
39 ptr
->io_msg_watermark
= 500;
40 ptr
->io_bytes_watermark
= 65 * 1024;
42 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
47 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
54 for (x
= 0; x
< memcached_servers_count(servers
); x
++)
55 if (servers
[x
].address_info
)
57 freeaddrinfo(servers
[x
].address_info
);
58 servers
[x
].address_info
= NULL
;
63 ptr
->call_free(ptr
, servers
);
69 void memcached_servers_reset(memcached_st
*ptr
)
71 server_list_free(ptr
, ptr
->hosts
);
74 ptr
->number_of_hosts
= 0;
75 ptr
->last_disconnected_server
= NULL
;
76 ptr
->server_failure_limit
= 0;
79 void memcached_free(memcached_st
*ptr
)
81 /* If we have anything open, lets close it now */
83 server_list_free(ptr
, ptr
->hosts
);
84 memcached_result_free(&ptr
->result
);
90 ptr
->call_free(ptr
, ptr
->continuum
);
92 if (memcached_is_allocated(ptr
))
94 ptr
->call_free(ptr
, ptr
);
98 ptr
->options
.is_initialized
= false;
103 clone is the destination, while source is the structure to clone.
104 If source is NULL the call is the same as if a memcached_create() was
107 memcached_st
*memcached_clone(memcached_st
*clone
, memcached_st
*source
)
109 memcached_return_t rc
= MEMCACHED_SUCCESS
;
110 memcached_st
*new_clone
;
113 return memcached_create(clone
);
115 if (clone
&& memcached_is_allocated(clone
))
120 new_clone
= memcached_create(clone
);
122 if (new_clone
== NULL
)
125 new_clone
->flags
= source
->flags
;
126 new_clone
->send_size
= source
->send_size
;
127 new_clone
->recv_size
= source
->recv_size
;
128 new_clone
->poll_timeout
= source
->poll_timeout
;
129 new_clone
->connect_timeout
= source
->connect_timeout
;
130 new_clone
->retry_timeout
= source
->retry_timeout
;
131 new_clone
->distribution
= source
->distribution
;
132 new_clone
->hash
= source
->hash
;
133 new_clone
->distribution_hash
= source
->distribution_hash
;
134 new_clone
->user_data
= source
->user_data
;
136 new_clone
->snd_timeout
= source
->snd_timeout
;
137 new_clone
->rcv_timeout
= source
->rcv_timeout
;
139 new_clone
->on_clone
= source
->on_clone
;
140 new_clone
->on_cleanup
= source
->on_cleanup
;
141 new_clone
->call_free
= source
->call_free
;
142 new_clone
->call_malloc
= source
->call_malloc
;
143 new_clone
->call_realloc
= source
->call_realloc
;
144 new_clone
->call_calloc
= source
->call_calloc
;
145 new_clone
->get_key_failure
= source
->get_key_failure
;
146 new_clone
->delete_trigger
= source
->delete_trigger
;
147 new_clone
->server_failure_limit
= source
->server_failure_limit
;
148 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
149 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
150 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
151 new_clone
->number_of_replicas
= source
->number_of_replicas
;
154 rc
= memcached_server_push(new_clone
, source
->hosts
);
156 if (rc
!= MEMCACHED_SUCCESS
)
158 memcached_free(new_clone
);
164 if (source
->prefix_key
[0] != 0)
166 strcpy(new_clone
->prefix_key
, source
->prefix_key
);
167 new_clone
->prefix_key_length
= source
->prefix_key_length
;
170 rc
= run_distribution(new_clone
);
172 if (rc
!= MEMCACHED_SUCCESS
)
174 memcached_free(new_clone
);
179 if (source
->on_clone
)
180 source
->on_clone(source
, new_clone
);
185 void *memcached_get_user_data(memcached_st
*ptr
)
187 return ptr
->user_data
;
190 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
192 void *ret
= ptr
->user_data
;
193 ptr
->user_data
= data
;