Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Forgot to remove that comment
[simgrid.git] / src / simdag / sd_link.c
1 #include "private.h"
2 #include "simdag/simdag.h"
3 #include "xbt/dict.h"
4 #include "xbt/sysdep.h"
5 #include "surf/surf.h"
6
7 /* Creates a link and registers it in SD.
8  */
9 SD_link_t __SD_link_create(void *surf_link, void *data) {
10   SD_CHECK_INIT_DONE();
11   xbt_assert0(surf_link != NULL, "surf_link is NULL !");
12
13   SD_link_t link = xbt_new0(s_SD_link_t, 1);
14   link->surf_link = surf_link;
15   link->data = data; /* user data */
16
17   const char *name = SD_link_get_name(link);
18   xbt_dict_set(sd_global->links, name, link, __SD_link_destroy); /* add the link to the dictionary */
19   sd_global->link_count++;
20
21   return link;
22 }
23 /**
24  * \brief Returns the link list
25  *
26  * Use SD_link_get_number() to know the array size.
27  *
28  * \return an array of \ref SD_link_t containing all links
29  * \see SD_link_get_number()
30  */
31 const SD_link_t*  SD_link_get_list(void) {
32   SD_CHECK_INIT_DONE();
33   xbt_assert0(SD_link_get_number() > 0, "There is no link!");
34
35   xbt_dict_cursor_t cursor;
36   char *key;
37   void *data;
38   int i;
39
40   if (sd_global->link_list == NULL) { /* this is the first time the function is called */
41     sd_global->link_list = xbt_new0(SD_link_t, sd_global->link_count);
42   
43     i = 0;
44     xbt_dict_foreach(sd_global->links, cursor, key, data) {
45       sd_global->link_list[i++] = (SD_link_t) data;
46     }
47   }
48   return sd_global->link_list;
49 }
50
51 /**
52  * \brief Returns the number of links
53  *
54  * \return the number of existing links
55  * \see SD_link_get_list()
56  */
57 int SD_link_get_number(void) {
58   SD_CHECK_INIT_DONE();
59   return sd_global->link_count;
60 }
61
62 /**
63  * \brief Returns the user data of a link
64  *
65  * \param link a link
66  * \return the user data associated with this link (can be \c NULL)
67  * \see SD_link_set_data()
68  */
69 void* SD_link_get_data(SD_link_t link) {
70   SD_CHECK_INIT_DONE();
71   xbt_assert0(link != NULL, "Invalid parameter");
72   return link->data;
73 }
74
75 /**
76  * \brief Sets the user data of a link
77  *
78  * The new data can be \c NULL. The old data should have been freed first
79  * if it was not \c NULL.
80  *
81  * \param link a link
82  * \param data the new data you want to associate with this link
83  * \see SD_link_get_data()
84  */
85 void SD_link_set_data(SD_link_t link, void *data) {
86   SD_CHECK_INIT_DONE();
87   xbt_assert0(link != NULL, "Invalid parameter");
88   link->data = data;
89 }
90
91 /**
92  * \brief Returns the name of a link
93  *
94  * \param link a link
95  * \return the name of this link (cannot be \c NULL)
96  */
97 const char* SD_link_get_name(SD_link_t link) {
98   SD_CHECK_INIT_DONE();
99   xbt_assert0(link != NULL, "Invalid parameter");
100   return surf_workstation_resource->extension_public->get_link_name(link->surf_link);
101 }
102
103 /**
104  * \brief Returns the current bandwidth of a link
105  *
106  * \param link a link
107  * \return the current bandwidth of this link, in Flops
108  */
109 double SD_link_get_current_bandwidth(SD_link_t link) {
110   SD_CHECK_INIT_DONE();
111   xbt_assert0(link != NULL, "Invalid parameter");
112   return surf_workstation_resource->extension_public->get_link_bandwidth(link->surf_link);
113 }
114
115 /**
116  * \brief Returns the current latency of a link
117  *
118  * \param link a link
119  * \return the current latency of this link, in seconds
120  */
121 double SD_link_get_current_latency(SD_link_t link) {
122   SD_CHECK_INIT_DONE();
123   xbt_assert0(link != NULL, "Invalid parameter");
124   return surf_workstation_resource->extension_public->get_link_latency(link->surf_link);
125 }
126
127 /* Destroys a link.
128  */
129 void __SD_link_destroy(void *link) {
130   SD_CHECK_INIT_DONE();
131   xbt_assert0(link != NULL, "Invalid parameter");
132   /* link->surf_link is freed by surf_exit and link->data is freed by the user */
133   xbt_free(link);
134 }
135