Skip to content

Commit 13f82df

Browse files
committed
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 4ea7873)
1 parent 7d95002 commit 13f82df

4 files changed

Lines changed: 12 additions & 17 deletions

File tree

daemon/media_player.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ static struct media_player_media_file *media_player_media_file_new(str blob) {
12811281
__auto_type fo = obj_alloc0(struct media_player_media_file,
12821282
media_player_media_file_free);
12831283
fo->blob = blob;
1284-
fo->blob.dup = call_ref; // string is allocated by reference on `fo`
1284+
fo->blob.arena = memory_arena; // string is allocated by reference on `fo`
12851285
RTPE_GAUGE_ADD(media_cache, blob.len);
12861286
fo->atime_us = fo->mtime_us = rtpe_now;
12871287
return fo;

include/arena.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,12 @@ INLINE char *memory_arena_dup(const char *b, size_t len) {
2929
ret[len] = '\0';
3030
return ret;
3131
}
32-
INLINE char *memory_arena_ref(const char *b, size_t len) {
33-
return (char *) b;
34-
}
35-
INLINE char *memory_arena_strdup_len(const char *s, size_t len, char *(*dup)(const char *, size_t)) {
36-
char *r;
32+
INLINE char *memory_arena_strdup_len(const char *s, size_t len, void *arena) {
3733
if (!s)
3834
return NULL;
39-
dup = dup ?: memory_arena_dup;
40-
r = dup(s, len);
41-
return r;
35+
if (arena == memory_arena)
36+
return (char *) s;
37+
return memory_arena_dup(s, len);
4238
}
4339

4440
INLINE char *memory_arena_strdup(const char *s) {
@@ -49,24 +45,24 @@ INLINE char *memory_arena_strdup(const char *s) {
4945
INLINE char *memory_arena_strdup_str(const str *s) {
5046
if (!s)
5147
return NULL;
52-
return memory_arena_strdup_len(s->s, s->len, s->dup);
48+
return memory_arena_strdup_len(s->s, s->len, s->arena);
5349
}
54-
INLINE str memory_arena_str_cpy_fn(const char *in, size_t len, char *(*dup)(const char *, size_t)) {
50+
INLINE str memory_arena_str_cpy_fn(const char *in, size_t len, void *arena) {
5551
str out;
5652
if (!in) {
5753
out = STR_NULL;
5854
return out;
5955
}
60-
out.s = memory_arena_strdup_len(in, len, dup);
56+
out.s = memory_arena_strdup_len(in, len, arena);
6157
out.len = len;
62-
out.dup = memory_arena_ref;
58+
out.arena = memory_arena;
6359
return out;
6460
}
6561
INLINE str memory_arena_str_cpy_len(const char *in, size_t len) {
6662
return memory_arena_str_cpy_fn(in, len, NULL);
6763
}
6864
INLINE str memory_arena_str_cpy(const str *in) {
69-
return memory_arena_str_cpy_fn((in ? in->s : NULL), (in ? in->len : 0), (in ? in->dup : NULL));
65+
return memory_arena_str_cpy_fn((in ? in->s : NULL), (in ? in->len : 0), (in ? in->arena : NULL));
7066
}
7167
INLINE str memory_arena_str_cpy_c(const char *in) {
7268
return memory_arena_str_cpy_len(in, in ? strlen(in) : 0);

include/call.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,6 @@ const rtp_payload_type *__rtp_stats_codec(struct call_media *m);
924924

925925
#define call_malloc memory_arena_alloc
926926
#define call_dup memory_arena_dup
927-
#define call_ref memory_arena_ref
928927

929928
#define call_strdup memory_arena_strdup
930929
#define call_strdup_str memory_arena_strdup_str

lib/str.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
struct _str {
1616
char *s;
1717
size_t len;
18-
char *(*dup)(const char *, size_t);
18+
void *arena;
1919
};
2020

2121
typedef struct _str str;
@@ -325,7 +325,7 @@ INLINE str *str_alloc(size_t len) {
325325
r = malloc(sizeof(*r) + len + 1);
326326
r->s = ((char *) r) + sizeof(*r);
327327
r->len = 0;
328-
r->dup = NULL;
328+
r->arena = NULL;
329329
return r;
330330
}
331331
INLINE str *str_dup(const str *s) {

0 commit comments

Comments
 (0)