Merge pull request #1665 from signalwire/switch_buffer_zero_fill

[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-05-23 21:53:55 +03:00 committed by GitHub
commit 3da5bd8587
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
* \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
* \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)
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
* \param buffer any buffer of type switch_buffer_t
* \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;
}
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->actually_used += datalen;
return buffer->used;