3 inline static memcached_return
_string_check(memcached_string_st
*string
, size_t need
)
5 if (need
&& need
> (size_t)(string
->current_size
- (size_t)(string
->end
- string
->string
)))
7 size_t current_offset
= (size_t) (string
->end
- string
->string
);
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
;
16 new_size
= sizeof(char) * (size_t)((adjust
* string
->block_size
) + string
->current_size
);
17 /* Test for overflow */
19 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
21 new_value
= string
->root
->call_realloc(string
->root
, string
->string
, new_size
);
23 if (new_value
== NULL
)
24 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
26 string
->string
= new_value
;
27 string
->end
= string
->string
+ current_offset
;
29 string
->current_size
+= (string
->block_size
* adjust
);
32 return MEMCACHED_SUCCESS
;
35 memcached_string_st
*memcached_string_create(memcached_st
*memc
, memcached_string_st
*string
, size_t initial_size
)
39 /* Saving malloc calls :) */
42 WATCHPOINT_ASSERT(memc
->options
.is_safe
&& string
->options
.is_initialized
== false);
44 memset(string
, 0, sizeof(memcached_string_st
));
48 string
= memc
->call_calloc(memc
, 1, sizeof(memcached_string_st
));
55 string
->options
.is_allocated
= true;
57 string
->block_size
= MEMCACHED_BLOCK_SIZE
;
60 rc
= _string_check(string
, initial_size
);
61 if (rc
!= MEMCACHED_SUCCESS
)
63 memc
->call_free(memc
, string
);
67 string
->options
.is_initialized
= true;
69 WATCHPOINT_ASSERT(string
->string
== string
->end
);
74 memcached_return
memcached_string_append_character(memcached_string_st
*string
,
79 rc
= _string_check(string
, 1);
81 if (rc
!= MEMCACHED_SUCCESS
)
84 *string
->end
= character
;
87 return MEMCACHED_SUCCESS
;
90 memcached_return
memcached_string_append(memcached_string_st
*string
,
91 const char *value
, size_t length
)
95 rc
= _string_check(string
, length
);
97 if (rc
!= MEMCACHED_SUCCESS
)
100 WATCHPOINT_ASSERT(length
<= string
->current_size
);
101 WATCHPOINT_ASSERT(string
->string
);
102 WATCHPOINT_ASSERT(string
->end
>= string
->string
);
104 memcpy(string
->end
, value
, length
);
105 string
->end
+= length
;
107 return MEMCACHED_SUCCESS
;
110 char *memcached_string_c_copy(memcached_string_st
*string
)
114 if (memcached_string_length(string
) == 0)
117 c_ptr
= string
->root
->call_malloc(string
->root
, (memcached_string_length(string
)+1) * sizeof(char));
122 memcpy(c_ptr
, memcached_string_value(string
), memcached_string_length(string
));
123 c_ptr
[memcached_string_length(string
)]= 0;
128 memcached_return
memcached_string_reset(memcached_string_st
*string
)
130 string
->end
= string
->string
;
132 return MEMCACHED_SUCCESS
;
135 void memcached_string_free(memcached_string_st
*ptr
)
142 ptr
->root
->call_free(ptr
->root
, ptr
->string
);
145 if (memcached_is_allocated(ptr
))
147 ptr
->root
->call_free(ptr
->root
, ptr
);
151 ptr
->options
.is_initialized
= false;
152 memset(ptr
, 0, sizeof(memcached_string_st
));
156 memcached_return
memcached_string_check(memcached_string_st
*string
, size_t need
)
158 return _string_check(string
, need
);