MT#55283 used aligned memory for bufferpool

Use allocators that return memory blocks aligned to the requested size,
instead of generic malloc. This makes it easier to play tricks with the
memory blocks.

Change-Id: Iad4b1127c76e48c2e9b4ad8489118d4883a24f6a
pull/1923/head
Richard Fuchs 9 months ago
parent 73616ae28d
commit 1a98502a1b

@ -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();

@ -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
}

@ -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);
}

@ -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;

@ -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");

@ -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;

Loading…
Cancel
Save