From 5f6739b9faecaec8d7fd7ce5f414365b48fe1a20 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Thu, 25 Jul 2024 16:27:45 -0400 Subject: [PATCH] MT#55283 formalise str_slice_q These helper functions were largely unused or inconsistently used. Create a new type and streamline usage. Change-Id: I5cd9756a26994d027dd4ff6a2d01a8c6c3cffe30 --- daemon/sdp.c | 11 +++++------ lib/str.c | 4 ---- lib/str.h | 13 +++++++++++-- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/daemon/sdp.c b/daemon/sdp.c index 95aeb4e41..e1338dc05 100644 --- a/daemon/sdp.c +++ b/daemon/sdp.c @@ -127,7 +127,7 @@ struct sdp_media { const char *c_line_pos; int as, rr, rs; struct sdp_attributes attributes; - GQueue format_list; /* list of slice-alloc'd str objects */ + str_slice_q format_list; /* list of slice-alloc'd str objects */ enum media_type media_type_id; int media_sdp_id; @@ -554,9 +554,8 @@ static int parse_media(str *value_str, struct sdp_media *output) { str formats = output->formats; str format; while (str_token_sep(&format, &formats, ' ')) { - sp = g_slice_alloc(sizeof(*sp)); - *sp = format; - g_queue_push_tail(&output->format_list, sp); + sp = str_slice_dup(&format); + t_queue_push_tail(&output->format_list, sp); } return 0; @@ -1441,7 +1440,7 @@ static void free_attributes(struct sdp_attributes *a) { } static void media_free(struct sdp_media *media) { free_attributes(&media->attributes); - g_queue_clear_full(&media->format_list, str_slice_free); + str_slice_q_clear_full(&media->format_list); g_slice_free1(sizeof(*media), media); } static void session_free(struct sdp_session *session) { @@ -1519,7 +1518,7 @@ static int __rtp_payload_types(struct stream_params *sp, struct sdp_media *media } /* then go through the format list and associate */ - for (GList *ql = media->format_list.head; ql; ql = ql->next) { + for (__auto_type ql = media->format_list.head; ql; ql = ql->next) { char *ep; str *s; unsigned int i; diff --git a/lib/str.c b/lib/str.c index e41e36599..69c896b72 100644 --- a/lib/str.c +++ b/lib/str.c @@ -49,10 +49,6 @@ str *__str_sprintf(const char *fmt, ...) { return ret; } -void str_slice_free(void *p) { - g_slice_free1(sizeof(str), p); -} - /** * Generates a random hexadecimal string representing n random bytes. diff --git a/lib/str.h b/lib/str.h index 5084100d3..d0cf3f61e 100644 --- a/lib/str.h +++ b/lib/str.h @@ -190,7 +190,7 @@ TYPED_GHASHTABLE(str_case_value_ht, str, str, str_case_hash, str_case_equal, fre /* returns a new str object, duplicates the pointers but doesn't duplicate the contents */ INLINE str *str_slice_dup(const str *); /* destroy function, frees a slice-alloc'd str */ -void str_slice_free(void *); +INLINE void str_slice_free(str *); /* saves "in" into "out" pseudo-URI encoded. "out" point to a buffer with sufficient length. returns length */ size_t str_uri_encode_len(char *out, const char *in, size_t in_len); @@ -201,6 +201,12 @@ str *str_uri_decode_len(const char *in, size_t in_len); G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(str, str_free_dup); +typedef str_q str_slice_q; +INLINE void str_slice_q_clear_full(str_slice_q *q) { + t_queue_clear_full(q, str_slice_free); +} +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(str_slice_q, str_slice_q_clear_full) + @@ -362,10 +368,13 @@ INLINE void str_free(str *s) { } INLINE str *str_slice_dup(const str *s) { str *r; - r = g_slice_alloc(sizeof(*r)); + r = g_new(str, 1); *r = *s; return r; } +INLINE void str_slice_free(str *p) { + g_free(p); +} #define STR_MALLOC_PADDING "xxxxxxxxxxxxxxxx" INLINE str *__str_vsprintf(const char *fmt, va_list ap) {