From 162d306256b7eaaaeda8e1db8903cec74dbcc7b8 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Thu, 19 Feb 2026 09:35:50 -0400 Subject: [PATCH] MT#55283 update memory arena logic Use a pointer to the arena plus a comparison test to determine whether to ref or to dup the string, instead of a function pointer. The function pointer alone wasn't enough as there can be multiple arenas. Change-Id: Icd317e524fc2e5842f90d172ef7089d2cb0a5a28 (cherry picked from commit 4ea7873ca788cbc712a193398e5705f0d083d399) (cherry picked from commit 13f82df819ab1352a215a99d8884d1d3057be5a6) --- daemon/media_player.c | 2 +- include/arena.h | 22 +++++++++------------- include/call.h | 1 - lib/str.h | 4 ++-- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/daemon/media_player.c b/daemon/media_player.c index bee6ad08c..81ce2d870 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -1281,7 +1281,7 @@ static struct media_player_media_file *media_player_media_file_new(str blob) { __auto_type fo = obj_alloc0(struct media_player_media_file, media_player_media_file_free); fo->blob = blob; - fo->blob.dup = call_ref; // string is allocated by reference on `fo` + fo->blob.arena = memory_arena; // string is allocated by reference on `fo` RTPE_GAUGE_ADD(media_cache, blob.len); fo->atime_us = fo->mtime_us = rtpe_now; return fo; diff --git a/include/arena.h b/include/arena.h index 577c6addf..707114442 100644 --- a/include/arena.h +++ b/include/arena.h @@ -29,16 +29,12 @@ INLINE char *memory_arena_dup(const char *b, size_t len) { ret[len] = '\0'; return ret; } -INLINE char *memory_arena_ref(const char *b, size_t len) { - return (char *) b; -} -INLINE char *memory_arena_strdup_len(const char *s, size_t len, char *(*dup)(const char *, size_t)) { - char *r; +INLINE char *memory_arena_strdup_len(const char *s, size_t len, void *arena) { if (!s) return NULL; - dup = dup ?: memory_arena_dup; - r = dup(s, len); - return r; + if (arena == memory_arena) + return (char *) s; + return memory_arena_dup(s, len); } INLINE char *memory_arena_strdup(const char *s) { @@ -49,24 +45,24 @@ INLINE char *memory_arena_strdup(const char *s) { INLINE char *memory_arena_strdup_str(const str *s) { if (!s) return NULL; - return memory_arena_strdup_len(s->s, s->len, s->dup); + return memory_arena_strdup_len(s->s, s->len, s->arena); } -INLINE str memory_arena_str_cpy_fn(const char *in, size_t len, char *(*dup)(const char *, size_t)) { +INLINE str memory_arena_str_cpy_fn(const char *in, size_t len, void *arena) { str out; if (!in) { out = STR_NULL; return out; } - out.s = memory_arena_strdup_len(in, len, dup); + out.s = memory_arena_strdup_len(in, len, arena); out.len = len; - out.dup = memory_arena_ref; + out.arena = memory_arena; return out; } INLINE str memory_arena_str_cpy_len(const char *in, size_t len) { return memory_arena_str_cpy_fn(in, len, NULL); } INLINE str memory_arena_str_cpy(const str *in) { - return memory_arena_str_cpy_fn((in ? in->s : NULL), (in ? in->len : 0), (in ? in->dup : NULL)); + return memory_arena_str_cpy_fn((in ? in->s : NULL), (in ? in->len : 0), (in ? in->arena : NULL)); } INLINE str memory_arena_str_cpy_c(const char *in) { return memory_arena_str_cpy_len(in, in ? strlen(in) : 0); diff --git a/include/call.h b/include/call.h index 49a19061c..f7e490035 100644 --- a/include/call.h +++ b/include/call.h @@ -924,7 +924,6 @@ const rtp_payload_type *__rtp_stats_codec(struct call_media *m); #define call_malloc memory_arena_alloc #define call_dup memory_arena_dup -#define call_ref memory_arena_ref #define call_strdup memory_arena_strdup #define call_strdup_str memory_arena_strdup_str diff --git a/lib/str.h b/lib/str.h index 62b1d4de2..cffa03ffa 100644 --- a/lib/str.h +++ b/lib/str.h @@ -15,7 +15,7 @@ struct _str { char *s; size_t len; - char *(*dup)(const char *, size_t); + void *arena; }; typedef struct _str str; @@ -325,7 +325,7 @@ INLINE str *str_alloc(size_t len) { r = malloc(sizeof(*r) + len + 1); r->s = ((char *) r) + sizeof(*r); r->len = 0; - r->dup = NULL; + r->arena = NULL; return r; } INLINE str *str_dup(const str *s) {