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;
25 ptr
->options
.is_purging
= false;
26 ptr
->options
.is_processing_input
= false;
28 memcached_set_memory_allocators(ptr
, NULL
, NULL
, NULL
, NULL
);
30 if (! memcached_result_create(ptr
, &ptr
->result
))
35 ptr
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
36 ptr
->connect_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
37 ptr
->retry_timeout
= 0;
38 ptr
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
40 /* TODO, Document why we picked these defaults */
41 ptr
->io_msg_watermark
= 500;
42 ptr
->io_bytes_watermark
= 65 * 1024;
44 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
49 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
56 for (x
= 0; x
< memcached_servers_count(servers
); x
++)
58 if (servers
[x
].address_info
)
60 freeaddrinfo(servers
[x
].address_info
);
61 servers
[x
].address_info
= NULL
;
67 ptr
->call_free(ptr
, servers
);
75 void memcached_servers_reset(memcached_st
*ptr
)
77 server_list_free(ptr
, memcached_server_list(ptr
));
79 memcached_server_list_set(ptr
, NULL
);
80 ptr
->number_of_hosts
= 0;
81 ptr
->last_disconnected_server
= NULL
;
82 ptr
->server_failure_limit
= 0;
85 void memcached_free(memcached_st
*ptr
)
87 /* If we have anything open, lets close it now */
89 server_list_free(ptr
, memcached_server_list(ptr
));
90 memcached_result_free(&ptr
->result
);
96 ptr
->call_free(ptr
, ptr
->continuum
);
98 if (memcached_is_allocated(ptr
))
100 ptr
->call_free(ptr
, ptr
);
104 ptr
->options
.is_initialized
= false;
109 clone is the destination, while source is the structure to clone.
110 If source is NULL the call is the same as if a memcached_create() was
113 memcached_st
*memcached_clone(memcached_st
*clone
, memcached_st
*source
)
115 memcached_return_t rc
= MEMCACHED_SUCCESS
;
116 memcached_st
*new_clone
;
119 return memcached_create(clone
);
121 if (clone
&& memcached_is_allocated(clone
))
126 new_clone
= memcached_create(clone
);
128 if (new_clone
== NULL
)
131 new_clone
->flags
= source
->flags
;
132 new_clone
->send_size
= source
->send_size
;
133 new_clone
->recv_size
= source
->recv_size
;
134 new_clone
->poll_timeout
= source
->poll_timeout
;
135 new_clone
->connect_timeout
= source
->connect_timeout
;
136 new_clone
->retry_timeout
= source
->retry_timeout
;
137 new_clone
->distribution
= source
->distribution
;
138 new_clone
->hash
= source
->hash
;
139 new_clone
->distribution_hash
= source
->distribution_hash
;
140 new_clone
->user_data
= source
->user_data
;
142 new_clone
->snd_timeout
= source
->snd_timeout
;
143 new_clone
->rcv_timeout
= source
->rcv_timeout
;
145 new_clone
->on_clone
= source
->on_clone
;
146 new_clone
->on_cleanup
= source
->on_cleanup
;
147 new_clone
->call_free
= source
->call_free
;
148 new_clone
->call_malloc
= source
->call_malloc
;
149 new_clone
->call_realloc
= source
->call_realloc
;
150 new_clone
->call_calloc
= source
->call_calloc
;
151 new_clone
->get_key_failure
= source
->get_key_failure
;
152 new_clone
->delete_trigger
= source
->delete_trigger
;
153 new_clone
->server_failure_limit
= source
->server_failure_limit
;
154 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
155 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
156 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
157 new_clone
->number_of_replicas
= source
->number_of_replicas
;
159 if (memcached_server_list(source
))
160 rc
= memcached_server_push(new_clone
, memcached_server_list(source
));
162 if (rc
!= MEMCACHED_SUCCESS
)
164 memcached_free(new_clone
);
170 if (source
->prefix_key
[0] != 0)
172 strcpy(new_clone
->prefix_key
, source
->prefix_key
);
173 new_clone
->prefix_key_length
= source
->prefix_key_length
;
176 rc
= run_distribution(new_clone
);
178 if (rc
!= MEMCACHED_SUCCESS
)
180 memcached_free(new_clone
);
185 if (source
->on_clone
)
186 source
->on_clone(source
, new_clone
);
191 void *memcached_get_user_data(memcached_st
*ptr
)
193 return ptr
->user_data
;
196 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
198 void *ret
= ptr
->user_data
;
199 ptr
->user_data
= data
;