Remove mass calloc() on memcached_st creation.
[m6w6/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);
24 WATCHPOINT_ASSERT(memc->options.is_initialized);
25
26 /* Saving malloc calls :) */
27 if (ptr)
28 {
29 memset(ptr, 0, sizeof(memcached_result_st));
30 }
31 else
32 {
33 ptr= memc->call_calloc(memc, 1, sizeof(memcached_result_st));
34
35 if (ptr == NULL)
36 return NULL;
37 ptr->options.is_allocated= true;
38 }
39
40 ptr->options.is_initialized= true;
41
42 ptr->root= memc;
43 memcached_string_create(memc, &ptr->value, 0);
44 WATCHPOINT_ASSERT_INITIALIZED(&ptr->value);
45 WATCHPOINT_ASSERT(ptr->value.string == NULL);
46
47 return ptr;
48 }
49
50 void memcached_result_reset(memcached_result_st *ptr)
51 {
52 ptr->key_length= 0;
53 memcached_string_reset(&ptr->value);
54 ptr->flags= 0;
55 ptr->cas= 0;
56 ptr->expiration= 0;
57 }
58
59 void memcached_result_free(memcached_result_st *ptr)
60 {
61 if (ptr == NULL)
62 return;
63
64 memcached_string_free(&ptr->value);
65
66 if (memcached_is_allocated(ptr))
67 {
68 if (ptr->root != NULL)
69 {
70 ptr->root->call_free(ptr->root, ptr);
71 }
72 else
73 {
74 free(ptr);
75 }
76 }
77 else
78 {
79 ptr->options.is_initialized= false;
80 }
81 }