Flip call (NULL is more common). Also add in helgrind and fix calloc.
[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 *)calloc(1, sizeof(memcached_result_st));
21
22 if (ptr == NULL)
23 return NULL;
24 ptr->is_allocated= true;
25 }
26
27 ptr->root= memc;
28 memcached_string_create(memc, &ptr->value, 0);
29 WATCHPOINT_ASSERT(ptr->value.string == NULL);
30
31 return ptr;
32 }
33
34 void memcached_result_reset(memcached_result_st *ptr)
35 {
36 ptr->key_length= 0;
37 memcached_string_reset(&ptr->value);
38 ptr->flags= 0;
39 ptr->cas= 0;
40 ptr->expiration= 0;
41 }
42
43 /*
44 NOTE turn into macro
45 */
46 memcached_return memcached_result_set_value(memcached_result_st *ptr, char *value, size_t length)
47 {
48 return memcached_string_append(&ptr->value, value, length);
49 }
50
51 void memcached_result_free(memcached_result_st *ptr)
52 {
53 if (ptr == NULL)
54 return;
55
56 memcached_string_free(&ptr->value);
57
58 if (ptr->is_allocated)
59 free(ptr);
60 }