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