3
gearman_return gearman_byte_array_check(gearman_byte_array_st *byte_array, size_t need)
5
if (need && need > (size_t)(byte_array->current_size - (size_t)(byte_array->end - byte_array->byte_array)))
7
size_t current_offset= byte_array->end - byte_array->byte_array;
12
/* This is the block multiplier. To keep it larger and surive division errors we must round it up */
13
adjust= (need - (size_t)(byte_array->current_size - (size_t)(byte_array->end - byte_array->byte_array))) / byte_array->block_size;
16
new_size= sizeof(char) * (size_t)((adjust * byte_array->block_size) + byte_array->current_size);
17
/* Test for overflow */
19
return GEARMAN_MEMORY_ALLOCATION_FAILURE;
21
new_value= (char *)realloc(byte_array->byte_array, new_size);
23
if (new_value == NULL)
24
return GEARMAN_MEMORY_ALLOCATION_FAILURE;
26
byte_array->byte_array= new_value;
27
byte_array->end= byte_array->byte_array + current_offset;
29
byte_array->current_size+= (byte_array->block_size * adjust);
32
return GEARMAN_SUCCESS;
35
gearman_byte_array_st *gearman_byte_array_create(gearman_st *ptr, gearman_byte_array_st *byte_array, size_t initial_size)
39
/* Saving malloc calls :) */
42
memset(byte_array, 0, sizeof(gearman_byte_array_st));
43
byte_array->is_allocated= GEARMAN_NOT_ALLOCATED;
47
byte_array= (gearman_byte_array_st *)malloc(sizeof(gearman_byte_array_st));
49
if (byte_array == NULL)
51
memset(byte_array, 0, sizeof(gearman_byte_array_st));
52
byte_array->is_allocated= GEARMAN_ALLOCATED;
54
byte_array->block_size= GEARMAN_BLOCK_SIZE;
55
byte_array->root= ptr;
57
rc= gearman_byte_array_check(byte_array, initial_size);
58
if (rc != GEARMAN_SUCCESS)
65
WATCHPOINT_ASSERT(byte_array->byte_array == byte_array->end);
70
gearman_return gearman_byte_array_append_character(gearman_byte_array_st *byte_array,
75
WATCHPOINT_ASSERT(byte_array->is_allocated != GEARMAN_USED);
77
rc= gearman_byte_array_check(byte_array, 1);
79
if (rc != GEARMAN_SUCCESS)
82
*byte_array->end= ' ';
85
return GEARMAN_SUCCESS;
88
gearman_return gearman_byte_array_append(gearman_byte_array_st *byte_array,
89
const char *value, size_t length)
94
return GEARMAN_SUCCESS;
95
WATCHPOINT_ASSERT(byte_array->is_allocated != GEARMAN_USED);
97
rc= gearman_byte_array_check(byte_array, length);
99
if (rc != GEARMAN_SUCCESS)
102
WATCHPOINT_ASSERT(length <= byte_array->current_size);
103
WATCHPOINT_ASSERT(byte_array->byte_array);
104
WATCHPOINT_ASSERT(byte_array->end >= byte_array->byte_array);
106
memcpy(byte_array->end, value, length);
107
byte_array->end+= length;
109
return GEARMAN_SUCCESS;
112
size_t gearman_byte_array_backspace(gearman_byte_array_st *byte_array, size_t remove)
114
WATCHPOINT_ASSERT(byte_array->is_allocated != GEARMAN_USED);
116
if (byte_array->end - byte_array->byte_array > remove)
120
difference= byte_array->end - byte_array->byte_array;
121
byte_array->end= byte_array->byte_array;
125
byte_array->end-= remove;
130
char *gearman_byte_array_c_copy(gearman_byte_array_st *byte_array)
134
WATCHPOINT_ASSERT(byte_array->is_allocated != GEARMAN_USED);
136
if (gearman_byte_array_length(byte_array) == 0)
139
c_ptr= (char *)malloc((gearman_byte_array_length(byte_array)+1) * sizeof(char));
144
memcpy(c_ptr, gearman_byte_array_value(byte_array), gearman_byte_array_length(byte_array));
145
c_ptr[gearman_byte_array_length(byte_array)]= 0;
150
gearman_return gearman_byte_array_reset(gearman_byte_array_st *byte_array)
152
WATCHPOINT_ASSERT(byte_array->is_allocated != GEARMAN_USED);
153
byte_array->end= byte_array->byte_array;
155
return GEARMAN_SUCCESS;
158
void gearman_byte_array_free(gearman_byte_array_st *ptr)
164
free(ptr->byte_array);
166
if (ptr->is_allocated == GEARMAN_ALLOCATED)
169
ptr->is_allocated= GEARMAN_USED;
173
gearman_return gearman_byte_array_store(gearman_byte_array_st *byte_array,
174
const char *value, size_t length)
178
(void)gearman_byte_array_reset(byte_array);
179
rc= gearman_byte_array_append(byte_array, value, length);