summaryrefslogtreecommitdiffstats
path: root/src/allocator.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-09-10 20:11:47 -0400
committerDavid Robillard <d@drobilla.net>2021-09-10 20:54:28 -0400
commit731ce39ef6fa35f64c19947bdb1719028478fdb9 (patch)
tree87304c55061fc9cd0ab1c3007a78eff44de137dc /src/allocator.c
parent904c1b4d699aeb1ce170f0cd996a01d2d06812e3 (diff)
downloadzix-731ce39ef6fa35f64c19947bdb1719028478fdb9.tar.gz
zix-731ce39ef6fa35f64c19947bdb1719028478fdb9.tar.bz2
zix-731ce39ef6fa35f64c19947bdb1719028478fdb9.zip
Add custom allocator support
Diffstat (limited to 'src/allocator.c')
-rw-r--r--src/allocator.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/allocator.c b/src/allocator.c
new file mode 100644
index 0000000..ccd48ea
--- /dev/null
+++ b/src/allocator.c
@@ -0,0 +1,67 @@
+/*
+ Copyright 2011-2021 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "zix/allocator.h"
+
+#include <stdlib.h>
+
+ZIX_MALLOC_FUNC
+static void*
+zix_default_malloc(ZixAllocatorHandle* const handle, const size_t size)
+{
+ (void)handle;
+ return malloc(size);
+}
+
+ZIX_MALLOC_FUNC
+static void*
+zix_default_calloc(ZixAllocatorHandle* const handle,
+ const size_t nmemb,
+ const size_t size)
+{
+ (void)handle;
+ return calloc(nmemb, size);
+}
+
+static void*
+zix_default_realloc(ZixAllocatorHandle* const handle,
+ void* const ptr,
+ const size_t size)
+{
+ (void)handle;
+ return realloc(ptr, size);
+}
+
+static void
+zix_default_free(ZixAllocatorHandle* const handle, void* const ptr)
+{
+ (void)handle;
+ free(ptr);
+}
+
+const ZixAllocator*
+zix_default_allocator(void)
+{
+ static const ZixAllocator default_allocator = {
+ NULL,
+ zix_default_malloc,
+ zix_default_calloc,
+ zix_default_realloc,
+ zix_default_free,
+ };
+
+ return &default_allocator;
+}