Fixed autoconf macros for cross compile
[awesomized/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
15 /*
16 These functions provide data and function callback support
17 */
18
19 memcached_return_t memcached_callback_set(memcached_st *ptr,
20 const memcached_callback_t flag,
21 void *data)
22 {
23 switch (flag)
24 {
25 case MEMCACHED_CALLBACK_PREFIX_KEY:
26 {
27 char *key= (char *)data;
28
29 if (key)
30 {
31 size_t key_length= strlen(key);
32
33 if (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)
34 {
35 return MEMCACHED_BAD_KEY_PROVIDED;
36 }
37
38 if ((key_length > MEMCACHED_PREFIX_KEY_MAX_SIZE -1)
39 || (strncpy(ptr->prefix_key, key, MEMCACHED_PREFIX_KEY_MAX_SIZE) == NULL))
40 {
41 ptr->prefix_key_length= 0;
42 return MEMCACHED_BAD_KEY_PROVIDED;
43 }
44 else
45 {
46 ptr->prefix_key_length= key_length;
47 }
48 }
49 else
50 {
51 ptr->prefix_key[0]= 0;
52 ptr->prefix_key_length= 0;
53 }
54
55 break;
56 }
57 case MEMCACHED_CALLBACK_USER_DATA:
58 {
59 ptr->user_data= data;
60 break;
61 }
62 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
63 {
64 memcached_cleanup_fn func= *(memcached_cleanup_fn *)&data;
65 ptr->on_cleanup= func;
66 break;
67 }
68 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
69 {
70 memcached_clone_fn func= *(memcached_clone_fn *)&data;
71 ptr->on_clone= func;
72 break;
73 }
74 #ifdef MEMCACHED_ENABLE_DEPRECATED
75 case MEMCACHED_CALLBACK_MALLOC_FUNCTION:
76 {
77 memcached_malloc_function func= *(memcached_malloc_fn *)&data;
78 ptr->call_malloc= func;
79 break;
80 }
81 case MEMCACHED_CALLBACK_REALLOC_FUNCTION:
82 {
83 memcached_realloc_function func= *(memcached_realloc_fn *)&data;
84 ptr->call_realloc= func;
85 break;
86 }
87 case MEMCACHED_CALLBACK_FREE_FUNCTION:
88 {
89 memcached_free_function func= *(memcached_free_fn *)&data;
90 ptr->call_free= func;
91 break;
92 }
93 #endif
94 case MEMCACHED_CALLBACK_GET_FAILURE:
95 {
96 memcached_trigger_key_fn func= *(memcached_trigger_key_fn *)&data;
97 ptr->get_key_failure= func;
98 break;
99 }
100 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
101 {
102 memcached_trigger_delete_key_fn func= *(memcached_trigger_delete_key_fn *)&data;
103 ptr->delete_trigger= func;
104 break;
105 }
106 case MEMCACHED_CALLBACK_MAX:
107 default:
108 return MEMCACHED_FAILURE;
109 }
110
111 return MEMCACHED_SUCCESS;
112 }
113
114 void *memcached_callback_get(memcached_st *ptr,
115 const memcached_callback_t flag,
116 memcached_return_t *error)
117 {
118 memcached_return_t local_error;
119
120 if (!error)
121 error = &local_error;
122
123 switch (flag)
124 {
125 case MEMCACHED_CALLBACK_PREFIX_KEY:
126 {
127 if (ptr->prefix_key_length)
128 {
129 *error= MEMCACHED_SUCCESS;
130 return (void *)ptr->prefix_key;
131 }
132 else
133 {
134 *error= MEMCACHED_FAILURE;
135 return NULL;
136 }
137 }
138 case MEMCACHED_CALLBACK_USER_DATA:
139 {
140 *error= ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
141 return (void *)ptr->user_data;
142 }
143 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
144 {
145 *error= ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
146 return *(void **)&ptr->on_cleanup;
147 }
148 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
149 {
150 *error= ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
151 return *(void **)&ptr->on_clone;
152 }
153 #ifdef MEMCACHED_ENABLE_DEPRECATED
154 case MEMCACHED_CALLBACK_MALLOC_FUNCTION:
155 {
156 *error= ptr->call_malloc ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
157 return *(void **)&ptr->call_malloc;
158 }
159 case MEMCACHED_CALLBACK_REALLOC_FUNCTION:
160 {
161 *error= ptr->call_realloc ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
162 return *(void **)&ptr->call_realloc;
163 }
164 case MEMCACHED_CALLBACK_FREE_FUNCTION:
165 {
166 *error= ptr->call_free ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
167 return *(void **)&ptr->call_free;
168 }
169 #endif
170 case MEMCACHED_CALLBACK_GET_FAILURE:
171 {
172 *error= ptr->get_key_failure ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
173 return *(void **)&ptr->get_key_failure;
174 }
175 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
176 {
177 *error= ptr->delete_trigger ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
178 return *(void **)&ptr->delete_trigger;
179 }
180 case MEMCACHED_CALLBACK_MAX:
181 default:
182 WATCHPOINT_ASSERT(0);
183 *error= MEMCACHED_FAILURE;
184 return NULL;
185 }
186 }