Merge Trond
[m6w6/libmemcached] / libmemcached / callback.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: Change any of the possible callbacks.
9 *
10 */
11
12 #include "common.h"
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/tcp.h>
16
17 /*
18 These functions provide data and function callback support
19 */
20
21 memcached_return_t memcached_callback_set(memcached_st *ptr,
22 const memcached_callback_t flag,
23 void *data)
24 {
25 switch (flag)
26 {
27 case MEMCACHED_CALLBACK_PREFIX_KEY:
28 {
29 char *key= (char *)data;
30
31 if (key)
32 {
33 size_t key_length= strlen(key);
34
35 if (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)
36 {
37 return MEMCACHED_BAD_KEY_PROVIDED;
38 }
39
40 if ((key_length > MEMCACHED_PREFIX_KEY_MAX_SIZE -1)
41 || (strcpy(ptr->prefix_key, key) == NULL))
42 {
43 ptr->prefix_key_length= 0;
44 return MEMCACHED_BAD_KEY_PROVIDED;
45 }
46 else
47 {
48 ptr->prefix_key_length= key_length;
49 }
50 }
51 else
52 {
53 memset(ptr->prefix_key, 0, MEMCACHED_PREFIX_KEY_MAX_SIZE);
54 ptr->prefix_key_length= 0;
55 }
56
57 break;
58 }
59 case MEMCACHED_CALLBACK_USER_DATA:
60 {
61 ptr->user_data= data;
62 break;
63 }
64 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
65 {
66 memcached_cleanup_fn func= *(memcached_cleanup_fn *)&data;
67 ptr->on_cleanup= func;
68 break;
69 }
70 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
71 {
72 memcached_clone_fn func= *(memcached_clone_fn *)&data;
73 ptr->on_clone= func;
74 break;
75 }
76 #ifdef MEMCACHED_ENABLE_DEPRECATED
77 case MEMCACHED_CALLBACK_MALLOC_FUNCTION:
78 {
79 memcached_malloc_function func= *(memcached_malloc_fn *)&data;
80 ptr->call_malloc= func;
81 break;
82 }
83 case MEMCACHED_CALLBACK_REALLOC_FUNCTION:
84 {
85 memcached_realloc_function func= *(memcached_realloc_fn *)&data;
86 ptr->call_realloc= func;
87 break;
88 }
89 case MEMCACHED_CALLBACK_FREE_FUNCTION:
90 {
91 memcached_free_function func= *(memcached_free_fn *)&data;
92 ptr->call_free= func;
93 break;
94 }
95 #endif
96 case MEMCACHED_CALLBACK_GET_FAILURE:
97 {
98 memcached_trigger_key_fn func= *(memcached_trigger_key_fn *)&data;
99 ptr->get_key_failure= func;
100 break;
101 }
102 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
103 {
104 memcached_trigger_delete_key_fn func= *(memcached_trigger_delete_key_fn *)&data;
105 ptr->delete_trigger= func;
106 break;
107 }
108 case MEMCACHED_CALLBACK_MAX:
109 default:
110 return MEMCACHED_FAILURE;
111 }
112
113 return MEMCACHED_SUCCESS;
114 }
115
116 void *memcached_callback_get(memcached_st *ptr,
117 const memcached_callback_t flag,
118 memcached_return_t *error)
119 {
120 memcached_return_t local_error;
121
122 if (!error)
123 error = &local_error;
124
125 switch (flag)
126 {
127 case MEMCACHED_CALLBACK_PREFIX_KEY:
128 {
129 if (ptr->prefix_key[0] == 0)
130 {
131 *error= MEMCACHED_FAILURE;
132 return NULL;
133 }
134 else
135 {
136 *error= MEMCACHED_SUCCESS;
137 return (void *)ptr->prefix_key;
138 }
139 }
140 case MEMCACHED_CALLBACK_USER_DATA:
141 {
142 *error= ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
143 return (void *)ptr->user_data;
144 }
145 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
146 {
147 *error= ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
148 return *(void **)&ptr->on_cleanup;
149 }
150 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
151 {
152 *error= ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
153 return *(void **)&ptr->on_clone;
154 }
155 #ifdef MEMCACHED_ENABLE_DEPRECATED
156 case MEMCACHED_CALLBACK_MALLOC_FUNCTION:
157 {
158 *error= ptr->call_malloc ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
159 return *(void **)&ptr->call_malloc;
160 }
161 case MEMCACHED_CALLBACK_REALLOC_FUNCTION:
162 {
163 *error= ptr->call_realloc ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
164 return *(void **)&ptr->call_realloc;
165 }
166 case MEMCACHED_CALLBACK_FREE_FUNCTION:
167 {
168 *error= ptr->call_free ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
169 return *(void **)&ptr->call_free;
170 }
171 #endif
172 case MEMCACHED_CALLBACK_GET_FAILURE:
173 {
174 *error= ptr->get_key_failure ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
175 return *(void **)&ptr->get_key_failure;
176 }
177 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
178 {
179 *error= ptr->delete_trigger ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
180 return *(void **)&ptr->delete_trigger;
181 }
182 case MEMCACHED_CALLBACK_MAX:
183 default:
184 WATCHPOINT_ASSERT(0);
185 *error= MEMCACHED_FAILURE;
186 return NULL;
187 }
188 }