From 2ce79f9bb49085dddc95aba67d8450f487ecae36 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Thu, 2 Jan 2025 10:49:22 -0400 Subject: [PATCH] MT#61822 add type safety to obj_alloc Make the macro return the appropriate pointer type, and make sure the free function takes an argument of the same type. This also eliminates some boilerplate type-casting code. Change-Id: I3094271fa2c53ec93b9ff9f837d461cf422e0f12 --- daemon/call.c | 7 +++---- daemon/cli.c | 5 ++--- daemon/codec.c | 36 +++++++++++++++--------------------- daemon/control_ng.c | 12 +++++------- daemon/control_tcp.c | 5 ++--- daemon/control_udp.c | 5 ++--- daemon/dtls.c | 6 ++---- daemon/ice.c | 8 +++----- daemon/janus.c | 5 ++--- daemon/media_player.c | 6 ++---- daemon/media_socket.c | 5 ++--- daemon/ssrc.c | 7 +++---- daemon/t38.c | 5 ++--- daemon/tcp_listener.c | 15 ++++++--------- daemon/timerthread.c | 2 +- daemon/udp_listener.c | 7 +++---- daemon/websocket.c | 7 +++---- lib/mix_buffer.c | 2 +- lib/obj.h | 26 ++++++++++++++++++++++---- lib/poller.c | 5 ++--- perf-tester/main.c | 5 ++--- 21 files changed, 85 insertions(+), 96 deletions(-) diff --git a/daemon/call.c b/daemon/call.c index 559eece1d..aa7767171 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -68,7 +68,7 @@ unsigned int call_socket_cpu_affinity = 0; */ static struct timeval add_ongoing_calls_dur_in_interval(struct timeval *interval_start, struct timeval *interval_duration); -static void __call_free(void *p); +static void __call_free(call_t *p); static void __call_cleanup(call_t *c); static void __monologue_stop(struct call_monologue *ml); static void media_stop(struct call_media *m); @@ -4256,8 +4256,7 @@ void __monologue_free(struct call_monologue *m) { g_slice_free1(sizeof(*m), m); } -static void __call_free(void *p) { - call_t *c = p; +static void __call_free(call_t *c) { struct call_monologue *m; struct call_media *md; struct packet_stream *ps; @@ -4317,7 +4316,7 @@ static call_t *call_create(const str *callid) { call_t *c; ilog(LOG_NOTICE, "Creating new call"); - c = obj_alloc0("call", sizeof(*c), __call_free); + c = obj_alloc0(call_t, __call_free); call_buffer_init(&c->buffer); rwlock_init(&c->master_lock); c->tags = tags_ht_new(); diff --git a/daemon/cli.c b/daemon/cli.c index 2d9ee46a9..f5eca76d9 100644 --- a/daemon/cli.c +++ b/daemon/cli.c @@ -1232,15 +1232,14 @@ void cli_handle(str *instr, struct cli_writer *cw) { release_closed_sockets(); } -static void cli_free(void *p) { - struct cli *c = p; +static void cli_free(struct cli *c) { streambuf_listener_shutdown(&c->listener); } struct cli *cli_new(const endpoint_t *ep) { struct cli *c; - c = obj_alloc0("cli", sizeof(*c), cli_free); + c = obj_alloc0(struct cli, cli_free); if (streambuf_listener_init(&c->listener, ep, cli_incoming, cli_stream_readable, diff --git a/daemon/codec.c b/daemon/codec.c index ea5adb5ca..9bb335808 100644 --- a/daemon/codec.c +++ b/daemon/codec.c @@ -281,7 +281,7 @@ static struct ssrc_entry *__ssrc_handler_transcode_new(void *p); static struct ssrc_entry *__ssrc_handler_decode_new(void *p); static struct ssrc_entry *__ssrc_handler_new(void *p); static void __ssrc_handler_stop(void *p, void *dummy); -static void __free_ssrc_handler(void *); +static void __free_ssrc_handler(struct codec_ssrc_handler *); static void __transcode_packet_free(struct transcode_packet *); @@ -966,8 +966,7 @@ static int codec_handler_non_rtp_update(struct call_media *receiver, struct call } -static void __rtcp_timer_free(void *p) { - struct rtcp_timer *rt = p; +static void __rtcp_timer_free(struct rtcp_timer *rt) { if (rt->call) obj_put(rt->call); } @@ -976,7 +975,7 @@ static void __rtcp_timer_run(struct codec_timer *); static void __codec_rtcp_timer_schedule(struct call_media *media) { struct rtcp_timer *rt = media->rtcp_timer; if (!rt) { - media->rtcp_timer = rt = obj_alloc0("rtcp_timer", sizeof(*rt), __rtcp_timer_free); + media->rtcp_timer = rt = obj_alloc0(struct rtcp_timer, __rtcp_timer_free); rt->ct.tt_obj.tt = &codec_timers_thread; rt->call = obj_get(media->call); rt->media = media; @@ -1578,8 +1577,7 @@ static struct codec_handler *codec_handler_get_udptl(struct call_media *m) { #endif -static void __mqtt_timer_free(void *p) { - struct mqtt_timer *mqt = p; +static void __mqtt_timer_free(struct mqtt_timer *mqt) { if (mqt->call) obj_put(mqt->call); } @@ -1639,7 +1637,7 @@ void mqtt_timer_start(struct mqtt_timer **mqtp, call_t *call, struct call_media if (*mqtp) // already scheduled return; - struct mqtt_timer *mqt = *mqtp = obj_alloc0("mqtt_timer", sizeof(*mqt), __mqtt_timer_free); + __auto_type mqt = *mqtp = obj_alloc0(struct mqtt_timer, __mqtt_timer_free); mqt->ct.tt_obj.tt = &codec_timers_thread; mqt->call = call ? obj_get(call) : NULL; mqt->self = mqtp; @@ -2672,7 +2670,7 @@ static void __transcode_packet_free(struct transcode_packet *p) { static struct ssrc_entry *__ssrc_handler_new(void *p) { // XXX combine with __ssrc_handler_transcode_new struct codec_handler *h = p; - struct codec_ssrc_handler *ch = obj_alloc0("codec_ssrc_handler", sizeof(*ch), __free_ssrc_handler); + __auto_type ch = obj_alloc0(struct codec_ssrc_handler, __free_ssrc_handler); ch->handler = h; ch->ptime = h->source_pt.ptime; if (!ch->ptime) @@ -3600,14 +3598,12 @@ static void __delay_buffer_shutdown(struct delay_buffer *dbuf, bool flush) { t_queue_clear_full(&dbuf->frames, delay_frame_free); obj_release(dbuf->call); } -static void __dtx_free(void *p) { - struct dtx_buffer *dtxb = p; +static void __dtx_free(struct dtx_buffer *dtxb) { __dtx_shutdown(dtxb); media_packet_release(&dtxb->last_mp); mutex_destroy(&dtxb->lock); } -static void __delay_buffer_free(void *p) { - struct delay_buffer *dbuf = p; +static void __delay_buffer_free(struct delay_buffer *dbuf) { __delay_buffer_shutdown(dbuf, false); mutex_destroy(&dbuf->lock); } @@ -3622,7 +3618,7 @@ static void __dtx_setup(struct codec_ssrc_handler *ch) { struct dtx_buffer *dtx = ch->dtx_buffer; if (!dtx) { - dtx = ch->dtx_buffer = obj_alloc0("dtx_buffer", sizeof(*dtx), __dtx_free); + dtx = ch->dtx_buffer = obj_alloc0(struct dtx_buffer, __dtx_free); dtx->ct.tt_obj.tt = &codec_timers_thread; dtx->ct.timer_func = __dtx_send_later; mutex_init(&dtx->lock); @@ -3660,7 +3656,7 @@ static void __delay_buffer_setup(struct delay_buffer **dbufp, if (!dbuf) { if (!delay) return; - dbuf = obj_alloc0("delay_buffer", sizeof(*dbuf), __delay_buffer_free); + dbuf = obj_alloc0(struct delay_buffer, __delay_buffer_free); dbuf->ct.tt_obj.tt = &codec_timers_thread; dbuf->ct.timer_func = __delay_send_later; dbuf->handler = h; @@ -3878,7 +3874,7 @@ static struct ssrc_entry *__ssrc_handler_transcode_new(void *p) { h->dest_pt.codec_def->rtpname, h->dest_pt.clock_rate, h->dest_pt.channels); - struct codec_ssrc_handler *ch = obj_alloc0("codec_ssrc_handler", sizeof(*ch), __free_ssrc_handler); + __auto_type ch = obj_alloc0(struct codec_ssrc_handler, __free_ssrc_handler); ch->handler = h; ch->ptime = h->dest_pt.ptime; ch->sample_buffer = g_string_new(""); @@ -3948,7 +3944,7 @@ static struct ssrc_entry *__ssrc_handler_decode_new(void *p) { h->source_pt.codec_def->rtpname, h->source_pt.clock_rate, h->source_pt.channels); - struct codec_ssrc_handler *ch = obj_alloc0("codec_ssrc_handler", sizeof(*ch), __free_ssrc_handler); + __auto_type ch = obj_alloc0(struct codec_ssrc_handler, __free_ssrc_handler); ch->handler = h; ch->ptime = h->dest_pt.ptime; @@ -3972,8 +3968,7 @@ static int __encoder_flush(encoder_t *enc, void *u1, void *u2) { *going = 1; return 0; } -static void __free_ssrc_handler(void *chp) { - struct codec_ssrc_handler *ch = chp; +static void __free_ssrc_handler(struct codec_ssrc_handler *ch) { if (ch->decoder) decoder_close(ch->decoder); if (ch->encoder) { @@ -5820,8 +5815,7 @@ bool codec_store_is_full_answer(const struct codec_store *src, const struct code -static void __codec_timer_callback_free(void *p) { - struct timer_callback *cb = p; +static void __codec_timer_callback_free(struct timer_callback *cb) { if (cb->call) obj_put(cb->call); } @@ -5835,7 +5829,7 @@ static void __codec_timer_callback_fire(struct codec_timer *ct) { void codec_timer_callback(call_t *c, void (*func)(call_t *, codec_timer_callback_arg_t), codec_timer_callback_arg_t a, uint64_t delay) { - struct timer_callback *cb = obj_alloc0("codec_timer_callback", sizeof(*cb), __codec_timer_callback_free); + __auto_type cb = obj_alloc0(struct timer_callback, __codec_timer_callback_free); cb->ct.tt_obj.tt = &codec_timers_thread; cb->call = obj_get(c); cb->timer_callback_func = func; diff --git a/daemon/control_ng.c b/daemon/control_ng.c index 058b46879..2dd1ddaa6 100644 --- a/daemon/control_ng.c +++ b/daemon/control_ng.c @@ -645,8 +645,7 @@ struct control_ng_stats* get_control_ng_stats(const sockaddr_t *addr) { return cur; } -static void __ng_buffer_free(void *p) { - ng_buffer *ngbuf = p; +static void __ng_buffer_free(ng_buffer *ngbuf) { bencode_buffer_free(&ngbuf->buffer); if (ngbuf->ref) obj_put_o(ngbuf->ref); @@ -659,7 +658,7 @@ static void __ng_buffer_free(void *p) { } ng_buffer *ng_buffer_new(struct obj *ref) { - ng_buffer *ngbuf = obj_alloc0("ng_buffer", sizeof(*ngbuf), __ng_buffer_free); + __auto_type ngbuf = obj_alloc0(ng_buffer, __ng_buffer_free); if (ref) ngbuf->ref = obj_get_o(ref); // hold until we're done @@ -1092,8 +1091,7 @@ static void control_stream_readable(struct streambuf_stream *s) { streambuf_stream_close(s); } -void control_ng_free(void *p) { - struct control_ng *c = p; +void control_ng_free(struct control_ng *c) { // XXX this should go elsewhere if (rtpe_cngs_hash) { GList *ll = g_hash_table_get_values(rtpe_cngs_hash); @@ -1115,7 +1113,7 @@ void control_ng_free(void *p) { struct control_ng *control_ng_new(const endpoint_t *ep) { struct control_ng *c; - c = obj_alloc0("control_ng", sizeof(*c), control_ng_free); + c = obj_alloc0(struct control_ng, control_ng_free); c->udp_listener.fd = -1; @@ -1134,7 +1132,7 @@ fail2: } struct control_ng *control_ng_tcp_new(const endpoint_t *ep) { - struct control_ng * ctrl_ng = obj_alloc0("control_ng", sizeof(*ctrl_ng), NULL); + struct control_ng *ctrl_ng = obj_alloc0(struct control_ng, NULL); ctrl_ng->udp_listener.fd = -1; if (streambuf_listener_init(&ctrl_ng->tcp_listener, ep, diff --git a/daemon/control_tcp.c b/daemon/control_tcp.c index 96c8af603..1ec956c06 100644 --- a/daemon/control_tcp.c +++ b/daemon/control_tcp.c @@ -142,8 +142,7 @@ static void control_incoming(struct streambuf_stream *s) { } -static void control_tcp_free(void *p) { - struct control_tcp *c = p; +static void control_tcp_free(struct control_tcp *c) { streambuf_listener_shutdown(&c->listener); pcre2_code_free(c->parse_re); } @@ -151,7 +150,7 @@ static void control_tcp_free(void *p) { struct control_tcp *control_tcp_new(const endpoint_t *ep) { struct control_tcp *c; - c = obj_alloc0("control", sizeof(*c), control_tcp_free); + c = obj_alloc0(struct control_tcp, control_tcp_free); if (streambuf_listener_init(&c->listener, ep, control_incoming, control_stream_readable, diff --git a/daemon/control_udp.c b/daemon/control_udp.c index 446010f22..6a44b4b61 100644 --- a/daemon/control_udp.c +++ b/daemon/control_udp.c @@ -140,8 +140,7 @@ out: log_info_pop(); } -void control_udp_free(void *p) { - struct control_udp *u = p; +void control_udp_free(struct control_udp *u) { pcre2_code_free(u->parse_re); pcre2_code_free(u->fallback_re); close_socket(&u->udp_listener); @@ -153,7 +152,7 @@ struct control_udp *control_udp_new(const endpoint_t *ep) { PCRE2_SIZE erroff; int errcode; - c = obj_alloc0("control_udp", sizeof(*c), control_udp_free); + c = obj_alloc0(struct control_udp, control_udp_free); c->parse_re = pcre2_compile( /* cookie cmd flags callid viabranch:5 */ diff --git a/daemon/dtls.c b/daemon/dtls.c index d757514ed..680d0a5d1 100644 --- a/daemon/dtls.c +++ b/daemon/dtls.c @@ -118,9 +118,7 @@ const struct dtls_hash_func *dtls_find_hash_func(const str *s) { return NULL; } -static void cert_free(void *p) { - struct dtls_cert *cert = p; - +static void cert_free(struct dtls_cert *cert) { if (cert->pkey) EVP_PKEY_free(cert->pkey); if (cert->x509) @@ -321,7 +319,7 @@ static int cert_init(void) { /* digest */ - new_cert = obj_alloc0("dtls_cert", sizeof(*new_cert), cert_free); + new_cert = obj_alloc0(struct dtls_cert, cert_free); for (int i = 0; i < num_hash_funcs; i++) { struct dtls_fingerprint *fp = malloc(sizeof(*fp)); diff --git a/daemon/ice.c b/daemon/ice.c index 3a7feb8af..e44cf9789 100644 --- a/daemon/ice.c +++ b/daemon/ice.c @@ -42,7 +42,7 @@ struct sdp_fragment { -static void __ice_agent_free(void *p); +static void __ice_agent_free(struct ice_agent *); static void create_random_ice_string(call_t *call, str *s, int len); static void __do_ice_checks(struct ice_agent *ag); static struct ice_candidate_pair *__pair_lookup(struct ice_agent *, struct ice_candidate *cand, @@ -385,7 +385,7 @@ static struct ice_agent *__ice_agent_new(struct call_media *media) { struct ice_agent *ag; call_t *call = media->call; - ag = obj_alloc0("ice_agent", sizeof(*ag), __ice_agent_free); + ag = obj_alloc0(struct ice_agent, __ice_agent_free); ag->tt_obj.tt = &ice_agents_timer_thread; ag->tt_obj.thread = &ice_agents_timer_thread.threads[0]; // there's only one thread ag->call = obj_get(call); @@ -648,9 +648,7 @@ static void __ice_agent_free_components(struct ice_agent *ag) { ice_candidates_free(&ag->remote_candidates); ice_candidate_pairs_free(&ag->candidate_pairs); } -static void __ice_agent_free(void *p) { - struct ice_agent *ag = p; - +static void __ice_agent_free(struct ice_agent *ag) { if (!ag) { ilogs(ice, LOG_ERR, "ice ag is NULL"); return; diff --git a/daemon/janus.c b/daemon/janus.c index cbe3188cc..a028cd011 100644 --- a/daemon/janus.c +++ b/daemon/janus.c @@ -65,8 +65,7 @@ static janus_handles_ht janus_handles; // handle ID -> handle static janus_rooms_ht janus_rooms; // room ID -> room -static void __janus_session_free(void *p) { - struct janus_session *s = p; +static void __janus_session_free(struct janus_session *s) { if (t_hash_table_size(s->websockets) != 0) ilog(LOG_WARN, "Janus session is leaking %i WS references", t_hash_table_size(s->websockets)); t_hash_table_destroy(s->websockets); @@ -1178,7 +1177,7 @@ static const char *janus_create(JsonReader *reader, JsonBuilder *builder, struct session_id = jr_str_int(reader); json_reader_end_member(reader); - struct janus_session *session = obj_alloc0("janus_session", sizeof(*session), __janus_session_free); + __auto_type session = obj_alloc0(struct janus_session, __janus_session_free); mutex_init(&session->lock); mutex_lock(&session->lock); // not really necessary but Coverity complains session->last_act = rtpe_now.tv_sec; diff --git a/daemon/media_player.c b/daemon/media_player.c index 1acbce797..4b3665936 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -177,9 +177,7 @@ long long media_player_stop(struct media_player *mp) { #ifdef WITH_TRANSCODING -static void __media_player_free(void *p) { - struct media_player *mp = p; - +static void __media_player_free(struct media_player *mp) { media_player_shutdown(mp); ssrc_ctx_put(&mp->ssrc_out); mutex_destroy(&mp->lock); @@ -205,7 +203,7 @@ void media_player_new(struct media_player **mpp, struct call_monologue *ml) { struct ssrc_ctx *ssrc_ctx = get_ssrc_ctx(ssrc, ml->ssrc_hash, SSRC_DIR_OUTPUT, ml); ssrc_ctx->next_rtcp = rtpe_now; - struct media_player *mp = *mpp = obj_alloc0("media_player", sizeof(*mp), __media_player_free); + __auto_type mp = *mpp = obj_alloc0(struct media_player, __media_player_free); mp->tt_obj.tt = &media_player_thread; mutex_init(&mp->lock); diff --git a/daemon/media_socket.c b/daemon/media_socket.c index 91409ad3b..7671eb769 100644 --- a/daemon/media_socket.c +++ b/daemon/media_socket.c @@ -3114,8 +3114,7 @@ out: -static void stream_fd_free(void *p) { - stream_fd *f = p; +static void stream_fd_free(stream_fd *f) { release_port(&f->socket, f->local_intf->spec); crypto_cleanup(&f->crypto); dtls_connection_cleanup(&f->dtls); @@ -3127,7 +3126,7 @@ stream_fd *stream_fd_new(socket_t *fd, call_t *call, struct local_intf *lif) { stream_fd *sfd; struct poller_item pi; - sfd = obj_alloc0("stream_fd", sizeof(*sfd), stream_fd_free); + sfd = obj_alloc0(stream_fd, stream_fd_free); sfd->unique_id = t_queue_get_length(&call->stream_fds); sfd->socket = *fd; sfd->call = obj_get(call); diff --git a/daemon/ssrc.c b/daemon/ssrc.c index 014824c9b..91b7c1948 100644 --- a/daemon/ssrc.c +++ b/daemon/ssrc.c @@ -23,7 +23,7 @@ static mos_calc_fn *mos_calcs[__MOS_TYPES] = { }; #endif -static void __free_ssrc_entry_call(void *e); +static void __free_ssrc_entry_call(struct ssrc_entry_call *e); static void init_ssrc_ctx(struct ssrc_ctx *c, struct ssrc_entry_call *parent) { @@ -42,7 +42,7 @@ static void init_ssrc_entry(struct ssrc_entry *ent, uint32_t ssrc) { } static struct ssrc_entry *create_ssrc_entry_call(void *uptr) { struct ssrc_entry_call *ent; - ent = obj_alloc0("ssrc_entry_call", sizeof(*ent), __free_ssrc_entry_call); + ent = obj_alloc0(struct ssrc_entry_call, __free_ssrc_entry_call); init_ssrc_ctx(&ent->input_ctx, ent); init_ssrc_ctx(&ent->output_ctx, ent); //ent->seq_out = ssl_random(); @@ -66,8 +66,7 @@ static void free_rr_time(struct ssrc_rr_time_item *i) { static void free_stats_block(struct ssrc_stats_block *ssb) { g_slice_free1(sizeof(*ssb), ssb); } -static void __free_ssrc_entry_call(void *ep) { - struct ssrc_entry_call *e = ep; +static void __free_ssrc_entry_call(struct ssrc_entry_call *e) { g_queue_clear_full(&e->sender_reports, (GDestroyNotify) free_sender_report); g_queue_clear_full(&e->rr_time_reports, (GDestroyNotify) free_rr_time); g_queue_clear_full(&e->stats_blocks, (GDestroyNotify) free_stats_block); diff --git a/daemon/t38.c b/daemon/t38.c index d0d397886..34e347d5c 100644 --- a/daemon/t38.c +++ b/daemon/t38.c @@ -237,8 +237,7 @@ static int t38_gateway_handler(t38_core_state_t *stat, void *user_data, const ui return 0; } -void __t38_gateway_free(void *p) { - struct t38_gateway *tg = p; +void __t38_gateway_free(struct t38_gateway *tg) { ilog(LOG_DEBUG, "Destroying T.38 gateway"); if (tg->gw) t38_gateway_free(tg->gw); @@ -382,7 +381,7 @@ int t38_gateway_pair(struct call_media *t38_media, struct call_media *pcm_media, ilog(LOG_DEBUG, "Creating new T.38 gateway"); // create and init new - struct t38_gateway *tg = obj_alloc0("t38_gateway", sizeof(*tg), __t38_gateway_free); + __auto_type tg = obj_alloc0(struct t38_gateway, __t38_gateway_free); tg->t38_media = t38_media; tg->pcm_media = pcm_media; diff --git a/daemon/tcp_listener.c b/daemon/tcp_listener.c index 8fe42259a..9fade2daa 100644 --- a/daemon/tcp_listener.c +++ b/daemon/tcp_listener.c @@ -63,8 +63,7 @@ static void tcp_listener_closed(int fd, void *p) { abort(); } -static void __tlc_free(void *p) { - struct tcp_listener_callback *cb = p; +static void __tlc_free(struct tcp_listener_callback *cb) { obj_put_o(cb->p); } @@ -74,7 +73,7 @@ static int tcp_listener_init(socket_t *sock, const endpoint_t *ep, struct poller_item i; struct tcp_listener_callback *cb; - cb = obj_alloc("tcp_listener_callback", sizeof(*cb), __tlc_free); + cb = obj_alloc(struct tcp_listener_callback, __tlc_free); cb->func = func; cb->p = obj_get_o(obj); cb->ul = sock; @@ -101,8 +100,7 @@ fail: return -1; } -static void streambuf_stream_free(void *p) { - struct streambuf_stream *s = p; +static void streambuf_stream_free(struct streambuf_stream *s) { streambuf_destroy(s->inbuf); streambuf_destroy(s->outbuf); obj_put(s->cb); @@ -163,7 +161,7 @@ static void streambuf_listener_newconn(struct obj *p, socket_t *newsock, char *a listener = cb->listener; - s = obj_alloc0("streambuf_stream", sizeof(*s), streambuf_stream_free); + s = obj_alloc0(struct streambuf_stream, streambuf_stream_free); s->sock = *newsock; s->inbuf = streambuf_new(rtpe_control_poller, newsock->fd); s->outbuf = streambuf_new(rtpe_control_poller, newsock->fd); @@ -206,8 +204,7 @@ fail: obj_put(s); } -static void __sb_free(void *p) { - struct streambuf_callback *cb = p; +static void __sb_free(struct streambuf_callback *cb) { obj_put_o(cb->parent); } @@ -224,7 +221,7 @@ int streambuf_listener_init(struct streambuf_listener *listener, const endpoint_ mutex_init(&listener->lock); listener->streams = tcp_streams_ht_new(); - cb = obj_alloc("streambuf_callback", sizeof(*cb), __sb_free); + cb = obj_alloc(struct streambuf_callback, __sb_free); cb->newconn_func = newconn_func; cb->newdata_func = newdata_func; cb->closed_func = closed_func; diff --git a/daemon/timerthread.c b/daemon/timerthread.c index 37042ee59..7e30b91c8 100644 --- a/daemon/timerthread.c +++ b/daemon/timerthread.c @@ -254,7 +254,7 @@ void *timerthread_queue_new(const char *type, size_t size, void (*free_func)(void *), void (*entry_free_func)(void *)) { - struct timerthread_queue *ttq = obj_alloc0(type, size, __timerthread_queue_free); + struct timerthread_queue *ttq = obj_alloc0_gen(type, size, __timerthread_queue_free); ttq->type = type; ttq->tt_obj.tt = tt; assert(tt->func == timerthread_queue_run); diff --git a/daemon/udp_listener.c b/daemon/udp_listener.c index ea56b9dd4..8c0316b47 100644 --- a/daemon/udp_listener.c +++ b/daemon/udp_listener.c @@ -36,7 +36,7 @@ static void udp_listener_incoming(int fd, void *p) { for (;;) { if (!udp_buf) { // initialise if we need to - udp_buf = obj_alloc0("udp_buffer", sizeof(*udp_buf), NULL); + udp_buf = obj_alloc0(struct udp_buffer, NULL); udp_buf->str.s = udp_buf->buf + RTP_BUFFER_HEAD_ROOM; udp_buf->listener = cb->ul; } @@ -71,8 +71,7 @@ static void udp_listener_incoming(int fd, void *p) { obj_put(udp_buf); } -static void __ulc_free(void *p) { - struct udp_listener_callback *cb = p; +static void __ulc_free(struct udp_listener_callback *cb) { obj_put_o(cb->p); } @@ -82,7 +81,7 @@ int udp_listener_init(socket_t *sock, const endpoint_t *ep, struct poller_item i; struct udp_listener_callback *cb; - cb = obj_alloc("udp_listener_callback", sizeof(*cb), __ulc_free); + cb = obj_alloc(struct udp_listener_callback, __ulc_free); cb->func = func; cb->p = obj_get_o(obj); cb->ul = sock; diff --git a/daemon/websocket.c b/daemon/websocket.c index 01fc9a944..5819fa967 100644 --- a/daemon/websocket.c +++ b/daemon/websocket.c @@ -496,15 +496,14 @@ static void websocket_ng_send_http(str *cookie, str *body, const endpoint_t *sin websocket_write_http(wc, NULL, true); } -static void __ng_buf_free(void *p) { - struct websocket_ng_buf *buf = p; +static void __ng_buf_free(struct websocket_ng_buf *buf) { g_string_free(buf->body, TRUE); } static const char *websocket_ng_process_generic(struct websocket_message *wm, __typeof__(control_ng_process) cb) { - struct websocket_ng_buf *buf = obj_alloc0("websocket_ng_buf", sizeof(*buf), __ng_buf_free); + __auto_type buf = obj_alloc0(struct websocket_ng_buf, __ng_buf_free); endpoint_print(&wm->wc->endpoint, buf->addr, sizeof(buf->addr)); @@ -531,7 +530,7 @@ static const char *websocket_ng_plain_process(struct websocket_message *wm) { static const char *websocket_http_ng_generic(struct websocket_message *wm, __typeof__(control_ng_process) cb) { - struct websocket_ng_buf *buf = obj_alloc0("websocket_ng_buf", sizeof(*buf), __ng_buf_free); + __auto_type buf = obj_alloc0(struct websocket_ng_buf, __ng_buf_free); endpoint_print(&wm->wc->endpoint, buf->addr, sizeof(buf->addr)); diff --git a/lib/mix_buffer.c b/lib/mix_buffer.c index 927e9abeb..bbd6806c1 100644 --- a/lib/mix_buffer.c +++ b/lib/mix_buffer.c @@ -310,7 +310,7 @@ bool mix_buffer_write_delay(struct mix_buffer *mb, uint32_t ssrc, const void *bu static struct ssrc_entry *mix_buffer_ssrc_new(void *p) { struct mix_buffer *mb = p; - mix_buffer_ssrc_source *src = obj_alloc0("mix_buffer_ssrc", sizeof(*src), NULL); + mix_buffer_ssrc_source *src = obj_alloc0(mix_buffer_ssrc_source, NULL); mix_buffer_src_init_pos(mb, src); return &src->h; } diff --git a/lib/obj.h b/lib/obj.h index 8237f6a0e..6c282dc93 100644 --- a/lib/obj.h +++ b/lib/obj.h @@ -53,8 +53,17 @@ struct obj { #define OBJ_MAGIC 0xf1eef1ee -#define obj_alloc(t,a,b) __obj_alloc(a,b,t,__FILE__,__func__,__LINE__) -#define obj_alloc0(t,a,b) __obj_alloc0(a,b,t,__FILE__,__func__,__LINE__) +#define obj_alloc(t,f) ({ \ + void (*__ff)(t *) = (f); \ + void *__r = __obj_alloc(sizeof(t), (void (*)(void *)) __ff, #t, __FILE__, __func__, __LINE__); \ + (t *) __r; \ + }) +#define obj_alloc0(t,f) ({ \ + void (*__ff)(t *) = (f); \ + void *__r = __obj_alloc0(sizeof(t), (void (*)(void *)) __ff, #t, __FILE__, __func__, __LINE__); \ + (t *) __r; \ + }) +#define obj_alloc0_gen(t,a,b) __obj_alloc0(a,b,t,__FILE__,__func__,__LINE__) #define obj_hold(a) __obj_hold(&(a)->obj,__FILE__,__func__,__LINE__) #define obj_get(a) ((__typeof__(a)) (__obj_get(&(a)->obj,__FILE__,__func__,__LINE__))) #define obj_put(a) __obj_put(&(a)->obj,__FILE__,__func__,__LINE__) @@ -77,8 +86,17 @@ INLINE void __obj_put(struct obj *o, #else -#define obj_alloc(t,a,b) __obj_alloc(a,b) -#define obj_alloc0(t,a,b) __obj_alloc0(a,b) +#define obj_alloc(t,f) ({ \ + void (*__ff)(t *) = (f); \ + void *__r = __obj_alloc(sizeof(t), (void (*)(void *)) __ff); \ + (t *) __r; \ + }) +#define obj_alloc0(t,f) ({ \ + void (*__ff)(t *) = (f); \ + void *__r = __obj_alloc0(sizeof(t), (void (*)(void *)) __ff); \ + (t *) __r; \ + }) +#define obj_alloc0_gen(t,a,b) __obj_alloc0(a,b) #define obj_hold(a) __obj_hold(&(a)->obj) #define obj_get(a) ((__typeof__(a)) (__obj_get(&(a)->obj))) #define obj_put(a) __obj_put(&(a)->obj) diff --git a/lib/poller.c b/lib/poller.c index 064a532ea..34d263af5 100644 --- a/lib/poller.c +++ b/lib/poller.c @@ -90,8 +90,7 @@ static int epoll_events(struct poller_item *it, struct poller_item_int *ii) { } -static void poller_item_free(void *p) { - struct poller_item_int *i = p; +static void poller_item_free(struct poller_item_int *i) { obj_put_o(i->item.obj); } @@ -127,7 +126,7 @@ bool poller_add_item(struct poller *p, struct poller_item *i) { if (i->fd >= p->items->len) g_ptr_array_set_size(p->items, i->fd + 1); - ip = obj_alloc0("poller_item_int", sizeof(*ip), poller_item_free); + ip = obj_alloc0(struct poller_item_int, poller_item_free); memcpy(&ip->item, i, sizeof(*i)); obj_hold_o(ip->item.obj); /* new ref in *ip */ p->items->pdata[i->fd] = obj_get(ip); diff --git a/perf-tester/main.c b/perf-tester/main.c index 7e9667155..32b083dbe 100644 --- a/perf-tester/main.c +++ b/perf-tester/main.c @@ -432,8 +432,7 @@ static void kill_threads(uint num) { } -static void stream_free(void *p) { - struct stream *s = p; +static void stream_free(struct stream *s) { close(s->output_fd); dump_close(s); if (s->encoder) @@ -467,7 +466,7 @@ static void new_stream_params( const codec_def_t *out_def, const struct testparams *outprm ) { - struct stream *s = obj_alloc0("stream", sizeof(*s), stream_free); + __auto_type s = obj_alloc0(struct stream, stream_free); // create timerfd s->timer_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);