3 memcached_return
memcached_string_check(memcached_string_st
*string
, size_t need
)
5 if (need
> (size_t)(string
->current_size
- (size_t)(string
->end
- string
->string
)))
7 size_t current_offset
= string
->end
- string
->string
;
10 new_value
= (char *)realloc(string
->string
, sizeof(char) * string
->block_size
);
13 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
15 string
->string
= new_value
;
16 string
->end
= string
->string
+ current_offset
;
18 string
->current_size
+= string
->block_size
;
20 /* We zero the block structure we just realloced */
21 memset((string
->string
+ string
->current_size
) - string
->block_size
, 0,
22 sizeof(char) * string
->block_size
);
25 return MEMCACHED_SUCCESS
;
28 memcached_string_st
*memcached_string_init(memcached_st
*ptr
, size_t initial_size
)
30 memcached_string_st
*string
;
32 /* Saving malloc calls :) */
33 string
= (memcached_string_st
*)malloc(sizeof(memcached_string_st
) + (sizeof(char) * initial_size
));
36 memset(string
, 0, sizeof(memcached_string_st
));
37 string
->block_size
= initial_size
;
38 memcached_string_check(string
, initial_size
);
43 memcached_return
memcached_string_append_character(memcached_st
*ptr
,
44 memcached_string_st
*string
,
47 memcached_string_check(string
, 1);
51 return MEMCACHED_SUCCESS
;
54 memcached_return
memcached_string_append(memcached_st
*ptr
, memcached_string_st
*string
,
55 char *value
, size_t length
)
57 memcached_string_check(string
, length
);
58 memcpy(string
->end
, value
, length
);
61 return MEMCACHED_SUCCESS
;
65 size_t memcached_string_length(memcached_st
*ptr
, memcached_string_st
*string
)
67 return (size_t)(string
->end
- string
->string
);
70 size_t memcached_string_size(memcached_st
*ptr
, memcached_string_st
*string
)
72 return string
->current_size
;
76 size_t memcached_string_backspace(memcached_st
*ptr
, memcached_string_st
*string
, size_t remove
)
78 if (string
->end
- string
->string
> remove
)
82 difference
= string
->end
- string
->string
;
83 string
->end
= string
->string
;
92 memcached_return
memcached_string_reset(memcached_st
*ptr
, memcached_string_st
*string
)
94 string
->end
= string
->string
;
96 return MEMCACHED_SUCCESS
;
99 void memcached_string_free(memcached_st
*ptr
, memcached_string_st
*string
)
101 free(string
->string
);