[xferbuf] Record maximum required size

Record the maximum size required when writing into a data transfer
buffer.  This allows the maximum size to be determined even if
allocation fails (e.g. due to a fixed-size buffer or an out-of-memory
condition).

In the case of a fixed-size buffer (which may already be larger than
required), this allows the caller to determine the actual size used
for written data.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2026-02-25 00:00:28 +00:00
parent 7948ffe329
commit 3194c8ad0a
2 changed files with 7 additions and 0 deletions

View File

@ -63,6 +63,7 @@ void xferbuf_detach ( struct xfer_buffer *xferbuf ) {
xferbuf->data = NULL;
xferbuf->len = 0;
xferbuf->max = 0;
xferbuf->pos = 0;
}
@ -87,6 +88,10 @@ void xferbuf_free ( struct xfer_buffer *xferbuf ) {
static int xferbuf_ensure_size ( struct xfer_buffer *xferbuf, size_t len ) {
int rc;
/* Record maximum required size */
if ( len > xferbuf->max )
xferbuf->max = len;
/* If buffer is already large enough, do nothing */
if ( len <= xferbuf->len )
return 0;

View File

@ -21,6 +21,8 @@ struct xfer_buffer {
void *data;
/** Size of data */
size_t len;
/** Maximum required size of data */
size_t max;
/** Current offset within data */
size_t pos;
/** Data transfer buffer operations */