Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a memory leak.
[simgrid.git] / src / simdag / sd_workstation.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 workstation and registers it in SD.
8  */
9 SD_workstation_t __SD_workstation_create(void *surf_workstation, void *data) {
10   CHECK_INIT_DONE();
11
12   SD_workstation_data_t sd_data = xbt_new0(s_SD_workstation_data_t, 1); /* workstation private data */
13   sd_data->surf_workstation = surf_workstation;
14   
15   SD_workstation_t workstation = xbt_new0(s_SD_workstation_t, 1);
16   workstation->data = data; /* user data */
17   workstation->sd_data = sd_data; /* private data */
18   
19   const char *name = SD_workstation_get_name(workstation);
20   xbt_dict_set(sd_global->workstations, name, workstation, __SD_workstation_destroy); /* add the workstation to the dictionary */
21
22   /* TODO: route */
23   return workstation;
24 }
25
26 /* Returns a workstation given its name, or NULL if there is no such workstation.
27  */
28 SD_workstation_t SD_workstation_get_by_name(const char *name) {
29   CHECK_INIT_DONE();
30
31   xbt_assert0(name != NULL, "Invalid parameter");
32
33   return xbt_dict_get_or_null(sd_global->workstations, name);
34 }
35
36 /* Returns a NULL-terminated array of existing workstations.
37  */
38 SD_workstation_t*  SD_workstation_get_list(void) {
39   CHECK_INIT_DONE();
40
41   SD_workstation_t* array = xbt_new0(SD_workstation_t, sd_global->workstation_count + 1);
42   
43   xbt_dict_cursor_t cursor;
44   char *key;
45   void *data;
46   int i=0;
47
48   xbt_dict_foreach(sd_global->workstations,cursor,key,data) {
49     array[i++] = (SD_workstation_t) data;
50   }
51   array[i] = NULL;
52
53   return array;
54 }
55
56 /* Returns the number or workstations.
57  */
58 int SD_workstation_get_number(void) {
59   CHECK_INIT_DONE();
60   return sd_global->workstation_count;
61 }
62
63 /* Sets the data of a workstation. The new data can be NULL. The old data should have been freed first if it was not NULL.
64  */
65 void SD_workstation_set_data(SD_workstation_t workstation, void *data) {
66   CHECK_INIT_DONE();
67   xbt_assert0(workstation != NULL, "Invalid parameter");
68   workstation->data = data;
69 }
70
71 /* Returns the data of a workstation. The user data can be NULL.
72  */
73 void* SD_workstation_get_data(SD_workstation_t workstation) {
74   CHECK_INIT_DONE();
75   xbt_assert0(workstation != NULL, "Invalid parameter");
76   return workstation->data;
77 }
78
79 /* Returns the name of a workstation.
80  */
81 const char* SD_workstation_get_name(SD_workstation_t workstation) {
82   CHECK_INIT_DONE();
83   xbt_assert0(workstation != NULL, "Invalid parameter");
84   return surf_workstation_resource->common_public->get_resource_name(workstation->sd_data->surf_workstation);
85 }
86
87 SD_link_t* SD_workstation_route_get_list(SD_workstation_t src, SD_workstation_t dst) {
88   CHECK_INIT_DONE();
89   /* TODO */
90   return NULL;
91 }
92
93 int SD_workstation_route_get_size(SD_workstation_t src, SD_workstation_t dst) {
94   CHECK_INIT_DONE();
95   /* TODO */
96   return 0;
97 }
98
99 /* Returns the total power of a workstation.
100  */
101 double SD_workstation_get_power(SD_workstation_t workstation) {
102   CHECK_INIT_DONE();
103   xbt_assert0(workstation != NULL, "Invalid parameter");
104   return surf_workstation_resource->extension_public->get_speed(workstation->sd_data->surf_workstation, 1.0);
105 }
106
107 /* Returns the proportion of available power in a workstation (normally a number between 0 and 1).
108  */
109 double SD_workstation_get_available_power(SD_workstation_t workstation) {
110   CHECK_INIT_DONE();
111   xbt_assert0(workstation != NULL, "Invalid parameter");
112   /* TODO */
113   return 0;
114   /*return workstation->available_power;*/
115 }
116
117 /* Destroys a workstation. The user data (if any) should have been destroyed first.
118  */
119 void __SD_workstation_destroy(void *workstation) {
120   CHECK_INIT_DONE();
121   xbt_assert0(workstation != NULL, "Invalid parameter");
122
123   if (((SD_workstation_t) workstation)->sd_data != NULL) {
124     xbt_free(((SD_workstation_t) workstation)->sd_data);
125   }
126   
127   /* TODO: route */
128
129   xbt_free(workstation);
130 }