[Core] Allow switch_buffer_write() to fill in a buffer with zeros. Add switch_buffer_zero_fill()

This commit is contained in:
Andrey Volk 2022-02-19 18:10:26 +03:00
parent f31dcd26f8
commit aa161b9e58
2 changed files with 9 additions and 2 deletions

View File

@ -132,13 +132,15 @@ SWITCH_DECLARE(void) switch_buffer_set_loops(_In_ switch_buffer_t *buffer, _In_
/*! \brief Write data into a switch_buffer_t up to the length of datalen /*! \brief Write data into a switch_buffer_t up to the length of datalen
* \param buffer any buffer of type switch_buffer_t * \param buffer any buffer of type switch_buffer_t
* \param data pointer to the data to be written * \param data pointer to the data to be written or 0 to fill in with datalen zeros
* \param datalen amount of data to be written * \param datalen amount of data to be written
* \return int amount of buffer used after the write, or 0 if no space available * \return int amount of buffer used after the write, or 0 if no space available
*/ */
SWITCH_DECLARE(switch_size_t) switch_buffer_write(_In_ switch_buffer_t *buffer, _In_bytecount_(datalen) SWITCH_DECLARE(switch_size_t) switch_buffer_write(_In_ switch_buffer_t *buffer, _In_bytecount_(datalen)
const void *data, _In_ switch_size_t datalen); const void *data, _In_ switch_size_t datalen);
#define switch_buffer_zero_fill(_buffer, _datalen) switch_buffer_write(_buffer, 0, _datalen);
/*! \brief Remove data from the buffer /*! \brief Remove data from the buffer
* \param buffer any buffer of type switch_buffer_t * \param buffer any buffer of type switch_buffer_t
* \param datalen amount of data to be removed * \param datalen amount of data to be removed

View File

@ -344,7 +344,12 @@ SWITCH_DECLARE(switch_size_t) switch_buffer_write(switch_buffer_t *buffer, const
return 0; return 0;
} }
memcpy(buffer->head + buffer->used, data, datalen); if (data) {
memcpy(buffer->head + buffer->used, data, datalen);
} else {
memset(buffer->head + buffer->used, 0, datalen);
}
buffer->used += datalen; buffer->used += datalen;
buffer->actually_used += datalen; buffer->actually_used += datalen;
return buffer->used; return buffer->used;