[ofa-general] [PATCH] opensm: Use calloc rather than malloc/memset

Hal Rosenstock hnrose at comcast.net
Wed Jul 15 12:21:19 PDT 2009


Signed-off-by: Hal Rosenstock <hal.rosenstock at gmail.com>
---
diff --git a/opensm/opensm/osm_mcast_mgr.c b/opensm/opensm/osm_mcast_mgr.c
index ea49588..a393608 100644
--- a/opensm/opensm/osm_mcast_mgr.c
+++ b/opensm/opensm/osm_mcast_mgr.c
@@ -74,11 +74,9 @@ static osm_mcast_work_obj_t *mcast_work_obj_new(IN const osm_port_t * p_port)
 	   qlist.
 	   see cl_qlist_insert_tail(): CL_ASSERT(p_list_item->p_list != p_list)
 	 */
-	p_obj = malloc(sizeof(*p_obj));
-	if (p_obj) {
-		memset(p_obj, 0, sizeof(*p_obj));
+	p_obj = calloc(1, sizeof(*p_obj));
+	if (p_obj)
 		p_obj->p_port = (osm_port_t *) p_port;
-	}
 
 	return (p_obj);
 }
@@ -582,9 +580,9 @@ static osm_mtree_node_t *mcast_mgr_branch(osm_sm_t * sm, osm_mgrp_t * p_mgrp,
 	/*
 	   Prepare an empty list for each port in the switch.
 	   TO DO - this list array could probably be moved
-	   inside the switch element to save on malloc thrashing.
+	   inside the switch element to save on calloc thrashing.
 	 */
-	list_array = malloc(sizeof(cl_qlist_t) * max_children);
+	list_array = calloc(max_children, sizeof(cl_qlist_t));
 	if (list_array == NULL) {
 		OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 0A16: "
 			"Unable to allocate list array\n");
@@ -592,8 +590,6 @@ static osm_mtree_node_t *mcast_mgr_branch(osm_sm_t * sm, osm_mgrp_t * p_mgrp,
 		goto Exit;
 	}
 
-	memset(list_array, 0, sizeof(cl_qlist_t) * max_children);
-
 	for (i = 0; i < max_children; i++)
 		cl_qlist_init(&list_array[i]);
 
diff --git a/opensm/opensm/osm_mtree.c b/opensm/opensm/osm_mtree.c
index 4df7517..22b0adf 100644
--- a/opensm/opensm/osm_mtree.c
+++ b/opensm/opensm/osm_mtree.c
@@ -52,12 +52,11 @@ osm_mtree_node_t *osm_mtree_node_new(IN const osm_switch_t * p_sw)
 	osm_mtree_node_t *p_mtn;
 	uint32_t i;
 
-	p_mtn = malloc(sizeof(osm_mtree_node_t) +
+	p_mtn = calloc(1, sizeof(osm_mtree_node_t) +
 		       sizeof(void *) * (p_sw->num_ports - 1));
 	if (!p_mtn)
 		return NULL;
 
-	memset(p_mtn, 0, sizeof(*p_mtn));
 	p_mtn->p_sw = p_sw;
 	p_mtn->max_children = p_sw->num_ports;
 	for (i = 0; i < p_mtn->max_children; i++)
diff --git a/opensm/opensm/osm_multicast.c b/opensm/opensm/osm_multicast.c
index d2733c4..d254a03 100644
--- a/opensm/opensm/osm_multicast.c
+++ b/opensm/opensm/osm_multicast.c
@@ -79,11 +79,10 @@ osm_mgrp_t *osm_mgrp_new(IN const ib_net16_t mlid)
 {
 	osm_mgrp_t *p_mgrp;
 
-	p_mgrp = (osm_mgrp_t *) malloc(sizeof(*p_mgrp));
+	p_mgrp = (osm_mgrp_t *) calloc(1, sizeof(*p_mgrp));
 	if (!p_mgrp)
 		return NULL;
 
-	memset(p_mgrp, 0, sizeof(*p_mgrp));
 	cl_qmap_init(&p_mgrp->mcm_port_tbl);
 	p_mgrp->mlid = mlid;
 	p_mgrp->last_change_id = 0;
diff --git a/opensm/opensm/osm_node.c b/opensm/opensm/osm_node.c
index ee2fbed..9288b30 100644
--- a/opensm/opensm/osm_node.c
+++ b/opensm/opensm/osm_node.c
@@ -93,11 +93,10 @@ osm_node_t *osm_node_new(IN const osm_madw_t * const p_madw)
 	 */
 	size = p_ni->num_ports;
 
-	p_node = malloc(sizeof(*p_node) + sizeof(osm_physp_t) * size);
+	p_node = calloc(1, sizeof(*p_node) + sizeof(osm_physp_t) * size);
 	if (!p_node)
 		return NULL;
 
-	memset(p_node, 0, sizeof(*p_node) + sizeof(osm_physp_t) * size);
 	p_node->node_info = *p_ni;
 	p_node->physp_tbl_size = size + 1;
 
diff --git a/opensm/opensm/osm_opensm.c b/opensm/opensm/osm_opensm.c
index 50d1349..dfc522a 100644
--- a/opensm/opensm/osm_opensm.c
+++ b/opensm/opensm/osm_opensm.c
@@ -160,13 +160,12 @@ static void setup_routing_engine(osm_opensm_t *osm, const char *name)
 
 	for (m = routing_modules; m->name && *m->name; m++) {
 		if (!strcmp(m->name, name)) {
-			re = malloc(sizeof(struct osm_routing_engine));
+			re = calloc(1, sizeof(struct osm_routing_engine));
 			if (!re) {
 				OSM_LOG(&osm->log, OSM_LOG_VERBOSE,
 					"memory allocation failed\n");
 				return;
 			}
-			memset(re, 0, sizeof(struct osm_routing_engine));
 
 			re->name = m->name;
 			if (m->setup(re, osm)) {
diff --git a/opensm/opensm/osm_pkey.c b/opensm/opensm/osm_pkey.c
index ea918d6..62e97ae 100644
--- a/opensm/opensm/osm_pkey.c
+++ b/opensm/opensm/osm_pkey.c
@@ -144,10 +144,9 @@ ib_api_status_t osm_pkey_tbl_set(IN osm_pkey_tbl_t * p_pkey_tbl,
 
 	if (!p_pkey_block) {
 		p_pkey_block =
-		    (ib_pkey_table_t *) malloc(sizeof(ib_pkey_table_t));
+		    (ib_pkey_table_t *) calloc(1, sizeof(ib_pkey_table_t));
 		if (!p_pkey_block)
 			return (IB_ERROR);
-		memset(p_pkey_block, 0, sizeof(ib_pkey_table_t));
 		cl_ptr_vector_set(&p_pkey_tbl->blocks, block, p_pkey_block);
 	}
 
@@ -208,10 +207,9 @@ ib_api_status_t osm_pkey_tbl_set_new_entry(IN osm_pkey_tbl_t * p_pkey_tbl,
 	ib_pkey_table_t *p_block;
 
 	if (!(p_block = osm_pkey_tbl_new_block_get(p_pkey_tbl, block_idx))) {
-		p_block = (ib_pkey_table_t *) malloc(sizeof(ib_pkey_table_t));
+		p_block = (ib_pkey_table_t *) calloc(1, sizeof(ib_pkey_table_t));
 		if (!p_block)
 			return (IB_ERROR);
-		memset(p_block, 0, sizeof(ib_pkey_table_t));
 		cl_ptr_vector_set(&p_pkey_tbl->new_blocks, block_idx, p_block);
 	}
 
diff --git a/opensm/opensm/osm_port.c b/opensm/opensm/osm_port.c
index 751c0f0..48b95fe 100644
--- a/opensm/opensm/osm_port.c
+++ b/opensm/opensm/osm_port.c
@@ -117,10 +117,9 @@ void osm_physp_init(IN osm_physp_t * p_physp, IN const ib_net64_t port_guid,
 
 	cl_ptr_vector_init(&p_physp->slvl_by_port, num_slvl, 1);
 	for (i = 0; i < num_slvl; i++) {
-		p_slvl = (ib_slvl_table_t *) malloc(sizeof(ib_slvl_table_t));
+		p_slvl = (ib_slvl_table_t *) calloc(1, sizeof(ib_slvl_table_t));
 		if (!p_slvl)
 			break;
-		memset(p_slvl, 0, sizeof(ib_slvl_table_t));
 		cl_ptr_vector_set(&p_physp->slvl_by_port, i, p_slvl);
 	}
 
@@ -148,11 +147,10 @@ osm_port_t *osm_port_new(IN const ib_node_info_t * p_ni,
 	osm_physp_t *p_physp;
 	uint8_t port_num;
 
-	p_port = malloc(sizeof(*p_port));
+	p_port = calloc(1, sizeof(*p_port));
 	if (!p_port)
 		return NULL;
 
-	memset(p_port, 0, sizeof(*p_port));
 	cl_qlist_init(&p_port->mcm_list);
 	p_port->p_node = (struct osm_node *)p_parent_node;
 	port_guid = p_ni->port_guid;
diff --git a/opensm/opensm/osm_prtn.c b/opensm/opensm/osm_prtn.c
index 3f4cf00..b9df7d3 100644
--- a/opensm/opensm/osm_prtn.c
+++ b/opensm/opensm/osm_prtn.c
@@ -62,11 +62,10 @@ static uint16_t global_pkey_counter;
 
 osm_prtn_t *osm_prtn_new(IN const char *name, IN const uint16_t pkey)
 {
-	osm_prtn_t *p = malloc(sizeof(*p));
+	osm_prtn_t *p = calloc(1, sizeof(*p));
 	if (!p)
 		return NULL;
 
-	memset(p, 0, sizeof(*p));
 	p->pkey = pkey;
 	p->sl = OSM_DEFAULT_SL;
 	cl_map_construct(&p->full_guid_tbl);
diff --git a/opensm/opensm/osm_sa_guidinfo_record.c b/opensm/opensm/osm_sa_guidinfo_record.c
index 362c2ac..de96bb3 100644
--- a/opensm/opensm/osm_sa_guidinfo_record.c
+++ b/opensm/opensm/osm_sa_guidinfo_record.c
@@ -85,7 +85,7 @@ static ib_api_status_t gir_rcv_new_gir(IN osm_sa_t * sa,
 
 	OSM_LOG_ENTER(sa->p_log);
 
-	p_rec_item = malloc(sizeof(*p_rec_item));
+	p_rec_item = calloc(1, sizeof(*p_rec_item));
 	if (p_rec_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5102: "
 			"rec_item alloc failed\n");
@@ -97,8 +97,6 @@ static ib_api_status_t gir_rcv_new_gir(IN osm_sa_t * sa,
 		"New GUIDInfoRecord: lid %u, block num %d\n",
 		cl_ntoh16(match_lid), block_num);
 
-	memset(p_rec_item, 0, sizeof(*p_rec_item));
-
 	p_rec_item->rec.lid = match_lid;
 	p_rec_item->rec.block_num = block_num;
 	if (!block_num)
diff --git a/opensm/opensm/osm_sa_lft_record.c b/opensm/opensm/osm_sa_lft_record.c
index d092129..c57b839 100644
--- a/opensm/opensm/osm_sa_lft_record.c
+++ b/opensm/opensm/osm_sa_lft_record.c
@@ -79,7 +79,7 @@ static ib_api_status_t lftr_rcv_new_lftr(IN osm_sa_t * sa,
 
 	OSM_LOG_ENTER(sa->p_log);
 
-	p_rec_item = malloc(sizeof(*p_rec_item));
+	p_rec_item = calloc(1, sizeof(*p_rec_item));
 	if (p_rec_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4402: "
 			"rec_item alloc failed\n");
@@ -93,8 +93,6 @@ static ib_api_status_t lftr_rcv_new_lftr(IN osm_sa_t * sa,
 		cl_ntoh64(osm_node_get_node_guid(p_sw->p_node)),
 		block, cl_ntoh16(lid));
 
-	memset(p_rec_item, 0, sizeof(*p_rec_item));
-
 	p_rec_item->rec.lid = lid;
 	p_rec_item->rec.block_num = cl_hton16(block);
 
diff --git a/opensm/opensm/osm_sa_link_record.c b/opensm/opensm/osm_sa_link_record.c
index bf0b5ee..cd63b45 100644
--- a/opensm/opensm/osm_sa_link_record.c
+++ b/opensm/opensm/osm_sa_link_record.c
@@ -68,7 +68,7 @@ static void lr_rcv_build_physp_link(IN osm_sa_t * sa, IN ib_net16_t from_lid,
 {
 	osm_lr_item_t *p_lr_item;
 
-	p_lr_item = malloc(sizeof(*p_lr_item));
+	p_lr_item = calloc(1, sizeof(*p_lr_item));
 	if (p_lr_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1801: "
 			"Unable to acquire link record\n"
@@ -78,7 +78,6 @@ static void lr_rcv_build_physp_link(IN osm_sa_t * sa, IN ib_net16_t from_lid,
 			cl_ntoh16(from_lid), cl_ntoh16(to_lid));
 		return;
 	}
-	memset(p_lr_item, 0, sizeof(*p_lr_item));
 
 	p_lr_item->link_rec.from_port_num = from_port;
 	p_lr_item->link_rec.to_port_num = to_port;
diff --git a/opensm/opensm/osm_sa_mcmember_record.c b/opensm/opensm/osm_sa_mcmember_record.c
index 5543221..7a44f79 100644
--- a/opensm/opensm/osm_sa_mcmember_record.c
+++ b/opensm/opensm/osm_sa_mcmember_record.c
@@ -1352,7 +1352,7 @@ static ib_api_status_t mcmr_rcv_new_mcmr(IN osm_sa_t * sa,
 
 	OSM_LOG_ENTER(sa->p_log);
 
-	p_rec_item = malloc(sizeof(*p_rec_item));
+	p_rec_item = calloc(1, sizeof(*p_rec_item));
 	if (p_rec_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1B15: "
 			"rec_item alloc failed\n");
@@ -1360,8 +1360,6 @@ static ib_api_status_t mcmr_rcv_new_mcmr(IN osm_sa_t * sa,
 		goto Exit;
 	}
 
-	memset(p_rec_item, 0, sizeof(*p_rec_item));
-
 	/* HACK: Untrusted requesters should result with 0 Join
 	   State, Port Guid, and Proxy */
 	p_rec_item->rec = *p_rcvd_rec;
diff --git a/opensm/opensm/osm_sa_mft_record.c b/opensm/opensm/osm_sa_mft_record.c
index 841eb86..6e166fa 100644
--- a/opensm/opensm/osm_sa_mft_record.c
+++ b/opensm/opensm/osm_sa_mft_record.c
@@ -81,7 +81,7 @@ static ib_api_status_t mftr_rcv_new_mftr(IN osm_sa_t * sa,
 
 	OSM_LOG_ENTER(sa->p_log);
 
-	p_rec_item = malloc(sizeof(*p_rec_item));
+	p_rec_item = calloc(1, sizeof(*p_rec_item));
 	if (p_rec_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A02: "
 			"rec_item alloc failed\n");
@@ -98,8 +98,6 @@ static ib_api_status_t mftr_rcv_new_mftr(IN osm_sa_t * sa,
 	position_block_num = ((uint16_t) position << 12) |
 	    (block & IB_MCAST_BLOCK_ID_MASK_HO);
 
-	memset(p_rec_item, 0, sizeof(*p_rec_item));
-
 	p_rec_item->rec.lid = lid;
 	p_rec_item->rec.position_block_num = cl_hton16(position_block_num);
 
diff --git a/opensm/opensm/osm_sa_multipath_record.c b/opensm/opensm/osm_sa_multipath_record.c
index 6ea5ba0..a5ec071 100644
--- a/opensm/opensm/osm_sa_multipath_record.c
+++ b/opensm/opensm/osm_sa_multipath_record.c
@@ -812,13 +812,12 @@ static osm_mpr_item_t *mpr_rcv_get_lid_pair_path(IN osm_sa_t * sa,
 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG, "Src LID %u, Dest LID %u\n",
 		src_lid_ho, dest_lid_ho);
 
-	p_pr_item = malloc(sizeof(*p_pr_item));
+	p_pr_item = calloc(1, sizeof(*p_pr_item));
 	if (p_pr_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4501: "
 			"Unable to allocate path record\n");
 		goto Exit;
 	}
-	memset(p_pr_item, 0, sizeof(*p_pr_item));
 
 	status = mpr_rcv_get_path_parms(sa, p_mpr, p_src_port, p_dest_port,
 					dest_lid_ho, comp_mask, &path_parms);
diff --git a/opensm/opensm/osm_sa_node_record.c b/opensm/opensm/osm_sa_node_record.c
index 15fb763..18a3ae1 100644
--- a/opensm/opensm/osm_sa_node_record.c
+++ b/opensm/opensm/osm_sa_node_record.c
@@ -79,7 +79,7 @@ static ib_api_status_t nr_rcv_new_nr(osm_sa_t * sa,
 
 	OSM_LOG_ENTER(sa->p_log);
 
-	p_rec_item = malloc(sizeof(*p_rec_item));
+	p_rec_item = calloc(1, sizeof(*p_rec_item));
 	if (p_rec_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1D02: "
 			"rec_item alloc failed\n");
@@ -93,8 +93,6 @@ static ib_api_status_t nr_rcv_new_nr(osm_sa_t * sa,
 		cl_ntoh64(osm_node_get_node_guid(p_node)),
 		cl_ntoh64(port_guid), cl_ntoh16(lid));
 
-	memset(p_rec_item, 0, sizeof(*p_rec_item));
-
 	p_rec_item->rec.lid = lid;
 
 	p_rec_item->rec.node_info = p_node->node_info;
diff --git a/opensm/opensm/osm_sa_path_record.c b/opensm/opensm/osm_sa_path_record.c
index 75d9516..43acf9f 100644
--- a/opensm/opensm/osm_sa_path_record.c
+++ b/opensm/opensm/osm_sa_path_record.c
@@ -839,13 +839,12 @@ static osm_pr_item_t *pr_rcv_get_lid_pair_path(IN osm_sa_t * sa,
 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG, "Src LID %u, Dest LID %u\n",
 		src_lid_ho, dest_lid_ho);
 
-	p_pr_item = malloc(sizeof(*p_pr_item));
+	p_pr_item = calloc(1, sizeof(*p_pr_item));
 	if (p_pr_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1F01: "
 			"Unable to allocate path record\n");
 		goto Exit;
 	}
-	memset(p_pr_item, 0, sizeof(*p_pr_item));
 
 	status = pr_rcv_get_path_parms(sa, p_pr, p_src_port, p_dest_port,
 				       dest_lid_ho, comp_mask, &path_parms);
@@ -1711,13 +1710,12 @@ McastDest:
 			goto Unlock;
 		}
 
-		p_pr_item = malloc(sizeof(*p_pr_item));
+		p_pr_item = calloc(1, sizeof(*p_pr_item));
 		if (p_pr_item == NULL) {
 			OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1F18: "
 				"Unable to allocate path record for MC group\n");
 			goto Unlock;
 		}
-		memset(p_pr_item, 0, sizeof(*p_pr_item));
 
 		/* Copy PathRecord request into response */
 		p_sa_mad = osm_madw_get_sa_mad_ptr(p_madw);
diff --git a/opensm/opensm/osm_sa_pkey_record.c b/opensm/opensm/osm_sa_pkey_record.c
index 8e47745..bb7a9cf 100644
--- a/opensm/opensm/osm_sa_pkey_record.c
+++ b/opensm/opensm/osm_sa_pkey_record.c
@@ -76,7 +76,7 @@ static void sa_pkey_create(IN osm_sa_t * sa, IN osm_physp_t * p_physp,
 
 	OSM_LOG_ENTER(sa->p_log);
 
-	p_rec_item = malloc(sizeof(*p_rec_item));
+	p_rec_item = calloc(1, sizeof(*p_rec_item));
 	if (p_rec_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4602: "
 			"rec_item alloc failed\n");
@@ -95,8 +95,6 @@ static void sa_pkey_create(IN osm_sa_t * sa, IN osm_physp_t * p_physp,
 		cl_ntoh64(osm_physp_get_port_guid(p_physp)),
 		cl_ntoh16(lid), osm_physp_get_port_num(p_physp), block);
 
-	memset(p_rec_item, 0, sizeof(*p_rec_item));
-
 	p_rec_item->rec.lid = lid;
 	p_rec_item->rec.block_num = block;
 	p_rec_item->rec.port_num = osm_physp_get_port_num(p_physp);
diff --git a/opensm/opensm/osm_sa_portinfo_record.c b/opensm/opensm/osm_sa_portinfo_record.c
index b5ef101..2add401 100644
--- a/opensm/opensm/osm_sa_portinfo_record.c
+++ b/opensm/opensm/osm_sa_portinfo_record.c
@@ -84,7 +84,7 @@ static ib_api_status_t pir_rcv_new_pir(IN osm_sa_t * sa,
 
 	OSM_LOG_ENTER(sa->p_log);
 
-	p_rec_item = malloc(sizeof(*p_rec_item));
+	p_rec_item = calloc(1, sizeof(*p_rec_item));
 	if (p_rec_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2102: "
 			"rec_item alloc failed\n");
@@ -98,8 +98,6 @@ static ib_api_status_t pir_rcv_new_pir(IN osm_sa_t * sa,
 		cl_ntoh64(osm_physp_get_port_guid(p_physp)),
 		cl_ntoh16(lid), osm_physp_get_port_num(p_physp));
 
-	memset(p_rec_item, 0, sizeof(*p_rec_item));
-
 	p_rec_item->rec.lid = lid;
 	p_rec_item->rec.port_info = p_physp->port_info;
 	p_rec_item->rec.port_num = osm_physp_get_port_num(p_physp);
diff --git a/opensm/opensm/osm_sa_slvl_record.c b/opensm/opensm/osm_sa_slvl_record.c
index 061d970..64d3da7 100644
--- a/opensm/opensm/osm_sa_slvl_record.c
+++ b/opensm/opensm/osm_sa_slvl_record.c
@@ -83,7 +83,7 @@ static void sa_slvl_create(IN osm_sa_t * sa, IN const osm_physp_t * p_physp,
 
 	OSM_LOG_ENTER(sa->p_log);
 
-	p_rec_item = malloc(sizeof(*p_rec_item));
+	p_rec_item = calloc(1, sizeof(*p_rec_item));
 	if (p_rec_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2602: "
 			"rec_item alloc failed\n");
@@ -102,8 +102,6 @@ static void sa_slvl_create(IN osm_sa_t * sa, IN const osm_physp_t * p_physp,
 		cl_ntoh64(osm_physp_get_port_guid(p_physp)),
 		cl_ntoh16(lid), osm_physp_get_port_num(p_physp), in_port_idx);
 
-	memset(p_rec_item, 0, sizeof(*p_rec_item));
-
 	p_rec_item->rec.lid = lid;
 	p_rec_item->rec.out_port_num = osm_physp_get_port_num(p_physp);
 	p_rec_item->rec.in_port_num = in_port_idx;
diff --git a/opensm/opensm/osm_sa_sw_info_record.c b/opensm/opensm/osm_sa_sw_info_record.c
index 2ea8baf..04f0d07 100644
--- a/opensm/opensm/osm_sa_sw_info_record.c
+++ b/opensm/opensm/osm_sa_sw_info_record.c
@@ -79,7 +79,7 @@ static ib_api_status_t sir_rcv_new_sir(IN osm_sa_t * sa,
 
 	OSM_LOG_ENTER(sa->p_log);
 
-	p_rec_item = malloc(sizeof(*p_rec_item));
+	p_rec_item = calloc(1, sizeof(*p_rec_item));
 	if (p_rec_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 5308: "
 			"rec_item alloc failed\n");
@@ -90,8 +90,6 @@ static ib_api_status_t sir_rcv_new_sir(IN osm_sa_t * sa,
 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
 		"New SwitchInfoRecord: lid %u\n", cl_ntoh16(lid));
 
-	memset(p_rec_item, 0, sizeof(*p_rec_item));
-
 	p_rec_item->rec.lid = lid;
 	p_rec_item->rec.switch_info = p_sw->switch_info;
 
diff --git a/opensm/opensm/osm_sa_vlarb_record.c b/opensm/opensm/osm_sa_vlarb_record.c
index f9f11b7..93850c7 100644
--- a/opensm/opensm/osm_sa_vlarb_record.c
+++ b/opensm/opensm/osm_sa_vlarb_record.c
@@ -83,7 +83,7 @@ static void sa_vl_arb_create(IN osm_sa_t * sa, IN osm_physp_t * p_physp,
 
 	OSM_LOG_ENTER(sa->p_log);
 
-	p_rec_item = malloc(sizeof(*p_rec_item));
+	p_rec_item = calloc(1, sizeof(*p_rec_item));
 	if (p_rec_item == NULL) {
 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2A02: "
 			"rec_item alloc failed\n");
@@ -102,8 +102,6 @@ static void sa_vl_arb_create(IN osm_sa_t * sa, IN osm_physp_t * p_physp,
 		cl_ntoh64(osm_physp_get_port_guid(p_physp)),
 		cl_ntoh16(lid), osm_physp_get_port_num(p_physp), block);
 
-	memset(p_rec_item, 0, sizeof(*p_rec_item));
-
 	p_rec_item->rec.lid = lid;
 	p_rec_item->rec.port_num = osm_physp_get_port_num(p_physp);
 	p_rec_item->rec.block_num = block;
diff --git a/opensm/opensm/osm_service.c b/opensm/opensm/osm_service.c
index cc834b2..0cb550b 100644
--- a/opensm/opensm/osm_service.c
+++ b/opensm/opensm/osm_service.c
@@ -79,11 +79,9 @@ osm_svcr_t *osm_svcr_new(IN const ib_service_record_t * p_svc_rec)
 
 	CL_ASSERT(p_svc_rec);
 
-	p_svcr = (osm_svcr_t *) malloc(sizeof(*p_svcr));
-	if (p_svcr) {
-		memset(p_svcr, 0, sizeof(*p_svcr));
+	p_svcr = (osm_svcr_t *) calloc(1, sizeof(*p_svcr));
+	if (p_svcr)
 		osm_svcr_init(p_svcr, p_svc_rec);
-	}
 
 	return (p_svcr);
 }
diff --git a/opensm/opensm/osm_sm.c b/opensm/opensm/osm_sm.c
index daa60ff..6b6d456 100644
--- a/opensm/opensm/osm_sm.c
+++ b/opensm/opensm/osm_sm.c
@@ -450,10 +450,9 @@ static ib_api_status_t sm_mgrp_process(IN osm_sm_t * p_sm,
 	 * 'Schedule' all the QP0 traffic for when the state manager
 	 * isn't busy trying to do something else.
 	 */
-	ctx = malloc(sizeof(*ctx));
+	ctx = calloc(1, sizeof(*ctx));
 	if (!ctx)
 		return IB_ERROR;
-	memset(ctx, 0, sizeof(*ctx));
 	ctx->mlid = p_mgrp->mlid;
 
 	cl_spinlock_acquire(&p_sm->mgrp_lock);
diff --git a/opensm/opensm/osm_switch.c b/opensm/opensm/osm_switch.c
index ce1ca63..6a06b05 100644
--- a/opensm/opensm/osm_switch.c
+++ b/opensm/opensm/osm_switch.c
@@ -121,12 +121,10 @@ osm_switch_t *osm_switch_new(IN osm_node_t * const p_node,
 	if (!p_si->lin_cap) /* The switch doesn't support LFT */
 		return NULL;
 
-	p_sw = malloc(sizeof(*p_sw));
+	p_sw = calloc(1, sizeof(*p_sw));
 	if (!p_sw)
 		return NULL;
 
-	memset(p_sw, 0, sizeof(*p_sw));
-
 	p_sw->p_node = p_node;
 	p_sw->switch_info = *p_si;
 	p_sw->num_ports = num_ports;
@@ -138,12 +136,10 @@ osm_switch_t *osm_switch_new(IN osm_node_t * const p_node,
 
 	memset(p_sw->lft, OSM_NO_PATH, IB_LID_UCAST_END_HO + 1);
 
-	p_sw->p_prof = malloc(sizeof(*p_sw->p_prof) * num_ports);
+	p_sw->p_prof = calloc(num_ports, sizeof(*p_sw->p_prof));
 	if (!p_sw->p_prof)
 		goto err;
 
-	memset(p_sw->p_prof, 0, sizeof(*p_sw->p_prof) * num_ports);
-
 	if (osm_mcast_tbl_init(&p_sw->mcast_tbl, osm_node_get_num_physp(p_node),
 			       cl_ntoh16(p_si->mcast_cap)))
 		goto err;
@@ -523,10 +519,9 @@ osm_switch_prepare_path_rebuild(IN osm_switch_t * p_sw, IN uint16_t max_lids)
 	memset(p_sw->new_lft, OSM_NO_PATH, IB_LID_UCAST_END_HO + 1);
 
 	if (!p_sw->hops) {
-		hops = malloc((max_lids + 1) * sizeof(hops[0]));
+		hops = calloc(max_lids + 1, sizeof(hops[0]));
 		if (!hops)
 			return -1;
-		memset(hops, 0, (max_lids + 1) * sizeof(hops[0]));
 		p_sw->hops = hops;
 		p_sw->num_hops = max_lids + 1;
 	} else if (max_lids + 1 > p_sw->num_hops) {
diff --git a/opensm/opensm/osm_ucast_cache.c b/opensm/opensm/osm_ucast_cache.c
index 216b496..33ca177 100644
--- a/opensm/opensm/osm_ucast_cache.c
+++ b/opensm/opensm/osm_ucast_cache.c
@@ -106,14 +106,11 @@ static void cache_sw_set_leaf(cache_switch_t * p_sw)
 
 static cache_switch_t *cache_sw_new(uint16_t lid_ho, unsigned num_ports)
 {
-	cache_switch_t *p_cache_sw = malloc(sizeof(cache_switch_t) +
+	cache_switch_t *p_cache_sw = calloc(1, sizeof(cache_switch_t) +
 					    num_ports * sizeof(cache_port_t));
 	if (!p_cache_sw)
 		return NULL;
 
-	memset(p_cache_sw, 0,
-	       sizeof(*p_cache_sw) + num_ports * sizeof(cache_port_t));
-
 	p_cache_sw->num_ports = num_ports;
 
 	/* port[0] fields represent this switch details - lid and type */
diff --git a/opensm/opensm/osm_ucast_ftree.c b/opensm/opensm/osm_ucast_ftree.c
index 26cdcab..a2aa171 100644
--- a/opensm/opensm/osm_ucast_ftree.c
+++ b/opensm/opensm/osm_ucast_ftree.c
@@ -323,10 +323,9 @@ static inline void tuple_from_key(IN ftree_tuple_t tuple,
 static ftree_sw_tbl_element_t *sw_tbl_element_create(IN ftree_sw_t * p_sw)
 {
 	ftree_sw_tbl_element_t *p_element =
-	    (ftree_sw_tbl_element_t *) malloc(sizeof(ftree_sw_tbl_element_t));
+	    (ftree_sw_tbl_element_t *) calloc(1, sizeof(ftree_sw_tbl_element_t));
 	if (!p_element)
 		return NULL;
-	memset(p_element, 0, sizeof(ftree_sw_tbl_element_t));
 
 	p_element->p_sw = p_sw;
 	return p_element;
@@ -350,10 +349,9 @@ static void sw_tbl_element_destroy(IN ftree_sw_tbl_element_t * p_element)
 static ftree_port_t *port_create(IN uint8_t port_num,
 				 IN uint8_t remote_port_num)
 {
-	ftree_port_t *p_port = (ftree_port_t *) malloc(sizeof(ftree_port_t));
+	ftree_port_t *p_port = (ftree_port_t *) calloc(1, sizeof(ftree_port_t));
 	if (!p_port)
 		return NULL;
-	memset(p_port, 0, sizeof(ftree_port_t));
 
 	p_port->port_num = port_num;
 	p_port->remote_port_num = remote_port_num;
@@ -389,10 +387,9 @@ static ftree_port_group_t *port_group_create(IN uint16_t base_lid,
 					     IN boolean_t is_io)
 {
 	ftree_port_group_t *p_group =
-	    (ftree_port_group_t *) malloc(sizeof(ftree_port_group_t));
+	    (ftree_port_group_t *) calloc(1, sizeof(ftree_port_group_t));
 	if (p_group == NULL)
 		return NULL;
-	memset(p_group, 0, sizeof(ftree_port_group_t));
 
 	p_group->base_lid = base_lid;
 	p_group->remote_base_lid = remote_base_lid;
@@ -533,10 +530,9 @@ static ftree_sw_t *sw_create(IN ftree_fabric_t * p_ftree,
 	if (p_osm_sw->num_ports == 1)
 		return NULL;
 
-	p_sw = (ftree_sw_t *) malloc(sizeof(ftree_sw_t));
+	p_sw = (ftree_sw_t *) calloc(1, sizeof(ftree_sw_t));
 	if (p_sw == NULL)
 		return NULL;
-	memset(p_sw, 0, sizeof(ftree_sw_t));
 
 	p_sw->p_osm_sw = p_osm_sw;
 	p_sw->rank = 0xFFFFFFFF;
@@ -757,10 +753,9 @@ sw_get_least_hops(IN ftree_sw_t * p_sw, IN uint16_t target_lid)
 
 static ftree_hca_t *hca_create(IN osm_node_t * p_osm_node)
 {
-	ftree_hca_t *p_hca = (ftree_hca_t *) malloc(sizeof(ftree_hca_t));
+	ftree_hca_t *p_hca = (ftree_hca_t *) calloc(1, sizeof(ftree_hca_t));
 	if (p_hca == NULL)
 		return NULL;
-	memset(p_hca, 0, sizeof(ftree_hca_t));
 
 	p_hca->p_osm_node = p_osm_node;
 	p_hca->up_port_groups = (ftree_port_group_t **)
@@ -884,12 +879,10 @@ static void hca_add_port(IN ftree_hca_t * p_hca, IN uint8_t port_num,
 static ftree_fabric_t *fabric_create()
 {
 	ftree_fabric_t *p_ftree =
-	    (ftree_fabric_t *) malloc(sizeof(ftree_fabric_t));
+	    (ftree_fabric_t *) calloc(1, sizeof(ftree_fabric_t));
 	if (p_ftree == NULL)
 		return NULL;
 
-	memset(p_ftree, 0, sizeof(ftree_fabric_t));
-
 	cl_qmap_init(&p_ftree->hca_tbl);
 	cl_qmap_init(&p_ftree->sw_tbl);
 	cl_qmap_init(&p_ftree->sw_by_tuple_tbl);
@@ -1579,15 +1572,13 @@ static int fabric_create_leaf_switch_array(IN ftree_fabric_t * p_ftree)
 
 	/* create array of ALL the switches that have leaf rank */
 	all_switches_at_leaf_level = (ftree_sw_t **)
-	    malloc(cl_qmap_count(&p_ftree->sw_tbl) * sizeof(ftree_sw_t *));
+	    calloc(cl_qmap_count(&p_ftree->sw_tbl), sizeof(ftree_sw_t *));
 	if (!all_switches_at_leaf_level) {
 		osm_log(&p_ftree->p_osm->log, OSM_LOG_SYS,
 			"Fat-tree routing: Memory allocation failed\n");
 		res = -1;
 		goto Exit;
 	}
-	memset(all_switches_at_leaf_level, 0,
-	       cl_qmap_count(&p_ftree->sw_tbl) * sizeof(ftree_sw_t *));
 
 	p_next_sw = (ftree_sw_t *) cl_qmap_head(&p_ftree->sw_tbl);
 	while (p_next_sw != (ftree_sw_t *) cl_qmap_end(&p_ftree->sw_tbl)) {
@@ -1698,13 +1689,12 @@ static boolean_t fabric_validate_topology(IN ftree_fabric_t * p_ftree)
 		"Validating fabric topology\n");
 
 	reference_sw_arr =
-	    (ftree_sw_t **) malloc(tree_rank * sizeof(ftree_sw_t *));
+	    (ftree_sw_t **) calloc(tree_rank, sizeof(ftree_sw_t *));
 	if (reference_sw_arr == NULL) {
 		osm_log(&p_ftree->p_osm->log, OSM_LOG_SYS,
 			"Fat-tree routing: Memory allocation failed\n");
 		return FALSE;
 	}
-	memset(reference_sw_arr, 0, tree_rank * sizeof(ftree_sw_t *));
 
 	p_next_sw = (ftree_sw_t *) cl_qmap_head(&p_ftree->sw_tbl);
 	while (res && p_next_sw != (ftree_sw_t *) cl_qmap_end(&p_ftree->sw_tbl)) {
diff --git a/opensm/opensm/osm_ucast_mgr.c b/opensm/opensm/osm_ucast_mgr.c
index 78a7031..d8b794c 100644
--- a/opensm/opensm/osm_ucast_mgr.c
+++ b/opensm/opensm/osm_ucast_mgr.c
@@ -444,7 +444,7 @@ static void alloc_ports_priv(osm_ucast_mgr_t * mgr)
 		lmc = ib_port_info_get_lmc(&port->p_physp->port_info);
 		if (!lmc)
 			continue;
-		r = malloc(sizeof(*r) + sizeof(r->guids[0]) * (1 << lmc));
+		r = calloc(1, sizeof(*r) + sizeof(r->guids[0]) * (1 << lmc));
 		if (!r) {
 			OSM_LOG(mgr->p_log, OSM_LOG_ERROR, "ERR 3A09: "
 				"cannot allocate memory to track remote"
@@ -452,7 +452,6 @@ static void alloc_ports_priv(osm_ucast_mgr_t * mgr)
 			port->priv = NULL;
 			continue;
 		}
-		memset(r, 0, sizeof(*r) + sizeof(r->guids[0]) * (1 << lmc));
 		port->priv = r;
 	}
 }
diff --git a/opensm/opensm/osm_ucast_updn.c b/opensm/opensm/osm_ucast_updn.c
index bb9ccda..e4985e6 100644
--- a/opensm/opensm/osm_ucast_updn.c
+++ b/opensm/opensm/osm_ucast_updn.c
@@ -394,10 +394,9 @@ static struct updn_node *create_updn_node(osm_switch_t * sw)
 {
 	struct updn_node *u;
 
-	u = malloc(sizeof(*u));
+	u = calloc(1, sizeof(*u));
 	if (!u)
 		return NULL;
-	memset(u, 0, sizeof(*u));
 	u->sw = sw;
 	u->id = cl_ntoh64(osm_node_get_node_guid(sw->p_node));
 	u->rank = 0xffffffff;
@@ -431,13 +430,12 @@ static void updn_find_root_nodes_by_min_hop(OUT updn_t * p_updn)
 		"Current number of ports in the subnet is %d\n",
 		cl_qmap_count(&p_osm->subn.port_guid_tbl));
 
-	cas_per_sw = malloc((IB_LID_UCAST_END_HO + 1) * sizeof(*cas_per_sw));
+	cas_per_sw = calloc(IB_LID_UCAST_END_HO + 1, sizeof(*cas_per_sw));
 	if (!cas_per_sw) {
 		OSM_LOG(&p_osm->log, OSM_LOG_ERROR, "ERR AA14: "
 			"cannot alloc mem for CAs per switch counter array\n");
 		goto _exit;
 	}
-	memset(cas_per_sw, 0, (IB_LID_UCAST_END_HO + 1) * sizeof(*cas_per_sw));
 
 	/* Find the Maximum number of CAs (and routers) for histogram normalization */
 	OSM_LOG(&p_osm->log, OSM_LOG_VERBOSE,
@@ -671,10 +669,9 @@ int osm_ucast_updn_setup(struct osm_routing_engine *r, osm_opensm_t *osm)
 {
 	updn_t *updn;
 
-	updn = malloc(sizeof(updn_t));
+	updn = calloc(1, sizeof(updn_t));
 	if (!updn)
 		return -1;
-	memset(updn, 0, sizeof(updn_t));
 
 	updn->p_osm = osm;
 



More information about the general mailing list