1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-27 13:21:11 +03:00

buffer: Add and use ssh_buffer_allocate_size()

Add a small helper for ssh_buffer to ensure that the buffer has a
certain amount of space already preallocated. This can be useful in case
it is known how much data is going to be added to a buffer, to avoid
multiple reallocations.

Make use of it in few places in the library.

Signed-off-by: Pino Toscano <ptoscano@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Pino Toscano
2018-06-27 14:10:26 +02:00
committed by Andreas Schneider
parent afa4021ded
commit 12284b75fa
6 changed files with 221 additions and 0 deletions

View File

@@ -248,6 +248,34 @@ int ssh_buffer_add_data(struct ssh_buffer_struct *buffer, const void *data, uint
return 0;
}
/**
* @brief Ensure the buffer has at least a certain preallocated size.
*
* @param[in] buffer The buffer to enlarge.
*
* @param[in] len The length to ensure as allocated.
*
* @return 0 on success, < 0 on error.
*/
int ssh_buffer_allocate_size(struct ssh_buffer_struct *buffer,
uint32_t len)
{
buffer_verify(buffer);
if (buffer->allocated < len) {
if (buffer->pos > 0) {
buffer_shift(buffer);
}
if (realloc_buffer(buffer, len) < 0) {
return -1;
}
}
buffer_verify(buffer);
return 0;
}
/**
* @internal
*