Changeset View
Changeset View
Standalone View
Standalone View
contrib/ldns/buffer.c
Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | |||||
bool | bool | ||||
ldns_buffer_set_capacity(ldns_buffer *buffer, size_t capacity) | ldns_buffer_set_capacity(ldns_buffer *buffer, size_t capacity) | ||||
{ | { | ||||
void *data; | void *data; | ||||
ldns_buffer_invariant(buffer); | ldns_buffer_invariant(buffer); | ||||
assert(buffer->_position <= capacity); | assert(buffer->_position <= capacity); | ||||
assert(!buffer->_fixed); | |||||
data = (uint8_t *) LDNS_XREALLOC(buffer->_data, uint8_t, capacity); | data = (uint8_t *) LDNS_XREALLOC(buffer->_data, uint8_t, capacity); | ||||
if (!data) { | if (!data) { | ||||
buffer->_status = LDNS_STATUS_MEM_ERR; | buffer->_status = LDNS_STATUS_MEM_ERR; | ||||
return false; | return false; | ||||
} else { | } else { | ||||
buffer->_data = data; | buffer->_data = data; | ||||
buffer->_limit = buffer->_capacity = capacity; | buffer->_limit = buffer->_capacity = capacity; | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
bool | bool | ||||
ldns_buffer_reserve(ldns_buffer *buffer, size_t amount) | ldns_buffer_reserve(ldns_buffer *buffer, size_t amount) | ||||
{ | { | ||||
ldns_buffer_invariant(buffer); | ldns_buffer_invariant(buffer); | ||||
assert(!buffer->_fixed); | |||||
if (buffer->_capacity < buffer->_position + amount) { | if (buffer->_capacity < buffer->_position + amount) { | ||||
size_t new_capacity = buffer->_capacity * 3 / 2; | size_t new_capacity = buffer->_capacity * 3 / 2; | ||||
if (new_capacity < buffer->_position + amount) { | if (new_capacity < buffer->_position + amount) { | ||||
new_capacity = buffer->_position + amount; | new_capacity = buffer->_position + amount; | ||||
} | } | ||||
if (!ldns_buffer_set_capacity(buffer, new_capacity)) { | if (!ldns_buffer_set_capacity(buffer, new_capacity)) { | ||||
buffer->_status = LDNS_STATUS_MEM_ERR; | buffer->_status = LDNS_STATUS_MEM_ERR; | ||||
▲ Show 20 Lines • Show All 87 Lines • Show Last 20 Lines |