[ofa-general] [PATCH] opensm/osm_mesh: simplify mesh node links and ports allocation

Sasha Khapyorsky sashak at voltaire.com
Sun Dec 21 19:21:44 PST 2008


Simplify mesh node links and ports allocation - use zero sized arrays
and alloc node and link structures as single memory chunk.

Signed-off-by: Sasha Khapyorsky <sashak at voltaire.com>
---
 opensm/include/opensm/osm_mesh.h |    8 ++++----
 opensm/opensm/osm_mesh.c         |   24 ++++++------------------
 2 files changed, 10 insertions(+), 22 deletions(-)

diff --git a/opensm/include/opensm/osm_mesh.h b/opensm/include/opensm/osm_mesh.h
index 9e23498..173fa86 100644
--- a/opensm/include/opensm/osm_mesh.h
+++ b/opensm/include/opensm/osm_mesh.h
@@ -48,17 +48,15 @@ struct _switch;
 typedef struct _link {
 	int switch_id;
 	int link_id;
-	int *ports;
-	int num_ports;
 	int next_port;
+	int num_ports;
+	int ports[0];
 } link_t;
 
 /*
  * per switch node mesh info
  */
 typedef struct _mesh_node {
-	unsigned int num_links;		/* number of 'links' to adjacent switches */
-	link_t **links;			/* per link information */
 	int *axes;			/* used to hold and reorder assigned axes */
 	int *coord;			/* mesh coordinates of switch */
 	int **matrix;			/* distances between adjacant switches */
@@ -67,6 +65,8 @@ typedef struct _mesh_node {
 	int dimension;			/* apparent dimension of mesh around node */
 	int temp;			/* temporary holder for distance info */
 	int type;			/* index of node type in mesh_info array */
+	unsigned int num_links;		/* number of 'links' to adjacent switches */
+	link_t *links[0];		/* per link information */
 } mesh_node_t;
 
 void osm_mesh_node_delete(struct _lash *p_lash, struct _switch *sw);
diff --git a/opensm/opensm/osm_mesh.c b/opensm/opensm/osm_mesh.c
index 9e3e9de..263d29e 100644
--- a/opensm/opensm/osm_mesh.c
+++ b/opensm/opensm/osm_mesh.c
@@ -1253,16 +1253,9 @@ void osm_mesh_node_delete(lash_t *p_lash, switch_t *sw)
 	OSM_LOG_ENTER(p_log);
 
 	if (node) {
-		if (node->links) {
-			for (i = 0; i < num_ports; i++) {
-				if (node->links[i]) {
-					if (node->links[i]->ports)
-						free(node->links[i]->ports);
-					free(node->links[i]);
-				}
-			}
-			free(node->links);
-		}
+		for (i = 0; i < num_ports; i++)
+			if (node->links[i])
+				free(node->links[i]);
 
 		if (node->poly)
 			free(node->poly);
@@ -1301,17 +1294,12 @@ int osm_mesh_node_create(lash_t *p_lash, switch_t *sw)
 
 	OSM_LOG_ENTER(p_log);
 
-	if (!(node = sw->node = calloc(1, sizeof(mesh_node_t))))
+	if (!(node = sw->node = calloc(1, sizeof(mesh_node_t) + num_ports * sizeof(link_t *))))
 		goto err;
 
-	if (!(node->links = calloc(num_ports, sizeof(link_t *))))
-		goto err;
-
-	for (i = 0; i < num_ports; i++) {
-		if (!(node->links[i] = calloc(1, sizeof(link_t))) ||
-		    !(node->links[i]->ports = calloc(num_ports, sizeof(int))))
+	for (i = 0; i < num_ports; i++)
+		if (!(node->links[i] = calloc(1, sizeof(link_t) + num_ports * sizeof(int))))
 			goto err;
-	}
 
 	if (!(node->axes = calloc(num_ports, sizeof(int))))
 		goto err;
-- 
1.6.0.4.766.g6fc4a




More information about the general mailing list