Flip call (NULL is more common). Also add in helgrind and fix calloc.
[awesomized/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 result_ptr= memcached_result_create(ptr, &ptr->result);
24 WATCHPOINT_ASSERT(result_ptr);
25 ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
26 ptr->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
27 ptr->retry_timeout= 0;
28 ptr->distribution= MEMCACHED_DISTRIBUTION_MODULA;
29
30 /* TODO, Document why we picked these defaults */
31 ptr->io_msg_watermark= 500;
32 ptr->io_bytes_watermark= 65 * 1024;
33
34 return ptr;
35 }
36
37 void memcached_free(memcached_st *ptr)
38 {
39 /* If we have anything open, lets close it now */
40 memcached_quit(ptr);
41 server_list_free(ptr, ptr->hosts);
42 memcached_result_free(&ptr->result);
43
44 if (ptr->on_cleanup)
45 ptr->on_cleanup(ptr);
46
47 if (ptr->continuum)
48 {
49 if (ptr->call_free)
50 ptr->call_free(ptr, ptr->continuum);
51 else
52 free(ptr->continuum);
53 }
54
55 if (ptr->is_allocated)
56 {
57 if (ptr->call_free)
58 ptr->call_free(ptr, ptr);
59 else
60 free(ptr);
61 }
62 else
63 memset(ptr, 0, sizeof(memcached_st));
64 }
65
66 /*
67 clone is the destination, while source is the structure to clone.
68 If source is NULL the call is the same as if a memcached_create() was
69 called.
70 */
71 memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
72 {
73 memcached_return rc= MEMCACHED_SUCCESS;
74 memcached_st *new_clone;
75
76 if (source == NULL)
77 return memcached_create(clone);
78
79 if (clone && clone->is_allocated)
80 {
81 return NULL;
82 }
83
84 new_clone= memcached_create(clone);
85
86 if (new_clone == NULL)
87 return NULL;
88
89 new_clone->flags= source->flags;
90 new_clone->send_size= source->send_size;
91 new_clone->recv_size= source->recv_size;
92 new_clone->poll_timeout= source->poll_timeout;
93 new_clone->connect_timeout= source->connect_timeout;
94 new_clone->retry_timeout= source->retry_timeout;
95 new_clone->distribution= source->distribution;
96 new_clone->hash= source->hash;
97 new_clone->hash_continuum= source->hash_continuum;
98 new_clone->user_data= source->user_data;
99
100 new_clone->snd_timeout= source->snd_timeout;
101 new_clone->rcv_timeout= source->rcv_timeout;
102
103 new_clone->on_clone= source->on_clone;
104 new_clone->on_cleanup= source->on_cleanup;
105 new_clone->call_free= source->call_free;
106 new_clone->call_malloc= source->call_malloc;
107 new_clone->call_realloc= source->call_realloc;
108 new_clone->get_key_failure= source->get_key_failure;
109 new_clone->delete_trigger= source->delete_trigger;
110 new_clone->server_failure_limit= source->server_failure_limit;
111 new_clone->io_msg_watermark= source->io_msg_watermark;
112 new_clone->io_bytes_watermark= source->io_bytes_watermark;
113 new_clone->io_key_prefetch= source->io_key_prefetch;
114
115 if (source->hosts)
116 rc= memcached_server_push(new_clone, source->hosts);
117
118 if (rc != MEMCACHED_SUCCESS)
119 {
120 memcached_free(new_clone);
121
122 return NULL;
123 }
124
125
126 if (source->prefix_key[0] != 0)
127 {
128 strcpy(new_clone->prefix_key, source->prefix_key);
129 new_clone->prefix_key_length= source->prefix_key_length;
130 }
131
132 rc= run_distribution(new_clone);
133 if (rc != MEMCACHED_SUCCESS)
134 {
135 memcached_free(new_clone);
136
137 return NULL;
138 }
139
140 if (source->on_clone)
141 source->on_clone(source, new_clone);
142
143 return new_clone;
144 }