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
= 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 if (string
->root
->call_realloc
)
22 new_value
= (char *)string
->root
->call_realloc(string
->root
, string
->string
, new_size
);
24 new_value
= (char *)realloc(string
->string
, new_size
);
26 if (new_value
== NULL
)
27 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
29 string
->string
= new_value
;
30 string
->end
= string
->string
+ current_offset
;
32 string
->current_size
+= (string
->block_size
* adjust
);
35 return MEMCACHED_SUCCESS
;
38 memcached_string_st
*memcached_string_create(memcached_st
*ptr
, memcached_string_st
*string
, size_t initial_size
)
42 /* Saving malloc calls :) */
45 memset(string
, 0, sizeof(memcached_string_st
));
46 string
->is_allocated
= MEMCACHED_NOT_ALLOCATED
;
51 string
= (memcached_string_st
*)ptr
->call_malloc(ptr
, sizeof(memcached_string_st
));
53 string
= (memcached_string_st
*)malloc(sizeof(memcached_string_st
));
57 memset(string
, 0, sizeof(memcached_string_st
));
58 string
->is_allocated
= MEMCACHED_ALLOCATED
;
60 string
->block_size
= MEMCACHED_BLOCK_SIZE
;
63 rc
= memcached_string_check(string
, initial_size
);
64 if (rc
!= MEMCACHED_SUCCESS
)
67 ptr
->call_free(ptr
, string
);
74 WATCHPOINT_ASSERT(string
->string
== string
->end
);
79 memcached_return
memcached_string_append_character(memcached_string_st
*string
,
84 WATCHPOINT_ASSERT(string
->is_allocated
!= MEMCACHED_USED
);
86 rc
= memcached_string_check(string
, 1);
88 if (rc
!= MEMCACHED_SUCCESS
)
94 return MEMCACHED_SUCCESS
;
97 memcached_return
memcached_string_append(memcached_string_st
*string
,
98 char *value
, size_t length
)
102 WATCHPOINT_ASSERT(string
->is_allocated
!= MEMCACHED_USED
);
104 rc
= memcached_string_check(string
, length
);
106 if (rc
!= MEMCACHED_SUCCESS
)
109 WATCHPOINT_ASSERT(length
<= string
->current_size
);
110 WATCHPOINT_ASSERT(string
->string
);
111 WATCHPOINT_ASSERT(string
->end
>= string
->string
);
113 memcpy(string
->end
, value
, length
);
114 string
->end
+= length
;
116 return MEMCACHED_SUCCESS
;
119 size_t memcached_string_backspace(memcached_string_st
*string
, size_t remove
)
121 WATCHPOINT_ASSERT(string
->is_allocated
!= MEMCACHED_USED
);
123 if (string
->end
- string
->string
> remove
)
127 difference
= string
->end
- string
->string
;
128 string
->end
= string
->string
;
132 string
->end
-= remove
;
137 char *memcached_string_c_copy(memcached_string_st
*string
)
141 WATCHPOINT_ASSERT(string
->is_allocated
!= MEMCACHED_USED
);
143 if (string
->root
->call_malloc
)
144 c_ptr
= (char *)string
->root
->call_malloc(string
->root
, (memcached_string_length(string
)+1) * sizeof(char));
146 c_ptr
= (char *)malloc((memcached_string_length(string
)+1) * sizeof(char));
151 memcpy(c_ptr
, memcached_string_value(string
), memcached_string_length(string
));
152 c_ptr
[memcached_string_length(string
)]= 0;
157 memcached_return
memcached_string_reset(memcached_string_st
*string
)
159 WATCHPOINT_ASSERT(string
->is_allocated
!= MEMCACHED_USED
);
160 string
->end
= string
->string
;
162 return MEMCACHED_SUCCESS
;
165 void memcached_string_free(memcached_string_st
*ptr
)
172 if (ptr
->root
->call_free
)
173 ptr
->root
->call_free(ptr
->root
, ptr
->string
);
178 if (ptr
->is_allocated
== MEMCACHED_ALLOCATED
)
180 if (ptr
->root
->call_free
)
181 ptr
->root
->call_free(ptr
->root
, ptr
);
186 ptr
->is_allocated
= MEMCACHED_USED
;