summaryrefslogtreecommitdiffstats
path: root/src/ring.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/ring.c
parent904c1b4d699aeb1ce170f0cd996a01d2d06812e3 (diff)
downloadzix-731ce39ef6fa35f64c19947bdb1719028478fdb9.tar.gz
zix-731ce39ef6fa35f64c19947bdb1719028478fdb9.tar.bz2
zix-731ce39ef6fa35f64c19947bdb1719028478fdb9.zip
Add custom allocator support
Diffstat (limited to 'src/ring.c')
-rw-r--r--src/ring.c38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/ring.c b/src/ring.c
index 4a4692f..ed1f32e 100644
--- a/src/ring.c
+++ b/src/ring.c
@@ -44,11 +44,12 @@
#endif
struct ZixRingImpl {
- uint32_t write_head; ///< Read index into buf
- uint32_t read_head; ///< Write index into buf
- uint32_t size; ///< Size (capacity) in bytes
- uint32_t size_mask; ///< Mask for fast modulo
- char* buf; ///< Contents
+ const ZixAllocator* allocator; ///< User allocator
+ uint32_t write_head; ///< Read index into buf
+ uint32_t read_head; ///< Write index into buf
+ uint32_t size; ///< Size (capacity) in bytes
+ uint32_t size_mask; ///< Mask for fast modulo
+ char* buf; ///< Contents
};
static inline uint32_t
@@ -66,14 +67,23 @@ next_power_of_two(uint32_t size)
}
ZixRing*
-zix_ring_new(uint32_t size)
+zix_ring_new(const ZixAllocator* const allocator, uint32_t size)
{
- ZixRing* ring = (ZixRing*)malloc(sizeof(ZixRing));
- ring->write_head = 0;
- ring->read_head = 0;
- ring->size = next_power_of_two(size);
- ring->size_mask = ring->size - 1;
- ring->buf = (char*)malloc(ring->size);
+ ZixRing* ring = (ZixRing*)zix_malloc(allocator, sizeof(ZixRing));
+
+ if (ring) {
+ ring->allocator = allocator;
+ ring->write_head = 0;
+ ring->read_head = 0;
+ ring->size = next_power_of_two(size);
+ ring->size_mask = ring->size - 1;
+
+ if (!(ring->buf = (char*)zix_malloc(allocator, ring->size))) {
+ zix_free(allocator, ring);
+ return NULL;
+ }
+ }
+
return ring;
}
@@ -81,8 +91,8 @@ void
zix_ring_free(ZixRing* ring)
{
if (ring) {
- free(ring->buf);
- free(ring);
+ zix_free(ring->allocator, ring->buf);
+ zix_free(ring->allocator, ring);
}
}