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
))) / string
->block_size
;
27 new_size
= sizeof(char) * (size_t)((adjust
* string
->block_size
) + string
->current_size
);
28 /* Test for overflow */
30 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
32 new_value
= string
->root
->call_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
+= (string
->block_size
* adjust
);
43 return MEMCACHED_SUCCESS
;
46 memcached_string_st
*memcached_string_create(memcached_st
*memc
, memcached_string_st
*string
, size_t initial_size
)
48 memcached_return_t rc
;
50 /* Saving malloc calls :) */
53 WATCHPOINT_ASSERT(string
->options
.is_initialized
== false);
55 memset(string
, 0, sizeof(memcached_string_st
));
59 string
= memc
->call_calloc(memc
, 1, sizeof(memcached_string_st
));
66 string
->options
.is_allocated
= true;
68 string
->block_size
= MEMCACHED_BLOCK_SIZE
;
71 rc
= _string_check(string
, initial_size
);
72 if (rc
!= MEMCACHED_SUCCESS
)
74 memc
->call_free(memc
, string
);
78 string
->options
.is_initialized
= true;
80 WATCHPOINT_ASSERT(string
->string
== string
->end
);
85 memcached_return_t
memcached_string_append_character(memcached_string_st
*string
,
88 memcached_return_t rc
;
90 rc
= _string_check(string
, 1);
92 if (rc
!= MEMCACHED_SUCCESS
)
95 *string
->end
= character
;
98 return MEMCACHED_SUCCESS
;
101 memcached_return_t
memcached_string_append(memcached_string_st
*string
,
102 const char *value
, size_t length
)
104 memcached_return_t rc
;
106 rc
= _string_check(string
, length
);
108 if (rc
!= MEMCACHED_SUCCESS
)
111 WATCHPOINT_ASSERT(length
<= string
->current_size
);
112 WATCHPOINT_ASSERT(string
->string
);
113 WATCHPOINT_ASSERT(string
->end
>= string
->string
);
115 memcpy(string
->end
, value
, length
);
116 string
->end
+= length
;
118 return MEMCACHED_SUCCESS
;
121 char *memcached_string_c_copy(memcached_string_st
*string
)
125 if (memcached_string_length(string
) == 0)
128 c_ptr
= string
->root
->call_malloc(string
->root
, (memcached_string_length(string
)+1) * sizeof(char));
133 memcpy(c_ptr
, memcached_string_value(string
), memcached_string_length(string
));
134 c_ptr
[memcached_string_length(string
)]= 0;
139 memcached_return_t
memcached_string_reset(memcached_string_st
*string
)
141 string
->end
= string
->string
;
143 return MEMCACHED_SUCCESS
;
146 void memcached_string_free(memcached_string_st
*ptr
)
153 ptr
->root
->call_free(ptr
->root
, ptr
->string
);
156 if (memcached_is_allocated(ptr
))
158 ptr
->root
->call_free(ptr
->root
, ptr
);
162 ptr
->options
.is_initialized
= false;
163 memset(ptr
, 0, sizeof(memcached_string_st
));
167 memcached_return_t
memcached_string_check(memcached_string_st
*string
, size_t need
)
169 return _string_check(string
, need
);