New version update
[m6w6/libmemcached] / libmemcached / memcached_result.c
1 /*
2 memcached_result_st are used to internally represent the return values from
3 memcached. We use a structure so that long term as identifiers are added
4 to memcached we will be able to absorb new attributes without having
5 to addjust the entire API.
6 */
7 #include "common.h"
8
9 memcached_result_st *memcached_result_create(memcached_st *memc,
10 memcached_result_st *ptr)
11 {
12 /* Saving malloc calls :) */
13 if (ptr)
14 {
15 memset(ptr, 0, sizeof(memcached_result_st));
16 ptr->is_allocated= MEMCACHED_NOT_ALLOCATED;
17 }
18 else
19 {
20 if (memc->call_malloc)
21 ptr= (memcached_result_st *)memc->call_malloc(memc, sizeof(memcached_result_st));
22 else
23 ptr= (memcached_result_st *)malloc(sizeof(memcached_result_st));
24
25 if (ptr == NULL)
26 return NULL;
27 memset(ptr, 0, sizeof(memcached_result_st));
28 ptr->is_allocated= MEMCACHED_ALLOCATED;
29 }
30
31 ptr->root= memc;
32 memcached_string_create(memc, &ptr->value, 0);
33 WATCHPOINT_ASSERT(ptr->value.string == NULL);
34 WATCHPOINT_ASSERT(ptr->value.is_allocated == MEMCACHED_NOT_ALLOCATED);
35
36 return ptr;
37 }
38
39 void memcached_result_reset(memcached_result_st *ptr)
40 {
41 ptr->key_length= 0;
42 memcached_string_reset(&ptr->value);
43 ptr->flags= 0;
44 ptr->cas= 0;
45 ptr->expiration= 0;
46 }
47
48 /*
49 NOTE turn into macro
50 */
51 memcached_return memcached_result_set_value(memcached_result_st *ptr, char *value, size_t length)
52 {
53 return memcached_string_append(&ptr->value, value, length);
54 }
55
56 void memcached_result_free(memcached_result_st *ptr)
57 {
58 if (ptr == NULL)
59 return;
60
61 memcached_string_free(&ptr->value);
62
63 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
64 free(ptr);
65 else
66 ptr->is_allocated= MEMCACHED_USED;
67 }