This is an automated email from the ASF dual-hosted git repository. astitcher pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-proton.git
commit 42a1ae5783ace2ad8f35cb9f8259486735185356 Author: Andrew Stitcher <[email protected]> AuthorDate: Wed May 12 14:04:50 2021 -0400 PROTON-2447: Added API to pn_buffer to get access to available memory --- c/src/core/buffer.c | 37 +++++++++++++++++++++++++------------ c/src/core/buffer.h | 1 + 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/c/src/core/buffer.c b/c/src/core/buffer.c index 8e7d73d..5b697d6 100644 --- a/c/src/core/buffer.c +++ b/c/src/core/buffer.c @@ -293,24 +293,37 @@ int pn_buffer_defrag(pn_buffer_t *buf) pn_bytes_t pn_buffer_bytes(pn_buffer_t *buf) { - if (buf) { - pn_buffer_defrag(buf); - return pn_bytes(buf->size, buf->bytes); - } else { - return pn_bytes(0, NULL); + if (!buf) { + return (pn_bytes_t){0, NULL}; } + pn_buffer_defrag(buf); + return (pn_bytes_t){.size=buf->size, .start=buf->bytes}; } pn_rwbytes_t pn_buffer_memory(pn_buffer_t *buf) { - if (buf) { - pn_buffer_defrag(buf); - pn_rwbytes_t r = {buf->size, buf->bytes}; - return r; - } else { - pn_rwbytes_t r = {0, NULL}; - return r; + if (!buf) { + return (pn_rwbytes_t){0, NULL}; + } + pn_buffer_defrag(buf); + return (pn_rwbytes_t){.size=buf->size, .start=buf->bytes}; +} + +pn_rwbytes_t pn_buffer_free_memory(pn_buffer_t *buf) +{ + if (!buf) { + return (pn_rwbytes_t){0, NULL}; + } + size_t free_size = buf->capacity-buf->size; + if (buf->start == 0) { + return (pn_rwbytes_t){.size=free_size, .start=buf->bytes}; + } + // If free memory in one single blob don't need to defragment + if (buf->start+buf->size > buf->capacity) { + return (pn_rwbytes_t){.size=free_size, .start=buf->bytes+buf->start-free_size}; } + pn_buffer_defrag(buf); + return (pn_rwbytes_t){.size=free_size, .start=buf->bytes+buf->start+buf->size}; } int pn_buffer_quote(pn_buffer_t *buf, pn_string_t *str, size_t n) diff --git a/c/src/core/buffer.h b/c/src/core/buffer.h index 7193f8f..b9bd757 100644 --- a/c/src/core/buffer.h +++ b/c/src/core/buffer.h @@ -46,6 +46,7 @@ void pn_buffer_clear(pn_buffer_t *buf); int pn_buffer_defrag(pn_buffer_t *buf); pn_bytes_t pn_buffer_bytes(pn_buffer_t *buf); pn_rwbytes_t pn_buffer_memory(pn_buffer_t *buf); +pn_rwbytes_t pn_buffer_free_memory(pn_buffer_t *buf); int pn_buffer_quote(pn_buffer_t *buf, pn_string_t *string, size_t n); #ifdef __cplusplus --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
