Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
instr: fix a new/free mismatch, and small cleanups
[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 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 (values_, cursor, value_name, val) {
50     XBT_DEBUG("free value %s, child of %s", val->name_, val->father_->name_);
51     delete val;
52   }
53   xbt_dict_free(&values_);
54   simgrid::instr::Type* child;
55   char *child_name;
56   xbt_dict_foreach (children_, cursor, child_name, child) {
57     delete child;
58   }
59   xbt_dict_free(&children_);
60   xbt_free(name_);
61   xbt_free(id_);
62   xbt_free(color_);
63 }
64
65 simgrid::instr::Type* PJ_type_get(const char* name, simgrid::instr::Type* father)
66 {
67   simgrid::instr::Type* ret = simgrid::instr::Type::getOrNull(name, father);
68   if (ret == nullptr){
69     THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name, father->name_);
70   }
71   return ret;
72 }
73
74 simgrid::instr::Type* simgrid::instr::Type::getOrNull(const char* name, simgrid::instr::Type* father)
75 {
76   if (name == nullptr || father == nullptr){
77     THROWF (tracing_error, 0, "can't get type with a nullptr name or from a nullptr father");
78   }
79
80   simgrid::instr::Type* ret = nullptr;
81   simgrid::instr::Type* child;
82   char *child_name;
83   xbt_dict_cursor_t cursor = nullptr;
84   xbt_dict_foreach (father->children_, cursor, child_name, child) {
85     if (strcmp(child->name_, name) == 0) {
86       if (ret != nullptr){
87         THROWF (tracing_error, 0, "there are two children types with the same name?");
88       }else{
89         ret = child;
90       }
91     }
92   }
93   return ret;
94 }
95
96 simgrid::instr::Type* simgrid::instr::Type::containerNew(const char* name, simgrid::instr::Type* father)
97 {
98   if (name == nullptr){
99     THROWF (tracing_error, 0, "can't create a container type with a nullptr name");
100   }
101
102   simgrid::instr::Type* ret = new simgrid::instr::Type(name, name, nullptr, TYPE_CONTAINER, father);
103   if (father == nullptr) {
104     rootType = ret;
105   } else {
106     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
107     LogContainerTypeDefinition(ret);
108   }
109   return ret;
110 }
111
112 simgrid::instr::Type* simgrid::instr::Type::eventNew(const char* name, simgrid::instr::Type* father)
113 {
114   if (name == nullptr){
115     THROWF (tracing_error, 0, "can't create an event type with a nullptr name");
116   }
117
118   Type* ret = new Type (name, name, nullptr, TYPE_EVENT, father);
119   XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
120   LogDefineEventType(ret);
121   return ret;
122 }
123
124 simgrid::instr::Type* simgrid::instr::Type::variableNew(const char* name, const char* color,
125                                                         simgrid::instr::Type* father)
126 {
127   if (name == nullptr){
128     THROWF (tracing_error, 0, "can't create a variable type with a nullptr name");
129   }
130
131   Type* ret = nullptr;
132
133   if (not color) {
134     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
135     ret = new Type (name, name, white, TYPE_VARIABLE, father);
136   }else{
137     ret = new Type (name, name, color, TYPE_VARIABLE, father);
138   }
139   XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
140   LogVariableTypeDefinition (ret);
141   return ret;
142 }
143
144 simgrid::instr::Type* simgrid::instr::Type::linkNew(const char* name, Type* father, Type* source, Type* dest)
145 {
146   if (name == nullptr){
147     THROWF (tracing_error, 0, "can't create a link type with a nullptr name");
148   }
149
150   char key[INSTR_DEFAULT_STR_SIZE];
151   snprintf(key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", name, source->id_, dest->id_);
152   Type* ret = new Type(name, key, nullptr, TYPE_LINK, father);
153   XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->name_, ret->id_, father->name_, father->id_,
154             source->name_, source->id_, dest->name_, dest->id_);
155   LogLinkTypeDefinition(ret, source, dest);
156   return ret;
157 }
158
159 simgrid::instr::Type* simgrid::instr::Type::stateNew(const char* name, Type* father)
160 {
161   if (name == nullptr){
162     THROWF (tracing_error, 0, "can't create a state type with a nullptr name");
163   }
164
165   Type* ret = new Type(name, name, nullptr, TYPE_STATE, father);
166   XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name_, ret->id_, father->name_, father->id_);
167   LogStateTypeDefinition(ret);
168   return ret;
169 }