Skip to content

Commit

Permalink
MT#61822 add type safety to obj_alloc
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rfuchs committed Jan 7, 2025
1 parent d3c46d2 commit 2ce79f9
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 96 deletions.
7 changes: 3 additions & 4 deletions daemon/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions daemon/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 15 additions & 21 deletions daemon/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);

Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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("");
Expand Down Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions daemon/control_ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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

Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -1134,7 +1132,7 @@ struct control_ng *control_ng_new(const endpoint_t *ep) {
}

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,
Expand Down
5 changes: 2 additions & 3 deletions daemon/control_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,15 @@ 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);
}

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,
Expand Down
5 changes: 2 additions & 3 deletions daemon/control_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ static void control_udp_incoming(struct obj *obj, struct udp_buffer *udp_buf) {
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);
Expand All @@ -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 */
Expand Down
6 changes: 2 additions & 4 deletions daemon/dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down
8 changes: 3 additions & 5 deletions daemon/ice.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions daemon/janus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions daemon/media_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions daemon/media_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -3114,8 +3114,7 @@ static void stream_fd_recv(struct obj *obj, char *buf, size_t len, struct sockad



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);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 2ce79f9

Please sign in to comment.