From 027c38939700ef6468ab77395bd1ee1cb5ad3bf0 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Tue, 24 Sep 2024 09:35:40 -0400 Subject: [PATCH] MT#55283 avoid duplication of buf-alloc'd strings Do a quick test to see if a string is already allocated from the memory arena. If it is, don't duplicate it, just return the original allocation. Change-Id: Ia74442c15eef6dc7f3fabd3975ec11e4130eca21 --- include/call.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/call.h b/include/call.h index cd7eb9b2f..54e871462 100644 --- a/include/call.h +++ b/include/call.h @@ -314,6 +314,7 @@ typedef bencode_buffer_t call_buffer_t; #define call_buffer_alloc bencode_buffer_alloc #define call_buffer_init bencode_buffer_init #define call_buffer_free bencode_buffer_free +#define call_buffer_contains bencode_buffer_contains @@ -864,11 +865,16 @@ INLINE void *call_malloc(size_t l) { ret = call_buffer_alloc(&call_memory_arena->buffer, l); return ret; } +INLINE bool call_contains(const void *p) { + return call_buffer_contains(&call_memory_arena->buffer, p); +} INLINE char *call_strdup_len(const char *s, size_t len) { char *r; if (!s) return NULL; + if (call_contains(s)) + return (char *) s; r = call_malloc(len + 1); memcpy(r, s, len); r[len] = 0; @@ -897,6 +903,8 @@ INLINE str call_str_cpy_c(const char *in) { return call_str_cpy_len(in, in ? strlen(in) : 0); } INLINE str *call_str_dup(const str *in) { + if (call_contains(in)) + return (str *) in; str *out; out = call_malloc(sizeof(*out)); *out = call_str_cpy_len(in->s, in->len);