Updated for the creation of memcached_server_by_key().
[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->is_allocated == MEMCACHED_ALLOCATED)
53 {
54 if (ptr->call_free)
55 ptr->call_free(ptr, ptr);
56 else
57 free(ptr);
58 }
59 else
60 ptr->is_allocated= MEMCACHED_USED;
61 }
62
63 /*
64 clone is the destination, while source is the structure to clone.
65 If source is NULL the call is the same as if a memcached_create() was
66 called.
67 */
68 memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
69 {
70 memcached_return rc= MEMCACHED_SUCCESS;
71 memcached_st *new_clone;
72
73 if (source == NULL)
74 return memcached_create(clone);
75
76 if (clone && clone->is_allocated == MEMCACHED_USED)
77 {
78 return NULL;
79 }
80
81 new_clone= memcached_create(clone);
82
83 if (new_clone == NULL)
84 return NULL;
85
86 if (source->hosts)
87 rc= memcached_server_push(new_clone, source->hosts);
88
89 if (rc != MEMCACHED_SUCCESS)
90 {
91 memcached_free(new_clone);
92
93 return NULL;
94 }
95
96
97 new_clone->flags= source->flags;
98 new_clone->send_size= source->send_size;
99 new_clone->recv_size= source->recv_size;
100 new_clone->poll_timeout= source->poll_timeout;
101 new_clone->connect_timeout= source->connect_timeout;
102 new_clone->retry_timeout= source->retry_timeout;
103 new_clone->distribution= source->distribution;
104 new_clone->hash= source->hash;
105 new_clone->hash_continuum= source->hash_continuum;
106 new_clone->user_data= source->user_data;
107
108 new_clone->snd_timeout= source->snd_timeout;
109 new_clone->rcv_timeout= source->rcv_timeout;
110
111 new_clone->on_clone= source->on_clone;
112 new_clone->on_cleanup= source->on_cleanup;
113 new_clone->call_free= source->call_free;
114 new_clone->call_malloc= source->call_malloc;
115 new_clone->call_realloc= source->call_realloc;
116 new_clone->get_key_failure= source->get_key_failure;
117 new_clone->delete_trigger= source->delete_trigger;
118
119 if (source->prefix_key[0] != 0)
120 {
121 strcpy(new_clone->prefix_key, source->prefix_key);
122 new_clone->prefix_key_length= source->prefix_key_length;
123 }
124
125 rc= run_distribution(new_clone);
126 if (rc != MEMCACHED_SUCCESS)
127 {
128 memcached_free(new_clone);
129
130 return NULL;
131 }
132
133 if (source->on_clone)
134 source->on_clone(source, new_clone);
135
136 return new_clone;
137 }