Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleanup plugging a memleak
[simgrid.git] / src / instr / instr_paje_types.cpp
1 /* Copyright (c) 2012, 2014-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "src/instr/instr_private.h"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_types, instr, "Paje tracing event system (types)");
10
11 static simgrid::instr::Type* rootType = nullptr; /* the root type */
12
13 void PJ_type_release ()
14 {
15   rootType = nullptr;
16 }
17
18 simgrid::instr::Type* PJ_type_get_root()
19 {
20   return rootType;
21 }
22
23 simgrid::instr::Type::Type(const char* typeNameBuff, const char* key, const char* color, e_entity_types kind,
24                            Type* father)
25     : kind_(kind), father_(father)
26 {
27   if (typeNameBuff == nullptr || key == nullptr){
28     THROWF(tracing_error, 0, "can't create a new type with name or key equal nullptr");
29   }
30
31   this->name_     = xbt_strdup(typeNameBuff);
32   this->children_ = xbt_dict_new_homogeneous(nullptr);
33   this->values_   = xbt_dict_new_homogeneous(nullptr);
34   this->color_    = xbt_strdup(color);
35
36   this->id_ = bprintf("%lld", instr_new_paje_id());
37
38   if (father != nullptr){
39     xbt_dict_set(father->children_, key, this, nullptr);
40     XBT_DEBUG("new type %s, child of %s", typeNameBuff, father->name_);
41   }
42 }
43
44 void PJ_type_free(simgrid::instr::Type* type)
45 {
46   simgrid::instr::Value* val;
47   char *value_name;
48   xbt_dict_cursor_t cursor = nullptr;
49   xbt_dict_foreach (type->values_, cursor, value_name, val) {
50     XBT_DEBUG("free value %s, child of %s", val->name_, val->father_->name_);
51     xbt_free(val);
52   }
53   xbt_dict_free(&type->values_);
54   xbt_free(type->name_);
55   xbt_free(type->id_);
56   xbt_free(type->color_);
57   xbt_dict_free(&type->children_);
58   delete type;
59 }
60
61 void recursiveDestroyType(simgrid::instr::Type* type)
62 {
63   XBT_DEBUG("recursiveDestroyType %s", type->name_);
64   xbt_dict_cursor_t cursor = nullptr;
65   simgrid::instr::Type* child;
66   char *child_name;
67   xbt_dict_foreach (type->children_, cursor, child_name, child) {
68     recursiveDestroyType (child);
69   }
70   PJ_type_free(type);
71 }
72
73 simgrid::instr::Type* PJ_type_get(const char* name, simgrid::instr::Type* father)
74 {
75   simgrid::instr::Type* ret = simgrid::instr::Type::getOrNull(name, father);
76   if (ret == nullptr){
77     THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name, father->name_);
78   }
79   return ret;
80 }
81
82 simgrid::instr::Type* simgrid::instr::Type::getOrNull(const char* name, simgrid::instr::Type* father)
83 {
84   if (name == nullptr || father == nullptr){
85     THROWF (tracing_error, 0, "can't get type with a nullptr name or from a nullptr father");
86   }
87
88   simgrid::instr::Type* ret = nullptr;
89   simgrid::instr::Type* child;
90   char *child_name;
91   xbt_dict_cursor_t cursor = nullptr;
92   xbt_dict_foreach (father->children_, cursor, child_name, child) {
93     if (strcmp(child->name_, name) == 0) {
94       if (ret != nullptr){
95         THROWF (tracing_error, 0, "there are two children types with the same name?");
96       }else{
97         ret = child;
98       }
99     }
100   }
101   return ret;
102 }
103
104 simgrid::instr::Type* simgrid::instr::Type::containerNew(const char* name, simgrid::instr::Type* father)
105 {
106   if (name == nullptr){
107     THROWF (tracing_error, 0, "can't create a container type with a nullptr name");
108   }
109
110   simgrid::instr::Type* ret = new simgrid::instr::Type(name, name, nullptr, TYPE_CONTAINER, father);
111   if (father == nullptr) {
112     rootType = ret;
113   } else {
114     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
115     LogContainerTypeDefinition(ret);
116   }
117   return ret;
118 }
119
120 simgrid::instr::Type* simgrid::instr::Type::eventNew(const char* name, simgrid::instr::Type* father)
121 {
122   if (name == nullptr){
123     THROWF (tracing_error, 0, "can't create an event type with a nullptr name");
124   }
125
126   Type* ret = new Type (name, name, nullptr, TYPE_EVENT, father);
127   XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
128   LogDefineEventType(ret);
129   return ret;
130 }
131
132 simgrid::instr::Type* simgrid::instr::Type::variableNew(const char* name, const char* color,
133                                                         simgrid::instr::Type* father)
134 {
135   if (name == nullptr){
136     THROWF (tracing_error, 0, "can't create a variable type with a nullptr name");
137   }
138
139   Type* ret = nullptr;
140
141   if (not color) {
142     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
143     ret = new Type (name, name, white, TYPE_VARIABLE, father);
144   }else{
145     ret = new Type (name, name, color, TYPE_VARIABLE, father);
146   }
147   XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
148   LogVariableTypeDefinition (ret);
149   return ret;
150 }
151
152 simgrid::instr::Type* simgrid::instr::Type::linkNew(const char* name, Type* father, Type* source, Type* dest)
153 {
154   if (name == nullptr){
155     THROWF (tracing_error, 0, "can't create a link type with a nullptr name");
156   }
157
158   char key[INSTR_DEFAULT_STR_SIZE];
159   snprintf(key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", name, source->id_, dest->id_);
160   Type* ret = new Type(name, key, nullptr, TYPE_LINK, father);
161   XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->name_, ret->id_, father->name_, father->id_,
162             source->name_, source->id_, dest->name_, dest->id_);
163   LogLinkTypeDefinition(ret, source, dest);
164   return ret;
165 }
166
167 simgrid::instr::Type* simgrid::instr::Type::stateNew(const char* name, Type* father)
168 {
169   if (name == nullptr){
170     THROWF (tracing_error, 0, "can't create a state type with a nullptr name");
171   }
172
173   Type* ret = new Type(name, name, nullptr, TYPE_STATE, father);
174   XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
175   LogStateTypeDefinition(ret);
176   return ret;
177 }