]> git.alsa-project.org Git - alsa-lib.git/commitdiff
output: Add snd_output_buffer_steal() function
authorJaroslav Kysela <perex@perex.cz>
Mon, 12 Apr 2021 14:47:58 +0000 (16:47 +0200)
committerJaroslav Kysela <perex@perex.cz>
Tue, 13 Apr 2021 07:23:06 +0000 (09:23 +0200)
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
include/output.h
src/output.c

index 74ff78a2a2b66d1e6ef99ca307182031a7e3ce8a..4a970dc4ad14672246974415043e896958c2c0b1 100644 (file)
@@ -65,6 +65,7 @@ int snd_output_stdio_open(snd_output_t **outputp, const char *file, const char *
 int snd_output_stdio_attach(snd_output_t **outputp, FILE *fp, int _close);
 int snd_output_buffer_open(snd_output_t **outputp);
 size_t snd_output_buffer_string(snd_output_t *output, char **buf);
+size_t snd_output_buffer_steal(snd_output_t *output, char **buf);
 int snd_output_close(snd_output_t *output);
 int snd_output_printf(snd_output_t *output, const char *format, ...)
 #ifndef DOC_HIDDEN
index 7e3a91b3e5dac232fefd8dc8cdb73450e7d18092..70e6d65d84e7edfdcb7dcc8e35a2042d5676d99c 100644 (file)
@@ -252,6 +252,9 @@ static int snd_output_buffer_need(snd_output_t *output, size_t size)
        size_t alloc;
        unsigned char *buf;
 
+       /* use 'size++' to allow to add the '\0' string terminator */
+       /* without reallocation */
+       size++;
        if (_free >= size)
                return _free;
        if (buffer->alloc == 0)
@@ -349,6 +352,28 @@ size_t snd_output_buffer_string(snd_output_t *output, char **buf)
        return buffer->size;
 }
 
+/**
+ * \brief Returns the address of the buffer of a #SND_OUTPUT_BUFFER output handle.
+ * \param output The output handle.
+ * \param buf The functions puts the current address of the buffer at the
+ *            address specified by \p buf.
+ * \return The current size of valid data in the buffer.
+ *
+ * The internal buffer is empty after this call. The caller has the responsibility
+ * to clean the buffer using the free() call.
+ */
+size_t snd_output_buffer_steal(snd_output_t *output, char **buf)
+{
+       snd_output_buffer_t *buffer = output->private_data;
+       size_t size;
+       *buf = (char *)buffer->buf;
+       size = buffer->size;
+       buffer->buf = NULL;
+       buffer->alloc = 0;
+       buffer->size = 0;
+       return size;
+}
+
 /**
  * \brief Creates a new output object with an auto-extending memory buffer.
  * \param outputp The function puts the pointer to the new output object
@@ -377,4 +402,3 @@ int snd_output_buffer_open(snd_output_t **outputp)
        *outputp = output;
        return 0;
 }
-