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