Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use new/delete for s_xbt_cfgelm_t
[simgrid.git] / src / surf / AsFull.cpp
1 /* Copyright (c) 2009-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/surf/AsFull.hpp"
7 #include "src/surf/network_interface.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
10
11 #define TO_ROUTE_FULL(i,j) routingTable_[(i)+(j)*table_size]
12
13 namespace simgrid {
14 namespace surf {
15   AsFull::AsFull(const char*name)
16     : AsRoutedGraph(name)
17   {
18   }
19
20 void AsFull::seal() {
21   int i;
22   sg_platf_route_cbarg_t e_route;
23
24   /* set utils vars */
25   int table_size = (int)xbt_dynar_length(vertices_);
26
27   /* Create table if necessary */
28   if (!routingTable_)
29     routingTable_ = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
30
31   /* Add the loopback if needed */
32   if (routing_platf->loopback_ && hierarchy_ == RoutingMode::base) {
33     for (i = 0; i < table_size; i++) {
34       e_route = TO_ROUTE_FULL(i, i);
35       if (!e_route) {
36         e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
37         e_route->gw_src = NULL;
38         e_route->gw_dst = NULL;
39         e_route->link_list.push_back(routing_platf->loopback_);
40         TO_ROUTE_FULL(i, i) = e_route;
41       }
42     }
43   }
44 }
45
46 AsFull::~AsFull(){
47   if (routingTable_) {
48     int table_size = (int)xbt_dynar_length(vertices_);
49     /* Delete routing table */
50     for (int i = 0; i < table_size; i++)
51       for (int j = 0; j < table_size; j++)
52         if (TO_ROUTE_FULL(i,j))
53           xbt_free(TO_ROUTE_FULL(i,j));
54     xbt_free(routingTable_);
55   }
56 }
57
58 void AsFull::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t res, double *lat)
59 {
60   XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
61       src->name(), src->id(), dst->name(), dst->id());
62
63   /* set utils vars */
64   size_t table_size = xbt_dynar_length(vertices_);
65
66   sg_platf_route_cbarg_t e_route = NULL;
67
68   e_route = TO_ROUTE_FULL(src->id(), dst->id());
69
70   if (e_route) {
71     res->gw_src = e_route->gw_src;
72     res->gw_dst = e_route->gw_dst;
73     for (auto link : e_route->link_list) {
74       res->link_list.push_back(link);
75       if (lat)
76         *lat += static_cast<Link*>(link)->getLatency();
77     }
78   }
79 }
80
81 void AsFull::addRoute(sg_platf_route_cbarg_t route)
82 {
83   const char *src = route->src;
84   const char *dst = route->dst;
85   NetCard *src_net_elm = sg_netcard_by_name_or_null(src);
86   NetCard *dst_net_elm = sg_netcard_by_name_or_null(dst);
87
88   addRouteCheckParams(route);
89
90   size_t table_size = xbt_dynar_length(vertices_);
91
92   if (!routingTable_)
93     routingTable_ = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
94
95   /* Check that the route does not already exist */
96   if (route->gw_dst) // AS route (to adapt the error message, if any)
97     xbt_assert(nullptr == TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()),
98         "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
99         src,route->gw_src->name(),dst,route->gw_dst->name());
100   else
101     xbt_assert(nullptr == TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()),
102         "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src,dst);
103
104   /* Add the route to the base */
105   TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()) = newExtendedRoute(hierarchy_, route, 1);
106   TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id())->link_list.shrink_to_fit();
107
108   if (route->symmetrical == true && src_net_elm != dst_net_elm) {
109     if (route->gw_dst && route->gw_src) {
110       NetCard* gw_tmp = route->gw_src;
111       route->gw_src = route->gw_dst;
112       route->gw_dst = gw_tmp;
113     }
114     if (route->gw_dst) // AS route (to adapt the error message, if any)
115       xbt_assert(nullptr == TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()),
116           "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
117           dst,route->gw_dst->name(),src,route->gw_src->name());
118     else
119       xbt_assert(nullptr == TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()),
120           "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.", dst,src);
121
122     TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()) = newExtendedRoute(hierarchy_, route, 0);
123     TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id())->link_list.shrink_to_fit();
124   }
125 }
126
127 }
128 }