diff --git a/daemon/helpers.c b/daemon/helpers.c index 7e640967a..b77835f53 100644 --- a/daemon/helpers.c +++ b/daemon/helpers.c @@ -254,7 +254,7 @@ static void *thread_detach_func(void *d) { dt->priority, strerror(errno)); } - media_bufferpool = bufferpool_new(g_malloc, g_free, 64 * 65536); + media_bufferpool = bufferpool_new(bufferpool_aligned_alloc, bufferpool_aligned_free, 64 * 65536); #ifdef HAVE_LIBURING if (rtpe_config.common.io_uring) uring_thread_init(); diff --git a/daemon/main.c b/daemon/main.c index 528055dd5..3cc0ba518 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -1458,7 +1458,7 @@ static void early_init(void) { #ifdef WITH_TRANSCODING static void clib_init(void) { - media_bufferpool = bufferpool_new(g_malloc, g_free, 64 * 65536); + media_bufferpool = bufferpool_new(bufferpool_aligned_alloc, bufferpool_aligned_free, 64 * 65536); #ifdef HAVE_LIBURING if (rtpe_config.common.io_uring) uring_thread_init(); @@ -1506,7 +1506,7 @@ static void kernel_setup(void) { return; fallback: - shm_bufferpool = bufferpool_new(g_malloc, g_free, 4096); // fallback userspace bufferpool + shm_bufferpool = bufferpool_new(bufferpool_aligned_alloc, bufferpool_aligned_free, 4096); // fallback userspace bufferpool } diff --git a/lib/bufferpool.c b/lib/bufferpool.c index baa8a1076..08e0cfbaa 100644 --- a/lib/bufferpool.c +++ b/lib/bufferpool.c @@ -10,6 +10,7 @@ struct bufferpool { void (*dealloc)(void *); void (*dealloc2)(void *, size_t); size_t shard_size; + size_t address_mask; mutex_t lock; GQueue empty_shards; GQueue full_shards; @@ -36,6 +37,8 @@ static struct bufferpool *bufferpool_new_common(void *(*alloc)(size_t), size_t s struct bufferpool *ret = g_new0(__typeof(*ret), 1); ret->alloc = alloc; ret->shard_size = shard_size; + ret->address_mask = shard_size - 1; + assert((ret->address_mask & shard_size) == 0); // must be a power of two mutex_init(&ret->lock); g_queue_init(&ret->empty_shards); g_queue_init(&ret->full_shards); @@ -103,6 +106,8 @@ static struct bpool_shard *bufferpool_new_shard(struct bufferpool *bp) { if (!buf) return NULL; + assert(((size_t) buf & bp->address_mask) == 0); + struct bpool_shard *ret = g_new0(__typeof(*ret), 1); ret->bp = bp; ret->buf = buf; @@ -316,3 +321,13 @@ void bufferpool_cleanup(void) { assert(bpool_shards->len == 0); g_ptr_array_free(bpool_shards, true); } + +void *bufferpool_aligned_alloc(size_t len) { + void *m = aligned_alloc(len, len); + assert(m != NULL); + return m; +} + +void bufferpool_aligned_free(void *p) { + free(p); +} diff --git a/lib/bufferpool.h b/lib/bufferpool.h index c28615bf6..aef2e87e4 100644 --- a/lib/bufferpool.h +++ b/lib/bufferpool.h @@ -27,6 +27,9 @@ INLINE void *bufferpool_alloc0(struct bufferpool *bp, size_t len) { return ret; } +void *bufferpool_aligned_alloc(size_t); +void bufferpool_aligned_free(void *); + typedef char bp_char; G_DEFINE_AUTOPTR_CLEANUP_FUNC(bp_char, bufferpool_unref); typedef char bp_void; diff --git a/t/test-stats.c b/t/test-stats.c index d739c58d7..f5778369d 100644 --- a/t/test-stats.c +++ b/t/test-stats.c @@ -68,7 +68,7 @@ static void __assert_metrics_eq(stats_metric_q *q, const char *b, unsigned int l int main(void) { rtpe_common_config_ptr = &rtpe_config.common; bufferpool_init(); - shm_bufferpool = bufferpool_new(g_malloc, g_free, 4096); + shm_bufferpool = bufferpool_new(bufferpool_aligned_alloc, bufferpool_aligned_free, 4096); endpoint_parse_any(&rtpe_config.graphite_ep, "1.2.3.4:4567"); diff --git a/t/test-transcode.c b/t/test-transcode.c index aa83ebced..11876828a 100644 --- a/t/test-transcode.c +++ b/t/test-transcode.c @@ -418,8 +418,8 @@ static void dtmf(const char *s) { int main(void) { rtpe_common_config_ptr = &rtpe_config.common; bufferpool_init(); - media_bufferpool = bufferpool_new(g_malloc, g_free, 4096); - shm_bufferpool = bufferpool_new(g_malloc, g_free, 4096); + media_bufferpool = bufferpool_new(bufferpool_aligned_alloc, bufferpool_aligned_free, 4096); + shm_bufferpool = bufferpool_new(bufferpool_aligned_alloc, bufferpool_aligned_free, 4096); unsigned long random_seed = 0;