Fix for wheel algo to be dynamic
[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 *)malloc(sizeof(memcached_st));
13
14 if (!ptr)
15 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
16
17 memset(ptr, 0, sizeof(memcached_st));
18 ptr->is_allocated= MEMCACHED_ALLOCATED;
19 }
20 else
21 {
22 memset(ptr, 0, sizeof(memcached_st));
23 }
24 result_ptr= memcached_result_create(ptr, &ptr->result);
25 WATCHPOINT_ASSERT(result_ptr);
26 ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
27 ptr->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
28 ptr->retry_timeout= 0;
29 ptr->distribution= MEMCACHED_DISTRIBUTION_MODULA;
30
31 return ptr;
32 }
33
34 void memcached_free(memcached_st *ptr)
35 {
36 /* If we have anything open, lets close it now */
37 memcached_quit(ptr);
38 server_list_free(ptr, ptr->hosts);
39 memcached_result_free(&ptr->result);
40
41 if (ptr->on_cleanup)
42 ptr->on_cleanup(ptr);
43
44 if (ptr->continuum)
45 {
46 if (ptr->call_free)
47 ptr->call_free(ptr, ptr->continuum);
48 else
49 free(ptr->continuum);
50 }
51
52 if (ptr->wheel)
53 {
54 if (ptr->call_free)
55 ptr->call_free(ptr, ptr->wheel);
56 else
57 free(ptr->wheel);
58 }
59
60 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
61 {
62 if (ptr->call_free)
63 ptr->call_free(ptr, ptr);
64 else
65 free(ptr);
66 }
67 else
68 ptr->is_allocated= MEMCACHED_USED;
69 }
70
71 /*
72 clone is the destination, while ptr is the structure to clone.
73 If ptr is NULL the call is the same as if a memcached_create() was
74 called.
75 */
76 memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr)
77 {
78 memcached_return rc= MEMCACHED_SUCCESS;
79 memcached_st *new_clone;
80
81 if (ptr == NULL)
82 return memcached_create(clone);
83
84 if (ptr->is_allocated == MEMCACHED_USED)
85 {
86 WATCHPOINT_ASSERT(0);
87 return NULL;
88 }
89
90 new_clone= memcached_create(clone);
91
92 if (new_clone == NULL)
93 return NULL;
94
95 if (ptr->hosts)
96 rc= memcached_server_push(new_clone, ptr->hosts);
97
98 if (rc != MEMCACHED_SUCCESS)
99 {
100 memcached_free(new_clone);
101
102 return NULL;
103 }
104
105
106 new_clone->flags= ptr->flags;
107 new_clone->send_size= ptr->send_size;
108 new_clone->recv_size= ptr->recv_size;
109 new_clone->poll_timeout= ptr->poll_timeout;
110 new_clone->connect_timeout= ptr->connect_timeout;
111 new_clone->retry_timeout= ptr->retry_timeout;
112 new_clone->distribution= ptr->distribution;
113 new_clone->hash= ptr->hash;
114 new_clone->user_data= ptr->user_data;
115
116 new_clone->on_clone= ptr->on_clone;
117 new_clone->on_cleanup= ptr->on_cleanup;
118 new_clone->call_free= ptr->call_free;
119 new_clone->call_malloc= ptr->call_malloc;
120 new_clone->call_realloc= ptr->call_realloc;
121 new_clone->get_key_failure= ptr->get_key_failure;
122 new_clone->delete_trigger= ptr->delete_trigger;
123
124 rc= run_distribution(new_clone);
125 if (rc != MEMCACHED_SUCCESS)
126 {
127 memcached_free(new_clone);
128
129 return NULL;
130 }
131
132 if (ptr->on_clone)
133 ptr->on_clone(ptr, new_clone);
134
135 return new_clone;
136 }