From ad6fb06b138e7b46312c9f9b0b650a9fd2a4c5ae Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Mon, 3 Feb 2025 11:44:34 -0400 Subject: [PATCH] MT#55283 type safety for uid_alloc Change-Id: I4b5975d22f2713b66edba128f0749461578f9cde --- daemon/call.c | 16 ++++++++-------- daemon/media_socket.c | 4 ++-- daemon/redis.c | 3 +-- lib/helpers.h | 28 ++++++---------------------- 4 files changed, 17 insertions(+), 34 deletions(-) diff --git a/daemon/call.c b/daemon/call.c index f82e2fba0..9e88d2c0b 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -660,7 +660,7 @@ void call_free(void) { struct call_media *call_media_new(call_t *call) { struct call_media *med; - med = uid_slice_alloc0(med, &call->medias.q); + med = uid_alloc(&call->medias); med->call = call; codec_store_init(&med->codecs, med); codec_store_init(&med->offered_codecs, med); @@ -844,7 +844,7 @@ static struct endpoint_map *__get_endpoint_map(struct call_media *media, unsigne } else { __C_DBG("allocating new %sendpoint map", ep ? "" : "wildcard "); - em = uid_slice_alloc0(em, &media->call->endpoint_maps.q); + em = uid_alloc(&media->call->endpoint_maps); if (ep) em->endpoint = *ep; else @@ -960,7 +960,7 @@ static void __rtp_stats_free(void *p) { struct packet_stream *__packet_stream_new(call_t *call) { struct packet_stream *stream; - stream = uid_slice_alloc0(stream, &call->streams.q); + stream = uid_alloc(&call->streams); mutex_init(&stream->in_lock); mutex_init(&stream->out_lock); stream->call = call; @@ -4231,7 +4231,7 @@ void call_media_free(struct call_media **mdp) { t_queue_clear_full(&md->media_subscriptions, media_subscription_free); ice_candidates_free(&md->ice_candidates); mutex_destroy(&md->dtmf_lock); - g_slice_free1(sizeof(*md), md); + g_free(md); *mdp = NULL; } @@ -4250,7 +4250,7 @@ void __monologue_free(struct call_monologue *m) { t_queue_clear_full(&m->all_attributes, sdp_attr_free); t_queue_clear(&m->tag_aliases); sdp_streams_clear(&m->last_in_sdp_streams); - g_slice_free1(sizeof(*m), m); + g_free(m); } static void __call_free(call_t *c) { @@ -4279,7 +4279,7 @@ static void __call_free(call_t *c) { em = t_queue_pop_head(&c->endpoint_maps); t_queue_clear_full(&em->intf_sfds, free_sfd_intf_list); - g_slice_free1(sizeof(*em), em); + g_free(em); } t_hash_table_destroy(c->tags); @@ -4298,7 +4298,7 @@ static void __call_free(call_t *c) { ssrc_ctx_put(&ps->ssrc_out[u]); bufferpool_unref(ps->stats_in); bufferpool_unref(ps->stats_out); - g_slice_free1(sizeof(*ps), ps); + g_free(ps); } memory_arena_free(&c->buffer); @@ -4610,7 +4610,7 @@ struct call_monologue *__monologue_create(call_t *call) { struct call_monologue *ret; __C_DBG("creating new monologue"); - ret = uid_slice_alloc0(ret, &call->monologues.q); + ret = uid_alloc(&call->monologues); ret->call = call; ret->created = rtpe_now.tv_sec; diff --git a/daemon/media_socket.c b/daemon/media_socket.c index 514708913..fb75c82b2 100644 --- a/daemon/media_socket.c +++ b/daemon/media_socket.c @@ -848,7 +848,7 @@ static void __interface_append(struct intf_config *ifa, sockfamily_t *fam, bool } } - ifc = uid_slice_alloc0(ifc, &lif->list.q); + ifc = uid_alloc(&lif->list); ice_foundation(&ifc->ice_foundation); ifc->advertised_address = ifa->advertised_address; ifc->spec = spec; @@ -3231,7 +3231,7 @@ void interfaces_free(void) { while ((ifc = g_queue_pop_head(&all_local_interfaces))) { free(ifc->ice_foundation.s); bufferpool_unref(ifc->stats); - g_slice_free1(sizeof(*ifc), ifc); + g_free(ifc); } t_hash_table_destroy(__logical_intf_name_family_hash); diff --git a/daemon/redis.c b/daemon/redis.c index c4c0d34c5..4478ca71f 100644 --- a/daemon/redis.c +++ b/daemon/redis.c @@ -1,5 +1,4 @@ #include "redis.h" - #include #include #include @@ -1684,7 +1683,7 @@ static int redis_maps(call_t *c, struct redis_list *maps) { rh = &maps->rh[i]; /* from call.c:__get_endpoint_map() */ - em = uid_slice_alloc0(em, &c->endpoint_maps.q); + em = uid_alloc(&c->endpoint_maps); t_queue_init(&em->intf_sfds); em->wildcard = redis_hash_get_bool_flag(rh, "wildcard"); diff --git a/lib/helpers.h b/lib/helpers.h index f717a57ac..69eec55b7 100644 --- a/lib/helpers.h +++ b/lib/helpers.h @@ -254,28 +254,12 @@ INLINE void thread_create_detach(void (*f)(void *), void *a, const char *name) { /*** ALLOC WITH UNIQUE ID HELPERS ***/ -#define uid_slice_alloc(ptr, q) __uid_slice_alloc(sizeof(*(ptr)), q, \ - G_STRUCT_OFFSET(__typeof__(*(ptr)), unique_id)) -#define uid_slice_alloc0(ptr, q) __uid_slice_alloc0(sizeof(*(ptr)), q, \ - G_STRUCT_OFFSET(__typeof__(*(ptr)), unique_id)) -INLINE void __uid_slice_alloc_fill(void *ptr, GQueue *q, unsigned int offset) { - unsigned int *id; - id = G_STRUCT_MEMBER_P(ptr, offset); - *id = g_queue_get_length(q); - g_queue_push_tail(q, ptr); -} -INLINE void *__uid_slice_alloc(unsigned int size, GQueue *q, unsigned int offset) { - void *ret; - ret = g_slice_alloc(size); - __uid_slice_alloc_fill(ret, q, offset); - return ret; -} -INLINE void *__uid_slice_alloc0(unsigned int size, GQueue *q, unsigned int offset) { - void *ret; - ret = g_slice_alloc0(size); - __uid_slice_alloc_fill(ret, q, offset); - return ret; -} +#define uid_alloc(q) ({ \ + __typeof__((q)->__t) __ret = g_new0(__typeof__(*(q)->__t), 1); \ + __ret->unique_id = (q)->length; \ + t_queue_push_tail(q, __ret); \ + __ret; \ + }) #endif