diff options
author | David Robillard <d@drobilla.net> | 2023-09-10 15:06:42 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 248a874d7425749d29cf900a1c3783c624ea8d8c (patch) | |
tree | aed59f5a484a815cd254506866e98a947858904d /src/buffer.c | |
parent | 0bd10132c6707353dba80bd89cf0102ee7ca4e34 (diff) | |
download | serd-248a874d7425749d29cf900a1c3783c624ea8d8c.tar.gz serd-248a874d7425749d29cf900a1c3783c624ea8d8c.tar.bz2 serd-248a874d7425749d29cf900a1c3783c624ea8d8c.zip |
Add support for custom allocators
This makes it explicit in the API where memory is allocated, and allows the
user to provide a custom allocator to avoid the use of the default system
allocator for whatever reason.
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/src/buffer.c b/src/buffer.c index befa3943..c16a0ad6 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -1,11 +1,12 @@ // Copyright 2011-2021 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC +#include "memory.h" + #include "serd/buffer.h" #include <assert.h> #include <stddef.h> -#include <stdlib.h> #include <string.h> size_t @@ -20,20 +21,21 @@ serd_buffer_write(const void* const buf, SerdBuffer* const buffer = (SerdBuffer*)stream; const size_t n_bytes = size * nmemb; - char* const new_buf = (char*)realloc(buffer->buf, buffer->len + n_bytes); + char* const new_buf = + (char*)serd_arealloc(buffer->allocator, buffer->buf, buffer->len + n_bytes); + if (new_buf) { memcpy(new_buf + buffer->len, buf, n_bytes); buffer->buf = new_buf; buffer->len += nmemb; + return n_bytes; } - return new_buf ? nmemb : 0; + return 0; } int serd_buffer_close(void* const stream) { - serd_buffer_write("", 1, 1, stream); // Write null terminator - - return 0; + return serd_buffer_write("", 1, 1, stream) != 1; // Write null terminator } |