1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
3219820Sjeff * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4219820Sjeff * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff *
34219820Sjeff */
35219820Sjeff
36219820Sjeff/*
37219820Sjeff * Abstract:
38219820Sjeff *    Implementation of osm_mtree_node_t.
39219820Sjeff * This file implements the Multicast Tree object.
40219820Sjeff */
41219820Sjeff
42219820Sjeff#if HAVE_CONFIG_H
43219820Sjeff#  include <config.h>
44219820Sjeff#endif				/* HAVE_CONFIG_H */
45219820Sjeff
46219820Sjeff#include <stdlib.h>
47219820Sjeff#include <complib/cl_debug.h>
48219820Sjeff#include <opensm/osm_mtree.h>
49219820Sjeff
50219820Sjeff/**********************************************************************
51219820Sjeff **********************************************************************/
52219820Sjeffstatic void
53219820Sjeffosm_mtree_node_init(IN osm_mtree_node_t * const p_mtn,
54219820Sjeff		    IN const osm_switch_t * const p_sw)
55219820Sjeff{
56219820Sjeff	uint32_t i;
57219820Sjeff
58219820Sjeff	CL_ASSERT(p_mtn);
59219820Sjeff	CL_ASSERT(p_sw);
60219820Sjeff
61219820Sjeff	memset(p_mtn, 0, sizeof(*p_mtn));
62219820Sjeff
63219820Sjeff	p_mtn->p_sw = (osm_switch_t *) p_sw;
64219820Sjeff	p_mtn->max_children = p_sw->num_ports;
65219820Sjeff
66219820Sjeff	for (i = 0; i < p_mtn->max_children; i++)
67219820Sjeff		p_mtn->child_array[i] = NULL;
68219820Sjeff}
69219820Sjeff
70219820Sjeff/**********************************************************************
71219820Sjeff **********************************************************************/
72219820Sjeffosm_mtree_node_t *osm_mtree_node_new(IN const osm_switch_t * const p_sw)
73219820Sjeff{
74219820Sjeff	osm_mtree_node_t *p_mtn;
75219820Sjeff
76219820Sjeff	p_mtn = malloc(sizeof(osm_mtree_node_t) +
77219820Sjeff		       sizeof(void *) * (p_sw->num_ports - 1));
78219820Sjeff
79219820Sjeff	if (p_mtn != NULL)
80219820Sjeff		osm_mtree_node_init(p_mtn, p_sw);
81219820Sjeff
82219820Sjeff	return (p_mtn);
83219820Sjeff}
84219820Sjeff
85219820Sjeff/**********************************************************************
86219820Sjeff **********************************************************************/
87219820Sjeffvoid osm_mtree_destroy(IN osm_mtree_node_t * p_mtn)
88219820Sjeff{
89219820Sjeff	uint32_t i;
90219820Sjeff
91219820Sjeff	if (p_mtn == NULL)
92219820Sjeff		return;
93219820Sjeff
94219820Sjeff	if (p_mtn->child_array != NULL)
95219820Sjeff		for (i = 0; i < p_mtn->max_children; i++)
96219820Sjeff			if ((p_mtn->child_array[i] != NULL) &&
97219820Sjeff			    (p_mtn->child_array[i] != OSM_MTREE_LEAF))
98219820Sjeff				osm_mtree_destroy(p_mtn->child_array[i]);
99219820Sjeff
100219820Sjeff	free(p_mtn);
101219820Sjeff}
102219820Sjeff
103219820Sjeff/**********************************************************************
104219820Sjeff **********************************************************************/
105219820Sjeff#if 0
106219820Sjeffstatic void __osm_mtree_dump(IN osm_mtree_node_t * p_mtn)
107219820Sjeff{
108219820Sjeff	uint32_t i;
109219820Sjeff
110219820Sjeff	if (p_mtn == NULL)
111219820Sjeff		return;
112219820Sjeff
113219820Sjeff	printf("GUID:0x%016" PRIx64 " max_children:%u\n",
114219820Sjeff	       cl_ntoh64(p_mtn->p_sw->p_node->node_info.node_guid),
115219820Sjeff	       p_mtn->max_children);
116219820Sjeff	if (p_mtn->child_array != NULL) {
117219820Sjeff		for (i = 0; i < p_mtn->max_children; i++) {
118219820Sjeff			printf("i=%d\n", i);
119219820Sjeff			if ((p_mtn->child_array[i] != NULL)
120219820Sjeff			    && (p_mtn->child_array[i] != OSM_MTREE_LEAF))
121219820Sjeff				__osm_mtree_dump(p_mtn->child_array[i]);
122219820Sjeff		}
123219820Sjeff	}
124219820Sjeff}
125219820Sjeff#endif
126