Fix for minor rewrite in clone method of number of hosts.
[awesomized/libmemcached] / lib / memcached.c
1 /*
2 Memcached library
3 */
4 #include "common.h"
5
6 memcached_st *memcached_create(memcached_st *ptr)
7 {
8 memcached_string_st *string_ptr;
9 if (!ptr)
10 {
11 ptr= (memcached_st *)malloc(sizeof(memcached_st));
12
13 if (!ptr)
14 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
15
16 memset(ptr, 0, sizeof(memcached_st));
17 ptr->is_allocated= MEMCACHED_ALLOCATED;
18 }
19 else
20 {
21 memset(ptr, 0, sizeof(memcached_st));
22 }
23 string_ptr= memcached_string_create(ptr, &ptr->result_buffer, 0);
24 WATCHPOINT_ASSERT(string_ptr);
25 ptr->poll_timeout= -1;
26 ptr->distribution= MEMCACHED_DISTRIBUTION_MODULO;
27
28 return ptr;
29 }
30
31 void memcached_free(memcached_st *ptr)
32 {
33 /* If we have anything open, lets close it now */
34 memcached_quit(ptr);
35 memcached_server_list_free(ptr->hosts);
36 memcached_string_free(&ptr->result_buffer);
37
38 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
39 free(ptr);
40 else
41 ptr->is_allocated= MEMCACHED_USED;
42 }
43
44 /*
45 clone is the destination, while ptr is the structure to clone.
46 If ptr is NULL the call is the same as if a memcached_create() was
47 called.
48 */
49 memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr)
50 {
51 memcached_return rc;
52 memcached_st *new_clone;
53
54 if (ptr == NULL)
55 return memcached_create(clone);
56
57 if (ptr->is_allocated == MEMCACHED_USED)
58 {
59 WATCHPOINT_ASSERT(0);
60 return NULL;
61 }
62
63 new_clone= memcached_create(clone);
64
65 if (new_clone == NULL)
66 return NULL;
67
68 if (ptr->hosts)
69 rc= memcached_server_push(new_clone, ptr->hosts);
70
71 if (rc != MEMCACHED_SUCCESS)
72 {
73 memcached_free(new_clone);
74
75 return NULL;
76 }
77
78
79 new_clone->flags= ptr->flags;
80 new_clone->send_size= ptr->send_size;
81 new_clone->recv_size= ptr->recv_size;
82 new_clone->poll_timeout= ptr->poll_timeout;
83 new_clone->distribution= ptr->distribution;
84
85 return new_clone;
86 }