3 memcached_return
memcached_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
*ptr
, memcached_string_st
*string
, size_t initial_size
)
39 /* Saving malloc calls :) */
41 memset(string
, 0, sizeof(memcached_string_st
));
44 string
= ptr
->call_calloc(ptr
, 1, sizeof(memcached_string_st
));
48 string
->is_allocated
= true;
50 string
->block_size
= MEMCACHED_BLOCK_SIZE
;
53 rc
= memcached_string_check(string
, initial_size
);
54 if (rc
!= MEMCACHED_SUCCESS
)
56 ptr
->call_free(ptr
, string
);
60 WATCHPOINT_ASSERT(string
->string
== string
->end
);
65 memcached_return
memcached_string_append_character(memcached_string_st
*string
,
70 rc
= memcached_string_check(string
, 1);
72 if (rc
!= MEMCACHED_SUCCESS
)
75 *string
->end
= character
;
78 return MEMCACHED_SUCCESS
;
81 memcached_return
memcached_string_append(memcached_string_st
*string
,
82 const char *value
, size_t length
)
86 rc
= memcached_string_check(string
, length
);
88 if (rc
!= MEMCACHED_SUCCESS
)
91 WATCHPOINT_ASSERT(length
<= string
->current_size
);
92 WATCHPOINT_ASSERT(string
->string
);
93 WATCHPOINT_ASSERT(string
->end
>= string
->string
);
95 memcpy(string
->end
, value
, length
);
98 return MEMCACHED_SUCCESS
;
101 char *memcached_string_c_copy(memcached_string_st
*string
)
105 if (memcached_string_length(string
) == 0)
108 c_ptr
= string
->root
->call_malloc(string
->root
, (memcached_string_length(string
)+1) * sizeof(char));
113 memcpy(c_ptr
, memcached_string_value(string
), memcached_string_length(string
));
114 c_ptr
[memcached_string_length(string
)]= 0;
119 memcached_return
memcached_string_reset(memcached_string_st
*string
)
121 string
->end
= string
->string
;
123 return MEMCACHED_SUCCESS
;
126 void memcached_string_free(memcached_string_st
*ptr
)
132 ptr
->root
->call_free(ptr
->root
, ptr
->string
);
134 if (ptr
->is_allocated
)
135 ptr
->root
->call_free(ptr
->root
, ptr
);
137 memset(ptr
, 0, sizeof(memcached_string_st
));