8d376f680fd45ced6ef1cd6a88b4f3f21ed19666
[awesomized/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 memset(ptr, 0, sizeof(memcached_result_st));
15 else
16 {
17 if (memc->call_malloc)
18 ptr= (memcached_result_st *)memc->call_malloc(memc, sizeof(memcached_result_st));
19 else
20 ptr= (memcached_result_st *)malloc(sizeof(memcached_result_st));
21
22 if (ptr == NULL)
23 return NULL;
24 memset(ptr, 0, sizeof(memcached_result_st));
25 ptr->is_allocated= true;
26 }
27
28 ptr->root= memc;
29 memcached_string_create(memc, &ptr->value, 0);
30 WATCHPOINT_ASSERT(ptr->value.string == NULL);
31
32 return ptr;
33 }
34
35 void memcached_result_reset(memcached_result_st *ptr)
36 {
37 ptr->key_length= 0;
38 memcached_string_reset(&ptr->value);
39 ptr->flags= 0;
40 ptr->cas= 0;
41 ptr->expiration= 0;
42 }
43
44 /*
45 NOTE turn into macro
46 */
47 memcached_return memcached_result_set_value(memcached_result_st *ptr, char *value, size_t length)
48 {
49 return memcached_string_append(&ptr->value, value, length);
50 }
51
52 void memcached_result_free(memcached_result_st *ptr)
53 {
54 if (ptr == NULL)
55 return;
56
57 memcached_string_free(&ptr->value);
58
59 if (ptr->is_allocated)
60 free(ptr);
61 }