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/caret.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/caret.c')
-rw-r--r-- | src/caret.c | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/caret.c b/src/caret.c index 02d5b94b..2d7e9a8e 100644 --- a/src/caret.c +++ b/src/caret.c @@ -3,7 +3,10 @@ #include "caret.h" +#include "memory.h" + #include "serd/caret.h" +#include "serd/memory.h" #include <assert.h> #include <stdbool.h> @@ -11,13 +14,15 @@ #include <string.h> SerdCaret* -serd_caret_new(const SerdNode* const document, +serd_caret_new(SerdAllocator* const allocator, + const SerdNode* const document, const unsigned line, const unsigned column) { assert(document); - SerdCaret* caret = (SerdCaret*)malloc(sizeof(SerdCaret)); + SerdCaret* const caret = + (SerdCaret*)serd_amalloc(allocator, sizeof(SerdCaret)); if (caret) { caret->document = document; @@ -29,21 +34,26 @@ serd_caret_new(const SerdNode* const document, } SerdCaret* -serd_caret_copy(const SerdCaret* const caret) +serd_caret_copy(SerdAllocator* const allocator, const SerdCaret* const caret) { if (!caret) { return NULL; } - SerdCaret* copy = (SerdCaret*)malloc(sizeof(SerdCaret)); - memcpy(copy, caret, sizeof(SerdCaret)); + SerdCaret* const copy = + (SerdCaret*)serd_amalloc(allocator, sizeof(SerdCaret)); + + if (copy) { + memcpy(copy, caret, sizeof(SerdCaret)); + } + return copy; } void -serd_caret_free(SerdCaret* const caret) +serd_caret_free(SerdAllocator* const allocator, SerdCaret* const caret) { - free(caret); + serd_afree(allocator, caret); } bool |