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