Small cleanup in string.h and converted result_st macro to inline.
[awesomized/libmemcached] / libmemcached / result.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: Functions to manipulate the result structure.
9 *
10 */
11
12 /*
13 memcached_result_st are used to internally represent the return values from
14 memcached. We use a structure so that long term as identifiers are added
15 to memcached we will be able to absorb new attributes without having
16 to addjust the entire API.
17 */
18 #include "common.h"
19
20 memcached_result_st *memcached_result_create(memcached_st *memc,
21 memcached_result_st *ptr)
22 {
23 WATCHPOINT_ASSERT(memc && memc->options.is_initialized);
24
25 /* Saving malloc calls :) */
26 if (ptr)
27 {
28 memset(ptr, 0, sizeof(memcached_result_st));
29 }
30 else
31 {
32 ptr= memc->call_calloc(memc, 1, sizeof(memcached_result_st));
33
34 if (ptr == NULL)
35 return NULL;
36 ptr->options.is_allocated= true;
37 }
38
39 ptr->options.is_initialized= true;
40
41 ptr->root= memc;
42 memcached_string_create(memc, &ptr->value, 0);
43 WATCHPOINT_ASSERT_INITIALIZED(&ptr->value);
44 WATCHPOINT_ASSERT(ptr->value.string == NULL);
45
46 return ptr;
47 }
48
49 void memcached_result_reset(memcached_result_st *ptr)
50 {
51 ptr->key_length= 0;
52 memcached_string_reset(&ptr->value);
53 ptr->flags= 0;
54 ptr->cas= 0;
55 ptr->expiration= 0;
56 }
57
58 void memcached_result_free(memcached_result_st *ptr)
59 {
60 if (ptr == NULL)
61 return;
62
63 memcached_string_free(&ptr->value);
64
65 if (memcached_is_allocated(ptr))
66 {
67 if (ptr->root != NULL)
68 {
69 ptr->root->call_free(ptr->root, ptr);
70 }
71 else
72 {
73 free(ptr);
74 }
75 }
76 else
77 {
78 ptr->options.is_initialized= false;
79 }
80 }