Custom memory patch work (based on Sean Chittenden's patch)
[m6w6/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_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->distribution= MEMCACHED_DISTRIBUTION_MODULA;
28
29 return ptr;
30 }
31
32 void memcached_free(memcached_st *ptr)
33 {
34 /* If we have anything open, lets close it now */
35 memcached_quit(ptr);
36 server_list_free(ptr, ptr->hosts);
37 memcached_result_free(&ptr->result);
38
39 if (ptr->on_cleanup)
40 ptr->on_cleanup(ptr);
41
42 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
43 {
44 if (ptr->call_free)
45 ptr->call_free(ptr, ptr);
46 else
47 free(ptr);
48 }
49 else
50 ptr->is_allocated= MEMCACHED_USED;
51 }
52
53 /*
54 clone is the destination, while ptr is the structure to clone.
55 If ptr is NULL the call is the same as if a memcached_create() was
56 called.
57 */
58 memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr)
59 {
60 memcached_return rc= MEMCACHED_SUCCESS;
61 memcached_st *new_clone;
62
63 if (ptr == NULL)
64 {
65 new_clone= memcached_create(clone);
66
67 if (ptr->on_clone)
68 ptr->on_clone(NULL, new_clone);
69
70 return new_clone;
71 }
72
73 if (ptr->is_allocated == MEMCACHED_USED)
74 {
75 WATCHPOINT_ASSERT(0);
76 return NULL;
77 }
78
79 new_clone= memcached_create(clone);
80
81 if (new_clone == NULL)
82 return NULL;
83
84 if (ptr->hosts)
85 rc= memcached_server_push(new_clone, ptr->hosts);
86
87 if (rc != MEMCACHED_SUCCESS)
88 {
89 memcached_free(new_clone);
90
91 return NULL;
92 }
93
94
95 new_clone->flags= ptr->flags;
96 new_clone->send_size= ptr->send_size;
97 new_clone->recv_size= ptr->recv_size;
98 new_clone->poll_timeout= ptr->poll_timeout;
99 new_clone->distribution= ptr->distribution;
100 new_clone->hash= ptr->hash;
101 new_clone->user_data= ptr->user_data;
102
103 new_clone->on_clone= ptr->on_clone;
104 new_clone->on_cleanup= ptr->on_cleanup;
105 new_clone->call_free= ptr->call_free;
106 new_clone->call_malloc= ptr->call_malloc;
107 new_clone->call_realloc= ptr->call_realloc;
108
109 if (ptr->on_clone)
110 ptr->on_clone(ptr, new_clone);
111
112 return new_clone;
113 }