2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: String structure used for libmemcached.
14 inline static memcached_return_t
_string_check(memcached_string_st
*string
, size_t need
)
16 if (need
&& need
> (size_t)(string
->current_size
- (size_t)(string
->end
- string
->string
)))
18 size_t current_offset
= (size_t) (string
->end
- string
->string
);
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
))) / MEMCACHED_BLOCK_SIZE
;
27 new_size
= sizeof(char) * (size_t)((adjust
* MEMCACHED_BLOCK_SIZE
) + string
->current_size
);
28 /* Test for overflow */
30 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
32 new_value
= libmemcached_realloc(string
->root
, string
->string
, new_size
);
34 if (new_value
== NULL
)
35 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
37 string
->string
= new_value
;
38 string
->end
= string
->string
+ current_offset
;
40 string
->current_size
+= (MEMCACHED_BLOCK_SIZE
* adjust
);
43 return MEMCACHED_SUCCESS
;
46 static inline void _init_string(memcached_string_st
*self
)
48 self
->current_size
= 0;
49 self
->end
= self
->string
= NULL
;
52 memcached_string_st
*memcached_string_create(const memcached_st
*memc
, memcached_string_st
*self
, size_t initial_size
)
54 memcached_return_t rc
;
56 WATCHPOINT_ASSERT(memc
);
58 /* Saving malloc calls :) */
61 WATCHPOINT_ASSERT(self
->options
.is_initialized
== false);
63 self
->options
.is_allocated
= false;
67 self
= libmemcached_malloc(memc
, sizeof(memcached_string_st
));
74 self
->options
.is_allocated
= true;
80 rc
= _string_check(self
, initial_size
);
81 if (rc
!= MEMCACHED_SUCCESS
)
83 libmemcached_free(memc
, self
);
87 self
->options
.is_initialized
= true;
89 WATCHPOINT_ASSERT(self
->string
== self
->end
);
94 memcached_return_t
memcached_string_append_character(memcached_string_st
*string
,
97 memcached_return_t rc
;
99 rc
= _string_check(string
, 1);
101 if (rc
!= MEMCACHED_SUCCESS
)
104 *string
->end
= character
;
107 return MEMCACHED_SUCCESS
;
110 memcached_return_t
memcached_string_append(memcached_string_st
*string
,
111 const char *value
, size_t length
)
113 memcached_return_t rc
;
115 rc
= _string_check(string
, length
);
117 if (rc
!= MEMCACHED_SUCCESS
)
120 WATCHPOINT_ASSERT(length
<= string
->current_size
);
121 WATCHPOINT_ASSERT(string
->string
);
122 WATCHPOINT_ASSERT(string
->end
>= string
->string
);
124 memcpy(string
->end
, value
, length
);
125 string
->end
+= length
;
127 return MEMCACHED_SUCCESS
;
130 char *memcached_string_c_copy(memcached_string_st
*string
)
134 if (memcached_string_length(string
) == 0)
137 c_ptr
= libmemcached_malloc(string
->root
, (memcached_string_length(string
)+1) * sizeof(char));
142 memcpy(c_ptr
, memcached_string_value(string
), memcached_string_length(string
));
143 c_ptr
[memcached_string_length(string
)]= 0;
148 memcached_return_t
memcached_string_reset(memcached_string_st
*string
)
150 string
->end
= string
->string
;
152 return MEMCACHED_SUCCESS
;
155 void memcached_string_free(memcached_string_st
*ptr
)
162 libmemcached_free(ptr
->root
, ptr
->string
);
165 if (memcached_is_allocated(ptr
))
167 libmemcached_free(ptr
->root
, ptr
);
171 ptr
->options
.is_initialized
= false;
175 memcached_return_t
memcached_string_check(memcached_string_st
*string
, size_t need
)
177 return _string_check(string
, need
);
180 size_t memcached_string_length(const memcached_string_st
*self
)
182 return (size_t)(self
->end
- self
->string
);
185 size_t memcached_string_size(const memcached_string_st
*self
)
187 return self
->current_size
;
190 const char *memcached_string_value(const memcached_string_st
*self
)
195 char *memcached_string_value_mutable(const memcached_string_st
*self
)
200 void memcached_string_set_length(memcached_string_st
*self
, size_t length
)
202 self
->end
= self
->string
+ length
;