Flip call (NULL is more common). Also add in helgrind and fix calloc.
[awesomized/libmemcached] / libmemcached / memcached_string.c
1 #include "common.h"
2
3 memcached_return memcached_string_check(memcached_string_st *string, size_t need)
4 {
5 if (need && need > (size_t)(string->current_size - (size_t)(string->end - string->string)))
6 {
7 size_t current_offset= string->end - string->string;
8 char *new_value;
9 size_t adjust;
10 size_t new_size;
11
12 /* This is the block multiplier. To keep it larger and surive division errors we must round it up */
13 adjust= (need - (size_t)(string->current_size - (size_t)(string->end - string->string))) / string->block_size;
14 adjust++;
15
16 new_size= sizeof(char) * (size_t)((adjust * string->block_size) + string->current_size);
17 /* Test for overflow */
18 if (new_size < need)
19 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
20
21 if (string->root->call_realloc)
22 new_value= (char *)string->root->call_realloc(string->root, string->string, new_size);
23 else
24 new_value= (char *)realloc(string->string, new_size);
25
26 if (new_value == NULL)
27 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
28
29 string->string= new_value;
30 string->end= string->string + current_offset;
31
32 string->current_size+= (string->block_size * adjust);
33 }
34
35 return MEMCACHED_SUCCESS;
36 }
37
38 memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string_st *string, size_t initial_size)
39 {
40 memcached_return rc;
41
42 /* Saving malloc calls :) */
43 if (string)
44 memset(string, 0, sizeof(memcached_string_st));
45 else
46 {
47 if (ptr->call_malloc)
48 string= (memcached_string_st *)ptr->call_malloc(ptr, sizeof(memcached_string_st));
49 else
50 string= (memcached_string_st *)calloc(1, sizeof(memcached_string_st));
51
52 if (string == NULL)
53 return NULL;
54 string->is_allocated= true;
55 }
56 string->block_size= MEMCACHED_BLOCK_SIZE;
57 string->root= ptr;
58
59 rc= memcached_string_check(string, initial_size);
60 if (rc != MEMCACHED_SUCCESS)
61 {
62 if (ptr->call_free)
63 ptr->call_free(ptr, string);
64 else
65 free(string);
66
67 return NULL;
68 }
69
70 WATCHPOINT_ASSERT(string->string == string->end);
71
72 return string;
73 }
74
75 memcached_return memcached_string_append_character(memcached_string_st *string,
76 char character)
77 {
78 memcached_return rc;
79
80 rc= memcached_string_check(string, 1);
81
82 if (rc != MEMCACHED_SUCCESS)
83 return rc;
84
85 *string->end= character;
86 string->end++;
87
88 return MEMCACHED_SUCCESS;
89 }
90
91 memcached_return memcached_string_append(memcached_string_st *string,
92 char *value, size_t length)
93 {
94 memcached_return rc;
95
96 rc= memcached_string_check(string, length);
97
98 if (rc != MEMCACHED_SUCCESS)
99 return rc;
100
101 WATCHPOINT_ASSERT(length <= string->current_size);
102 WATCHPOINT_ASSERT(string->string);
103 WATCHPOINT_ASSERT(string->end >= string->string);
104
105 memcpy(string->end, value, length);
106 string->end+= length;
107
108 return MEMCACHED_SUCCESS;
109 }
110
111 char *memcached_string_c_copy(memcached_string_st *string)
112 {
113 char *c_ptr;
114
115 if (memcached_string_length(string) == 0)
116 return NULL;
117
118 if (string->root->call_malloc)
119 c_ptr= (char *)string->root->call_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char));
120 else
121 c_ptr= (char *)malloc((memcached_string_length(string)+1) * sizeof(char));
122
123 if (c_ptr == NULL)
124 return NULL;
125
126 memcpy(c_ptr, memcached_string_value(string), memcached_string_length(string));
127 c_ptr[memcached_string_length(string)]= 0;
128
129 return c_ptr;
130 }
131
132 memcached_return memcached_string_reset(memcached_string_st *string)
133 {
134 string->end= string->string;
135
136 return MEMCACHED_SUCCESS;
137 }
138
139 void memcached_string_free(memcached_string_st *ptr)
140 {
141 if (ptr == NULL)
142 return;
143
144 if (ptr->string)
145 {
146 if (ptr->root->call_free)
147 ptr->root->call_free(ptr->root, ptr->string);
148 else
149 free(ptr->string);
150 }
151
152 if (ptr->is_allocated)
153 {
154 if (ptr->root->call_free)
155 ptr->root->call_free(ptr->root, ptr);
156 else
157 free(ptr);
158 }
159 else
160 memset(ptr, 0, sizeof(memcached_string_st));
161 }