5f0d98207e3269ca9c3d21f8e991c927edaa9dfa
[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
26 return ptr;
27 }
28
29 void memcached_free(memcached_st *ptr)
30 {
31 if (ptr->hosts)
32 {
33 memcached_quit(ptr);
34 memcached_server_list_free(ptr->hosts);
35 ptr->hosts= NULL;
36 }
37
38 memcached_string_free(&ptr->result_buffer);
39
40 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
41 free(ptr);
42 else
43 memset(ptr, 0, sizeof(memcached_st));
44 }
45
46 /*
47 clone is the destination, while ptr is the structure to clone.
48 If ptr is NULL the call is the same as if a memcached_create() was
49 called.
50 */
51 memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr)
52 {
53 memcached_return rc;
54 memcached_st *new_clone;
55
56 if (ptr == NULL)
57 return memcached_create(clone);
58
59 new_clone= memcached_create(clone);
60
61
62 rc= memcached_server_push(new_clone, ptr->hosts);
63
64 if (rc != MEMCACHED_SUCCESS)
65 {
66 memcached_free(new_clone);
67
68 return NULL;
69 }
70
71
72 new_clone->flags= ptr->flags;
73 new_clone->number_of_hosts= ptr->number_of_hosts;
74 new_clone->send_size= ptr->send_size;
75 new_clone->recv_size= ptr->recv_size;
76
77 return new_clone;
78 }