Skip to content

Commit

Permalink
Fix redundant memory initializations
Browse files Browse the repository at this point in the history
Substituted g_malloc0() with g_malloc() in several places, avoiding a
redundant or unneeded memory initialization. Some places already had
explicit initializations of all members of the allocated structure;
others did a memcpy() over all the allocated memory. Where structures
were being dinamically allocated, all missing member initializations
were explicitly added.

Removed several memset() calls to initialize memory which where already
initialized by g_malloc0().

Removed some explicit member initialization of structures allocated with
g_malloc0().
  • Loading branch information
Douglas Caetano dos Santos committed Jan 23, 2018
1 parent d01b9ad commit 9a9a14e
Show file tree
Hide file tree
Showing 25 changed files with 96 additions and 102 deletions.
27 changes: 13 additions & 14 deletions ice.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ void janus_ice_notify_hangup(janus_ice_handle *handle, const char *reason) {
janus_ice_trickle *janus_ice_trickle_new(janus_ice_handle *handle, const char *transaction, json_t *candidate) {
if(transaction == NULL || candidate == NULL)
return NULL;
janus_ice_trickle *trickle = g_malloc0(sizeof(janus_ice_trickle));
janus_ice_trickle *trickle = g_malloc(sizeof(janus_ice_trickle));
trickle->handle = handle;
trickle->received = janus_get_monotonic_time();
trickle->transaction = g_strdup(transaction);
Expand Down Expand Up @@ -957,7 +957,7 @@ gint janus_ice_handle_attach_plugin(void *gateway_session, guint64 handle_id, ja
return JANUS_ERROR_PLUGIN_ATTACH;
}
int error = 0;
janus_plugin_session *session_handle = g_malloc0(sizeof(janus_plugin_session));
janus_plugin_session *session_handle = g_malloc(sizeof(janus_plugin_session));
if(session_handle == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return JANUS_ERROR_UNKNOWN; /* FIXME Do we need something like "Internal Server Error"? */
Expand Down Expand Up @@ -2321,8 +2321,8 @@ static void janus_ice_cb_nice_recv(NiceAgent *agent, guint stream_id, guint comp
p->last_retransmit = now;
retransmits_cnt++;
/* Enqueue it */
janus_ice_queued_packet *pkt = (janus_ice_queued_packet *)g_malloc0(sizeof(janus_ice_queued_packet));
pkt->data = g_malloc0(p->length);
janus_ice_queued_packet *pkt = (janus_ice_queued_packet *)g_malloc(sizeof(janus_ice_queued_packet));
pkt->data = g_malloc(p->length);
memcpy(pkt->data, p->data, p->length);
pkt->length = p->length;
pkt->type = video ? JANUS_ICE_PACKET_VIDEO : JANUS_ICE_PACKET_AUDIO;
Expand Down Expand Up @@ -3243,7 +3243,7 @@ void *janus_ice_send_thread(void *data) {
guint32 i = 0;
for (i = handle->stream->transport_wide_cc_last_feedback_seq_num+1; i<transport_seq_num; ++i) {
/* Create new stat */
janus_rtcp_transport_wide_cc_stats *missing = g_malloc0(sizeof(janus_rtcp_transport_wide_cc_stats));
janus_rtcp_transport_wide_cc_stats *missing = g_malloc(sizeof(janus_rtcp_transport_wide_cc_stats));
/* Add missing packet */
missing->transport_seq_num = i;
missing->timestamp = 0;
Expand Down Expand Up @@ -3417,7 +3417,6 @@ void *janus_ice_send_thread(void *data) {
/* There's a REMB, prepend a RR as it won't work otherwise */
int rrlen = 32;
char *rtcpbuf = g_malloc0(rrlen+pkt->length);
memset(rtcpbuf, 0, rrlen+pkt->length);
rtcp_rr *rr = (rtcp_rr *)rtcpbuf;
rr->header.version = 2;
rr->header.type = RTCP_RR;
Expand Down Expand Up @@ -3659,8 +3658,8 @@ void *janus_ice_send_thread(void *data) {
pkt = NULL;
continue;
}
janus_rtp_packet *p = (janus_rtp_packet *)g_malloc0(sizeof(janus_rtp_packet));
p->data = (char *)g_malloc0(protected);
janus_rtp_packet *p = (janus_rtp_packet *)g_malloc(sizeof(janus_rtp_packet));
p->data = (char *)g_malloc(protected);
memcpy(p->data, sbuf, protected);
p->length = protected;
p->created = janus_get_monotonic_time();
Expand Down Expand Up @@ -3762,8 +3761,8 @@ void janus_ice_relay_rtp(janus_ice_handle *handle, int video, char *buf, int len
|| (video && !janus_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_HAS_VIDEO)))
return;
/* Queue this packet */
janus_ice_queued_packet *pkt = (janus_ice_queued_packet *)g_malloc0(sizeof(janus_ice_queued_packet));
pkt->data = g_malloc0(len);
janus_ice_queued_packet *pkt = (janus_ice_queued_packet *)g_malloc(sizeof(janus_ice_queued_packet));
pkt->data = g_malloc(len);
memcpy(pkt->data, buf, len);
pkt->length = len;
pkt->type = video ? JANUS_ICE_PACKET_VIDEO : JANUS_ICE_PACKET_AUDIO;
Expand Down Expand Up @@ -3800,8 +3799,8 @@ void janus_ice_relay_rtcp_internal(janus_ice_handle *handle, int video, char *bu
video ? stream->video_ssrc_peer[0] : stream->audio_ssrc_peer);
}
/* Queue this packet */
janus_ice_queued_packet *pkt = (janus_ice_queued_packet *)g_malloc0(sizeof(janus_ice_queued_packet));
pkt->data = g_malloc0(len);
janus_ice_queued_packet *pkt = (janus_ice_queued_packet *)g_malloc(sizeof(janus_ice_queued_packet));
pkt->data = g_malloc(len);
memcpy(pkt->data, rtcp_buf, rtcp_len);
pkt->length = rtcp_len;
pkt->type = video ? JANUS_ICE_PACKET_VIDEO : JANUS_ICE_PACKET_AUDIO;
Expand All @@ -3824,8 +3823,8 @@ void janus_ice_relay_data(janus_ice_handle *handle, char *buf, int len) {
if(!handle || buf == NULL || len < 1)
return;
/* Queue this packet */
janus_ice_queued_packet *pkt = (janus_ice_queued_packet *)g_malloc0(sizeof(janus_ice_queued_packet));
pkt->data = g_malloc0(len);
janus_ice_queued_packet *pkt = (janus_ice_queued_packet *)g_malloc(sizeof(janus_ice_queued_packet));
pkt->data = g_malloc(len);
memcpy(pkt->data, buf, len);
pkt->length = len;
pkt->type = JANUS_ICE_PACKET_DATA;
Expand Down
5 changes: 3 additions & 2 deletions janus.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ janus_session *janus_session_create(guint64 session_id) {
}
}
JANUS_LOG(LOG_INFO, "Creating new session: %"SCNu64"\n", session_id);
janus_session *session = (janus_session *)g_malloc0(sizeof(janus_session));
janus_session *session = (janus_session *)g_malloc(sizeof(janus_session));
if(session == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
Expand All @@ -530,6 +530,7 @@ janus_session *janus_session_create(guint64 session_id) {
g_atomic_int_set(&session->destroy, 0);
g_atomic_int_set(&session->timeout, 0);
session->last_activity = janus_get_monotonic_time();
session->ice_handles = NULL;
janus_mutex_init(&session->mutex);
janus_mutex_lock(&sessions_mutex);
g_hash_table_insert(sessions, janus_uint64_dup(session->session_id), session);
Expand Down Expand Up @@ -599,7 +600,7 @@ void janus_session_free(janus_session *session) {

/* Requests management */
janus_request *janus_request_new(janus_transport *transport, void *instance, void *request_id, gboolean admin, json_t *message) {
janus_request *request = (janus_request *)g_malloc0(sizeof(janus_request));
janus_request *request = (janus_request *)g_malloc(sizeof(janus_request));
request->transport = transport;
request->instance = instance;
request->request_id = request_id;
Expand Down
15 changes: 9 additions & 6 deletions plugins/janus_audiobridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -2575,7 +2575,7 @@ struct janus_plugin_result *janus_audiobridge_handle_message(janus_plugin_sessio
|| !strcasecmp(request_text, "changeroom") || !strcasecmp(request_text, "leave")) {
/* These messages are handled asynchronously */
janus_mutex_unlock(&sessions_mutex);
janus_audiobridge_message *msg = g_malloc0(sizeof(janus_audiobridge_message));
janus_audiobridge_message *msg = g_malloc(sizeof(janus_audiobridge_message));
msg->handle = handle;
msg->transaction = transaction;
msg->message = root;
Expand Down Expand Up @@ -2671,13 +2671,14 @@ void janus_audiobridge_incoming_rtp(janus_plugin_session *handle, int video, cha
}
/* Decode frame (Opus -> slinear) */
janus_rtp_header *rtp = (janus_rtp_header *)buf;
janus_audiobridge_rtp_relay_packet *pkt = g_malloc0(sizeof(janus_audiobridge_rtp_relay_packet));
janus_audiobridge_rtp_relay_packet *pkt = g_malloc(sizeof(janus_audiobridge_rtp_relay_packet));
pkt->data = g_malloc0(BUFFER_SAMPLES*sizeof(opus_int16));
pkt->ssrc = 0;
pkt->timestamp = ntohl(rtp->timestamp);
pkt->seq_number = ntohs(rtp->seq_number);
/* We might check the audio level extension to see if this is silence */
pkt->silence = FALSE;
pkt->length = 0;

if(participant->extmap_id > 0) {
/* Check the audio levels, in case we need to notify participants about who's talking */
Expand Down Expand Up @@ -4105,14 +4106,15 @@ static void *janus_audiobridge_mixer_thread(void *data) {
/* FIXME Smoothen/Normalize instead of truncating? */
outBuffer[i] = sumBuffer[i];
/* Enqueue this mixed frame for encoding in the participant thread */
janus_audiobridge_rtp_relay_packet *mixedpkt = g_malloc0(sizeof(janus_audiobridge_rtp_relay_packet));
janus_audiobridge_rtp_relay_packet *mixedpkt = g_malloc(sizeof(janus_audiobridge_rtp_relay_packet));
if(mixedpkt != NULL) {
mixedpkt->data = g_malloc0(samples*2);
mixedpkt->data = g_malloc(samples*2);
memcpy(mixedpkt->data, outBuffer, samples*2);
mixedpkt->length = samples; /* We set the number of samples here, not the data length */
mixedpkt->timestamp = ts;
mixedpkt->seq_number = seq;
mixedpkt->ssrc = audiobridge->room_id;
mixedpkt->silence = FALSE;
g_async_queue_push(p->outbuf, mixedpkt);
}
if(pkt) {
Expand Down Expand Up @@ -4216,13 +4218,14 @@ static void *janus_audiobridge_participant_thread(void *data) {
janus_audiobridge_session *session = participant->session;

/* Output buffer */
janus_audiobridge_rtp_relay_packet *outpkt = g_malloc0(sizeof(janus_audiobridge_rtp_relay_packet));
janus_audiobridge_rtp_relay_packet *outpkt = g_malloc(sizeof(janus_audiobridge_rtp_relay_packet));
outpkt->data = (janus_rtp_header *)g_malloc0(1500);
outpkt->ssrc = 0;
outpkt->timestamp = 0;
outpkt->seq_number = 0;
outpkt->length = 0;
outpkt->silence = FALSE;
unsigned char *payload = (unsigned char *)outpkt->data;
memset(payload, 0, 1500);

janus_audiobridge_rtp_relay_packet *mixedpkt = NULL;

Expand Down
8 changes: 4 additions & 4 deletions plugins/janus_echotest.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ struct janus_plugin_result *janus_echotest_handle_message(janus_plugin_session *
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
return janus_plugin_result_new(JANUS_PLUGIN_ERROR, g_atomic_int_get(&stopping) ? "Shutting down" : "Plugin not initialized", NULL);

janus_echotest_message *msg = g_malloc0(sizeof(janus_echotest_message));
janus_echotest_message *msg = g_malloc(sizeof(janus_echotest_message));
msg->handle = handle;
msg->transaction = transaction;
msg->message = message;
Expand Down Expand Up @@ -727,15 +727,15 @@ void janus_echotest_incoming_data(janus_plugin_session *handle, char *buf, int l
return;
if(buf == NULL || len <= 0)
return;
char *text = g_malloc0(len+1);
char *text = g_malloc(len+1);
memcpy(text, buf, len);
*(text+len) = '\0';
JANUS_LOG(LOG_VERB, "Got a DataChannel message (%zu bytes) to bounce back: %s\n", strlen(text), text);
/* Save the frame if we're recording */
janus_recorder_save_frame(session->drc, text, strlen(text));
/* We send back the same text with a custom prefix */
const char *prefix = "Janus EchoTest here! You wrote: ";
char *reply = g_malloc0(strlen(prefix)+len+1);
char *reply = g_malloc(strlen(prefix)+len+1);
g_snprintf(reply, strlen(prefix)+len+1, "%s%s", prefix, text);
g_free(text);
gateway->relay_data(handle, reply, strlen(reply));
Expand Down Expand Up @@ -873,7 +873,7 @@ static void *janus_echotest_handler(void *data) {
JANUS_LOG(LOG_VERB, "Joining EchoTest handler thread\n");
janus_echotest_message *msg = NULL;
int error_code = 0;
char *error_cause = g_malloc0(512);
char *error_cause = g_malloc(512);
json_t *root = NULL;
while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
msg = g_async_queue_pop(messages);
Expand Down
2 changes: 1 addition & 1 deletion plugins/janus_nosip.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ json_t *janus_nosip_query_session(janus_plugin_session *handle) {
struct janus_plugin_result *janus_nosip_handle_message(janus_plugin_session *handle, char *transaction, json_t *message, json_t *jsep) {
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
return janus_plugin_result_new(JANUS_PLUGIN_ERROR, g_atomic_int_get(&stopping) ? "Shutting down" : "Plugin not initialized", NULL);
janus_nosip_message *msg = g_malloc0(sizeof(janus_nosip_message));
janus_nosip_message *msg = g_malloc(sizeof(janus_nosip_message));
msg->handle = handle;
msg->transaction = transaction;
msg->message = message;
Expand Down
5 changes: 2 additions & 3 deletions plugins/janus_recordplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ struct janus_plugin_result *janus_recordplay_handle_message(janus_plugin_session
|| !strcasecmp(request_text, "start") || !strcasecmp(request_text, "stop")) {
/* These messages are handled asynchronously */
janus_mutex_unlock(&sessions_mutex);
janus_recordplay_message *msg = g_malloc0(sizeof(janus_recordplay_message));
janus_recordplay_message *msg = g_malloc(sizeof(janus_recordplay_message));
msg->handle = handle;
msg->transaction = transaction;
msg->message = root;
Expand Down Expand Up @@ -2048,7 +2048,7 @@ janus_recordplay_frame_packet *janus_recordplay_get_frames(const char *dir, cons
JANUS_LOG(LOG_HUGE, " -- RTP packet (ssrc=%"SCNu32", pt=%"SCNu16", ext=%"SCNu16", seq=%"SCNu16", ts=%"SCNu32")\n",
ntohl(rtp->ssrc), rtp->type, rtp->extension, ntohs(rtp->seq_number), ntohl(rtp->timestamp));
/* Generate frame packet and insert in the ordered list */
janus_recordplay_frame_packet *p = g_malloc0(sizeof(janus_recordplay_frame_packet));
janus_recordplay_frame_packet *p = g_malloc(sizeof(janus_recordplay_frame_packet));
p->seq = ntohs(rtp->seq_number);
if(reset == 0) {
/* Simple enough... */
Expand Down Expand Up @@ -2214,7 +2214,6 @@ static void *janus_recordplay_playout_thread(void *data) {

janus_recordplay_frame_packet *audio = session->aframes, *video = session->vframes;
char *buffer = (char *)g_malloc0(1500);
memset(buffer, 0, 1500);
int bytes = 0;
int64_t ts_diff = 0, passed = 0;

Expand Down
6 changes: 3 additions & 3 deletions plugins/janus_sip.c
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ json_t *janus_sip_query_session(janus_plugin_session *handle) {
struct janus_plugin_result *janus_sip_handle_message(janus_plugin_session *handle, char *transaction, json_t *message, json_t *jsep) {
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
return janus_plugin_result_new(JANUS_PLUGIN_ERROR, g_atomic_int_get(&stopping) ? "Shutting down" : "Plugin not initialized", NULL);
janus_sip_message *msg = g_malloc0(sizeof(janus_sip_message));
janus_sip_message *msg = g_malloc(sizeof(janus_sip_message));
msg->handle = handle;
msg->transaction = transaction;
msg->message = message;
Expand Down Expand Up @@ -1432,7 +1432,7 @@ static void janus_sip_hangup_media_internal(janus_plugin_session *handle) {
session->vrc_peer = NULL;
janus_mutex_unlock(&session->rec_mutex);
/* FIXME Simulate a "hangup" coming from the browser */
janus_sip_message *msg = g_malloc0(sizeof(janus_sip_message));
janus_sip_message *msg = g_malloc(sizeof(janus_sip_message));
msg->handle = handle;
msg->message = json_pack("{ss}", "request", "hangup");
msg->transaction = NULL;
Expand Down Expand Up @@ -3970,7 +3970,7 @@ static void *janus_sip_relay_thread(void *data) {
JANUS_LOG(LOG_ERR, "[SIP-%s] -- %d (%s)\n", session->account.username, error, strerror(error));
goon = FALSE; /* Can we assume it's pretty much over, after a POLLERR? */
/* FIXME Simulate a "hangup" coming from the browser */
janus_sip_message *msg = g_malloc0(sizeof(janus_sip_message));
janus_sip_message *msg = g_malloc(sizeof(janus_sip_message));
msg->handle = session->handle;
msg->message = json_pack("{ss}", "request", "hangup");
msg->transaction = NULL;
Expand Down
6 changes: 3 additions & 3 deletions plugins/janus_sipre.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ typedef struct janus_sipre_mqueue_payload {
void *data; /* Payload specific data */
} janus_sipre_mqueue_payload;
static janus_sipre_mqueue_payload *janus_sipre_mqueue_payload_create(void *session, const struct sip_msg *msg, int rcode, void *data) {
janus_sipre_mqueue_payload *payload = g_malloc0(sizeof(janus_sipre_mqueue_payload));
janus_sipre_mqueue_payload *payload = g_malloc(sizeof(janus_sipre_mqueue_payload));
payload->session = session;
payload->msg = msg;
payload->rcode = rcode;
Expand Down Expand Up @@ -1201,7 +1201,7 @@ json_t *janus_sipre_query_session(janus_plugin_session *handle) {
struct janus_plugin_result *janus_sipre_handle_message(janus_plugin_session *handle, char *transaction, json_t *message, json_t *jsep) {
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
return janus_plugin_result_new(JANUS_PLUGIN_ERROR, g_atomic_int_get(&stopping) ? "Shutting down" : "Plugin not initialized", NULL);
janus_sipre_message *msg = g_malloc0(sizeof(janus_sipre_message));
janus_sipre_message *msg = g_malloc(sizeof(janus_sipre_message));
msg->handle = handle;
msg->transaction = transaction;
msg->message = message;
Expand Down Expand Up @@ -3137,7 +3137,7 @@ static void *janus_sipre_relay_thread(void *data) {
/* Can we assume it's pretty much over, after a POLLERR? */
goon = FALSE;
/* FIXME Simulate a "hangup" coming from the browser */
janus_sipre_message *msg = g_malloc0(sizeof(janus_sipre_message));
janus_sipre_message *msg = g_malloc(sizeof(janus_sipre_message));
msg->handle = session->handle;
msg->message = json_pack("{ss}", "request", "hangup");
msg->transaction = NULL;
Expand Down
12 changes: 6 additions & 6 deletions plugins/janus_streaming.c
Original file line number Diff line number Diff line change
Expand Up @@ -2460,7 +2460,7 @@ struct janus_plugin_result *janus_streaming_handle_message(janus_plugin_session
|| !strcasecmp(request_text, "configure") || !strcasecmp(request_text, "switch")) {
/* These messages are handled asynchronously */
janus_mutex_unlock(&sessions_mutex);
janus_streaming_message *msg = g_malloc0(sizeof(janus_streaming_message));
janus_streaming_message *msg = g_malloc(sizeof(janus_streaming_message));
msg->handle = handle;
msg->transaction = transaction;
msg->message = root;
Expand Down Expand Up @@ -3801,7 +3801,7 @@ static int janus_streaming_rtsp_connect_to_server(janus_streaming_mountpoint *mp
curl_easy_setopt(curl, CURLOPT_PASSWORD, source->rtsp_password);
}
/* Send an RTSP DESCRIBE */
janus_streaming_buffer *curldata = g_malloc0(sizeof(janus_streaming_buffer));
janus_streaming_buffer *curldata = g_malloc(sizeof(janus_streaming_buffer));
curldata->buffer = g_malloc0(1);
curldata->size = 0;
curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, source->rtsp_url);
Expand Down Expand Up @@ -4671,7 +4671,7 @@ static void *janus_streaming_relay_thread(void *data) {
janus_mutex_lock(&source->keyframe.mutex);
JANUS_LOG(LOG_HUGE, "[%s] ... other part of keyframe received! ts=%"SCNu32"\n", name, source->keyframe.temp_ts);
janus_streaming_rtp_relay_packet *pkt = g_malloc0(sizeof(janus_streaming_rtp_relay_packet));
pkt->data = g_malloc0(bytes);
pkt->data = g_malloc(bytes);
memcpy(pkt->data, buffer, bytes);
pkt->data->ssrc = htons(1);
pkt->data->type = mountpoint->codecs.video_pt;
Expand Down Expand Up @@ -4713,7 +4713,7 @@ static void *janus_streaming_relay_thread(void *data) {
JANUS_LOG(LOG_HUGE, "[%s] New keyframe received! ts=%"SCNu32"\n", name, source->keyframe.temp_ts);
janus_mutex_lock(&source->keyframe.mutex);
janus_streaming_rtp_relay_packet *pkt = g_malloc0(sizeof(janus_streaming_rtp_relay_packet));
pkt->data = g_malloc0(bytes);
pkt->data = g_malloc(bytes);
memcpy(pkt->data, buffer, bytes);
pkt->data->ssrc = htons(1);
pkt->data->type = mountpoint->codecs.video_pt;
Expand Down Expand Up @@ -4780,7 +4780,7 @@ static void *janus_streaming_relay_thread(void *data) {
continue;
}
/* Get a string out of the data */
char *text = g_malloc0(bytes+1);
char *text = g_malloc(bytes+1);
memcpy(text, buffer, bytes);
*(text+bytes) = '\0';
/* Relay on all sessions */
Expand All @@ -4793,7 +4793,7 @@ static void *janus_streaming_relay_thread(void *data) {
if(source->buffermsg) {
janus_mutex_lock(&source->buffermsg_mutex);
janus_streaming_rtp_relay_packet *pkt = g_malloc0(sizeof(janus_streaming_rtp_relay_packet));
pkt->data = g_malloc0(bytes+1);
pkt->data = g_malloc(bytes+1);
memcpy(pkt->data, text, bytes+1);
packet.is_rtp = FALSE;
pkt->length = bytes+1;
Expand Down
Loading

0 comments on commit 9a9a14e

Please sign in to comment.