Merge Trond.
[m6w6/libmemcached] / libmemcached / memcached.c
1 /*
2 Memcached library
3 */
4 #include "common.h"
5
6 memcached_st *memcached_create(memcached_st *ptr)
7 {
8 memcached_result_st *result_ptr;
9
10 if (ptr == NULL)
11 {
12 ptr= (memcached_st *)calloc(1, sizeof(memcached_st));
13
14 if (!ptr)
15 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
16
17 ptr->is_allocated= true;
18 }
19 else
20 {
21 memset(ptr, 0, sizeof(memcached_st));
22 }
23
24 memcached_set_memory_allocators(ptr, NULL, NULL, NULL, NULL);
25
26 result_ptr= memcached_result_create(ptr, &ptr->result);
27 WATCHPOINT_ASSERT(result_ptr);
28 ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
29 ptr->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
30 ptr->retry_timeout= 0;
31 ptr->distribution= MEMCACHED_DISTRIBUTION_MODULA;
32
33 /* TODO, Document why we picked these defaults */
34 ptr->io_msg_watermark= 500;
35 ptr->io_bytes_watermark= 65 * 1024;
36
37 return ptr;
38 }
39
40 void memcached_free(memcached_st *ptr)
41 {
42 /* If we have anything open, lets close it now */
43 memcached_quit(ptr);
44 server_list_free(ptr, ptr->hosts);
45 memcached_result_free(&ptr->result);
46
47 if (ptr->on_cleanup)
48 ptr->on_cleanup(ptr);
49
50 if (ptr->continuum)
51 ptr->call_free(ptr, ptr->continuum);
52
53 if (ptr->is_allocated)
54 ptr->call_free(ptr, ptr);
55 else
56 memset(ptr, 0, sizeof(memcached_st));
57 }
58
59 /*
60 clone is the destination, while source is the structure to clone.
61 If source is NULL the call is the same as if a memcached_create() was
62 called.
63 */
64 memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
65 {
66 memcached_return rc= MEMCACHED_SUCCESS;
67 memcached_st *new_clone;
68
69 if (source == NULL)
70 return memcached_create(clone);
71
72 if (clone && clone->is_allocated)
73 {
74 return NULL;
75 }
76
77 new_clone= memcached_create(clone);
78
79 if (new_clone == NULL)
80 return NULL;
81
82 new_clone->flags= source->flags;
83 new_clone->send_size= source->send_size;
84 new_clone->recv_size= source->recv_size;
85 new_clone->poll_timeout= source->poll_timeout;
86 new_clone->connect_timeout= source->connect_timeout;
87 new_clone->retry_timeout= source->retry_timeout;
88 new_clone->distribution= source->distribution;
89 new_clone->hash= source->hash;
90 new_clone->hash_continuum= source->hash_continuum;
91 new_clone->user_data= source->user_data;
92
93 new_clone->snd_timeout= source->snd_timeout;
94 new_clone->rcv_timeout= source->rcv_timeout;
95
96 new_clone->on_clone= source->on_clone;
97 new_clone->on_cleanup= source->on_cleanup;
98 new_clone->call_free= source->call_free;
99 new_clone->call_malloc= source->call_malloc;
100 new_clone->call_realloc= source->call_realloc;
101 new_clone->call_calloc= source->call_calloc;
102 new_clone->get_key_failure= source->get_key_failure;
103 new_clone->delete_trigger= source->delete_trigger;
104 new_clone->server_failure_limit= source->server_failure_limit;
105 new_clone->io_msg_watermark= source->io_msg_watermark;
106 new_clone->io_bytes_watermark= source->io_bytes_watermark;
107 new_clone->io_key_prefetch= source->io_key_prefetch;
108 new_clone->number_of_replicas= source->number_of_replicas;
109
110 if (source->hosts)
111 rc= memcached_server_push(new_clone, source->hosts);
112
113 if (rc != MEMCACHED_SUCCESS)
114 {
115 memcached_free(new_clone);
116
117 return NULL;
118 }
119
120
121 if (source->prefix_key[0] != 0)
122 {
123 strcpy(new_clone->prefix_key, source->prefix_key);
124 new_clone->prefix_key_length= source->prefix_key_length;
125 }
126
127 rc= run_distribution(new_clone);
128 if (rc != MEMCACHED_SUCCESS)
129 {
130 memcached_free(new_clone);
131
132 return NULL;
133 }
134
135 if (source->on_clone)
136 source->on_clone(source, new_clone);
137
138 return new_clone;
139 }
140 void *memcached_get_user_data(memcached_st *ptr)
141 {
142 return ptr->user_data;
143 }
144
145 void *memcached_set_user_data(memcached_st *ptr, void *data)
146 {
147 void *ret= ptr->user_data;
148 ptr->user_data= data;
149 return ret;
150 }