Fixed bug reported by Stuart Midgley about what happens when there are no
[awesomized/libmemcached] / lib / 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 ptr= (memcached_result_st *)malloc(sizeof(memcached_result_st));
21 if (!ptr)
22 return NULL;
23 memset(ptr, 0, sizeof(memcached_result_st));
24 ptr->is_allocated= MEMCACHED_ALLOCATED;
25 }
26
27 ptr->root= memc;
28 memcached_string_create(memc, &ptr->value, 0);
29 WATCHPOINT_ASSERT(ptr->value.string == NULL);
30 WATCHPOINT_ASSERT(ptr->value.is_allocated == MEMCACHED_NOT_ALLOCATED);
31
32 return ptr;
33 }
34
35 void memcached_result_free(memcached_result_st *ptr)
36 {
37 memcached_string_free(&ptr->value);
38
39 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
40 free(ptr);
41 }